4 * Definitions for the PostgreSQL statistics collector daemon.
6 * Copyright (c) 2001-2009, PostgreSQL Global Development Group
14 #include "libpq/pqcomm.h"
15 #include "portability/instr_time.h"
16 #include "utils/hsearch.h"
17 #include "utils/relcache.h"
18 #include "utils/timestamp.h"
21 /* Values for track_functions GUC variable --- order is significant! */
22 typedef enum TrackFunctionsLevel
27 } TrackFunctionsLevel
;
30 * The types of backend -> collector messages
33 typedef enum StatMsgType
38 PGSTAT_MTYPE_TABPURGE
,
40 PGSTAT_MTYPE_RESETCOUNTER
,
41 PGSTAT_MTYPE_AUTOVAC_START
,
44 PGSTAT_MTYPE_BGWRITER
,
45 PGSTAT_MTYPE_FUNCSTAT
,
46 PGSTAT_MTYPE_FUNCPURGE
50 * The data type used for counters.
53 typedef int64 PgStat_Counter
;
56 * PgStat_TableCounts The actual per-table counts kept by a backend
58 * This struct should contain only actual event counters, because we memcmp
59 * it against zeroes to detect whether there are any counts to transmit.
60 * It is a component of PgStat_TableStatus (within-backend state) and
61 * PgStat_TableEntry (the transmitted message format).
63 * Note: for a table, tuples_returned is the number of tuples successfully
64 * fetched by heap_getnext, while tuples_fetched is the number of tuples
65 * successfully fetched by heap_fetch under the control of bitmap indexscans.
66 * For an index, tuples_returned is the number of index entries returned by
67 * the index AM, while tuples_fetched is the number of tuples successfully
68 * fetched by heap_fetch under the control of simple indexscans for this index.
70 * tuples_inserted/updated/deleted/hot_updated count attempted actions,
71 * regardless of whether the transaction committed. new_live_tuples and
72 * new_dead_tuples are properly adjusted depending on commit or abort.
73 * Note that new_live_tuples and new_dead_tuples can be negative!
76 typedef struct PgStat_TableCounts
78 PgStat_Counter t_numscans
;
80 PgStat_Counter t_tuples_returned
;
81 PgStat_Counter t_tuples_fetched
;
83 PgStat_Counter t_tuples_inserted
;
84 PgStat_Counter t_tuples_updated
;
85 PgStat_Counter t_tuples_deleted
;
86 PgStat_Counter t_tuples_hot_updated
;
88 PgStat_Counter t_new_live_tuples
;
89 PgStat_Counter t_new_dead_tuples
;
91 PgStat_Counter t_blocks_fetched
;
92 PgStat_Counter t_blocks_hit
;
96 /* ------------------------------------------------------------
97 * Structures kept in backend local memory while accumulating counts
98 * ------------------------------------------------------------
103 * PgStat_TableStatus Per-table status within a backend
105 * Most of the event counters are nontransactional, ie, we count events
106 * in committed and aborted transactions alike. For these, we just count
107 * directly in the PgStat_TableStatus. However, new_live_tuples and
108 * new_dead_tuples must be derived from tuple insertion and deletion counts
109 * with awareness of whether the transaction or subtransaction committed or
110 * aborted. Hence, we also keep a stack of per-(sub)transaction status
111 * records for every table modified in the current transaction. At commit
112 * or abort, we propagate tuples_inserted and tuples_deleted up to the
113 * parent subtransaction level, or out to the parent PgStat_TableStatus,
117 typedef struct PgStat_TableStatus
119 Oid t_id
; /* table's OID */
120 bool t_shared
; /* is it a shared catalog? */
121 struct PgStat_TableXactStatus
*trans
; /* lowest subxact's counts */
122 PgStat_TableCounts t_counts
; /* event counts to be sent */
123 } PgStat_TableStatus
;
126 * PgStat_TableXactStatus Per-table, per-subtransaction status
129 typedef struct PgStat_TableXactStatus
131 PgStat_Counter tuples_inserted
; /* tuples inserted in (sub)xact */
132 PgStat_Counter tuples_deleted
; /* tuples deleted in (sub)xact */
133 int nest_level
; /* subtransaction nest level */
134 /* links to other structs for same relation: */
135 struct PgStat_TableXactStatus
*upper
; /* next higher subxact if any */
136 PgStat_TableStatus
*parent
; /* per-table status */
137 /* structs of same subxact level are linked here: */
138 struct PgStat_TableXactStatus
*next
; /* next of same subxact */
139 } PgStat_TableXactStatus
;
142 /* ------------------------------------------------------------
143 * Message formats follow
144 * ------------------------------------------------------------
149 * PgStat_MsgHdr The common message header
152 typedef struct PgStat_MsgHdr
159 * Space available in a message. This will keep the UDP packets below 1K,
160 * which should fit unfragmented into the MTU of the lo interface on most
161 * platforms. Does anybody care for platforms where it doesn't?
164 #define PGSTAT_MSG_PAYLOAD (1000 - sizeof(PgStat_MsgHdr))
168 * PgStat_MsgDummy A dummy message, ignored by the collector
171 typedef struct PgStat_MsgDummy
178 * PgStat_MsgInquiry Sent by a backend to ask the collector
179 * to write the stats file.
183 typedef struct PgStat_MsgInquiry
186 TimestampTz inquiry_time
; /* minimum acceptable file timestamp */
191 * PgStat_TableEntry Per-table info in a MsgTabstat
194 typedef struct PgStat_TableEntry
197 PgStat_TableCounts t_counts
;
201 * PgStat_MsgTabstat Sent by the backend to report table
202 * and buffer access statistics.
205 #define PGSTAT_NUM_TABENTRIES \
206 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int)) \
207 / sizeof(PgStat_TableEntry))
209 typedef struct PgStat_MsgTabstat
216 PgStat_TableEntry m_entry
[PGSTAT_NUM_TABENTRIES
];
221 * PgStat_MsgTabpurge Sent by the backend to tell the collector
225 #define PGSTAT_NUM_TABPURGE \
226 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
229 typedef struct PgStat_MsgTabpurge
234 Oid m_tableid
[PGSTAT_NUM_TABPURGE
];
235 } PgStat_MsgTabpurge
;
239 * PgStat_MsgDropdb Sent by the backend to tell the collector
240 * about a dropped database
243 typedef struct PgStat_MsgDropdb
251 * PgStat_MsgResetcounter Sent by the backend to tell the collector
255 typedef struct PgStat_MsgResetcounter
259 } PgStat_MsgResetcounter
;
263 * PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
264 * that a database is going to be processed
267 typedef struct PgStat_MsgAutovacStart
271 TimestampTz m_start_time
;
272 } PgStat_MsgAutovacStart
;
276 * PgStat_MsgVacuum Sent by the backend or autovacuum daemon
277 * after VACUUM or VACUUM ANALYZE
280 typedef struct PgStat_MsgVacuum
288 TimestampTz m_vacuumtime
;
289 PgStat_Counter m_tuples
;
294 * PgStat_MsgAnalyze Sent by the backend or autovacuum daemon
298 typedef struct PgStat_MsgAnalyze
304 TimestampTz m_analyzetime
;
305 PgStat_Counter m_live_tuples
;
306 PgStat_Counter m_dead_tuples
;
311 * PgStat_MsgBgWriter Sent by the bgwriter to update statistics.
314 typedef struct PgStat_MsgBgWriter
318 PgStat_Counter m_timed_checkpoints
;
319 PgStat_Counter m_requested_checkpoints
;
320 PgStat_Counter m_buf_written_checkpoints
;
321 PgStat_Counter m_buf_written_clean
;
322 PgStat_Counter m_maxwritten_clean
;
323 PgStat_Counter m_buf_written_backend
;
324 PgStat_Counter m_buf_alloc
;
325 } PgStat_MsgBgWriter
;
329 * PgStat_FunctionCounts The actual per-function counts kept by a backend
331 * This struct should contain only actual event counters, because we memcmp
332 * it against zeroes to detect whether there are any counts to transmit.
334 * Note that the time counters are in instr_time format here. We convert to
335 * microseconds in PgStat_Counter format when transmitting to the collector.
338 typedef struct PgStat_FunctionCounts
340 PgStat_Counter f_numcalls
;
342 instr_time f_time_self
;
343 } PgStat_FunctionCounts
;
346 * PgStat_BackendFunctionEntry Entry in backend's per-function hash table
349 typedef struct PgStat_BackendFunctionEntry
352 PgStat_FunctionCounts f_counts
;
353 } PgStat_BackendFunctionEntry
;
356 * PgStat_FunctionEntry Per-function info in a MsgFuncstat
359 typedef struct PgStat_FunctionEntry
362 PgStat_Counter f_numcalls
;
363 PgStat_Counter f_time
; /* times in microseconds */
364 PgStat_Counter f_time_self
;
365 } PgStat_FunctionEntry
;
368 * PgStat_MsgFuncstat Sent by the backend to report function
372 #define PGSTAT_NUM_FUNCENTRIES \
373 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
374 / sizeof(PgStat_FunctionEntry))
376 typedef struct PgStat_MsgFuncstat
381 PgStat_FunctionEntry m_entry
[PGSTAT_NUM_FUNCENTRIES
];
382 } PgStat_MsgFuncstat
;
385 * PgStat_MsgFuncpurge Sent by the backend to tell the collector
386 * about dead functions.
389 #define PGSTAT_NUM_FUNCPURGE \
390 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
393 typedef struct PgStat_MsgFuncpurge
398 Oid m_functionid
[PGSTAT_NUM_FUNCPURGE
];
399 } PgStat_MsgFuncpurge
;
403 * PgStat_Msg Union over all possible messages.
406 typedef union PgStat_Msg
408 PgStat_MsgHdr msg_hdr
;
409 PgStat_MsgDummy msg_dummy
;
410 PgStat_MsgInquiry msg_inquiry
;
411 PgStat_MsgTabstat msg_tabstat
;
412 PgStat_MsgTabpurge msg_tabpurge
;
413 PgStat_MsgDropdb msg_dropdb
;
414 PgStat_MsgResetcounter msg_resetcounter
;
415 PgStat_MsgAutovacStart msg_autovacuum
;
416 PgStat_MsgVacuum msg_vacuum
;
417 PgStat_MsgAnalyze msg_analyze
;
418 PgStat_MsgBgWriter msg_bgwriter
;
419 PgStat_MsgFuncstat msg_funcstat
;
420 PgStat_MsgFuncpurge msg_funcpurge
;
424 /* ------------------------------------------------------------
425 * Statistic collector data structures follow
427 * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
428 * data structures change.
429 * ------------------------------------------------------------
432 #define PGSTAT_FILE_FORMAT_ID 0x01A5BC98
435 * PgStat_StatDBEntry The collector's data per database
438 typedef struct PgStat_StatDBEntry
441 PgStat_Counter n_xact_commit
;
442 PgStat_Counter n_xact_rollback
;
443 PgStat_Counter n_blocks_fetched
;
444 PgStat_Counter n_blocks_hit
;
445 PgStat_Counter n_tuples_returned
;
446 PgStat_Counter n_tuples_fetched
;
447 PgStat_Counter n_tuples_inserted
;
448 PgStat_Counter n_tuples_updated
;
449 PgStat_Counter n_tuples_deleted
;
450 TimestampTz last_autovac_time
;
453 * tables and functions must be last in the struct, because we don't write
454 * the pointers out to the stats file.
458 } PgStat_StatDBEntry
;
462 * PgStat_StatTabEntry The collector's data per table (or index)
465 typedef struct PgStat_StatTabEntry
469 PgStat_Counter numscans
;
471 PgStat_Counter tuples_returned
;
472 PgStat_Counter tuples_fetched
;
474 PgStat_Counter tuples_inserted
;
475 PgStat_Counter tuples_updated
;
476 PgStat_Counter tuples_deleted
;
477 PgStat_Counter tuples_hot_updated
;
479 PgStat_Counter n_live_tuples
;
480 PgStat_Counter n_dead_tuples
;
481 PgStat_Counter last_anl_tuples
;
483 PgStat_Counter blocks_fetched
;
484 PgStat_Counter blocks_hit
;
486 TimestampTz vacuum_timestamp
; /* user initiated vacuum */
487 TimestampTz autovac_vacuum_timestamp
; /* autovacuum initiated */
488 TimestampTz analyze_timestamp
; /* user initiated */
489 TimestampTz autovac_analyze_timestamp
; /* autovacuum initiated */
490 } PgStat_StatTabEntry
;
494 * PgStat_StatFuncEntry The collector's data per function
497 typedef struct PgStat_StatFuncEntry
501 PgStat_Counter f_numcalls
;
503 PgStat_Counter f_time
; /* times in microseconds */
504 PgStat_Counter f_time_self
;
505 } PgStat_StatFuncEntry
;
509 * Global statistics kept in the stats collector
511 typedef struct PgStat_GlobalStats
513 TimestampTz stats_timestamp
; /* time of stats file update */
514 PgStat_Counter timed_checkpoints
;
515 PgStat_Counter requested_checkpoints
;
516 PgStat_Counter buf_written_checkpoints
;
517 PgStat_Counter buf_written_clean
;
518 PgStat_Counter maxwritten_clean
;
519 PgStat_Counter buf_written_backend
;
520 PgStat_Counter buf_alloc
;
521 } PgStat_GlobalStats
;
525 * Shared-memory data structures
532 * Each live backend maintains a PgBackendStatus struct in shared memory
533 * showing its current activity. (The structs are allocated according to
534 * BackendId, but that is not critical.) Note that the collector process
535 * has no involvement in, or even access to, these structs.
538 typedef struct PgBackendStatus
541 * To avoid locking overhead, we use the following protocol: a backend
542 * increments st_changecount before modifying its entry, and again after
543 * finishing a modification. A would-be reader should note the value of
544 * st_changecount, copy the entry into private memory, then check
545 * st_changecount again. If the value hasn't changed, and if it's even,
546 * the copy is valid; otherwise start over. This makes updates cheap
547 * while reads are potentially expensive, but that's the tradeoff we want.
551 /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
554 /* Times when current backend, transaction, and activity started */
555 TimestampTz st_proc_start_timestamp
;
556 TimestampTz st_xact_start_timestamp
;
557 TimestampTz st_activity_start_timestamp
;
559 /* Database OID, owning user's OID, connection client address */
562 SockAddr st_clientaddr
;
564 /* Is backend currently waiting on an lmgr lock? */
567 /* current command string; MUST be null-terminated */
572 * Working state needed to accumulate per-function-call timing statistics.
574 typedef struct PgStat_FunctionCallUsage
576 /* Link to function's hashtable entry (must still be there at exit!) */
577 /* NULL means we are not tracking the current function call */
578 PgStat_FunctionCounts
*fs
;
579 /* Total time previously charged to function, as of function start */
580 instr_time save_f_time
;
581 /* Backend-wide total time as of function start */
582 instr_time save_total
;
583 /* system clock as of function start */
585 } PgStat_FunctionCallUsage
;
592 extern bool pgstat_track_activities
;
593 extern bool pgstat_track_counts
;
594 extern int pgstat_track_functions
;
595 extern PGDLLIMPORT
int pgstat_track_activity_query_size
;
596 extern char *pgstat_stat_tmpname
;
597 extern char *pgstat_stat_filename
;
600 * BgWriter statistics counters are updated directly by bgwriter and bufmgr
602 extern PgStat_MsgBgWriter BgWriterStats
;
605 * Functions called from postmaster
608 extern Size
BackendStatusShmemSize(void);
609 extern void CreateSharedBackendStatus(void);
611 extern void pgstat_init(void);
612 extern int pgstat_start(void);
613 extern void pgstat_reset_all(void);
614 extern void allow_immediate_pgstat_restart(void);
617 extern void PgstatCollectorMain(int argc
, char *argv
[]);
622 * Functions called from backends
625 extern void pgstat_ping(void);
627 extern void pgstat_report_stat(bool force
);
628 extern void pgstat_vacuum_stat(void);
629 extern void pgstat_drop_database(Oid databaseid
);
631 extern void pgstat_clear_snapshot(void);
632 extern void pgstat_reset_counters(void);
634 extern void pgstat_report_autovac(Oid dboid
);
635 extern void pgstat_report_vacuum(Oid tableoid
, bool shared
, bool scanned_all
,
636 bool analyze
, PgStat_Counter tuples
);
637 extern void pgstat_report_analyze(Relation rel
,
638 PgStat_Counter livetuples
,
639 PgStat_Counter deadtuples
);
641 extern void pgstat_initialize(void);
642 extern void pgstat_bestart(void);
644 extern void pgstat_report_activity(const char *what
);
645 extern void pgstat_report_xact_timestamp(TimestampTz tstamp
);
646 extern void pgstat_report_waiting(bool waiting
);
647 extern const char *pgstat_get_backend_current_activity(int pid
, bool checkUser
);
649 extern void pgstat_initstats(Relation rel
);
651 /* nontransactional event counts are simple enough to inline */
653 #define pgstat_count_heap_scan(rel) \
655 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
656 (rel)->pgstat_info->t_counts.t_numscans++; \
658 #define pgstat_count_heap_getnext(rel) \
660 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
661 (rel)->pgstat_info->t_counts.t_tuples_returned++; \
663 #define pgstat_count_heap_fetch(rel) \
665 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
666 (rel)->pgstat_info->t_counts.t_tuples_fetched++; \
668 #define pgstat_count_index_scan(rel) \
670 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
671 (rel)->pgstat_info->t_counts.t_numscans++; \
673 #define pgstat_count_index_tuples(rel, n) \
675 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
676 (rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
678 #define pgstat_count_buffer_read(rel) \
680 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
681 (rel)->pgstat_info->t_counts.t_blocks_fetched++; \
683 #define pgstat_count_buffer_hit(rel) \
685 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
686 (rel)->pgstat_info->t_counts.t_blocks_hit++; \
689 extern void pgstat_count_heap_insert(Relation rel
);
690 extern void pgstat_count_heap_update(Relation rel
, bool hot
);
691 extern void pgstat_count_heap_delete(Relation rel
);
692 extern void pgstat_update_heap_dead_tuples(Relation rel
, int delta
);
694 extern void pgstat_init_function_usage(FunctionCallInfoData
*fcinfo
,
695 PgStat_FunctionCallUsage
*fcu
);
696 extern void pgstat_end_function_usage(PgStat_FunctionCallUsage
*fcu
,
699 extern void AtEOXact_PgStat(bool isCommit
);
700 extern void AtEOSubXact_PgStat(bool isCommit
, int nestDepth
);
702 extern void AtPrepare_PgStat(void);
703 extern void PostPrepare_PgStat(void);
705 extern void pgstat_twophase_postcommit(TransactionId xid
, uint16 info
,
706 void *recdata
, uint32 len
);
707 extern void pgstat_twophase_postabort(TransactionId xid
, uint16 info
,
708 void *recdata
, uint32 len
);
710 extern void pgstat_send_bgwriter(void);
713 * Support functions for the SQL-callable functions to
714 * generate the pgstat* views.
717 extern PgStat_StatDBEntry
*pgstat_fetch_stat_dbentry(Oid dbid
);
718 extern PgStat_StatTabEntry
*pgstat_fetch_stat_tabentry(Oid relid
);
719 extern PgBackendStatus
*pgstat_fetch_stat_beentry(int beid
);
720 extern PgStat_StatFuncEntry
*pgstat_fetch_stat_funcentry(Oid funcid
);
721 extern int pgstat_fetch_stat_numbackends(void);
722 extern PgStat_GlobalStats
*pgstat_fetch_global(void);
724 #endif /* PGSTAT_H */