README.monitoring_tables 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. -----------------
  2. Monitoring tables
  3. -----------------
  4. Function:
  5. Allow to monitor server-side activity happening inside a particular database.
  6. Concept:
  7. The engine offers a set of so called "virtual" tables that provides the user
  8. with a snapshot of the current activity within the given database. The word
  9. "virtual" means that the table data doesn't exist until explicitly asked for.
  10. However, its metadata is stable and can be retrieved from the schema. Virtual
  11. monitoring tables exist only in ODS 11.1 (and higher) databases, so a
  12. migration via backup/restore is required in order to use this feature.
  13. The key term of the monitoring feature is an activity snapshot. It represents
  14. the current state of the database, consisting of various information about
  15. the database itself, active attachments and users, transactions, prepared and
  16. running statements, etc. A snapshot is created the first time any of the
  17. monitoring tables is being selected from in the given transaction and it's
  18. preserved until the transaction ends, so multiple queries (e.g. master-detail
  19. ones) will always return the consistent view of the data. In other words, the
  20. monitoring tables always behave like a snapshot (aka consistency) transaction,
  21. even if the host transaction has been started with another isolation level.
  22. To refresh the snapshot, the current transaction should be finished and the
  23. monitoring tables should be queried in the new transaction context. Creation
  24. of a snapshot is usually quite fast operation, but some delay should be
  25. expected under high load (especially in the Classic Server).
  26. A valid database connection is required in order to retrieve the monitoring
  27. data. The monitoring tables return information about the attached database
  28. only. If multiple databases are being accessed on the server, each of them
  29. has to be connected to and monitored separately.
  30. System variables CURRENT_CONNECTION and CURRENT_TRANSACTION could be used
  31. to select data about the current (for the caller) connection and transaction
  32. respectively. These variables correspond to the ID columns of the appropriate
  33. monitoring tables.
  34. Security:
  35. Complete database monitoring is available to SYSDBA and a database owner.
  36. Regular users are restricted to the information about their own attachments
  37. only (other attachments are invisible for them).
  38. Author:
  39. Dmitry Yemanov <dimitr at firebirdsql dot org>
  40. Scope:
  41. DSQL and PSQL
  42. MON$DATABASE (connected database)
  43. - MON$DATABASE_NAME (database pathname or alias)
  44. - MON$PAGE_SIZE (page size)
  45. - MON$ODS_MAJOR (major ODS version)
  46. - MON$ODS_MINOR (minor ODS version)
  47. - MON$OLDEST_TRANSACTION (OIT number)
  48. - MON$OLDEST_ACTIVE (OAT number)
  49. - MON$OLDEST_SNAPSHOT (OST number)
  50. - MON$NEXT_TRANSACTION (next transaction number)
  51. - MON$PAGE_BUFFERS (number of pages allocated in the cache)
  52. - MON$SQL_DIALECT (SQL dialect of the database)
  53. - MON$SHUTDOWN_MODE (current shutdown mode)
  54. 0: online
  55. 1: multi-user shutdown
  56. 2: single-user shutdown
  57. 3: full shutdown
  58. - MON$SWEEP_INTERVAL (sweep interval)
  59. - MON$READ_ONLY (read-only flag)
  60. - MON$FORCED_WRITES (sync writes flag)
  61. - MON$RESERVE_SPACE (reserve space flag)
  62. - MON$CREATION_DATE (creation date/time)
  63. - MON$PAGES (number of pages allocated on disk)
  64. - MON$STAT_ID (statistics ID)
  65. - MON$BACKUP_STATE (current physical backup state)
  66. 0: normal
  67. 1: stalled
  68. 2: merge
  69. - MON$CRYPT_STATE (current encryption state)
  70. 0: not encrypted
  71. 1: encrypted
  72. 2: decrypt in progress
  73. 3: encrypt in progress
  74. - MON$CRYPT_PAGE (number of page being encrypted / decrypted)
  75. - MON$OWNER (database owner name)
  76. - MON$SEC_DATABASE (security database)
  77. - MON$GUID (database GUID)
  78. - MON$FILE_ID (unique filesystem-level ID)
  79. - MON$NEXT_ATTACHMENT (next attachment number)
  80. - MON$NEXT_STATEMENT (next statement number)
  81. MON$ATTACHMENTS (connected attachments)
  82. - MON$ATTACHMENT_ID (attachment ID)
  83. - MON$SERVER_PID (server process ID)
  84. - MON$STATE (attachment state)
  85. 0: idle
  86. 1: active
  87. - MON$ATTACHMENT_NAME (connection string)
  88. - MON$USER (user name)
  89. - MON$ROLE (role name)
  90. - MON$REMOTE_PROTOCOL (remote protocol name)
  91. - MON$REMOTE_ADDRESS (remote address)
  92. - MON$REMOTE_PID (remote client process ID)
  93. - MON$REMOTE_PROCESS (remote client process pathname)
  94. - MON$CHARACTER_SET_ID (attachment character set)
  95. - MON$TIMESTAMP (connection date/time)
  96. - MON$GARBAGE_COLLECTION (garbage collection flag)
  97. - MON$STAT_ID (statistics ID)
  98. - MON$CLIENT_VERSION (version of the client library)
  99. - MON$REMOTE_VERSION (version of the remote protocol)
  100. - MON$REMOTE_HOST (remote host name)
  101. - MON$REMOTE_OS_USER (remote OS user name)
  102. - MON$AUTH_METHOD (authentication method used for connection)
  103. - MON$SYSTEM_FLAG (system flag)
  104. 0: user attachment
  105. 1: system attachment
  106. - MON$IDLE_TIMEOUT (connection-level idle timeout)
  107. - MON$IDLE_TIMER (idle timer expiration time)
  108. - MON$STATEMENT_TIMEOUT (statement timeout)
  109. - MON$WIRE_COMPRESSED (wire compression enabled/disabled)
  110. - MON$WIRE_ENCRYPTED (wire encryption enabled/disabled)
  111. - MON$WIRE_CRYPT_PLUGIN (name of wire encryption plugin)
  112. MON$TRANSACTIONS (started transactions)
  113. - MON$TRANSACTION_ID (transaction ID)
  114. - MON$ATTACHMENT_ID (attachment ID)
  115. - MON$STATE (transaction state)
  116. 0: idle
  117. 1: active
  118. - MON$TIMESTAMP (transaction start date/time)
  119. - MON$TOP_TRANSACTION (top transaction)
  120. - MON$OLDEST_TRANSACTION (local OIT number)
  121. - MON$OLDEST_ACTIVE (local OAT number)
  122. - MON$ISOLATION_MODE (isolation mode)
  123. 0: consistency
  124. 1: concurrency
  125. 2: read committed record version
  126. 3: read committed no record version
  127. 4: read committed read consistency
  128. - MON$LOCK_TIMEOUT (lock timeout)
  129. -1: infinite wait
  130. 0: no wait
  131. N: timeout N
  132. - MON$READ_ONLY (read-only flag)
  133. - MON$AUTO_COMMIT (auto-commit flag)
  134. - MON$AUTO_UNDO (auto-undo flag)
  135. - MON$STAT_ID (statistics ID)
  136. MON$STATEMENTS (prepared statements)
  137. - MON$STATEMENT_ID (statement ID)
  138. - MON$ATTACHMENT_ID (attachment ID)
  139. - MON$TRANSACTION_ID (transaction ID)
  140. - MON$STATE (statement state)
  141. 0: idle
  142. 1: active
  143. - MON$TIMESTAMP (statement start date/time)
  144. - MON$SQL_TEXT (statement text, if appropriate)
  145. - MON$STAT_ID (statistics ID)
  146. - MON$EXPLAINED_PLAN (explained query plan)
  147. - MON$STATEMENT_TIMEOUT (statement timeout)
  148. - MON$STATEMENT_TIMER (statement timer expiration time)
  149. MON$CALL_STACK (call stack of active PSQL requests)
  150. - MON$CALL_ID (call ID)
  151. - MON$STATEMENT_ID (top-level DSQL statement ID)
  152. - MON$CALLER_ID (caller request ID)
  153. - MON$OBJECT_NAME (PSQL object name)
  154. - MON$OBJECT_TYPE (PSQL object type)
  155. - MON$TIMESTAMP (request start date/time)
  156. - MON$SOURCE_LINE (SQL source line number)
  157. - MON$SOURCE_COLUMN (SQL source column number)
  158. - MON$STAT_ID (statistics ID)
  159. - MON$PACKAGE_NAME (PSQL object package name)
  160. MON$IO_STATS (I/O statistics)
  161. - MON$STAT_ID (statistics ID)
  162. - MON$STAT_GROUP (statistics group)
  163. 0: database
  164. 1: attachment
  165. 2: transaction
  166. 3: statement
  167. 4: call
  168. - MON$PAGE_READS (number of page reads)
  169. - MON$PAGE_WRITES (number of page writes)
  170. - MON$PAGE_FETCHES (number of page fetches)
  171. - MON$PAGE_MARKS (number of page marks)
  172. MON$RECORD_STATS (record-level statistics)
  173. - MON$STAT_ID (statistics ID)
  174. - MON$STAT_GROUP (statistics group)
  175. 0: database
  176. 1: attachment
  177. 2: transaction
  178. 3: statement
  179. 4: call
  180. - MON$RECORD_SEQ_READS (number of records read sequentially)
  181. - MON$RECORD_IDX_READS (number of records read via an index)
  182. - MON$RECORD_INSERTS (number of inserted records)
  183. - MON$RECORD_UPDATES (number of updated records)
  184. - MON$RECORD_DELETES (number of deleted records)
  185. - MON$RECORD_BACKOUTS (number of backed out records)
  186. - MON$RECORD_PURGES (number of purged records)
  187. - MON$RECORD_EXPUNGES (number of expunged records)
  188. - MON$RECORD_LOCKS (number of explicit record-level locks)
  189. - MON$RECORD_WAITS (number of waits on conflicting records)
  190. - MON$RECORD_CONFLICTS (number of record-level conflicts reported)
  191. - MON$BACKVERSION_READS (number of backversions read while chasing version chains)
  192. - MON$FRAGMENT_READS (number of fragments read while composing full records)
  193. - MON$RECORD_RPT_READS (number of records read repeatedly, i.e. re-fetched after reading)
  194. - MON$RECORD_IMGC (number of records affected by the intermediate garbage collection)
  195. MON$MEMORY_USAGE (current memory usage)
  196. - MON$STAT_ID (statistics ID)
  197. - MON$STAT_GROUP (statistics group)
  198. 0: database
  199. 1: attachment
  200. 2: transaction
  201. 3: statement
  202. 4: call
  203. - MON$MEMORY_USED (number of bytes currently in use)
  204. - MON$MEMORY_ALLOCATED (number of bytes currently allocated at the OS level)
  205. - MON$MAX_MEMORY_USED (maximum number of bytes used by this object)
  206. - MON$MAX_MEMORY_ALLOCATED (maximum number of bytes allocated from OS by this object)
  207. MON$CONTEXT_VARIABLES (known context variables)
  208. - MON$ATTACHMENT_ID (attachment ID)
  209. - MON$TRANSACTION_ID (transaction ID)
  210. - MON$VARIABLE_NAME (name of context variable)
  211. - MON$VARIABLE_VALUE (value of context variable)
  212. MON$TABLE_STATS (per table record-level statistics)
  213. - MON$STAT_ID (statistics ID)
  214. - MON$STAT_GROUP (statistics group)
  215. 0: database
  216. 1: attachment
  217. 2: transaction
  218. 3: statement
  219. 4: call
  220. - MON$TABLE_NAME (table name)
  221. - MON$RECORD_STAT_ID (record-level statistics ID, refers to MON$RECORD_STATS)
  222. Notes:
  223. 1) Textual descriptions of all "state" and "mode" values can be found
  224. in the system table RDB$TYPES
  225. 2) For table MON$ATTACHMENTS:
  226. - columns MON$REMOTE_PID and MON$REMOTE_PROCESS contains non-NULL values
  227. only if the client library has version 2.1 or higher
  228. - column MON$REMOTE_PROCESS can contain a non-pathname value
  229. if an application has specified a custom process name via DPB
  230. 3) For table MON$STATEMENTS:
  231. - column MON$SQL_TEXT contains NULL for GDML statements
  232. - columns MON$TRANSACTION_ID and MON$TIMESTAMP contain valid values
  233. for active statements only
  234. 4) For table MON$CALL_STACK:
  235. - column MON$STATEMENT_ID groups call stacks by the top-level DSQL statement
  236. that initiated the call chain. This ID represents an active statement
  237. record in the table MON$STATEMENTS.
  238. - columns MON$SOURCE_LINE and MON$SOURCE_COLUMN contain line/column information
  239. related to the PSQL statement being currently executed
  240. 5) For table MON$MEMORY_USAGE:
  241. - the "used" values represent high-level memory allocations, i.e. the ones
  242. performed by the engine from its pools. They are useful to investigate unexpected
  243. memory consumptions and find the "guilty" objects (attachments, procedures, etc),
  244. as well as trace memory leaks.
  245. - the "allocated" values represent low-level memory allocations, i.e. the ones
  246. performed by the Firebird memory manager. This means bytes really allocated from OS,
  247. thus allowing to monitor the physical memory consumption. Please note that not every
  248. record has these columns populated with non-zero values. Small allocations don't go
  249. to the OS level, they're redirected to the database memory pool instead. So usually
  250. only MON$DATABASE and memory-bound objects point to non-zero "allocated" values.
  251. - the counter set linked to a record in MON$DATABASE reports the memory shared among
  252. all attachments. In Classic and SuperClassic, these counters are zero meaning no
  253. shared cache in these architectures.
  254. 6) For table MON$CONTEXT_VARIABLES:
  255. - column MON$ATTACHMENT_ID contains a valid ID only for session-level context variables.
  256. Transaction-level ones have this field set to NULL.
  257. - column MON$TRANSACTION_ID contains a valid ID only for transaction-level context variables.
  258. Session-level ones have this field set to NULL.
  259. Example(s):
  260. 1) Retrieve IDs of all CS processes loading CPU at the moment:
  261. SELECT MON$SERVER_PID
  262. FROM MON$ATTACHMENTS
  263. WHERE MON$ATTACHMENT_ID <> CURRENT_CONNECTION
  264. AND MON$STATE = 1
  265. 2) Retrieve information about client applications:
  266. SELECT MON$USER, MON$REMOTE_ADDRESS, MON$REMOTE_PID, MON$TIMESTAMP
  267. FROM MON$ATTACHMENTS
  268. WHERE MON$ATTACHMENT_ID <> CURRENT_CONNECTION
  269. 3) Get isolation level of the current transaction:
  270. SELECT MON$ISOLATION_MODE
  271. FROM MON$TRANSACTIONS
  272. WHERE MON$TRANSACTION_ID = CURRENT_TRANSACTION
  273. 4) Get statements that are currently active:
  274. SELECT ATT.MON$USER, ATT.MON$REMOTE_ADDRESS, STMT.MON$SQL_TEXT, STMT.MON$TIMESTAMP
  275. FROM MON$ATTACHMENTS ATT
  276. JOIN MON$STATEMENTS STMT ON ATT.MON$ATTACHMENT_ID = STMT.MON$ATTACHMENT_ID
  277. WHERE ATT.MON$ATTACHMENT_ID <> CURRENT_CONNECTION
  278. AND STMT.MON$STATE = 1
  279. 5) Retrieve call stacks for all connections:
  280. WITH RECURSIVE
  281. HEAD AS
  282. (
  283. SELECT CALL.MON$STATEMENT_ID, CALL.MON$CALL_ID, CALL.MON$OBJECT_NAME, CALL.MON$OBJECT_TYPE
  284. FROM MON$CALL_STACK CALL
  285. WHERE CALL.MON$CALLER_ID IS NULL
  286. UNION ALL
  287. SELECT CALL.MON$STATEMENT_ID, CALL.MON$CALL_ID, CALL.MON$OBJECT_NAME, CALL.MON$OBJECT_TYPE
  288. FROM MON$CALL_STACK CALL
  289. JOIN HEAD ON CALL.MON$CALLER_ID = HEAD.MON$CALL_ID
  290. )
  291. SELECT MON$ATTACHMENT_ID, MON$OBJECT_NAME, MON$OBJECT_TYPE
  292. FROM HEAD
  293. JOIN MON$STATEMENTS STMT ON STMT.MON$STATEMENT_ID = HEAD.MON$STATEMENT_ID
  294. WHERE STMT.MON$ATTACHMENT_ID <> CURRENT_CONNECTION
  295. 6) Enumerate all session-level context variables for the current connection:
  296. SELECT VAR.MON$VARIABLE_NAME, VAR.MON$VARIABLE_VALUE
  297. FROM MON$CONTEXT_VARIABLES VAR
  298. WHERE VAR.MON$ATTACHMENT_ID = CURRENT_CONNECTION
  299. 7) Report top 10 statements ranked by their memory usage:
  300. SELECT FIRST 10 STMT.MON$ATTACHMENT_ID, STMT.MON$SQL_TEXT, MEM.MON$MEMORY_USED
  301. FROM MON$MEMORY_USAGE MEM
  302. NATURAL JOIN MON$STATEMENTS STMT
  303. ORDER BY MEM.MON$MEMORY_USED DESC
  304. --------------------------------------
  305. Modifications of the monitoring tables
  306. --------------------------------------
  307. Monitoring tables also allow some special administration activities, in particular:
  308. cancelling running statements and terminating client sessions. This is done via deletes
  309. from tables MON$STATEMENTS and MON$ATTACHMENTS respectively. Deletes from other tables,
  310. as well as inserts/updates issued against them, are prohibited.
  311. Notes:
  312. 1) If there are no statements currently running by the client, then the cancellation
  313. attempt becomes a void operation. Once cancelled, the execute/fetch API call returns
  314. the isc_cancelled error code. Any subsequent operations are allowed.
  315. 2) If there are active transactions in the connection being terminated, their activity
  316. is immediately cancelled and they're rolled back. Once terminated, the client session
  317. receives the isc_att_shutdown error code. Subsequent attempts to use this connection
  318. handle will cause network read/write errors.
  319. 3) System attachment can not be cancelled, so engine silently skip system attachments
  320. affected by DELETE FROM MON$ATTACHMENTS statements
  321. Example(s):
  322. 1) Cancel current activity of connection #32:
  323. DELETE FROM MON$STATEMENTS WHERE MON$ATTACHMENT_ID = 32
  324. 2) Disconnect everybody but ourselves:
  325. DELETE FROM MON$ATTACHMENTS WHERE MON$ATTACHMENT_ID <> CURRENT_CONNECTION
  326. --------------
  327. Under the hood
  328. --------------
  329. The monitoring implementation is built around two corner stones: shared memory and
  330. notifications.
  331. All server processes share some region of memory where the current activity information
  332. is stored. This information consists of multiple variable-length items describing the
  333. various activity details. All items that belong to the same process are grouped into a
  334. single cluster, so that they can be processed as a whole.
  335. The monitoring information is not populated/collected in real time. Instead, server
  336. processes write their data into the shared memory only when explicitly asked to. When doing
  337. so, the old clusters are being replaced with newer ones. When the shared memory region is
  338. being read, the reading process scans all the clusters and performs the garbage collection:
  339. clusters that belong to dead processes are removed and the shared memory space is compacted.
  340. Every server process has a flag that indicates its ability to react to someone's monitoring
  341. request as soon as it arrives. When some user connection runs a query against some
  342. monitoring table, the worker process of that connection sends a broadcast notification to
  343. other processes requesting an up-to-date information. Those processes react to this request
  344. by updating their clusters inside the shared memory region and clearing their "ready" flags.
  345. Once the every notified process has finished, the requesting one reads the shared memory
  346. region, filters the necessary tags based on its user permissions, transforms the internal
  347. representation into records and fields and populates the in-memory monitoring tables cache.
  348. Processes that were idle since the last monitoring exchange have their "ready" flag clear,
  349. thus indicating that they have nothing to update in the shared memory. This way they're
  350. excluded from the next roundtrip. As soon as something significant changed inside the
  351. process, the flag is set and this process starts responding to the monitoring requests
  352. again.
  353. The requester holds an exclusive lock while coordinating the write/read operations. This lock
  354. affects the currently active user connections as well as the connections being established.
  355. Multiple simultaneous monitoring requests are serialized.
  356. ----------------------------
  357. Limitations and known issues
  358. ----------------------------
  359. 1) In a heavily loaded system running Classic, monitoring requests may take noticeable time
  360. to execute. In the meantime, other activity (both running statements and new connection
  361. attempts) may be blocked until the monitoring request completes.
  362. Improved since FB v2.1.2.
  363. 2) Monitoring requests may sometimes fail due to the out-of-memory condition, or cause other
  364. worker processes to swap. This is caused by the fact that the every record in MON$STATEMENTS
  365. has a blob MON$SQL_TEXT which is created for the duration of the monitoring transaction.
  366. Prior to FB v2.5, every blob occupied <page size> bytes of memory even if its contents is
  367. in fact smaller. So, with a huge number of prepared statements in the system, it becomes
  368. possible to get this failure.
  369. Another possible reason could be the temporary (very short in practice) growth of the
  370. transaction pool which caches the monitoring data while merging the clusters into a single
  371. fragment.
  372. Improved since FB v2.5.0.