gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1439 lines
41KB

  1. /* ----------
  2. * pgstat.h
  3. *
  4. * Definitions for the PostgreSQL statistics collector daemon.
  5. *
  6. * Copyright (c) 2001-2019, PostgreSQL Global Development Group
  7. *
  8. * src/include/pgstat.h
  9. * ----------
  10. */
  11. #ifndef PGSTAT_H
  12. #define PGSTAT_H
  13. #include "datatype/timestamp.h"
  14. #include "fmgr.h"
  15. #include "libpq/pqcomm.h"
  16. #include "port/atomics.h"
  17. #include "portability/instr_time.h"
  18. #include "postmaster/pgarch.h"
  19. #include "storage/proc.h"
  20. #include "utils/hsearch.h"
  21. #include "utils/relcache.h"
  22. /* ----------
  23. * Paths for the statistics files (relative to installation's $PGDATA).
  24. * ----------
  25. */
  26. #define PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat"
  27. #define PGSTAT_STAT_PERMANENT_FILENAME "pg_stat/global.stat"
  28. #define PGSTAT_STAT_PERMANENT_TMPFILE "pg_stat/global.tmp"
  29. /* Default directory to store temporary statistics data in */
  30. #define PG_STAT_TMP_DIR "pg_stat_tmp"
  31. /* Values for track_functions GUC variable --- order is significant! */
  32. typedef enum TrackFunctionsLevel
  33. {
  34. TRACK_FUNC_OFF,
  35. TRACK_FUNC_PL,
  36. TRACK_FUNC_ALL
  37. } TrackFunctionsLevel;
  38. /* ----------
  39. * The types of backend -> collector messages
  40. * ----------
  41. */
  42. typedef enum StatMsgType
  43. {
  44. PGSTAT_MTYPE_DUMMY,
  45. PGSTAT_MTYPE_INQUIRY,
  46. PGSTAT_MTYPE_TABSTAT,
  47. PGSTAT_MTYPE_TABPURGE,
  48. PGSTAT_MTYPE_DROPDB,
  49. PGSTAT_MTYPE_RESETCOUNTER,
  50. PGSTAT_MTYPE_RESETSHAREDCOUNTER,
  51. PGSTAT_MTYPE_RESETSINGLECOUNTER,
  52. PGSTAT_MTYPE_AUTOVAC_START,
  53. PGSTAT_MTYPE_VACUUM,
  54. PGSTAT_MTYPE_ANALYZE,
  55. PGSTAT_MTYPE_ARCHIVER,
  56. PGSTAT_MTYPE_BGWRITER,
  57. PGSTAT_MTYPE_FUNCSTAT,
  58. PGSTAT_MTYPE_FUNCPURGE,
  59. PGSTAT_MTYPE_RECOVERYCONFLICT,
  60. PGSTAT_MTYPE_TEMPFILE,
  61. PGSTAT_MTYPE_DEADLOCK,
  62. PGSTAT_MTYPE_CHECKSUMFAILURE
  63. } StatMsgType;
  64. /* ----------
  65. * The data type used for counters.
  66. * ----------
  67. */
  68. typedef int64 PgStat_Counter;
  69. /* ----------
  70. * PgStat_TableCounts The actual per-table counts kept by a backend
  71. *
  72. * This struct should contain only actual event counters, because we memcmp
  73. * it against zeroes to detect whether there are any counts to transmit.
  74. * It is a component of PgStat_TableStatus (within-backend state) and
  75. * PgStat_TableEntry (the transmitted message format).
  76. *
  77. * Note: for a table, tuples_returned is the number of tuples successfully
  78. * fetched by heap_getnext, while tuples_fetched is the number of tuples
  79. * successfully fetched by heap_fetch under the control of bitmap indexscans.
  80. * For an index, tuples_returned is the number of index entries returned by
  81. * the index AM, while tuples_fetched is the number of tuples successfully
  82. * fetched by heap_fetch under the control of simple indexscans for this index.
  83. *
  84. * tuples_inserted/updated/deleted/hot_updated count attempted actions,
  85. * regardless of whether the transaction committed. delta_live_tuples,
  86. * delta_dead_tuples, and changed_tuples are set depending on commit or abort.
  87. * Note that delta_live_tuples and delta_dead_tuples can be negative!
  88. * ----------
  89. */
  90. typedef struct PgStat_TableCounts
  91. {
  92. PgStat_Counter t_numscans;
  93. PgStat_Counter t_tuples_returned;
  94. PgStat_Counter t_tuples_fetched;
  95. PgStat_Counter t_tuples_inserted;
  96. PgStat_Counter t_tuples_updated;
  97. PgStat_Counter t_tuples_deleted;
  98. PgStat_Counter t_tuples_hot_updated;
  99. bool t_truncated;
  100. PgStat_Counter t_delta_live_tuples;
  101. PgStat_Counter t_delta_dead_tuples;
  102. PgStat_Counter t_changed_tuples;
  103. PgStat_Counter t_blocks_fetched;
  104. PgStat_Counter t_blocks_hit;
  105. } PgStat_TableCounts;
  106. /* Possible targets for resetting cluster-wide shared values */
  107. typedef enum PgStat_Shared_Reset_Target
  108. {
  109. RESET_ARCHIVER,
  110. RESET_BGWRITER
  111. } PgStat_Shared_Reset_Target;
  112. /* Possible object types for resetting single counters */
  113. typedef enum PgStat_Single_Reset_Type
  114. {
  115. RESET_TABLE,
  116. RESET_FUNCTION
  117. } PgStat_Single_Reset_Type;
  118. /* ------------------------------------------------------------
  119. * Structures kept in backend local memory while accumulating counts
  120. * ------------------------------------------------------------
  121. */
  122. /* ----------
  123. * PgStat_TableStatus Per-table status within a backend
  124. *
  125. * Many of the event counters are nontransactional, ie, we count events
  126. * in committed and aborted transactions alike. For these, we just count
  127. * directly in the PgStat_TableStatus. However, delta_live_tuples,
  128. * delta_dead_tuples, and changed_tuples must be derived from event counts
  129. * with awareness of whether the transaction or subtransaction committed or
  130. * aborted. Hence, we also keep a stack of per-(sub)transaction status
  131. * records for every table modified in the current transaction. At commit
  132. * or abort, we propagate tuples_inserted/updated/deleted up to the
  133. * parent subtransaction level, or out to the parent PgStat_TableStatus,
  134. * as appropriate.
  135. * ----------
  136. */
  137. typedef struct PgStat_TableStatus
  138. {
  139. Oid t_id; /* table's OID */
  140. bool t_shared; /* is it a shared catalog? */
  141. struct PgStat_TableXactStatus *trans; /* lowest subxact's counts */
  142. PgStat_TableCounts t_counts; /* event counts to be sent */
  143. } PgStat_TableStatus;
  144. /* ----------
  145. * PgStat_TableXactStatus Per-table, per-subtransaction status
  146. * ----------
  147. */
  148. typedef struct PgStat_TableXactStatus
  149. {
  150. PgStat_Counter tuples_inserted; /* tuples inserted in (sub)xact */
  151. PgStat_Counter tuples_updated; /* tuples updated in (sub)xact */
  152. PgStat_Counter tuples_deleted; /* tuples deleted in (sub)xact */
  153. bool truncated; /* relation truncated in this (sub)xact */
  154. PgStat_Counter inserted_pre_trunc; /* tuples inserted prior to truncate */
  155. PgStat_Counter updated_pre_trunc; /* tuples updated prior to truncate */
  156. PgStat_Counter deleted_pre_trunc; /* tuples deleted prior to truncate */
  157. int nest_level; /* subtransaction nest level */
  158. /* links to other structs for same relation: */
  159. struct PgStat_TableXactStatus *upper; /* next higher subxact if any */
  160. PgStat_TableStatus *parent; /* per-table status */
  161. /* structs of same subxact level are linked here: */
  162. struct PgStat_TableXactStatus *next; /* next of same subxact */
  163. } PgStat_TableXactStatus;
  164. /* ------------------------------------------------------------
  165. * Message formats follow
  166. * ------------------------------------------------------------
  167. */
  168. /* ----------
  169. * PgStat_MsgHdr The common message header
  170. * ----------
  171. */
  172. typedef struct PgStat_MsgHdr
  173. {
  174. StatMsgType m_type;
  175. int m_size;
  176. } PgStat_MsgHdr;
  177. /* ----------
  178. * Space available in a message. This will keep the UDP packets below 1K,
  179. * which should fit unfragmented into the MTU of the loopback interface.
  180. * (Larger values of PGSTAT_MAX_MSG_SIZE would work for that on most
  181. * platforms, but we're being conservative here.)
  182. * ----------
  183. */
  184. #define PGSTAT_MAX_MSG_SIZE 1000
  185. #define PGSTAT_MSG_PAYLOAD (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))
  186. /* ----------
  187. * PgStat_MsgDummy A dummy message, ignored by the collector
  188. * ----------
  189. */
  190. typedef struct PgStat_MsgDummy
  191. {
  192. PgStat_MsgHdr m_hdr;
  193. } PgStat_MsgDummy;
  194. /* ----------
  195. * PgStat_MsgInquiry Sent by a backend to ask the collector
  196. * to write the stats file(s).
  197. *
  198. * Ordinarily, an inquiry message prompts writing of the global stats file,
  199. * the stats file for shared catalogs, and the stats file for the specified
  200. * database. If databaseid is InvalidOid, only the first two are written.
  201. *
  202. * New file(s) will be written only if the existing file has a timestamp
  203. * older than the specified cutoff_time; this prevents duplicated effort
  204. * when multiple requests arrive at nearly the same time, assuming that
  205. * backends send requests with cutoff_times a little bit in the past.
  206. *
  207. * clock_time should be the requestor's current local time; the collector
  208. * uses this to check for the system clock going backward, but it has no
  209. * effect unless that occurs. We assume clock_time >= cutoff_time, though.
  210. * ----------
  211. */
  212. typedef struct PgStat_MsgInquiry
  213. {
  214. PgStat_MsgHdr m_hdr;
  215. TimestampTz clock_time; /* observed local clock time */
  216. TimestampTz cutoff_time; /* minimum acceptable file timestamp */
  217. Oid databaseid; /* requested DB (InvalidOid => shared only) */
  218. } PgStat_MsgInquiry;
  219. /* ----------
  220. * PgStat_TableEntry Per-table info in a MsgTabstat
  221. * ----------
  222. */
  223. typedef struct PgStat_TableEntry
  224. {
  225. Oid t_id;
  226. PgStat_TableCounts t_counts;
  227. } PgStat_TableEntry;
  228. /* ----------
  229. * PgStat_MsgTabstat Sent by the backend to report table
  230. * and buffer access statistics.
  231. * ----------
  232. */
  233. #define PGSTAT_NUM_TABENTRIES \
  234. ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int) - 2 * sizeof(PgStat_Counter)) \
  235. / sizeof(PgStat_TableEntry))
  236. typedef struct PgStat_MsgTabstat
  237. {
  238. PgStat_MsgHdr m_hdr;
  239. Oid m_databaseid;
  240. int m_nentries;
  241. int m_xact_commit;
  242. int m_xact_rollback;
  243. PgStat_Counter m_block_read_time; /* times in microseconds */
  244. PgStat_Counter m_block_write_time;
  245. PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
  246. } PgStat_MsgTabstat;
  247. /* ----------
  248. * PgStat_MsgTabpurge Sent by the backend to tell the collector
  249. * about dead tables.
  250. * ----------
  251. */
  252. #define PGSTAT_NUM_TABPURGE \
  253. ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
  254. / sizeof(Oid))
  255. typedef struct PgStat_MsgTabpurge
  256. {
  257. PgStat_MsgHdr m_hdr;
  258. Oid m_databaseid;
  259. int m_nentries;
  260. Oid m_tableid[PGSTAT_NUM_TABPURGE];
  261. } PgStat_MsgTabpurge;
  262. /* ----------
  263. * PgStat_MsgDropdb Sent by the backend to tell the collector
  264. * about a dropped database
  265. * ----------
  266. */
  267. typedef struct PgStat_MsgDropdb
  268. {
  269. PgStat_MsgHdr m_hdr;
  270. Oid m_databaseid;
  271. } PgStat_MsgDropdb;
  272. /* ----------
  273. * PgStat_MsgResetcounter Sent by the backend to tell the collector
  274. * to reset counters
  275. * ----------
  276. */
  277. typedef struct PgStat_MsgResetcounter
  278. {
  279. PgStat_MsgHdr m_hdr;
  280. Oid m_databaseid;
  281. } PgStat_MsgResetcounter;
  282. /* ----------
  283. * PgStat_MsgResetsharedcounter Sent by the backend to tell the collector
  284. * to reset a shared counter
  285. * ----------
  286. */
  287. typedef struct PgStat_MsgResetsharedcounter
  288. {
  289. PgStat_MsgHdr m_hdr;
  290. PgStat_Shared_Reset_Target m_resettarget;
  291. } PgStat_MsgResetsharedcounter;
  292. /* ----------
  293. * PgStat_MsgResetsinglecounter Sent by the backend to tell the collector
  294. * to reset a single counter
  295. * ----------
  296. */
  297. typedef struct PgStat_MsgResetsinglecounter
  298. {
  299. PgStat_MsgHdr m_hdr;
  300. Oid m_databaseid;
  301. PgStat_Single_Reset_Type m_resettype;
  302. Oid m_objectid;
  303. } PgStat_MsgResetsinglecounter;
  304. /* ----------
  305. * PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
  306. * that a database is going to be processed
  307. * ----------
  308. */
  309. typedef struct PgStat_MsgAutovacStart
  310. {
  311. PgStat_MsgHdr m_hdr;
  312. Oid m_databaseid;
  313. TimestampTz m_start_time;
  314. } PgStat_MsgAutovacStart;
  315. /* ----------
  316. * PgStat_MsgVacuum Sent by the backend or autovacuum daemon
  317. * after VACUUM
  318. * ----------
  319. */
  320. typedef struct PgStat_MsgVacuum
  321. {
  322. PgStat_MsgHdr m_hdr;
  323. Oid m_databaseid;
  324. Oid m_tableoid;
  325. bool m_autovacuum;
  326. TimestampTz m_vacuumtime;
  327. PgStat_Counter m_live_tuples;
  328. PgStat_Counter m_dead_tuples;
  329. } PgStat_MsgVacuum;
  330. /* ----------
  331. * PgStat_MsgAnalyze Sent by the backend or autovacuum daemon
  332. * after ANALYZE
  333. * ----------
  334. */
  335. typedef struct PgStat_MsgAnalyze
  336. {
  337. PgStat_MsgHdr m_hdr;
  338. Oid m_databaseid;
  339. Oid m_tableoid;
  340. bool m_autovacuum;
  341. bool m_resetcounter;
  342. TimestampTz m_analyzetime;
  343. PgStat_Counter m_live_tuples;
  344. PgStat_Counter m_dead_tuples;
  345. } PgStat_MsgAnalyze;
  346. /* ----------
  347. * PgStat_MsgArchiver Sent by the archiver to update statistics.
  348. * ----------
  349. */
  350. typedef struct PgStat_MsgArchiver
  351. {
  352. PgStat_MsgHdr m_hdr;
  353. bool m_failed; /* Failed attempt */
  354. char m_xlog[MAX_XFN_CHARS + 1];
  355. TimestampTz m_timestamp;
  356. } PgStat_MsgArchiver;
  357. /* ----------
  358. * PgStat_MsgBgWriter Sent by the bgwriter to update statistics.
  359. * ----------
  360. */
  361. typedef struct PgStat_MsgBgWriter
  362. {
  363. PgStat_MsgHdr m_hdr;
  364. PgStat_Counter m_timed_checkpoints;
  365. PgStat_Counter m_requested_checkpoints;
  366. PgStat_Counter m_buf_written_checkpoints;
  367. PgStat_Counter m_buf_written_clean;
  368. PgStat_Counter m_maxwritten_clean;
  369. PgStat_Counter m_buf_written_backend;
  370. PgStat_Counter m_buf_fsync_backend;
  371. PgStat_Counter m_buf_alloc;
  372. PgStat_Counter m_checkpoint_write_time; /* times in milliseconds */
  373. PgStat_Counter m_checkpoint_sync_time;
  374. } PgStat_MsgBgWriter;
  375. /* ----------
  376. * PgStat_MsgRecoveryConflict Sent by the backend upon recovery conflict
  377. * ----------
  378. */
  379. typedef struct PgStat_MsgRecoveryConflict
  380. {
  381. PgStat_MsgHdr m_hdr;
  382. Oid m_databaseid;
  383. int m_reason;
  384. } PgStat_MsgRecoveryConflict;
  385. /* ----------
  386. * PgStat_MsgTempFile Sent by the backend upon creating a temp file
  387. * ----------
  388. */
  389. typedef struct PgStat_MsgTempFile
  390. {
  391. PgStat_MsgHdr m_hdr;
  392. Oid m_databaseid;
  393. size_t m_filesize;
  394. } PgStat_MsgTempFile;
  395. /* ----------
  396. * PgStat_FunctionCounts The actual per-function counts kept by a backend
  397. *
  398. * This struct should contain only actual event counters, because we memcmp
  399. * it against zeroes to detect whether there are any counts to transmit.
  400. *
  401. * Note that the time counters are in instr_time format here. We convert to
  402. * microseconds in PgStat_Counter format when transmitting to the collector.
  403. * ----------
  404. */
  405. typedef struct PgStat_FunctionCounts
  406. {
  407. PgStat_Counter f_numcalls;
  408. instr_time f_total_time;
  409. instr_time f_self_time;
  410. } PgStat_FunctionCounts;
  411. /* ----------
  412. * PgStat_BackendFunctionEntry Entry in backend's per-function hash table
  413. * ----------
  414. */
  415. typedef struct PgStat_BackendFunctionEntry
  416. {
  417. Oid f_id;
  418. PgStat_FunctionCounts f_counts;
  419. } PgStat_BackendFunctionEntry;
  420. /* ----------
  421. * PgStat_FunctionEntry Per-function info in a MsgFuncstat
  422. * ----------
  423. */
  424. typedef struct PgStat_FunctionEntry
  425. {
  426. Oid f_id;
  427. PgStat_Counter f_numcalls;
  428. PgStat_Counter f_total_time; /* times in microseconds */
  429. PgStat_Counter f_self_time;
  430. } PgStat_FunctionEntry;
  431. /* ----------
  432. * PgStat_MsgFuncstat Sent by the backend to report function
  433. * usage statistics.
  434. * ----------
  435. */
  436. #define PGSTAT_NUM_FUNCENTRIES \
  437. ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
  438. / sizeof(PgStat_FunctionEntry))
  439. typedef struct PgStat_MsgFuncstat
  440. {
  441. PgStat_MsgHdr m_hdr;
  442. Oid m_databaseid;
  443. int m_nentries;
  444. PgStat_FunctionEntry m_entry[PGSTAT_NUM_FUNCENTRIES];
  445. } PgStat_MsgFuncstat;
  446. /* ----------
  447. * PgStat_MsgFuncpurge Sent by the backend to tell the collector
  448. * about dead functions.
  449. * ----------
  450. */
  451. #define PGSTAT_NUM_FUNCPURGE \
  452. ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
  453. / sizeof(Oid))
  454. typedef struct PgStat_MsgFuncpurge
  455. {
  456. PgStat_MsgHdr m_hdr;
  457. Oid m_databaseid;
  458. int m_nentries;
  459. Oid m_functionid[PGSTAT_NUM_FUNCPURGE];
  460. } PgStat_MsgFuncpurge;
  461. /* ----------
  462. * PgStat_MsgDeadlock Sent by the backend to tell the collector
  463. * about a deadlock that occurred.
  464. * ----------
  465. */
  466. typedef struct PgStat_MsgDeadlock
  467. {
  468. PgStat_MsgHdr m_hdr;
  469. Oid m_databaseid;
  470. } PgStat_MsgDeadlock;
  471. /* ----------
  472. * PgStat_MsgChecksumFailure Sent by the backend to tell the collector
  473. * about checksum failures noticed.
  474. * ----------
  475. */
  476. typedef struct PgStat_MsgChecksumFailure
  477. {
  478. PgStat_MsgHdr m_hdr;
  479. Oid m_databaseid;
  480. int m_failurecount;
  481. TimestampTz m_failure_time;
  482. } PgStat_MsgChecksumFailure;
  483. /* ----------
  484. * PgStat_Msg Union over all possible messages.
  485. * ----------
  486. */
  487. typedef union PgStat_Msg
  488. {
  489. PgStat_MsgHdr msg_hdr;
  490. PgStat_MsgDummy msg_dummy;
  491. PgStat_MsgInquiry msg_inquiry;
  492. PgStat_MsgTabstat msg_tabstat;
  493. PgStat_MsgTabpurge msg_tabpurge;
  494. PgStat_MsgDropdb msg_dropdb;
  495. PgStat_MsgResetcounter msg_resetcounter;
  496. PgStat_MsgResetsharedcounter msg_resetsharedcounter;
  497. PgStat_MsgResetsinglecounter msg_resetsinglecounter;
  498. PgStat_MsgAutovacStart msg_autovacuum_start;
  499. PgStat_MsgVacuum msg_vacuum;
  500. PgStat_MsgAnalyze msg_analyze;
  501. PgStat_MsgArchiver msg_archiver;
  502. PgStat_MsgBgWriter msg_bgwriter;
  503. PgStat_MsgFuncstat msg_funcstat;
  504. PgStat_MsgFuncpurge msg_funcpurge;
  505. PgStat_MsgRecoveryConflict msg_recoveryconflict;
  506. PgStat_MsgDeadlock msg_deadlock;
  507. PgStat_MsgTempFile msg_tempfile;
  508. PgStat_MsgChecksumFailure msg_checksumfailure;
  509. } PgStat_Msg;
  510. /* ------------------------------------------------------------
  511. * Statistic collector data structures follow
  512. *
  513. * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
  514. * data structures change.
  515. * ------------------------------------------------------------
  516. */
  517. #define PGSTAT_FILE_FORMAT_ID 0x01A5BC9D
  518. /* ----------
  519. * PgStat_StatDBEntry The collector's data per database
  520. * ----------
  521. */
  522. typedef struct PgStat_StatDBEntry
  523. {
  524. Oid databaseid;
  525. PgStat_Counter n_xact_commit;
  526. PgStat_Counter n_xact_rollback;
  527. PgStat_Counter n_blocks_fetched;
  528. PgStat_Counter n_blocks_hit;
  529. PgStat_Counter n_tuples_returned;
  530. PgStat_Counter n_tuples_fetched;
  531. PgStat_Counter n_tuples_inserted;
  532. PgStat_Counter n_tuples_updated;
  533. PgStat_Counter n_tuples_deleted;
  534. TimestampTz last_autovac_time;
  535. PgStat_Counter n_conflict_tablespace;
  536. PgStat_Counter n_conflict_lock;
  537. PgStat_Counter n_conflict_snapshot;
  538. PgStat_Counter n_conflict_bufferpin;
  539. PgStat_Counter n_conflict_startup_deadlock;
  540. PgStat_Counter n_temp_files;
  541. PgStat_Counter n_temp_bytes;
  542. PgStat_Counter n_deadlocks;
  543. PgStat_Counter n_checksum_failures;
  544. TimestampTz last_checksum_failure;
  545. PgStat_Counter n_block_read_time; /* times in microseconds */
  546. PgStat_Counter n_block_write_time;
  547. TimestampTz stat_reset_timestamp;
  548. TimestampTz stats_timestamp; /* time of db stats file update */
  549. /*
  550. * tables and functions must be last in the struct, because we don't write
  551. * the pointers out to the stats file.
  552. */
  553. HTAB *tables;
  554. HTAB *functions;
  555. } PgStat_StatDBEntry;
  556. /* ----------
  557. * PgStat_StatTabEntry The collector's data per table (or index)
  558. * ----------
  559. */
  560. typedef struct PgStat_StatTabEntry
  561. {
  562. Oid tableid;
  563. PgStat_Counter numscans;
  564. PgStat_Counter tuples_returned;
  565. PgStat_Counter tuples_fetched;
  566. PgStat_Counter tuples_inserted;
  567. PgStat_Counter tuples_updated;
  568. PgStat_Counter tuples_deleted;
  569. PgStat_Counter tuples_hot_updated;
  570. PgStat_Counter n_live_tuples;
  571. PgStat_Counter n_dead_tuples;
  572. PgStat_Counter changes_since_analyze;
  573. PgStat_Counter blocks_fetched;
  574. PgStat_Counter blocks_hit;
  575. TimestampTz vacuum_timestamp; /* user initiated vacuum */
  576. PgStat_Counter vacuum_count;
  577. TimestampTz autovac_vacuum_timestamp; /* autovacuum initiated */
  578. PgStat_Counter autovac_vacuum_count;
  579. TimestampTz analyze_timestamp; /* user initiated */
  580. PgStat_Counter analyze_count;
  581. TimestampTz autovac_analyze_timestamp; /* autovacuum initiated */
  582. PgStat_Counter autovac_analyze_count;
  583. } PgStat_StatTabEntry;
  584. /* ----------
  585. * PgStat_StatFuncEntry The collector's data per function
  586. * ----------
  587. */
  588. typedef struct PgStat_StatFuncEntry
  589. {
  590. Oid functionid;
  591. PgStat_Counter f_numcalls;
  592. PgStat_Counter f_total_time; /* times in microseconds */
  593. PgStat_Counter f_self_time;
  594. } PgStat_StatFuncEntry;
  595. /*
  596. * Archiver statistics kept in the stats collector
  597. */
  598. typedef struct PgStat_ArchiverStats
  599. {
  600. PgStat_Counter archived_count; /* archival successes */
  601. char last_archived_wal[MAX_XFN_CHARS + 1]; /* last WAL file
  602. * archived */
  603. TimestampTz last_archived_timestamp; /* last archival success time */
  604. PgStat_Counter failed_count; /* failed archival attempts */
  605. char last_failed_wal[MAX_XFN_CHARS + 1]; /* WAL file involved in
  606. * last failure */
  607. TimestampTz last_failed_timestamp; /* last archival failure time */
  608. TimestampTz stat_reset_timestamp;
  609. } PgStat_ArchiverStats;
  610. /*
  611. * Global statistics kept in the stats collector
  612. */
  613. typedef struct PgStat_GlobalStats
  614. {
  615. TimestampTz stats_timestamp; /* time of stats file update */
  616. PgStat_Counter timed_checkpoints;
  617. PgStat_Counter requested_checkpoints;
  618. PgStat_Counter checkpoint_write_time; /* times in milliseconds */
  619. PgStat_Counter checkpoint_sync_time;
  620. PgStat_Counter buf_written_checkpoints;
  621. PgStat_Counter buf_written_clean;
  622. PgStat_Counter maxwritten_clean;
  623. PgStat_Counter buf_written_backend;
  624. PgStat_Counter buf_fsync_backend;
  625. PgStat_Counter buf_alloc;
  626. TimestampTz stat_reset_timestamp;
  627. } PgStat_GlobalStats;
  628. /* ----------
  629. * Backend types
  630. * ----------
  631. */
  632. typedef enum BackendType
  633. {
  634. B_AUTOVAC_LAUNCHER,
  635. B_AUTOVAC_WORKER,
  636. B_BACKEND,
  637. B_BG_WORKER,
  638. B_BG_WRITER,
  639. B_CHECKPOINTER,
  640. B_STARTUP,
  641. B_WAL_RECEIVER,
  642. B_WAL_SENDER,
  643. B_WAL_WRITER
  644. } BackendType;
  645. /* ----------
  646. * Backend states
  647. * ----------
  648. */
  649. typedef enum BackendState
  650. {
  651. STATE_UNDEFINED,
  652. STATE_IDLE,
  653. STATE_RUNNING,
  654. STATE_IDLEINTRANSACTION,
  655. STATE_FASTPATH,
  656. STATE_IDLEINTRANSACTION_ABORTED,
  657. STATE_DISABLED
  658. } BackendState;
  659. /* ----------
  660. * Wait Classes
  661. * ----------
  662. */
  663. #define PG_WAIT_LWLOCK 0x01000000U
  664. #define PG_WAIT_LOCK 0x03000000U
  665. #define PG_WAIT_BUFFER_PIN 0x04000000U
  666. #define PG_WAIT_ACTIVITY 0x05000000U
  667. #define PG_WAIT_CLIENT 0x06000000U
  668. #define PG_WAIT_EXTENSION 0x07000000U
  669. #define PG_WAIT_IPC 0x08000000U
  670. #define PG_WAIT_TIMEOUT 0x09000000U
  671. #define PG_WAIT_IO 0x0A000000U
  672. /* ----------
  673. * Wait Events - Activity
  674. *
  675. * Use this category when a process is waiting because it has no work to do,
  676. * unless the "Client" or "Timeout" category describes the situation better.
  677. * Typically, this should only be used for background processes.
  678. * ----------
  679. */
  680. typedef enum
  681. {
  682. WAIT_EVENT_ARCHIVER_MAIN = PG_WAIT_ACTIVITY,
  683. WAIT_EVENT_AUTOVACUUM_MAIN,
  684. WAIT_EVENT_BGWRITER_HIBERNATE,
  685. WAIT_EVENT_BGWRITER_MAIN,
  686. WAIT_EVENT_CHECKPOINTER_MAIN,
  687. WAIT_EVENT_LOGICAL_APPLY_MAIN,
  688. WAIT_EVENT_LOGICAL_LAUNCHER_MAIN,
  689. WAIT_EVENT_PGSTAT_MAIN,
  690. WAIT_EVENT_RECOVERY_WAL_ALL,
  691. WAIT_EVENT_RECOVERY_WAL_STREAM,
  692. WAIT_EVENT_SYSLOGGER_MAIN,
  693. WAIT_EVENT_WAL_RECEIVER_MAIN,
  694. WAIT_EVENT_WAL_SENDER_MAIN,
  695. WAIT_EVENT_WAL_WRITER_MAIN
  696. } WaitEventActivity;
  697. /* ----------
  698. * Wait Events - Client
  699. *
  700. * Use this category when a process is waiting to send data to or receive data
  701. * from the frontend process to which it is connected. This is never used for
  702. * a background process, which has no client connection.
  703. * ----------
  704. */
  705. typedef enum
  706. {
  707. WAIT_EVENT_CLIENT_READ = PG_WAIT_CLIENT,
  708. WAIT_EVENT_CLIENT_WRITE,
  709. WAIT_EVENT_LIBPQWALRECEIVER_CONNECT,
  710. WAIT_EVENT_LIBPQWALRECEIVER_RECEIVE,
  711. WAIT_EVENT_SSL_OPEN_SERVER,
  712. WAIT_EVENT_WAL_RECEIVER_WAIT_START,
  713. WAIT_EVENT_WAL_SENDER_WAIT_WAL,
  714. WAIT_EVENT_WAL_SENDER_WRITE_DATA,
  715. WAIT_EVENT_GSS_OPEN_SERVER,
  716. } WaitEventClient;
  717. /* ----------
  718. * Wait Events - IPC
  719. *
  720. * Use this category when a process cannot complete the work it is doing because
  721. * it is waiting for a notification from another process.
  722. * ----------
  723. */
  724. typedef enum
  725. {
  726. WAIT_EVENT_BGWORKER_SHUTDOWN = PG_WAIT_IPC,
  727. WAIT_EVENT_BGWORKER_STARTUP,
  728. WAIT_EVENT_BTREE_PAGE,
  729. WAIT_EVENT_CLOG_GROUP_UPDATE,
  730. WAIT_EVENT_CHECKPOINT_DONE,
  731. WAIT_EVENT_CHECKPOINT_START,
  732. WAIT_EVENT_EXECUTE_GATHER,
  733. WAIT_EVENT_HASH_BATCH_ALLOCATING,
  734. WAIT_EVENT_HASH_BATCH_ELECTING,
  735. WAIT_EVENT_HASH_BATCH_LOADING,
  736. WAIT_EVENT_HASH_BUILD_ALLOCATING,
  737. WAIT_EVENT_HASH_BUILD_ELECTING,
  738. WAIT_EVENT_HASH_BUILD_HASHING_INNER,
  739. WAIT_EVENT_HASH_BUILD_HASHING_OUTER,
  740. WAIT_EVENT_HASH_GROW_BATCHES_ALLOCATING,
  741. WAIT_EVENT_HASH_GROW_BATCHES_DECIDING,
  742. WAIT_EVENT_HASH_GROW_BATCHES_ELECTING,
  743. WAIT_EVENT_HASH_GROW_BATCHES_FINISHING,
  744. WAIT_EVENT_HASH_GROW_BATCHES_REPARTITIONING,
  745. WAIT_EVENT_HASH_GROW_BUCKETS_ALLOCATING,
  746. WAIT_EVENT_HASH_GROW_BUCKETS_ELECTING,
  747. WAIT_EVENT_HASH_GROW_BUCKETS_REINSERTING,
  748. WAIT_EVENT_LOGICAL_SYNC_DATA,
  749. WAIT_EVENT_LOGICAL_SYNC_STATE_CHANGE,
  750. WAIT_EVENT_MQ_INTERNAL,
  751. WAIT_EVENT_MQ_PUT_MESSAGE,
  752. WAIT_EVENT_MQ_RECEIVE,
  753. WAIT_EVENT_MQ_SEND,
  754. WAIT_EVENT_PARALLEL_BITMAP_SCAN,
  755. WAIT_EVENT_PARALLEL_CREATE_INDEX_SCAN,
  756. WAIT_EVENT_PARALLEL_FINISH,
  757. WAIT_EVENT_PROCARRAY_GROUP_UPDATE,
  758. WAIT_EVENT_PROMOTE,
  759. WAIT_EVENT_REPLICATION_ORIGIN_DROP,
  760. WAIT_EVENT_REPLICATION_SLOT_DROP,
  761. WAIT_EVENT_SAFE_SNAPSHOT,
  762. WAIT_EVENT_SYNC_REP
  763. } WaitEventIPC;
  764. /* ----------
  765. * Wait Events - Timeout
  766. *
  767. * Use this category when a process is waiting for a timeout to expire.
  768. * ----------
  769. */
  770. typedef enum
  771. {
  772. WAIT_EVENT_BASE_BACKUP_THROTTLE = PG_WAIT_TIMEOUT,
  773. WAIT_EVENT_PG_SLEEP,
  774. WAIT_EVENT_RECOVERY_APPLY_DELAY
  775. } WaitEventTimeout;
  776. /* ----------
  777. * Wait Events - IO
  778. *
  779. * Use this category when a process is waiting for a IO.
  780. * ----------
  781. */
  782. typedef enum
  783. {
  784. WAIT_EVENT_BUFFILE_READ = PG_WAIT_IO,
  785. WAIT_EVENT_BUFFILE_WRITE,
  786. WAIT_EVENT_CONTROL_FILE_READ,
  787. WAIT_EVENT_CONTROL_FILE_SYNC,
  788. WAIT_EVENT_CONTROL_FILE_SYNC_UPDATE,
  789. WAIT_EVENT_CONTROL_FILE_WRITE,
  790. WAIT_EVENT_CONTROL_FILE_WRITE_UPDATE,
  791. WAIT_EVENT_COPY_FILE_READ,
  792. WAIT_EVENT_COPY_FILE_WRITE,
  793. WAIT_EVENT_DATA_FILE_EXTEND,
  794. WAIT_EVENT_DATA_FILE_FLUSH,
  795. WAIT_EVENT_DATA_FILE_IMMEDIATE_SYNC,
  796. WAIT_EVENT_DATA_FILE_PREFETCH,
  797. WAIT_EVENT_DATA_FILE_READ,
  798. WAIT_EVENT_DATA_FILE_SYNC,
  799. WAIT_EVENT_DATA_FILE_TRUNCATE,
  800. WAIT_EVENT_DATA_FILE_WRITE,
  801. WAIT_EVENT_DSM_FILL_ZERO_WRITE,
  802. WAIT_EVENT_LOCK_FILE_ADDTODATADIR_READ,
  803. WAIT_EVENT_LOCK_FILE_ADDTODATADIR_SYNC,
  804. WAIT_EVENT_LOCK_FILE_ADDTODATADIR_WRITE,
  805. WAIT_EVENT_LOCK_FILE_CREATE_READ,
  806. WAIT_EVENT_LOCK_FILE_CREATE_SYNC,
  807. WAIT_EVENT_LOCK_FILE_CREATE_WRITE,
  808. WAIT_EVENT_LOCK_FILE_RECHECKDATADIR_READ,
  809. WAIT_EVENT_LOGICAL_REWRITE_CHECKPOINT_SYNC,
  810. WAIT_EVENT_LOGICAL_REWRITE_MAPPING_SYNC,
  811. WAIT_EVENT_LOGICAL_REWRITE_MAPPING_WRITE,
  812. WAIT_EVENT_LOGICAL_REWRITE_SYNC,
  813. WAIT_EVENT_LOGICAL_REWRITE_TRUNCATE,
  814. WAIT_EVENT_LOGICAL_REWRITE_WRITE,
  815. WAIT_EVENT_RELATION_MAP_READ,
  816. WAIT_EVENT_RELATION_MAP_SYNC,
  817. WAIT_EVENT_RELATION_MAP_WRITE,
  818. WAIT_EVENT_REORDER_BUFFER_READ,
  819. WAIT_EVENT_REORDER_BUFFER_WRITE,
  820. WAIT_EVENT_REORDER_LOGICAL_MAPPING_READ,
  821. WAIT_EVENT_REPLICATION_SLOT_READ,
  822. WAIT_EVENT_REPLICATION_SLOT_RESTORE_SYNC,
  823. WAIT_EVENT_REPLICATION_SLOT_SYNC,
  824. WAIT_EVENT_REPLICATION_SLOT_WRITE,
  825. WAIT_EVENT_SLRU_FLUSH_SYNC,
  826. WAIT_EVENT_SLRU_READ,
  827. WAIT_EVENT_SLRU_SYNC,
  828. WAIT_EVENT_SLRU_WRITE,
  829. WAIT_EVENT_SNAPBUILD_READ,
  830. WAIT_EVENT_SNAPBUILD_SYNC,
  831. WAIT_EVENT_SNAPBUILD_WRITE,
  832. WAIT_EVENT_TIMELINE_HISTORY_FILE_SYNC,
  833. WAIT_EVENT_TIMELINE_HISTORY_FILE_WRITE,
  834. WAIT_EVENT_TIMELINE_HISTORY_READ,
  835. WAIT_EVENT_TIMELINE_HISTORY_SYNC,
  836. WAIT_EVENT_TIMELINE_HISTORY_WRITE,
  837. WAIT_EVENT_TWOPHASE_FILE_READ,
  838. WAIT_EVENT_TWOPHASE_FILE_SYNC,
  839. WAIT_EVENT_TWOPHASE_FILE_WRITE,
  840. WAIT_EVENT_WALSENDER_TIMELINE_HISTORY_READ,
  841. WAIT_EVENT_WAL_BOOTSTRAP_SYNC,
  842. WAIT_EVENT_WAL_BOOTSTRAP_WRITE,
  843. WAIT_EVENT_WAL_COPY_READ,
  844. WAIT_EVENT_WAL_COPY_SYNC,
  845. WAIT_EVENT_WAL_COPY_WRITE,
  846. WAIT_EVENT_WAL_INIT_SYNC,
  847. WAIT_EVENT_WAL_INIT_WRITE,
  848. WAIT_EVENT_WAL_READ,
  849. WAIT_EVENT_WAL_SYNC,
  850. WAIT_EVENT_WAL_SYNC_METHOD_ASSIGN,
  851. WAIT_EVENT_WAL_WRITE
  852. } WaitEventIO;
  853. /* ----------
  854. * Command type for progress reporting purposes
  855. * ----------
  856. */
  857. typedef enum ProgressCommandType
  858. {
  859. PROGRESS_COMMAND_INVALID,
  860. PROGRESS_COMMAND_VACUUM,
  861. PROGRESS_COMMAND_CLUSTER,
  862. PROGRESS_COMMAND_CREATE_INDEX
  863. } ProgressCommandType;
  864. #define PGSTAT_NUM_PROGRESS_PARAM 20
  865. /* ----------
  866. * Shared-memory data structures
  867. * ----------
  868. */
  869. /*
  870. * PgBackendSSLStatus
  871. *
  872. * For each backend, we keep the SSL status in a separate struct, that
  873. * is only filled in if SSL is enabled.
  874. *
  875. * All char arrays must be null-terminated.
  876. */
  877. typedef struct PgBackendSSLStatus
  878. {
  879. /* Information about SSL connection */
  880. int ssl_bits;
  881. bool ssl_compression;
  882. char ssl_version[NAMEDATALEN];
  883. char ssl_cipher[NAMEDATALEN];
  884. char ssl_client_dn[NAMEDATALEN];
  885. /*
  886. * serial number is max "20 octets" per RFC 5280, so this size should be
  887. * fine
  888. */
  889. char ssl_client_serial[NAMEDATALEN];
  890. char ssl_issuer_dn[NAMEDATALEN];
  891. } PgBackendSSLStatus;
  892. /*
  893. * PgBackendGSSStatus
  894. *
  895. * For each backend, we keep the GSS status in a separate struct, that
  896. * is only filled in if GSS is enabled.
  897. *
  898. * All char arrays must be null-terminated.
  899. */
  900. typedef struct PgBackendGSSStatus
  901. {
  902. /* Information about GSSAPI connection */
  903. char gss_princ[NAMEDATALEN]; /* GSSAPI Principal used to auth */
  904. bool gss_auth; /* If GSSAPI authentication was used */
  905. bool gss_enc; /* If encryption is being used */
  906. } PgBackendGSSStatus;
  907. /* ----------
  908. * PgBackendStatus
  909. *
  910. * Each live backend maintains a PgBackendStatus struct in shared memory
  911. * showing its current activity. (The structs are allocated according to
  912. * BackendId, but that is not critical.) Note that the collector process
  913. * has no involvement in, or even access to, these structs.
  914. *
  915. * Each auxiliary process also maintains a PgBackendStatus struct in shared
  916. * memory.
  917. * ----------
  918. */
  919. typedef struct PgBackendStatus
  920. {
  921. /*
  922. * To avoid locking overhead, we use the following protocol: a backend
  923. * increments st_changecount before modifying its entry, and again after
  924. * finishing a modification. A would-be reader should note the value of
  925. * st_changecount, copy the entry into private memory, then check
  926. * st_changecount again. If the value hasn't changed, and if it's even,
  927. * the copy is valid; otherwise start over. This makes updates cheap
  928. * while reads are potentially expensive, but that's the tradeoff we want.
  929. *
  930. * The above protocol needs memory barriers to ensure that the apparent
  931. * order of execution is as it desires. Otherwise, for example, the CPU
  932. * might rearrange the code so that st_changecount is incremented twice
  933. * before the modification on a machine with weak memory ordering. Hence,
  934. * use the macros defined below for manipulating st_changecount, rather
  935. * than touching it directly.
  936. */
  937. int st_changecount;
  938. /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
  939. int st_procpid;
  940. /* Type of backends */
  941. BackendType st_backendType;
  942. /* Times when current backend, transaction, and activity started */
  943. TimestampTz st_proc_start_timestamp;
  944. TimestampTz st_xact_start_timestamp;
  945. TimestampTz st_activity_start_timestamp;
  946. TimestampTz st_state_start_timestamp;
  947. /* Database OID, owning user's OID, connection client address */
  948. Oid st_databaseid;
  949. Oid st_userid;
  950. SockAddr st_clientaddr;
  951. char *st_clienthostname; /* MUST be null-terminated */
  952. /* Information about SSL connection */
  953. bool st_ssl;
  954. PgBackendSSLStatus *st_sslstatus;
  955. /* Information about GSSAPI connection */
  956. bool st_gss;
  957. PgBackendGSSStatus *st_gssstatus;
  958. /* current state */
  959. BackendState st_state;
  960. /* application name; MUST be null-terminated */
  961. char *st_appname;
  962. /*
  963. * Current command string; MUST be null-terminated. Note that this string
  964. * possibly is truncated in the middle of a multi-byte character. As
  965. * activity strings are stored more frequently than read, that allows to
  966. * move the cost of correct truncation to the display side. Use
  967. * pgstat_clip_activity() to truncate correctly.
  968. */
  969. char *st_activity_raw;
  970. /*
  971. * Command progress reporting. Any command which wishes can advertise
  972. * that it is running by setting st_progress_command,
  973. * st_progress_command_target, and st_progress_param[].
  974. * st_progress_command_target should be the OID of the relation which the
  975. * command targets (we assume there's just one, as this is meant for
  976. * utility commands), but the meaning of each element in the
  977. * st_progress_param array is command-specific.
  978. */
  979. ProgressCommandType st_progress_command;
  980. Oid st_progress_command_target;
  981. int64 st_progress_param[PGSTAT_NUM_PROGRESS_PARAM];
  982. } PgBackendStatus;
  983. /*
  984. * Macros to load and store st_changecount with appropriate memory barriers.
  985. *
  986. * Use PGSTAT_BEGIN_WRITE_ACTIVITY() before, and PGSTAT_END_WRITE_ACTIVITY()
  987. * after, modifying the current process's PgBackendStatus data. Note that,
  988. * since there is no mechanism for cleaning up st_changecount after an error,
  989. * THESE MACROS FORM A CRITICAL SECTION. Any error between them will be
  990. * promoted to PANIC, causing a database restart to clean up shared memory!
  991. * Hence, keep the critical section as short and straight-line as possible.
  992. * Aside from being safer, that minimizes the window in which readers will
  993. * have to loop.
  994. *
  995. * Reader logic should follow this sketch:
  996. *
  997. * for (;;)
  998. * {
  999. * int before_ct, after_ct;
  1000. *
  1001. * pgstat_begin_read_activity(beentry, before_ct);
  1002. * ... copy beentry data to local memory ...
  1003. * pgstat_end_read_activity(beentry, after_ct);
  1004. * if (pgstat_read_activity_complete(before_ct, after_ct))
  1005. * break;
  1006. * CHECK_FOR_INTERRUPTS();
  1007. * }
  1008. *
  1009. * For extra safety, we generally use volatile beentry pointers, although
  1010. * the memory barriers should theoretically be sufficient.
  1011. */
  1012. #define PGSTAT_BEGIN_WRITE_ACTIVITY(beentry) \
  1013. do { \
  1014. START_CRIT_SECTION(); \
  1015. (beentry)->st_changecount++; \
  1016. pg_write_barrier(); \
  1017. } while (0)
  1018. #define PGSTAT_END_WRITE_ACTIVITY(beentry) \
  1019. do { \
  1020. pg_write_barrier(); \
  1021. (beentry)->st_changecount++; \
  1022. Assert(((beentry)->st_changecount & 1) == 0); \
  1023. END_CRIT_SECTION(); \
  1024. } while (0)
  1025. #define pgstat_begin_read_activity(beentry, before_changecount) \
  1026. do { \
  1027. (before_changecount) = (beentry)->st_changecount; \
  1028. pg_read_barrier(); \
  1029. } while (0)
  1030. #define pgstat_end_read_activity(beentry, after_changecount) \
  1031. do { \
  1032. pg_read_barrier(); \
  1033. (after_changecount) = (beentry)->st_changecount; \
  1034. } while (0)
  1035. #define pgstat_read_activity_complete(before_changecount, after_changecount) \
  1036. ((before_changecount) == (after_changecount) && \
  1037. ((before_changecount) & 1) == 0)
  1038. /* ----------
  1039. * LocalPgBackendStatus
  1040. *
  1041. * When we build the backend status array, we use LocalPgBackendStatus to be
  1042. * able to add new values to the struct when needed without adding new fields
  1043. * to the shared memory. It contains the backend status as a first member.
  1044. * ----------
  1045. */
  1046. typedef struct LocalPgBackendStatus
  1047. {
  1048. /*
  1049. * Local version of the backend status entry.
  1050. */
  1051. PgBackendStatus backendStatus;
  1052. /*
  1053. * The xid of the current transaction if available, InvalidTransactionId
  1054. * if not.
  1055. */
  1056. TransactionId backend_xid;
  1057. /*
  1058. * The xmin of the current session if available, InvalidTransactionId if
  1059. * not.
  1060. */
  1061. TransactionId backend_xmin;
  1062. } LocalPgBackendStatus;
  1063. /*
  1064. * Working state needed to accumulate per-function-call timing statistics.
  1065. */
  1066. typedef struct PgStat_FunctionCallUsage
  1067. {
  1068. /* Link to function's hashtable entry (must still be there at exit!) */
  1069. /* NULL means we are not tracking the current function call */
  1070. PgStat_FunctionCounts *fs;
  1071. /* Total time previously charged to function, as of function start */
  1072. instr_time save_f_total_time;
  1073. /* Backend-wide total time as of function start */
  1074. instr_time save_total;
  1075. /* system clock as of function start */
  1076. instr_time f_start;
  1077. } PgStat_FunctionCallUsage;
  1078. /* ----------
  1079. * GUC parameters
  1080. * ----------
  1081. */
  1082. extern PGDLLIMPORT bool pgstat_track_activities;
  1083. extern PGDLLIMPORT bool pgstat_track_counts;
  1084. extern PGDLLIMPORT int pgstat_track_functions;
  1085. extern PGDLLIMPORT int pgstat_track_activity_query_size;
  1086. extern char *pgstat_stat_directory;
  1087. extern char *pgstat_stat_tmpname;
  1088. extern char *pgstat_stat_filename;
  1089. /*
  1090. * BgWriter statistics counters are updated directly by bgwriter and bufmgr
  1091. */
  1092. extern PgStat_MsgBgWriter BgWriterStats;
  1093. /*
  1094. * Updated by pgstat_count_buffer_*_time macros
  1095. */
  1096. extern PgStat_Counter pgStatBlockReadTime;
  1097. extern PgStat_Counter pgStatBlockWriteTime;
  1098. /* ----------
  1099. * Functions called from postmaster
  1100. * ----------
  1101. */
  1102. extern Size BackendStatusShmemSize(void);
  1103. extern void CreateSharedBackendStatus(void);
  1104. extern void pgstat_init(void);
  1105. extern int pgstat_start(void);
  1106. extern void pgstat_reset_all(void);
  1107. extern void allow_immediate_pgstat_restart(void);
  1108. #ifdef EXEC_BACKEND
  1109. extern void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn();
  1110. #endif
  1111. /* ----------
  1112. * Functions called from backends
  1113. * ----------
  1114. */
  1115. extern void pgstat_ping(void);
  1116. extern void pgstat_report_stat(bool force);
  1117. extern void pgstat_vacuum_stat(void);
  1118. extern void pgstat_drop_database(Oid databaseid);
  1119. extern void pgstat_clear_snapshot(void);
  1120. extern void pgstat_reset_counters(void);
  1121. extern void pgstat_reset_shared_counters(const char *);
  1122. extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type type);
  1123. extern void pgstat_report_autovac(Oid dboid);
  1124. extern void pgstat_report_vacuum(Oid tableoid, bool shared,
  1125. PgStat_Counter livetuples, PgStat_Counter deadtuples);
  1126. extern void pgstat_report_analyze(Relation rel,
  1127. PgStat_Counter livetuples, PgStat_Counter deadtuples,
  1128. bool resetcounter);
  1129. extern void pgstat_report_recovery_conflict(int reason);
  1130. extern void pgstat_report_deadlock(void);
  1131. extern void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount);
  1132. extern void pgstat_report_checksum_failure(void);
  1133. extern void pgstat_initialize(void);
  1134. extern void pgstat_bestart(void);
  1135. extern void pgstat_report_activity(BackendState state, const char *cmd_str);
  1136. extern void pgstat_report_tempfile(size_t filesize);
  1137. extern void pgstat_report_appname(const char *appname);
  1138. extern void pgstat_report_xact_timestamp(TimestampTz tstamp);
  1139. extern const char *pgstat_get_wait_event(uint32 wait_event_info);
  1140. extern const char *pgstat_get_wait_event_type(uint32 wait_event_info);
  1141. extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser);
  1142. extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer,
  1143. int buflen);
  1144. extern const char *pgstat_get_backend_desc(BackendType backendType);
  1145. extern void pgstat_progress_start_command(ProgressCommandType cmdtype,
  1146. Oid relid);
  1147. extern void pgstat_progress_update_param(int index, int64 val);
  1148. extern void pgstat_progress_update_multi_param(int nparam, const int *index,
  1149. const int64 *val);
  1150. extern void pgstat_progress_end_command(void);
  1151. extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
  1152. extern PgStat_BackendFunctionEntry *find_funcstat_entry(Oid func_id);
  1153. extern void pgstat_initstats(Relation rel);
  1154. extern char *pgstat_clip_activity(const char *raw_activity);
  1155. /* ----------
  1156. * pgstat_report_wait_start() -
  1157. *
  1158. * Called from places where server process needs to wait. This is called
  1159. * to report wait event information. The wait information is stored
  1160. * as 4-bytes where first byte represents the wait event class (type of
  1161. * wait, for different types of wait, refer WaitClass) and the next
  1162. * 3-bytes represent the actual wait event. Currently 2-bytes are used
  1163. * for wait event which is sufficient for current usage, 1-byte is
  1164. * reserved for future usage.
  1165. *
  1166. * NB: this *must* be able to survive being called before MyProc has been
  1167. * initialized.
  1168. * ----------
  1169. */
  1170. static inline void
  1171. pgstat_report_wait_start(uint32 wait_event_info)
  1172. {
  1173. volatile PGPROC *proc = MyProc;
  1174. if (!pgstat_track_activities || !proc)
  1175. return;
  1176. /*
  1177. * Since this is a four-byte field which is always read and written as
  1178. * four-bytes, updates are atomic.
  1179. */
  1180. proc->wait_event_info = wait_event_info;
  1181. }
  1182. /* ----------
  1183. * pgstat_report_wait_end() -
  1184. *
  1185. * Called to report end of a wait.
  1186. *
  1187. * NB: this *must* be able to survive being called before MyProc has been
  1188. * initialized.
  1189. * ----------
  1190. */
  1191. static inline void
  1192. pgstat_report_wait_end(void)
  1193. {
  1194. volatile PGPROC *proc = MyProc;
  1195. if (!pgstat_track_activities || !proc)
  1196. return;
  1197. /*
  1198. * Since this is a four-byte field which is always read and written as
  1199. * four-bytes, updates are atomic.
  1200. */
  1201. proc->wait_event_info = 0;
  1202. }
  1203. /* nontransactional event counts are simple enough to inline */
  1204. #define pgstat_count_heap_scan(rel) \
  1205. do { \
  1206. if ((rel)->pgstat_info != NULL) \
  1207. (rel)->pgstat_info->t_counts.t_numscans++; \
  1208. } while (0)
  1209. #define pgstat_count_heap_getnext(rel) \
  1210. do { \
  1211. if ((rel)->pgstat_info != NULL) \
  1212. (rel)->pgstat_info->t_counts.t_tuples_returned++; \
  1213. } while (0)
  1214. #define pgstat_count_heap_fetch(rel) \
  1215. do { \
  1216. if ((rel)->pgstat_info != NULL) \
  1217. (rel)->pgstat_info->t_counts.t_tuples_fetched++; \
  1218. } while (0)
  1219. #define pgstat_count_index_scan(rel) \
  1220. do { \
  1221. if ((rel)->pgstat_info != NULL) \
  1222. (rel)->pgstat_info->t_counts.t_numscans++; \
  1223. } while (0)
  1224. #define pgstat_count_index_tuples(rel, n) \
  1225. do { \
  1226. if ((rel)->pgstat_info != NULL) \
  1227. (rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
  1228. } while (0)
  1229. #define pgstat_count_buffer_read(rel) \
  1230. do { \
  1231. if ((rel)->pgstat_info != NULL) \
  1232. (rel)->pgstat_info->t_counts.t_blocks_fetched++; \
  1233. } while (0)
  1234. #define pgstat_count_buffer_hit(rel) \
  1235. do { \
  1236. if ((rel)->pgstat_info != NULL) \
  1237. (rel)->pgstat_info->t_counts.t_blocks_hit++; \
  1238. } while (0)
  1239. #define pgstat_count_buffer_read_time(n) \
  1240. (pgStatBlockReadTime += (n))
  1241. #define pgstat_count_buffer_write_time(n) \
  1242. (pgStatBlockWriteTime += (n))
  1243. extern void pgstat_count_heap_insert(Relation rel, PgStat_Counter n);
  1244. extern void pgstat_count_heap_update(Relation rel, bool hot);
  1245. extern void pgstat_count_heap_delete(Relation rel);
  1246. extern void pgstat_count_truncate(Relation rel);
  1247. extern void pgstat_update_heap_dead_tuples(Relation rel, int delta);
  1248. extern void pgstat_init_function_usage(FunctionCallInfo fcinfo,
  1249. PgStat_FunctionCallUsage *fcu);
  1250. extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu,
  1251. bool finalize);
  1252. extern void AtEOXact_PgStat(bool isCommit, bool parallel);
  1253. extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
  1254. extern void AtPrepare_PgStat(void);
  1255. extern void PostPrepare_PgStat(void);
  1256. extern void pgstat_twophase_postcommit(TransactionId xid, uint16 info,
  1257. void *recdata, uint32 len);
  1258. extern void pgstat_twophase_postabort(TransactionId xid, uint16 info,
  1259. void *recdata, uint32 len);
  1260. extern void pgstat_send_archiver(const char *xlog, bool failed);
  1261. extern void pgstat_send_bgwriter(void);
  1262. /* ----------
  1263. * Support functions for the SQL-callable functions to
  1264. * generate the pgstat* views.
  1265. * ----------
  1266. */
  1267. extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
  1268. extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
  1269. extern PgBackendStatus *pgstat_fetch_stat_beentry(int beid);
  1270. extern LocalPgBackendStatus *pgstat_fetch_stat_local_beentry(int beid);
  1271. extern PgStat_StatFuncEntry *pgstat_fetch_stat_funcentry(Oid funcid);
  1272. extern int pgstat_fetch_stat_numbackends(void);
  1273. extern PgStat_ArchiverStats *pgstat_fetch_stat_archiver(void);
  1274. extern PgStat_GlobalStats *pgstat_fetch_global(void);
  1275. #endif /* PGSTAT_H */
上海开阖软件有限公司 沪ICP备12045867号-1