Consistently use "superuser" instead of "super user"
[pgsql.git] / src / include / pgstat.h
blob509849c7ff4ae9df91f98089d6cfc4a2cea492d9
1 /* ----------
2 * pgstat.h
4 * Definitions for the PostgreSQL statistics collector daemon.
6 * Copyright (c) 2001-2021, PostgreSQL Global Development Group
8 * src/include/pgstat.h
9 * ----------
11 #ifndef PGSTAT_H
12 #define PGSTAT_H
14 #include "datatype/timestamp.h"
15 #include "portability/instr_time.h"
16 #include "postmaster/pgarch.h" /* for MAX_XFN_CHARS */
17 #include "utils/backend_progress.h" /* for backward compatibility */
18 #include "utils/backend_status.h" /* for backward compatibility */
19 #include "utils/hsearch.h"
20 #include "utils/relcache.h"
21 #include "utils/wait_event.h" /* for backward compatibility */
24 /* ----------
25 * Paths for the statistics files (relative to installation's $PGDATA).
26 * ----------
28 #define PGSTAT_STAT_PERMANENT_DIRECTORY "pg_stat"
29 #define PGSTAT_STAT_PERMANENT_FILENAME "pg_stat/global.stat"
30 #define PGSTAT_STAT_PERMANENT_TMPFILE "pg_stat/global.tmp"
32 /* Default directory to store temporary statistics data in */
33 #define PG_STAT_TMP_DIR "pg_stat_tmp"
35 /* Values for track_functions GUC variable --- order is significant! */
36 typedef enum TrackFunctionsLevel
38 TRACK_FUNC_OFF,
39 TRACK_FUNC_PL,
40 TRACK_FUNC_ALL
41 } TrackFunctionsLevel;
43 /* Values to track the cause of session termination */
44 typedef enum SessionEndType
46 DISCONNECT_NOT_YET, /* still active */
47 DISCONNECT_NORMAL,
48 DISCONNECT_CLIENT_EOF,
49 DISCONNECT_FATAL,
50 DISCONNECT_KILLED
51 } SessionEndType;
53 /* ----------
54 * The types of backend -> collector messages
55 * ----------
57 typedef enum StatMsgType
59 PGSTAT_MTYPE_DUMMY,
60 PGSTAT_MTYPE_INQUIRY,
61 PGSTAT_MTYPE_TABSTAT,
62 PGSTAT_MTYPE_TABPURGE,
63 PGSTAT_MTYPE_DROPDB,
64 PGSTAT_MTYPE_RESETCOUNTER,
65 PGSTAT_MTYPE_RESETSHAREDCOUNTER,
66 PGSTAT_MTYPE_RESETSINGLECOUNTER,
67 PGSTAT_MTYPE_RESETSLRUCOUNTER,
68 PGSTAT_MTYPE_RESETREPLSLOTCOUNTER,
69 PGSTAT_MTYPE_AUTOVAC_START,
70 PGSTAT_MTYPE_VACUUM,
71 PGSTAT_MTYPE_ANALYZE,
72 PGSTAT_MTYPE_ARCHIVER,
73 PGSTAT_MTYPE_BGWRITER,
74 PGSTAT_MTYPE_CHECKPOINTER,
75 PGSTAT_MTYPE_WAL,
76 PGSTAT_MTYPE_SLRU,
77 PGSTAT_MTYPE_FUNCSTAT,
78 PGSTAT_MTYPE_FUNCPURGE,
79 PGSTAT_MTYPE_RECOVERYCONFLICT,
80 PGSTAT_MTYPE_TEMPFILE,
81 PGSTAT_MTYPE_DEADLOCK,
82 PGSTAT_MTYPE_CHECKSUMFAILURE,
83 PGSTAT_MTYPE_REPLSLOT,
84 PGSTAT_MTYPE_CONNECTION,
85 } StatMsgType;
87 /* ----------
88 * The data type used for counters.
89 * ----------
91 typedef int64 PgStat_Counter;
93 /* ----------
94 * PgStat_TableCounts The actual per-table counts kept by a backend
96 * This struct should contain only actual event counters, because we memcmp
97 * it against zeroes to detect whether there are any counts to transmit.
98 * It is a component of PgStat_TableStatus (within-backend state) and
99 * PgStat_TableEntry (the transmitted message format).
101 * Note: for a table, tuples_returned is the number of tuples successfully
102 * fetched by heap_getnext, while tuples_fetched is the number of tuples
103 * successfully fetched by heap_fetch under the control of bitmap indexscans.
104 * For an index, tuples_returned is the number of index entries returned by
105 * the index AM, while tuples_fetched is the number of tuples successfully
106 * fetched by heap_fetch under the control of simple indexscans for this index.
108 * tuples_inserted/updated/deleted/hot_updated count attempted actions,
109 * regardless of whether the transaction committed. delta_live_tuples,
110 * delta_dead_tuples, and changed_tuples are set depending on commit or abort.
111 * Note that delta_live_tuples and delta_dead_tuples can be negative!
112 * ----------
114 typedef struct PgStat_TableCounts
116 PgStat_Counter t_numscans;
118 PgStat_Counter t_tuples_returned;
119 PgStat_Counter t_tuples_fetched;
121 PgStat_Counter t_tuples_inserted;
122 PgStat_Counter t_tuples_updated;
123 PgStat_Counter t_tuples_deleted;
124 PgStat_Counter t_tuples_hot_updated;
125 bool t_truncated;
127 PgStat_Counter t_delta_live_tuples;
128 PgStat_Counter t_delta_dead_tuples;
129 PgStat_Counter t_changed_tuples;
131 PgStat_Counter t_blocks_fetched;
132 PgStat_Counter t_blocks_hit;
133 } PgStat_TableCounts;
135 /* Possible targets for resetting cluster-wide shared values */
136 typedef enum PgStat_Shared_Reset_Target
138 RESET_ARCHIVER,
139 RESET_BGWRITER,
140 RESET_WAL
141 } PgStat_Shared_Reset_Target;
143 /* Possible object types for resetting single counters */
144 typedef enum PgStat_Single_Reset_Type
146 RESET_TABLE,
147 RESET_FUNCTION
148 } PgStat_Single_Reset_Type;
150 /* ------------------------------------------------------------
151 * Structures kept in backend local memory while accumulating counts
152 * ------------------------------------------------------------
156 /* ----------
157 * PgStat_TableStatus Per-table status within a backend
159 * Many of the event counters are nontransactional, ie, we count events
160 * in committed and aborted transactions alike. For these, we just count
161 * directly in the PgStat_TableStatus. However, delta_live_tuples,
162 * delta_dead_tuples, and changed_tuples must be derived from event counts
163 * with awareness of whether the transaction or subtransaction committed or
164 * aborted. Hence, we also keep a stack of per-(sub)transaction status
165 * records for every table modified in the current transaction. At commit
166 * or abort, we propagate tuples_inserted/updated/deleted up to the
167 * parent subtransaction level, or out to the parent PgStat_TableStatus,
168 * as appropriate.
169 * ----------
171 typedef struct PgStat_TableStatus
173 Oid t_id; /* table's OID */
174 bool t_shared; /* is it a shared catalog? */
175 struct PgStat_TableXactStatus *trans; /* lowest subxact's counts */
176 PgStat_TableCounts t_counts; /* event counts to be sent */
177 } PgStat_TableStatus;
179 /* ----------
180 * PgStat_TableXactStatus Per-table, per-subtransaction status
181 * ----------
183 typedef struct PgStat_TableXactStatus
185 PgStat_Counter tuples_inserted; /* tuples inserted in (sub)xact */
186 PgStat_Counter tuples_updated; /* tuples updated in (sub)xact */
187 PgStat_Counter tuples_deleted; /* tuples deleted in (sub)xact */
188 bool truncated; /* relation truncated in this (sub)xact */
189 PgStat_Counter inserted_pre_trunc; /* tuples inserted prior to truncate */
190 PgStat_Counter updated_pre_trunc; /* tuples updated prior to truncate */
191 PgStat_Counter deleted_pre_trunc; /* tuples deleted prior to truncate */
192 int nest_level; /* subtransaction nest level */
193 /* links to other structs for same relation: */
194 struct PgStat_TableXactStatus *upper; /* next higher subxact if any */
195 PgStat_TableStatus *parent; /* per-table status */
196 /* structs of same subxact level are linked here: */
197 struct PgStat_TableXactStatus *next; /* next of same subxact */
198 } PgStat_TableXactStatus;
201 /* ------------------------------------------------------------
202 * Message formats follow
203 * ------------------------------------------------------------
207 /* ----------
208 * PgStat_MsgHdr The common message header
209 * ----------
211 typedef struct PgStat_MsgHdr
213 StatMsgType m_type;
214 int m_size;
215 } PgStat_MsgHdr;
217 /* ----------
218 * Space available in a message. This will keep the UDP packets below 1K,
219 * which should fit unfragmented into the MTU of the loopback interface.
220 * (Larger values of PGSTAT_MAX_MSG_SIZE would work for that on most
221 * platforms, but we're being conservative here.)
222 * ----------
224 #define PGSTAT_MAX_MSG_SIZE 1000
225 #define PGSTAT_MSG_PAYLOAD (PGSTAT_MAX_MSG_SIZE - sizeof(PgStat_MsgHdr))
228 /* ----------
229 * PgStat_MsgDummy A dummy message, ignored by the collector
230 * ----------
232 typedef struct PgStat_MsgDummy
234 PgStat_MsgHdr m_hdr;
235 } PgStat_MsgDummy;
238 /* ----------
239 * PgStat_MsgInquiry Sent by a backend to ask the collector
240 * to write the stats file(s).
242 * Ordinarily, an inquiry message prompts writing of the global stats file,
243 * the stats file for shared catalogs, and the stats file for the specified
244 * database. If databaseid is InvalidOid, only the first two are written.
246 * New file(s) will be written only if the existing file has a timestamp
247 * older than the specified cutoff_time; this prevents duplicated effort
248 * when multiple requests arrive at nearly the same time, assuming that
249 * backends send requests with cutoff_times a little bit in the past.
251 * clock_time should be the requestor's current local time; the collector
252 * uses this to check for the system clock going backward, but it has no
253 * effect unless that occurs. We assume clock_time >= cutoff_time, though.
254 * ----------
257 typedef struct PgStat_MsgInquiry
259 PgStat_MsgHdr m_hdr;
260 TimestampTz clock_time; /* observed local clock time */
261 TimestampTz cutoff_time; /* minimum acceptable file timestamp */
262 Oid databaseid; /* requested DB (InvalidOid => shared only) */
263 } PgStat_MsgInquiry;
266 /* ----------
267 * PgStat_TableEntry Per-table info in a MsgTabstat
268 * ----------
270 typedef struct PgStat_TableEntry
272 Oid t_id;
273 PgStat_TableCounts t_counts;
274 } PgStat_TableEntry;
276 /* ----------
277 * PgStat_MsgTabstat Sent by the backend to report table
278 * and buffer access statistics.
279 * ----------
281 #define PGSTAT_NUM_TABENTRIES \
282 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int) - 2 * sizeof(PgStat_Counter)) \
283 / sizeof(PgStat_TableEntry))
285 typedef struct PgStat_MsgTabstat
287 PgStat_MsgHdr m_hdr;
288 Oid m_databaseid;
289 int m_nentries;
290 int m_xact_commit;
291 int m_xact_rollback;
292 PgStat_Counter m_block_read_time; /* times in microseconds */
293 PgStat_Counter m_block_write_time;
294 PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
295 } PgStat_MsgTabstat;
298 /* ----------
299 * PgStat_MsgTabpurge Sent by the backend to tell the collector
300 * about dead tables.
301 * ----------
303 #define PGSTAT_NUM_TABPURGE \
304 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
305 / sizeof(Oid))
307 typedef struct PgStat_MsgTabpurge
309 PgStat_MsgHdr m_hdr;
310 Oid m_databaseid;
311 int m_nentries;
312 Oid m_tableid[PGSTAT_NUM_TABPURGE];
313 } PgStat_MsgTabpurge;
316 /* ----------
317 * PgStat_MsgDropdb Sent by the backend to tell the collector
318 * about a dropped database
319 * ----------
321 typedef struct PgStat_MsgDropdb
323 PgStat_MsgHdr m_hdr;
324 Oid m_databaseid;
325 } PgStat_MsgDropdb;
328 /* ----------
329 * PgStat_MsgResetcounter Sent by the backend to tell the collector
330 * to reset counters
331 * ----------
333 typedef struct PgStat_MsgResetcounter
335 PgStat_MsgHdr m_hdr;
336 Oid m_databaseid;
337 } PgStat_MsgResetcounter;
339 /* ----------
340 * PgStat_MsgResetsharedcounter Sent by the backend to tell the collector
341 * to reset a shared counter
342 * ----------
344 typedef struct PgStat_MsgResetsharedcounter
346 PgStat_MsgHdr m_hdr;
347 PgStat_Shared_Reset_Target m_resettarget;
348 } PgStat_MsgResetsharedcounter;
350 /* ----------
351 * PgStat_MsgResetsinglecounter Sent by the backend to tell the collector
352 * to reset a single counter
353 * ----------
355 typedef struct PgStat_MsgResetsinglecounter
357 PgStat_MsgHdr m_hdr;
358 Oid m_databaseid;
359 PgStat_Single_Reset_Type m_resettype;
360 Oid m_objectid;
361 } PgStat_MsgResetsinglecounter;
363 /* ----------
364 * PgStat_MsgResetslrucounter Sent by the backend to tell the collector
365 * to reset a SLRU counter
366 * ----------
368 typedef struct PgStat_MsgResetslrucounter
370 PgStat_MsgHdr m_hdr;
371 int m_index;
372 } PgStat_MsgResetslrucounter;
374 /* ----------
375 * PgStat_MsgResetreplslotcounter Sent by the backend to tell the collector
376 * to reset replication slot counter(s)
377 * ----------
379 typedef struct PgStat_MsgResetreplslotcounter
381 PgStat_MsgHdr m_hdr;
382 NameData m_slotname;
383 bool clearall;
384 } PgStat_MsgResetreplslotcounter;
386 /* ----------
387 * PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
388 * that a database is going to be processed
389 * ----------
391 typedef struct PgStat_MsgAutovacStart
393 PgStat_MsgHdr m_hdr;
394 Oid m_databaseid;
395 TimestampTz m_start_time;
396 } PgStat_MsgAutovacStart;
399 /* ----------
400 * PgStat_MsgVacuum Sent by the backend or autovacuum daemon
401 * after VACUUM
402 * ----------
404 typedef struct PgStat_MsgVacuum
406 PgStat_MsgHdr m_hdr;
407 Oid m_databaseid;
408 Oid m_tableoid;
409 bool m_autovacuum;
410 TimestampTz m_vacuumtime;
411 PgStat_Counter m_live_tuples;
412 PgStat_Counter m_dead_tuples;
413 } PgStat_MsgVacuum;
416 /* ----------
417 * PgStat_MsgAnalyze Sent by the backend or autovacuum daemon
418 * after ANALYZE
419 * ----------
421 typedef struct PgStat_MsgAnalyze
423 PgStat_MsgHdr m_hdr;
424 Oid m_databaseid;
425 Oid m_tableoid;
426 bool m_autovacuum;
427 bool m_resetcounter;
428 TimestampTz m_analyzetime;
429 PgStat_Counter m_live_tuples;
430 PgStat_Counter m_dead_tuples;
431 } PgStat_MsgAnalyze;
434 /* ----------
435 * PgStat_MsgArchiver Sent by the archiver to update statistics.
436 * ----------
438 typedef struct PgStat_MsgArchiver
440 PgStat_MsgHdr m_hdr;
441 bool m_failed; /* Failed attempt */
442 char m_xlog[MAX_XFN_CHARS + 1];
443 TimestampTz m_timestamp;
444 } PgStat_MsgArchiver;
446 /* ----------
447 * PgStat_MsgBgWriter Sent by the bgwriter to update statistics.
448 * ----------
450 typedef struct PgStat_MsgBgWriter
452 PgStat_MsgHdr m_hdr;
454 PgStat_Counter m_buf_written_clean;
455 PgStat_Counter m_maxwritten_clean;
456 PgStat_Counter m_buf_alloc;
457 } PgStat_MsgBgWriter;
459 /* ----------
460 * PgStat_MsgCheckpointer Sent by the checkpointer to update statistics.
461 * ----------
463 typedef struct PgStat_MsgCheckpointer
465 PgStat_MsgHdr m_hdr;
467 PgStat_Counter m_timed_checkpoints;
468 PgStat_Counter m_requested_checkpoints;
469 PgStat_Counter m_buf_written_checkpoints;
470 PgStat_Counter m_buf_written_backend;
471 PgStat_Counter m_buf_fsync_backend;
472 PgStat_Counter m_checkpoint_write_time; /* times in milliseconds */
473 PgStat_Counter m_checkpoint_sync_time;
474 } PgStat_MsgCheckpointer;
476 /* ----------
477 * PgStat_MsgWal Sent by backends and background processes to update WAL statistics.
478 * ----------
480 typedef struct PgStat_MsgWal
482 PgStat_MsgHdr m_hdr;
483 PgStat_Counter m_wal_records;
484 PgStat_Counter m_wal_fpi;
485 uint64 m_wal_bytes;
486 PgStat_Counter m_wal_buffers_full;
487 PgStat_Counter m_wal_write;
488 PgStat_Counter m_wal_sync;
489 PgStat_Counter m_wal_write_time; /* time spent writing wal records in
490 * microseconds */
491 PgStat_Counter m_wal_sync_time; /* time spent syncing wal records in
492 * microseconds */
493 } PgStat_MsgWal;
495 /* ----------
496 * PgStat_MsgSLRU Sent by a backend to update SLRU statistics.
497 * ----------
499 typedef struct PgStat_MsgSLRU
501 PgStat_MsgHdr m_hdr;
502 PgStat_Counter m_index;
503 PgStat_Counter m_blocks_zeroed;
504 PgStat_Counter m_blocks_hit;
505 PgStat_Counter m_blocks_read;
506 PgStat_Counter m_blocks_written;
507 PgStat_Counter m_blocks_exists;
508 PgStat_Counter m_flush;
509 PgStat_Counter m_truncate;
510 } PgStat_MsgSLRU;
512 /* ----------
513 * PgStat_MsgReplSlot Sent by a backend or a wal sender to update replication
514 * slot statistics.
515 * ----------
517 typedef struct PgStat_MsgReplSlot
519 PgStat_MsgHdr m_hdr;
520 NameData m_slotname;
521 bool m_create;
522 bool m_drop;
523 PgStat_Counter m_spill_txns;
524 PgStat_Counter m_spill_count;
525 PgStat_Counter m_spill_bytes;
526 PgStat_Counter m_stream_txns;
527 PgStat_Counter m_stream_count;
528 PgStat_Counter m_stream_bytes;
529 PgStat_Counter m_total_txns;
530 PgStat_Counter m_total_bytes;
531 } PgStat_MsgReplSlot;
534 /* ----------
535 * PgStat_MsgRecoveryConflict Sent by the backend upon recovery conflict
536 * ----------
538 typedef struct PgStat_MsgRecoveryConflict
540 PgStat_MsgHdr m_hdr;
542 Oid m_databaseid;
543 int m_reason;
544 } PgStat_MsgRecoveryConflict;
546 /* ----------
547 * PgStat_MsgTempFile Sent by the backend upon creating a temp file
548 * ----------
550 typedef struct PgStat_MsgTempFile
552 PgStat_MsgHdr m_hdr;
554 Oid m_databaseid;
555 size_t m_filesize;
556 } PgStat_MsgTempFile;
558 /* ----------
559 * PgStat_FunctionCounts The actual per-function counts kept by a backend
561 * This struct should contain only actual event counters, because we memcmp
562 * it against zeroes to detect whether there are any counts to transmit.
564 * Note that the time counters are in instr_time format here. We convert to
565 * microseconds in PgStat_Counter format when transmitting to the collector.
566 * ----------
568 typedef struct PgStat_FunctionCounts
570 PgStat_Counter f_numcalls;
571 instr_time f_total_time;
572 instr_time f_self_time;
573 } PgStat_FunctionCounts;
575 /* ----------
576 * PgStat_BackendFunctionEntry Entry in backend's per-function hash table
577 * ----------
579 typedef struct PgStat_BackendFunctionEntry
581 Oid f_id;
582 PgStat_FunctionCounts f_counts;
583 } PgStat_BackendFunctionEntry;
585 /* ----------
586 * PgStat_FunctionEntry Per-function info in a MsgFuncstat
587 * ----------
589 typedef struct PgStat_FunctionEntry
591 Oid f_id;
592 PgStat_Counter f_numcalls;
593 PgStat_Counter f_total_time; /* times in microseconds */
594 PgStat_Counter f_self_time;
595 } PgStat_FunctionEntry;
597 /* ----------
598 * PgStat_MsgFuncstat Sent by the backend to report function
599 * usage statistics.
600 * ----------
602 #define PGSTAT_NUM_FUNCENTRIES \
603 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
604 / sizeof(PgStat_FunctionEntry))
606 typedef struct PgStat_MsgFuncstat
608 PgStat_MsgHdr m_hdr;
609 Oid m_databaseid;
610 int m_nentries;
611 PgStat_FunctionEntry m_entry[PGSTAT_NUM_FUNCENTRIES];
612 } PgStat_MsgFuncstat;
614 /* ----------
615 * PgStat_MsgFuncpurge Sent by the backend to tell the collector
616 * about dead functions.
617 * ----------
619 #define PGSTAT_NUM_FUNCPURGE \
620 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
621 / sizeof(Oid))
623 typedef struct PgStat_MsgFuncpurge
625 PgStat_MsgHdr m_hdr;
626 Oid m_databaseid;
627 int m_nentries;
628 Oid m_functionid[PGSTAT_NUM_FUNCPURGE];
629 } PgStat_MsgFuncpurge;
631 /* ----------
632 * PgStat_MsgDeadlock Sent by the backend to tell the collector
633 * about a deadlock that occurred.
634 * ----------
636 typedef struct PgStat_MsgDeadlock
638 PgStat_MsgHdr m_hdr;
639 Oid m_databaseid;
640 } PgStat_MsgDeadlock;
642 /* ----------
643 * PgStat_MsgChecksumFailure Sent by the backend to tell the collector
644 * about checksum failures noticed.
645 * ----------
647 typedef struct PgStat_MsgChecksumFailure
649 PgStat_MsgHdr m_hdr;
650 Oid m_databaseid;
651 int m_failurecount;
652 TimestampTz m_failure_time;
653 } PgStat_MsgChecksumFailure;
655 /* ----------
656 * PgStat_MsgConn Sent by the backend to update connection statistics.
657 * ----------
659 typedef struct PgStat_MsgConn
661 PgStat_MsgHdr m_hdr;
662 Oid m_databaseid;
663 PgStat_Counter m_count;
664 PgStat_Counter m_session_time;
665 PgStat_Counter m_active_time;
666 PgStat_Counter m_idle_in_xact_time;
667 SessionEndType m_disconnect;
668 } PgStat_MsgConn;
671 /* ----------
672 * PgStat_Msg Union over all possible messages.
673 * ----------
675 typedef union PgStat_Msg
677 PgStat_MsgHdr msg_hdr;
678 PgStat_MsgDummy msg_dummy;
679 PgStat_MsgInquiry msg_inquiry;
680 PgStat_MsgTabstat msg_tabstat;
681 PgStat_MsgTabpurge msg_tabpurge;
682 PgStat_MsgDropdb msg_dropdb;
683 PgStat_MsgResetcounter msg_resetcounter;
684 PgStat_MsgResetsharedcounter msg_resetsharedcounter;
685 PgStat_MsgResetsinglecounter msg_resetsinglecounter;
686 PgStat_MsgResetslrucounter msg_resetslrucounter;
687 PgStat_MsgResetreplslotcounter msg_resetreplslotcounter;
688 PgStat_MsgAutovacStart msg_autovacuum_start;
689 PgStat_MsgVacuum msg_vacuum;
690 PgStat_MsgAnalyze msg_analyze;
691 PgStat_MsgArchiver msg_archiver;
692 PgStat_MsgBgWriter msg_bgwriter;
693 PgStat_MsgCheckpointer msg_checkpointer;
694 PgStat_MsgWal msg_wal;
695 PgStat_MsgSLRU msg_slru;
696 PgStat_MsgFuncstat msg_funcstat;
697 PgStat_MsgFuncpurge msg_funcpurge;
698 PgStat_MsgRecoveryConflict msg_recoveryconflict;
699 PgStat_MsgDeadlock msg_deadlock;
700 PgStat_MsgTempFile msg_tempfile;
701 PgStat_MsgChecksumFailure msg_checksumfailure;
702 PgStat_MsgReplSlot msg_replslot;
703 PgStat_MsgConn msg_conn;
704 } PgStat_Msg;
707 /* ------------------------------------------------------------
708 * Statistic collector data structures follow
710 * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
711 * data structures change.
712 * ------------------------------------------------------------
715 #define PGSTAT_FILE_FORMAT_ID 0x01A5BCA4
717 /* ----------
718 * PgStat_StatDBEntry The collector's data per database
719 * ----------
721 typedef struct PgStat_StatDBEntry
723 Oid databaseid;
724 PgStat_Counter n_xact_commit;
725 PgStat_Counter n_xact_rollback;
726 PgStat_Counter n_blocks_fetched;
727 PgStat_Counter n_blocks_hit;
728 PgStat_Counter n_tuples_returned;
729 PgStat_Counter n_tuples_fetched;
730 PgStat_Counter n_tuples_inserted;
731 PgStat_Counter n_tuples_updated;
732 PgStat_Counter n_tuples_deleted;
733 TimestampTz last_autovac_time;
734 PgStat_Counter n_conflict_tablespace;
735 PgStat_Counter n_conflict_lock;
736 PgStat_Counter n_conflict_snapshot;
737 PgStat_Counter n_conflict_bufferpin;
738 PgStat_Counter n_conflict_startup_deadlock;
739 PgStat_Counter n_temp_files;
740 PgStat_Counter n_temp_bytes;
741 PgStat_Counter n_deadlocks;
742 PgStat_Counter n_checksum_failures;
743 TimestampTz last_checksum_failure;
744 PgStat_Counter n_block_read_time; /* times in microseconds */
745 PgStat_Counter n_block_write_time;
746 PgStat_Counter n_sessions;
747 PgStat_Counter total_session_time;
748 PgStat_Counter total_active_time;
749 PgStat_Counter total_idle_in_xact_time;
750 PgStat_Counter n_sessions_abandoned;
751 PgStat_Counter n_sessions_fatal;
752 PgStat_Counter n_sessions_killed;
754 TimestampTz stat_reset_timestamp;
755 TimestampTz stats_timestamp; /* time of db stats file update */
758 * tables and functions must be last in the struct, because we don't write
759 * the pointers out to the stats file.
761 HTAB *tables;
762 HTAB *functions;
763 } PgStat_StatDBEntry;
766 /* ----------
767 * PgStat_StatTabEntry The collector's data per table (or index)
768 * ----------
770 typedef struct PgStat_StatTabEntry
772 Oid tableid;
774 PgStat_Counter numscans;
776 PgStat_Counter tuples_returned;
777 PgStat_Counter tuples_fetched;
779 PgStat_Counter tuples_inserted;
780 PgStat_Counter tuples_updated;
781 PgStat_Counter tuples_deleted;
782 PgStat_Counter tuples_hot_updated;
784 PgStat_Counter n_live_tuples;
785 PgStat_Counter n_dead_tuples;
786 PgStat_Counter changes_since_analyze;
787 PgStat_Counter inserts_since_vacuum;
789 PgStat_Counter blocks_fetched;
790 PgStat_Counter blocks_hit;
792 TimestampTz vacuum_timestamp; /* user initiated vacuum */
793 PgStat_Counter vacuum_count;
794 TimestampTz autovac_vacuum_timestamp; /* autovacuum initiated */
795 PgStat_Counter autovac_vacuum_count;
796 TimestampTz analyze_timestamp; /* user initiated */
797 PgStat_Counter analyze_count;
798 TimestampTz autovac_analyze_timestamp; /* autovacuum initiated */
799 PgStat_Counter autovac_analyze_count;
800 } PgStat_StatTabEntry;
803 /* ----------
804 * PgStat_StatFuncEntry The collector's data per function
805 * ----------
807 typedef struct PgStat_StatFuncEntry
809 Oid functionid;
811 PgStat_Counter f_numcalls;
813 PgStat_Counter f_total_time; /* times in microseconds */
814 PgStat_Counter f_self_time;
815 } PgStat_StatFuncEntry;
819 * Archiver statistics kept in the stats collector
821 typedef struct PgStat_ArchiverStats
823 PgStat_Counter archived_count; /* archival successes */
824 char last_archived_wal[MAX_XFN_CHARS + 1]; /* last WAL file
825 * archived */
826 TimestampTz last_archived_timestamp; /* last archival success time */
827 PgStat_Counter failed_count; /* failed archival attempts */
828 char last_failed_wal[MAX_XFN_CHARS + 1]; /* WAL file involved in
829 * last failure */
830 TimestampTz last_failed_timestamp; /* last archival failure time */
831 TimestampTz stat_reset_timestamp;
832 } PgStat_ArchiverStats;
835 * Background writer statistics kept in the stats collector
837 typedef struct PgStat_BgWriterStats
839 PgStat_Counter buf_written_clean;
840 PgStat_Counter maxwritten_clean;
841 PgStat_Counter buf_alloc;
842 TimestampTz stat_reset_timestamp;
843 } PgStat_BgWriterStats;
846 * Checkpointer statistics kept in the stats collector
848 typedef struct PgStat_CheckpointerStats
850 TimestampTz stats_timestamp; /* time of stats file update */
851 PgStat_Counter timed_checkpoints;
852 PgStat_Counter requested_checkpoints;
853 PgStat_Counter checkpoint_write_time; /* times in milliseconds */
854 PgStat_Counter checkpoint_sync_time;
855 PgStat_Counter buf_written_checkpoints;
856 PgStat_Counter buf_written_backend;
857 PgStat_Counter buf_fsync_backend;
858 } PgStat_CheckpointerStats;
861 * Global statistics kept in the stats collector
863 typedef struct PgStat_GlobalStats
865 TimestampTz stats_timestamp; /* time of stats file update */
867 PgStat_CheckpointerStats checkpointer;
868 PgStat_BgWriterStats bgwriter;
869 } PgStat_GlobalStats;
872 * WAL statistics kept in the stats collector
874 typedef struct PgStat_WalStats
876 PgStat_Counter wal_records;
877 PgStat_Counter wal_fpi;
878 uint64 wal_bytes;
879 PgStat_Counter wal_buffers_full;
880 PgStat_Counter wal_write;
881 PgStat_Counter wal_sync;
882 PgStat_Counter wal_write_time;
883 PgStat_Counter wal_sync_time;
884 TimestampTz stat_reset_timestamp;
885 } PgStat_WalStats;
888 * SLRU statistics kept in the stats collector
890 typedef struct PgStat_SLRUStats
892 PgStat_Counter blocks_zeroed;
893 PgStat_Counter blocks_hit;
894 PgStat_Counter blocks_read;
895 PgStat_Counter blocks_written;
896 PgStat_Counter blocks_exists;
897 PgStat_Counter flush;
898 PgStat_Counter truncate;
899 TimestampTz stat_reset_timestamp;
900 } PgStat_SLRUStats;
903 * Replication slot statistics kept in the stats collector
905 typedef struct PgStat_StatReplSlotEntry
907 NameData slotname;
908 PgStat_Counter spill_txns;
909 PgStat_Counter spill_count;
910 PgStat_Counter spill_bytes;
911 PgStat_Counter stream_txns;
912 PgStat_Counter stream_count;
913 PgStat_Counter stream_bytes;
914 PgStat_Counter total_txns;
915 PgStat_Counter total_bytes;
916 TimestampTz stat_reset_timestamp;
917 } PgStat_StatReplSlotEntry;
921 * Working state needed to accumulate per-function-call timing statistics.
923 typedef struct PgStat_FunctionCallUsage
925 /* Link to function's hashtable entry (must still be there at exit!) */
926 /* NULL means we are not tracking the current function call */
927 PgStat_FunctionCounts *fs;
928 /* Total time previously charged to function, as of function start */
929 instr_time save_f_total_time;
930 /* Backend-wide total time as of function start */
931 instr_time save_total;
932 /* system clock as of function start */
933 instr_time f_start;
934 } PgStat_FunctionCallUsage;
937 /* ----------
938 * GUC parameters
939 * ----------
941 extern PGDLLIMPORT bool pgstat_track_counts;
942 extern PGDLLIMPORT int pgstat_track_functions;
943 extern char *pgstat_stat_directory;
944 extern char *pgstat_stat_tmpname;
945 extern char *pgstat_stat_filename;
948 * BgWriter statistics counters are updated directly by bgwriter and bufmgr
950 extern PgStat_MsgBgWriter PendingBgWriterStats;
953 * Checkpointer statistics counters are updated directly by checkpointer and
954 * bufmgr.
956 extern PgStat_MsgCheckpointer PendingCheckpointerStats;
959 * WAL statistics counter is updated by backends and background processes
961 extern PgStat_MsgWal WalStats;
964 * Updated by pgstat_count_buffer_*_time macros
966 extern PgStat_Counter pgStatBlockReadTime;
967 extern PgStat_Counter pgStatBlockWriteTime;
970 * Updated by pgstat_count_conn_*_time macros, called by
971 * pgstat_report_activity().
973 extern PgStat_Counter pgStatActiveTime;
974 extern PgStat_Counter pgStatTransactionIdleTime;
978 * Updated by the traffic cop and in errfinish()
980 extern SessionEndType pgStatSessionEndCause;
982 /* ----------
983 * Functions called from postmaster
984 * ----------
986 extern void pgstat_init(void);
987 extern int pgstat_start(void);
988 extern void pgstat_reset_all(void);
989 extern void allow_immediate_pgstat_restart(void);
991 #ifdef EXEC_BACKEND
992 extern void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn();
993 #endif
996 /* ----------
997 * Functions called from backends
998 * ----------
1000 extern void pgstat_ping(void);
1002 extern void pgstat_report_stat(bool force);
1003 extern void pgstat_vacuum_stat(void);
1004 extern void pgstat_drop_database(Oid databaseid);
1006 extern void pgstat_clear_snapshot(void);
1007 extern void pgstat_reset_counters(void);
1008 extern void pgstat_reset_shared_counters(const char *);
1009 extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type type);
1010 extern void pgstat_reset_slru_counter(const char *);
1011 extern void pgstat_reset_replslot_counter(const char *name);
1013 extern void pgstat_report_autovac(Oid dboid);
1014 extern void pgstat_report_vacuum(Oid tableoid, bool shared,
1015 PgStat_Counter livetuples, PgStat_Counter deadtuples);
1016 extern void pgstat_report_analyze(Relation rel,
1017 PgStat_Counter livetuples, PgStat_Counter deadtuples,
1018 bool resetcounter);
1020 extern void pgstat_report_recovery_conflict(int reason);
1021 extern void pgstat_report_deadlock(void);
1022 extern void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount);
1023 extern void pgstat_report_checksum_failure(void);
1024 extern void pgstat_report_replslot(const PgStat_StatReplSlotEntry *repSlotStat);
1025 extern void pgstat_report_replslot_create(const char *slotname);
1026 extern void pgstat_report_replslot_drop(const char *slotname);
1028 extern void pgstat_initialize(void);
1031 extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
1032 extern PgStat_BackendFunctionEntry *find_funcstat_entry(Oid func_id);
1034 extern void pgstat_initstats(Relation rel);
1036 /* nontransactional event counts are simple enough to inline */
1038 #define pgstat_count_heap_scan(rel) \
1039 do { \
1040 if ((rel)->pgstat_info != NULL) \
1041 (rel)->pgstat_info->t_counts.t_numscans++; \
1042 } while (0)
1043 #define pgstat_count_heap_getnext(rel) \
1044 do { \
1045 if ((rel)->pgstat_info != NULL) \
1046 (rel)->pgstat_info->t_counts.t_tuples_returned++; \
1047 } while (0)
1048 #define pgstat_count_heap_fetch(rel) \
1049 do { \
1050 if ((rel)->pgstat_info != NULL) \
1051 (rel)->pgstat_info->t_counts.t_tuples_fetched++; \
1052 } while (0)
1053 #define pgstat_count_index_scan(rel) \
1054 do { \
1055 if ((rel)->pgstat_info != NULL) \
1056 (rel)->pgstat_info->t_counts.t_numscans++; \
1057 } while (0)
1058 #define pgstat_count_index_tuples(rel, n) \
1059 do { \
1060 if ((rel)->pgstat_info != NULL) \
1061 (rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
1062 } while (0)
1063 #define pgstat_count_buffer_read(rel) \
1064 do { \
1065 if ((rel)->pgstat_info != NULL) \
1066 (rel)->pgstat_info->t_counts.t_blocks_fetched++; \
1067 } while (0)
1068 #define pgstat_count_buffer_hit(rel) \
1069 do { \
1070 if ((rel)->pgstat_info != NULL) \
1071 (rel)->pgstat_info->t_counts.t_blocks_hit++; \
1072 } while (0)
1073 #define pgstat_count_buffer_read_time(n) \
1074 (pgStatBlockReadTime += (n))
1075 #define pgstat_count_buffer_write_time(n) \
1076 (pgStatBlockWriteTime += (n))
1077 #define pgstat_count_conn_active_time(n) \
1078 (pgStatActiveTime += (n))
1079 #define pgstat_count_conn_txn_idle_time(n) \
1080 (pgStatTransactionIdleTime += (n))
1082 extern void pgstat_count_heap_insert(Relation rel, PgStat_Counter n);
1083 extern void pgstat_count_heap_update(Relation rel, bool hot);
1084 extern void pgstat_count_heap_delete(Relation rel);
1085 extern void pgstat_count_truncate(Relation rel);
1086 extern void pgstat_update_heap_dead_tuples(Relation rel, int delta);
1088 struct FunctionCallInfoBaseData;
1089 extern void pgstat_init_function_usage(struct FunctionCallInfoBaseData *fcinfo,
1090 PgStat_FunctionCallUsage *fcu);
1091 extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu,
1092 bool finalize);
1094 extern void AtEOXact_PgStat(bool isCommit, bool parallel);
1095 extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
1097 extern void AtPrepare_PgStat(void);
1098 extern void PostPrepare_PgStat(void);
1100 extern void pgstat_twophase_postcommit(TransactionId xid, uint16 info,
1101 void *recdata, uint32 len);
1102 extern void pgstat_twophase_postabort(TransactionId xid, uint16 info,
1103 void *recdata, uint32 len);
1105 extern void pgstat_send_archiver(const char *xlog, bool failed);
1106 extern void pgstat_send_bgwriter(void);
1107 extern void pgstat_send_checkpointer(void);
1108 extern void pgstat_send_wal(bool force);
1110 /* ----------
1111 * Support functions for the SQL-callable functions to
1112 * generate the pgstat* views.
1113 * ----------
1115 extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
1116 extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
1117 extern PgStat_StatFuncEntry *pgstat_fetch_stat_funcentry(Oid funcid);
1118 extern PgStat_ArchiverStats *pgstat_fetch_stat_archiver(void);
1119 extern PgStat_BgWriterStats *pgstat_fetch_stat_bgwriter(void);
1120 extern PgStat_CheckpointerStats *pgstat_fetch_stat_checkpointer(void);
1121 extern PgStat_GlobalStats *pgstat_fetch_global(void);
1122 extern PgStat_WalStats *pgstat_fetch_stat_wal(void);
1123 extern PgStat_SLRUStats *pgstat_fetch_slru(void);
1124 extern PgStat_StatReplSlotEntry *pgstat_fetch_replslot(NameData slotname);
1126 extern void pgstat_count_slru_page_zeroed(int slru_idx);
1127 extern void pgstat_count_slru_page_hit(int slru_idx);
1128 extern void pgstat_count_slru_page_read(int slru_idx);
1129 extern void pgstat_count_slru_page_written(int slru_idx);
1130 extern void pgstat_count_slru_page_exists(int slru_idx);
1131 extern void pgstat_count_slru_flush(int slru_idx);
1132 extern void pgstat_count_slru_truncate(int slru_idx);
1133 extern const char *pgstat_slru_name(int slru_idx);
1134 extern int pgstat_slru_index(const char *name);
1136 #endif /* PGSTAT_H */