4 * Definitions for the PostgreSQL statistics collector daemon.
6 * Copyright (c) 2001-2008, 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
37 PGSTAT_MTYPE_TABPURGE
,
39 PGSTAT_MTYPE_RESETCOUNTER
,
40 PGSTAT_MTYPE_AUTOVAC_START
,
43 PGSTAT_MTYPE_BGWRITER
,
44 PGSTAT_MTYPE_FUNCSTAT
,
45 PGSTAT_MTYPE_FUNCPURGE
49 * The data type used for counters.
52 typedef int64 PgStat_Counter
;
55 * PgStat_TableCounts The actual per-table counts kept by a backend
57 * This struct should contain only actual event counters, because we memcmp
58 * it against zeroes to detect whether there are any counts to transmit.
59 * It is a component of PgStat_TableStatus (within-backend state) and
60 * PgStat_TableEntry (the transmitted message format).
62 * Note: for a table, tuples_returned is the number of tuples successfully
63 * fetched by heap_getnext, while tuples_fetched is the number of tuples
64 * successfully fetched by heap_fetch under the control of bitmap indexscans.
65 * For an index, tuples_returned is the number of index entries returned by
66 * the index AM, while tuples_fetched is the number of tuples successfully
67 * fetched by heap_fetch under the control of simple indexscans for this index.
69 * tuples_inserted/updated/deleted/hot_updated count attempted actions,
70 * regardless of whether the transaction committed. new_live_tuples and
71 * new_dead_tuples are properly adjusted depending on commit or abort.
72 * Note that new_live_tuples and new_dead_tuples can be negative!
75 typedef struct PgStat_TableCounts
77 PgStat_Counter t_numscans
;
79 PgStat_Counter t_tuples_returned
;
80 PgStat_Counter t_tuples_fetched
;
82 PgStat_Counter t_tuples_inserted
;
83 PgStat_Counter t_tuples_updated
;
84 PgStat_Counter t_tuples_deleted
;
85 PgStat_Counter t_tuples_hot_updated
;
87 PgStat_Counter t_new_live_tuples
;
88 PgStat_Counter t_new_dead_tuples
;
90 PgStat_Counter t_blocks_fetched
;
91 PgStat_Counter t_blocks_hit
;
95 /* ------------------------------------------------------------
96 * Structures kept in backend local memory while accumulating counts
97 * ------------------------------------------------------------
102 * PgStat_TableStatus Per-table status within a backend
104 * Most of the event counters are nontransactional, ie, we count events
105 * in committed and aborted transactions alike. For these, we just count
106 * directly in the PgStat_TableStatus. However, new_live_tuples and
107 * new_dead_tuples must be derived from tuple insertion and deletion counts
108 * with awareness of whether the transaction or subtransaction committed or
109 * aborted. Hence, we also keep a stack of per-(sub)transaction status
110 * records for every table modified in the current transaction. At commit
111 * or abort, we propagate tuples_inserted and tuples_deleted up to the
112 * parent subtransaction level, or out to the parent PgStat_TableStatus,
116 typedef struct PgStat_TableStatus
118 Oid t_id
; /* table's OID */
119 bool t_shared
; /* is it a shared catalog? */
120 struct PgStat_TableXactStatus
*trans
; /* lowest subxact's counts */
121 PgStat_TableCounts t_counts
; /* event counts to be sent */
122 } PgStat_TableStatus
;
125 * PgStat_TableXactStatus Per-table, per-subtransaction status
128 typedef struct PgStat_TableXactStatus
130 PgStat_Counter tuples_inserted
; /* tuples inserted in (sub)xact */
131 PgStat_Counter tuples_deleted
; /* tuples deleted in (sub)xact */
132 int nest_level
; /* subtransaction nest level */
133 /* links to other structs for same relation: */
134 struct PgStat_TableXactStatus
*upper
; /* next higher subxact if any */
135 PgStat_TableStatus
*parent
; /* per-table status */
136 /* structs of same subxact level are linked here: */
137 struct PgStat_TableXactStatus
*next
; /* next of same subxact */
138 } PgStat_TableXactStatus
;
141 /* ------------------------------------------------------------
142 * Message formats follow
143 * ------------------------------------------------------------
148 * PgStat_MsgHdr The common message header
151 typedef struct PgStat_MsgHdr
158 * Space available in a message. This will keep the UDP packets below 1K,
159 * which should fit unfragmented into the MTU of the lo interface on most
160 * platforms. Does anybody care for platforms where it doesn't?
163 #define PGSTAT_MSG_PAYLOAD (1000 - sizeof(PgStat_MsgHdr))
167 * PgStat_MsgDummy A dummy message, ignored by the collector
170 typedef struct PgStat_MsgDummy
177 * PgStat_TableEntry Per-table info in a MsgTabstat
180 typedef struct PgStat_TableEntry
183 PgStat_TableCounts t_counts
;
187 * PgStat_MsgTabstat Sent by the backend to report table
188 * and buffer access statistics.
191 #define PGSTAT_NUM_TABENTRIES \
192 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int)) \
193 / sizeof(PgStat_TableEntry))
195 typedef struct PgStat_MsgTabstat
202 PgStat_TableEntry m_entry
[PGSTAT_NUM_TABENTRIES
];
207 * PgStat_MsgTabpurge Sent by the backend to tell the collector
211 #define PGSTAT_NUM_TABPURGE \
212 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
215 typedef struct PgStat_MsgTabpurge
220 Oid m_tableid
[PGSTAT_NUM_TABPURGE
];
221 } PgStat_MsgTabpurge
;
225 * PgStat_MsgDropdb Sent by the backend to tell the collector
226 * about a dropped database
229 typedef struct PgStat_MsgDropdb
237 * PgStat_MsgResetcounter Sent by the backend to tell the collector
241 typedef struct PgStat_MsgResetcounter
245 } PgStat_MsgResetcounter
;
249 * PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
250 * that a database is going to be processed
253 typedef struct PgStat_MsgAutovacStart
257 TimestampTz m_start_time
;
258 } PgStat_MsgAutovacStart
;
262 * PgStat_MsgVacuum Sent by the backend or autovacuum daemon
263 * after VACUUM or VACUUM ANALYZE
266 typedef struct PgStat_MsgVacuum
273 TimestampTz m_vacuumtime
;
274 PgStat_Counter m_tuples
;
279 * PgStat_MsgAnalyze Sent by the backend or autovacuum daemon
283 typedef struct PgStat_MsgAnalyze
289 TimestampTz m_analyzetime
;
290 PgStat_Counter m_live_tuples
;
291 PgStat_Counter m_dead_tuples
;
296 * PgStat_MsgBgWriter Sent by the bgwriter to update statistics.
299 typedef struct PgStat_MsgBgWriter
303 PgStat_Counter m_timed_checkpoints
;
304 PgStat_Counter m_requested_checkpoints
;
305 PgStat_Counter m_buf_written_checkpoints
;
306 PgStat_Counter m_buf_written_clean
;
307 PgStat_Counter m_maxwritten_clean
;
308 PgStat_Counter m_buf_written_backend
;
309 PgStat_Counter m_buf_alloc
;
310 } PgStat_MsgBgWriter
;
314 * PgStat_FunctionCounts The actual per-function counts kept by a backend
316 * This struct should contain only actual event counters, because we memcmp
317 * it against zeroes to detect whether there are any counts to transmit.
319 * Note that the time counters are in instr_time format here. We convert to
320 * microseconds in PgStat_Counter format when transmitting to the collector.
323 typedef struct PgStat_FunctionCounts
325 PgStat_Counter f_numcalls
;
327 instr_time f_time_self
;
328 } PgStat_FunctionCounts
;
331 * PgStat_BackendFunctionEntry Entry in backend's per-function hash table
334 typedef struct PgStat_BackendFunctionEntry
337 PgStat_FunctionCounts f_counts
;
338 } PgStat_BackendFunctionEntry
;
341 * PgStat_FunctionEntry Per-function info in a MsgFuncstat
344 typedef struct PgStat_FunctionEntry
347 PgStat_Counter f_numcalls
;
348 PgStat_Counter f_time
; /* times in microseconds */
349 PgStat_Counter f_time_self
;
350 } PgStat_FunctionEntry
;
353 * PgStat_MsgFuncstat Sent by the backend to report function
357 #define PGSTAT_NUM_FUNCENTRIES \
358 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
359 / sizeof(PgStat_FunctionEntry))
361 typedef struct PgStat_MsgFuncstat
366 PgStat_FunctionEntry m_entry
[PGSTAT_NUM_FUNCENTRIES
];
367 } PgStat_MsgFuncstat
;
370 * PgStat_MsgFuncpurge Sent by the backend to tell the collector
371 * about dead functions.
374 #define PGSTAT_NUM_FUNCPURGE \
375 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
378 typedef struct PgStat_MsgFuncpurge
383 Oid m_functionid
[PGSTAT_NUM_FUNCPURGE
];
384 } PgStat_MsgFuncpurge
;
388 * PgStat_Msg Union over all possible messages.
391 typedef union PgStat_Msg
393 PgStat_MsgHdr msg_hdr
;
394 PgStat_MsgDummy msg_dummy
;
395 PgStat_MsgTabstat msg_tabstat
;
396 PgStat_MsgTabpurge msg_tabpurge
;
397 PgStat_MsgDropdb msg_dropdb
;
398 PgStat_MsgResetcounter msg_resetcounter
;
399 PgStat_MsgAutovacStart msg_autovacuum
;
400 PgStat_MsgVacuum msg_vacuum
;
401 PgStat_MsgAnalyze msg_analyze
;
402 PgStat_MsgBgWriter msg_bgwriter
;
403 PgStat_MsgFuncstat msg_funcstat
;
404 PgStat_MsgFuncpurge msg_funcpurge
;
408 /* ------------------------------------------------------------
409 * Statistic collector data structures follow
411 * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
412 * data structures change.
413 * ------------------------------------------------------------
416 #define PGSTAT_FILE_FORMAT_ID 0x01A5BC97
419 * PgStat_StatDBEntry The collector's data per database
422 typedef struct PgStat_StatDBEntry
425 PgStat_Counter n_xact_commit
;
426 PgStat_Counter n_xact_rollback
;
427 PgStat_Counter n_blocks_fetched
;
428 PgStat_Counter n_blocks_hit
;
429 PgStat_Counter n_tuples_returned
;
430 PgStat_Counter n_tuples_fetched
;
431 PgStat_Counter n_tuples_inserted
;
432 PgStat_Counter n_tuples_updated
;
433 PgStat_Counter n_tuples_deleted
;
434 TimestampTz last_autovac_time
;
437 * tables and functions must be last in the struct, because we don't
438 * write the pointers out to the stats file.
442 } PgStat_StatDBEntry
;
446 * PgStat_StatTabEntry The collector's data per table (or index)
449 typedef struct PgStat_StatTabEntry
453 PgStat_Counter numscans
;
455 PgStat_Counter tuples_returned
;
456 PgStat_Counter tuples_fetched
;
458 PgStat_Counter tuples_inserted
;
459 PgStat_Counter tuples_updated
;
460 PgStat_Counter tuples_deleted
;
461 PgStat_Counter tuples_hot_updated
;
463 PgStat_Counter n_live_tuples
;
464 PgStat_Counter n_dead_tuples
;
465 PgStat_Counter last_anl_tuples
;
467 PgStat_Counter blocks_fetched
;
468 PgStat_Counter blocks_hit
;
470 TimestampTz vacuum_timestamp
; /* user initiated vacuum */
471 TimestampTz autovac_vacuum_timestamp
; /* autovacuum initiated */
472 TimestampTz analyze_timestamp
; /* user initiated */
473 TimestampTz autovac_analyze_timestamp
; /* autovacuum initiated */
474 } PgStat_StatTabEntry
;
478 * PgStat_StatFuncEntry The collector's data per function
481 typedef struct PgStat_StatFuncEntry
485 PgStat_Counter f_numcalls
;
487 PgStat_Counter f_time
; /* times in microseconds */
488 PgStat_Counter f_time_self
;
489 } PgStat_StatFuncEntry
;
493 * Global statistics kept in the stats collector
495 typedef struct PgStat_GlobalStats
497 PgStat_Counter timed_checkpoints
;
498 PgStat_Counter requested_checkpoints
;
499 PgStat_Counter buf_written_checkpoints
;
500 PgStat_Counter buf_written_clean
;
501 PgStat_Counter maxwritten_clean
;
502 PgStat_Counter buf_written_backend
;
503 PgStat_Counter buf_alloc
;
504 } PgStat_GlobalStats
;
508 * Shared-memory data structures
515 * Each live backend maintains a PgBackendStatus struct in shared memory
516 * showing its current activity. (The structs are allocated according to
517 * BackendId, but that is not critical.) Note that the collector process
518 * has no involvement in, or even access to, these structs.
521 typedef struct PgBackendStatus
524 * To avoid locking overhead, we use the following protocol: a backend
525 * increments st_changecount before modifying its entry, and again after
526 * finishing a modification. A would-be reader should note the value of
527 * st_changecount, copy the entry into private memory, then check
528 * st_changecount again. If the value hasn't changed, and if it's even,
529 * the copy is valid; otherwise start over. This makes updates cheap
530 * while reads are potentially expensive, but that's the tradeoff we want.
534 /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
537 /* Times when current backend, transaction, and activity started */
538 TimestampTz st_proc_start_timestamp
;
539 TimestampTz st_xact_start_timestamp
;
540 TimestampTz st_activity_start_timestamp
;
542 /* Database OID, owning user's OID, connection client address */
545 SockAddr st_clientaddr
;
547 /* Is backend currently waiting on an lmgr lock? */
550 /* current command string; MUST be null-terminated */
555 * Working state needed to accumulate per-function-call timing statistics.
557 typedef struct PgStat_FunctionCallUsage
559 /* Link to function's hashtable entry (must still be there at exit!) */
560 /* NULL means we are not tracking the current function call */
561 PgStat_FunctionCounts
*fs
;
562 /* Total time previously charged to function, as of function start */
563 instr_time save_f_time
;
564 /* Backend-wide total time as of function start */
565 instr_time save_total
;
566 /* system clock as of function start */
568 } PgStat_FunctionCallUsage
;
575 extern bool pgstat_track_activities
;
576 extern bool pgstat_track_counts
;
577 extern int pgstat_track_functions
;
578 extern int pgstat_track_activity_query_size
;
579 extern char *pgstat_stat_tmpname
;
580 extern char *pgstat_stat_filename
;
583 * BgWriter statistics counters are updated directly by bgwriter and bufmgr
585 extern PgStat_MsgBgWriter BgWriterStats
;
588 * Functions called from postmaster
591 extern Size
BackendStatusShmemSize(void);
592 extern void CreateSharedBackendStatus(void);
594 extern void pgstat_init(void);
595 extern int pgstat_start(void);
596 extern void pgstat_reset_all(void);
597 extern void allow_immediate_pgstat_restart(void);
600 extern void PgstatCollectorMain(int argc
, char *argv
[]);
605 * Functions called from backends
608 extern void pgstat_ping(void);
610 extern void pgstat_report_stat(bool force
);
611 extern void pgstat_vacuum_stat(void);
612 extern void pgstat_drop_database(Oid databaseid
);
614 extern void pgstat_clear_snapshot(void);
615 extern void pgstat_reset_counters(void);
617 extern void pgstat_report_autovac(Oid dboid
);
618 extern void pgstat_report_vacuum(Oid tableoid
, bool shared
,
619 bool analyze
, PgStat_Counter tuples
);
620 extern void pgstat_report_analyze(Relation rel
,
621 PgStat_Counter livetuples
,
622 PgStat_Counter deadtuples
);
624 extern void pgstat_initialize(void);
625 extern void pgstat_bestart(void);
627 extern void pgstat_report_activity(const char *what
);
628 extern void pgstat_report_xact_timestamp(TimestampTz tstamp
);
629 extern void pgstat_report_waiting(bool waiting
);
630 extern const char *pgstat_get_backend_current_activity(int pid
, bool checkUser
);
632 extern void pgstat_initstats(Relation rel
);
634 /* nontransactional event counts are simple enough to inline */
636 #define pgstat_count_heap_scan(rel) \
638 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
639 (rel)->pgstat_info->t_counts.t_numscans++; \
641 #define pgstat_count_heap_getnext(rel) \
643 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
644 (rel)->pgstat_info->t_counts.t_tuples_returned++; \
646 #define pgstat_count_heap_fetch(rel) \
648 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
649 (rel)->pgstat_info->t_counts.t_tuples_fetched++; \
651 #define pgstat_count_index_scan(rel) \
653 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
654 (rel)->pgstat_info->t_counts.t_numscans++; \
656 #define pgstat_count_index_tuples(rel, n) \
658 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
659 (rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
661 #define pgstat_count_buffer_read(rel) \
663 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
664 (rel)->pgstat_info->t_counts.t_blocks_fetched++; \
666 #define pgstat_count_buffer_hit(rel) \
668 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
669 (rel)->pgstat_info->t_counts.t_blocks_hit++; \
672 extern void pgstat_count_heap_insert(Relation rel
);
673 extern void pgstat_count_heap_update(Relation rel
, bool hot
);
674 extern void pgstat_count_heap_delete(Relation rel
);
675 extern void pgstat_update_heap_dead_tuples(Relation rel
, int delta
);
677 extern void pgstat_init_function_usage(FunctionCallInfoData
*fcinfo
,
678 PgStat_FunctionCallUsage
*fcu
);
679 extern void pgstat_end_function_usage(PgStat_FunctionCallUsage
*fcu
,
682 extern void AtEOXact_PgStat(bool isCommit
);
683 extern void AtEOSubXact_PgStat(bool isCommit
, int nestDepth
);
685 extern void AtPrepare_PgStat(void);
686 extern void PostPrepare_PgStat(void);
688 extern void pgstat_twophase_postcommit(TransactionId xid
, uint16 info
,
689 void *recdata
, uint32 len
);
690 extern void pgstat_twophase_postabort(TransactionId xid
, uint16 info
,
691 void *recdata
, uint32 len
);
693 extern void pgstat_send_bgwriter(void);
696 * Support functions for the SQL-callable functions to
697 * generate the pgstat* views.
700 extern PgStat_StatDBEntry
*pgstat_fetch_stat_dbentry(Oid dbid
);
701 extern PgStat_StatTabEntry
*pgstat_fetch_stat_tabentry(Oid relid
);
702 extern PgBackendStatus
*pgstat_fetch_stat_beentry(int beid
);
703 extern PgStat_StatFuncEntry
*pgstat_fetch_stat_funcentry(Oid funcid
);
704 extern int pgstat_fetch_stat_numbackends(void);
705 extern PgStat_GlobalStats
*pgstat_fetch_global(void);
707 #endif /* PGSTAT_H */