Appendix E: Monitoring Tables
The Firebird engine can monitor activities in a database and make them available for user queries via the monitoring tables. The definitions of these tables are always present in the database, all named with the prefix MON$
. The tables are virtual: they are populated with data only at the moment when the user queries them. That is also one good reason why it is no use trying to create triggers for them!
The key notion in understanding the monitoring feature is an activity snapshot. The activity snapshot represents the current state of the database at the start of the transaction in which the monitoring table query runs. It delivers a lot of information about the database itself, active connections, users, transactions prepared, running queries and more.
The snapshot is created when any monitoring table is queried for the first time. It is preserved until the end of the current transaction to maintain a stable, consistent view for queries across multiple tables, such as a master-detail query. In other words, monitoring tables always behave as though they were in SNAPSHOT TABLE STABILITY
(“consistency”) isolation, even if the current transaction is started with a lower isolation level.
To refresh the snapshot, the current transaction must be completed and the monitoring tables must be re-queried in a new transaction context.
Access Security
SYSDBA and the database owner have full access to all information available from the monitoring tables
Regular users can see information about their own connections; other connections are not visible to them
In a highly loaded environment, collecting information via the monitoring tables could have a negative impact on system performance. |
List of Monitoring Tables
MON$ATTACHMENTS
Information about active attachments to the database
MON$CALL_STACK
Calls to the stack by active queries of stored procedures and triggers
MON$CONTEXT_VARIABLES
Information about custom context variables
MON$DATABASE
Information about the database to which the CURRENT_CONNECTION
is attached
MON$IO_STATS
Input/output statistics
MON$MEMORY_USAGE
Memory usage statistics
MON$RECORD_STATS
Record-level statistics
MON$STATEMENTS
Statements prepared for execution
MON$TRANSACTIONS
Started transactions
MON$ATTACHMENTS
MON$ATTACHMENTS
displays information about active attachments to the database.
Column Name | Data Type | Description |
---|---|---|
|
| Connection identifier |
|
| Server process identifier |
|
| Connection state:
|
|
| Connection string — the file name and full path to the primary database file |
|
| The name of the user who is using this connection |
|
| The role name specified when the connection was established. If no role was specified when the connection was established, the field contains the text |
|
| Remote protocol name |
|
| Remote address (address and server name) |
|
| Remote client process identifier |
|
| Connection character set identifier (see |
|
| The date and time when the connection was started |
|
| Garbage collection flag (as specified in the attachment’s DPB): 1=allowed, 0=not allowed |
|
| The full file name and path to the executable file that established this connection |
|
| Statistics identifier |
Using MON$ATTACHMENTS
to Kill a Connection
Monitoring tables are read-only. However, the server has a built-in mechanism for deleting (and only deleting) records in the MON$ATTACHMENTS
table, which makes it possible to close a connection to the database.
Notes
|
Example
Closing all connections except for your own (current):
DELETE FROM MON$ATTACHMENTS
WHERE MON$ATTACHMENT_ID <> CURRENT_CONNECTION
MON$CALL_STACK
MON$CALL_STACK
displays calls to the stack from queries executing in stored procedures and triggers.
Column Name | Data Type | Description |
---|---|---|
|
| Call identifier |
|
| The identifier of the top-level SQL statement, the one that initiated the chain of calls. Use this identifier to find the records about the active statement in the |
|
| The identifier of the calling trigger or stored procedure |
|
| PSQL object (module) name |
|
| PSQL object type (trigger or stored procedure):
|
|
| The date and time when the call was started |
|
| The number of the source line in the SQL statement being executed at the moment of the snapshot |
|
| The number of the source column in the SQL statement being executed at the moment of the snapshot |
|
| Statistics identifier |
EXECUTE STATEMENT
Calls
Information about calls during the execution of the EXECUTE STATEMENT
statement does not get into the call stack.
Example using MON$CALL_STACK
Getting the call stack for all connections except own:
WITH RECURSIVE
HEAD AS (
SELECT
CALL.MON$STATEMENT_ID, CALL.MON$CALL_ID,
CALL.MON$OBJECT_NAME, CALL.MON$OBJECT_TYPE
FROM MON$CALL_STACK CALL
WHERE CALL.MON$CALLER_ID IS NULL
UNION ALL
SELECT
CALL.MON$STATEMENT_ID, CALL.MON$CALL_ID,
CALL.MON$OBJECT_NAME, CALL.MON$OBJECT_TYPE
FROM MON$CALL_STACK CALL
JOIN HEAD ON CALL.MON$CALLER_ID = HEAD.MON$CALL_ID
)
SELECT MON$ATTACHMENT_ID, MON$OBJECT_NAME, MON$OBJECT_TYPE
FROM HEAD
JOIN MON$STATEMENTS STMT ON STMT.MON$STATEMENT_ID = HEAD.MON$STATEMENT_ID
WHERE STMT.MON$ATTACHMENT_ID <> CURRENT_CONNECTION
MON$CONTEXT_VARIABLES
MON$CONTEXT_VARIABLES
displays information about custom context variables.
Column Name | Data Type | Description |
---|---|---|
|
| Connection identifier. It contains a valid value only for a connection-level context variable. For transaction-level variables it is |
|
| Transaction identifier. It contains a valid value only for transaction-level context variables. For connection-level variables it is |
|
| Context variable name |
|
| Context variable value |
MON$DATABASE
MON$DATABASE
displays the header information from the database the current user is connected to.
Column Name | Data Type | Description |
---|---|---|
|
| The file name and full path of the primary database file, or the database alias |
|
| Database page size in bytes |
|
| Major ODS version, e.g., 11 |
|
| Minor ODS version, e.g., 2 |
|
| The number of the oldest [interesting] transaction (OIT) |
|
| The number of the oldest active transaction (OAT) |
|
| The number of the transaction that was active at the moment when the OAT was started — oldest snapshot transaction (OST) |
|
| The number of the next transaction, as it stood when the monitoring snapshot was taken |
|
| The number of pages allocated in RAM for the database page cache |
|
| Database SQL Dialect: 1 or 3 |
|
| The current shutdown state of the database:
|
|
| Sweep interval |
|
| Flag indicating whether the database is read-only (value 1) or read-write (value 0) |
|
| Indicates whether the write mode of the database is set for synchronous write (forced writes ON, value is 1) or asynchronous write (forced writes OFF, value is 0) |
|
| The flag indicating reserve_space (value 1) or use_all_space (value 0) for filling database pages |
|
| The date and time when the database was created or was last restored |
|
| The number of pages allocated for the database on an external device |
|
| Statistics identifier |
|
| Current physical backup (nBackup) state:
|
MON$IO_STATS
MON$IO_STATS
displays input/output statistics. The counters are cumulative, by group, for each group of statistics.
Column Name | Data Type | Description |
---|---|---|
|
| Statistics identifier |
|
| Statistics group:
|
|
| Count of database pages read |
|
| Count of database pages written to |
|
| Count of database pages fetched |
|
| Count of database pages marked |
MON$MEMORY_USAGE
MON$MEMORY_USAGE
displays memory usage statistics.
Column Name | Data Type | Description |
---|---|---|
|
| Statistics identifier |
|
| Statistics group:
|
|
| The amount of memory in use, in bytes. This data is about the high-level memory allocation performed by the server. It can be useful to track down memory leaks and excessive memory usage in connections, procedures, etc. |
|
| The amount of memory allocated by the operating system, in bytes. This data is about the low-level memory allocation performed by the Firebird memory manager — the amount of memory allocated by the operating system — which can allow you to control the physical memory usage. |
|
| The maximum number of bytes used by this object |
|
| The maximum number of bytes allocated for this object by the operating system |
Not all records in this table have non-zero values. |
MON$RECORD_STATS
MON$RECORD_STATS
displays record-level statistics. The counters are cumulative, by group, for each group of statistics.
Column Name | Data Type | Description |
---|---|---|
|
| Statistics identifier |
|
| Statistics group:
|
|
| Count of records read sequentially |
|
| Count of records read via an index |
|
| Count of inserted records |
|
| Count of updated records |
|
| Count of deleted records |
|
| Count of records backed out |
|
| Count of records purged |
|
| Count of records expunged |
MON$STATEMENTS
MON$STATEMENTS
displays statements prepared for execution.
Column Name | Data Type | Description |
---|---|---|
|
| Statement identifier |
|
| Connection identifier |
|
| Transaction identifier |
|
| Statement state:
|
|
| The date and time when the statement was prepared |
|
| Statement text in SQL |
|
| Statistics identifier |
The STALLED state indicates that, at the time of the snapshot, the statement had an open cursor and was waiting for the client to resume fetching rows.
Using MON$STATEMENTS
to Cancel a Query
Monitoring tables are read-only. However, the server has a built-in mechanism for deleting (and only deleting) records in the MON$STATEMENTS
table, which makes it possible to cancel a running query.
Notes
|
Example
Cancelling all active queries for the specified connection:
DELETE FROM MON$STATEMENTS
WHERE MON$ATTACHMENT_ID = 32
MON$TRANSACTIONS
MON$TRANSACTIONS
reports started transactions.
Column Name | Data Type | Description |
---|---|---|
|
| Transaction identifier (number) |
|
| Connection identifier |
|
| Transaction state:
|
|
| The date and time when the transaction was started |
|
| Top-level transaction identifier (number) |
|
| Transaction ID of the oldest [interesting] transaction (OIT) |
|
| Transaction ID of the oldest active transaction (OAT) |
|
| Isolation mode (level):
|
|
| Lock timeout:
|
|
| Flag indicating whether the transaction is read-only (value 1) or read-write (value 0) |
|
| Flag indicating whether automatic commit is used for the transaction (value 1) or not (value 0) |
|
| Flag indicating whether the logging mechanism automatic undo is used for the transaction (value 1) or not (value 0) |
|
| Statistics identifier |