Don't reset pg_class.reltuples and relpages in VACUUM, if any pages were
[PostgreSQL.git] / src / include / pgstat.h
blob4a1e2749eb70df8e73d53b54f3e50f612129f23b
1 /* ----------
2 * pgstat.h
4 * Definitions for the PostgreSQL statistics collector daemon.
6 * Copyright (c) 2001-2008, PostgreSQL Global Development Group
8 * $PostgreSQL$
9 * ----------
11 #ifndef PGSTAT_H
12 #define PGSTAT_H
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
24 TRACK_FUNC_OFF,
25 TRACK_FUNC_PL,
26 TRACK_FUNC_ALL
27 } TrackFunctionsLevel;
29 /* ----------
30 * The types of backend -> collector messages
31 * ----------
33 typedef enum StatMsgType
35 PGSTAT_MTYPE_DUMMY,
36 PGSTAT_MTYPE_INQUIRY,
37 PGSTAT_MTYPE_TABSTAT,
38 PGSTAT_MTYPE_TABPURGE,
39 PGSTAT_MTYPE_DROPDB,
40 PGSTAT_MTYPE_RESETCOUNTER,
41 PGSTAT_MTYPE_AUTOVAC_START,
42 PGSTAT_MTYPE_VACUUM,
43 PGSTAT_MTYPE_ANALYZE,
44 PGSTAT_MTYPE_BGWRITER,
45 PGSTAT_MTYPE_FUNCSTAT,
46 PGSTAT_MTYPE_FUNCPURGE
47 } StatMsgType;
49 /* ----------
50 * The data type used for counters.
51 * ----------
53 typedef int64 PgStat_Counter;
55 /* ----------
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!
74 * ----------
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;
93 } PgStat_TableCounts;
96 /* ------------------------------------------------------------
97 * Structures kept in backend local memory while accumulating counts
98 * ------------------------------------------------------------
102 /* ----------
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,
114 * as appropriate.
115 * ----------
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;
125 /* ----------
126 * PgStat_TableXactStatus Per-table, per-subtransaction status
127 * ----------
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 * ------------------------------------------------------------
148 /* ----------
149 * PgStat_MsgHdr The common message header
150 * ----------
152 typedef struct PgStat_MsgHdr
154 StatMsgType m_type;
155 int m_size;
156 } PgStat_MsgHdr;
158 /* ----------
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?
162 * ----------
164 #define PGSTAT_MSG_PAYLOAD (1000 - sizeof(PgStat_MsgHdr))
167 /* ----------
168 * PgStat_MsgDummy A dummy message, ignored by the collector
169 * ----------
171 typedef struct PgStat_MsgDummy
173 PgStat_MsgHdr m_hdr;
174 } PgStat_MsgDummy;
177 /* ----------
178 * PgStat_MsgInquiry Sent by a backend to ask the collector
179 * to write the stats file.
180 * ----------
183 typedef struct PgStat_MsgInquiry
185 PgStat_MsgHdr m_hdr;
186 TimestampTz inquiry_time; /* minimum acceptable file timestamp */
187 } PgStat_MsgInquiry;
190 /* ----------
191 * PgStat_TableEntry Per-table info in a MsgTabstat
192 * ----------
194 typedef struct PgStat_TableEntry
196 Oid t_id;
197 PgStat_TableCounts t_counts;
198 } PgStat_TableEntry;
200 /* ----------
201 * PgStat_MsgTabstat Sent by the backend to report table
202 * and buffer access statistics.
203 * ----------
205 #define PGSTAT_NUM_TABENTRIES \
206 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int)) \
207 / sizeof(PgStat_TableEntry))
209 typedef struct PgStat_MsgTabstat
211 PgStat_MsgHdr m_hdr;
212 Oid m_databaseid;
213 int m_nentries;
214 int m_xact_commit;
215 int m_xact_rollback;
216 PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
217 } PgStat_MsgTabstat;
220 /* ----------
221 * PgStat_MsgTabpurge Sent by the backend to tell the collector
222 * about dead tables.
223 * ----------
225 #define PGSTAT_NUM_TABPURGE \
226 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
227 / sizeof(Oid))
229 typedef struct PgStat_MsgTabpurge
231 PgStat_MsgHdr m_hdr;
232 Oid m_databaseid;
233 int m_nentries;
234 Oid m_tableid[PGSTAT_NUM_TABPURGE];
235 } PgStat_MsgTabpurge;
238 /* ----------
239 * PgStat_MsgDropdb Sent by the backend to tell the collector
240 * about a dropped database
241 * ----------
243 typedef struct PgStat_MsgDropdb
245 PgStat_MsgHdr m_hdr;
246 Oid m_databaseid;
247 } PgStat_MsgDropdb;
250 /* ----------
251 * PgStat_MsgResetcounter Sent by the backend to tell the collector
252 * to reset counters
253 * ----------
255 typedef struct PgStat_MsgResetcounter
257 PgStat_MsgHdr m_hdr;
258 Oid m_databaseid;
259 } PgStat_MsgResetcounter;
262 /* ----------
263 * PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
264 * that a database is going to be processed
265 * ----------
267 typedef struct PgStat_MsgAutovacStart
269 PgStat_MsgHdr m_hdr;
270 Oid m_databaseid;
271 TimestampTz m_start_time;
272 } PgStat_MsgAutovacStart;
275 /* ----------
276 * PgStat_MsgVacuum Sent by the backend or autovacuum daemon
277 * after VACUUM or VACUUM ANALYZE
278 * ----------
280 typedef struct PgStat_MsgVacuum
282 PgStat_MsgHdr m_hdr;
283 Oid m_databaseid;
284 Oid m_tableoid;
285 bool m_analyze;
286 bool m_autovacuum;
287 bool m_scanned_all;
288 TimestampTz m_vacuumtime;
289 PgStat_Counter m_tuples;
290 } PgStat_MsgVacuum;
293 /* ----------
294 * PgStat_MsgAnalyze Sent by the backend or autovacuum daemon
295 * after ANALYZE
296 * ----------
298 typedef struct PgStat_MsgAnalyze
300 PgStat_MsgHdr m_hdr;
301 Oid m_databaseid;
302 Oid m_tableoid;
303 bool m_autovacuum;
304 TimestampTz m_analyzetime;
305 PgStat_Counter m_live_tuples;
306 PgStat_Counter m_dead_tuples;
307 } PgStat_MsgAnalyze;
310 /* ----------
311 * PgStat_MsgBgWriter Sent by the bgwriter to update statistics.
312 * ----------
314 typedef struct PgStat_MsgBgWriter
316 PgStat_MsgHdr m_hdr;
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;
328 /* ----------
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.
336 * ----------
338 typedef struct PgStat_FunctionCounts
340 PgStat_Counter f_numcalls;
341 instr_time f_time;
342 instr_time f_time_self;
343 } PgStat_FunctionCounts;
345 /* ----------
346 * PgStat_BackendFunctionEntry Entry in backend's per-function hash table
347 * ----------
349 typedef struct PgStat_BackendFunctionEntry
351 Oid f_id;
352 PgStat_FunctionCounts f_counts;
353 } PgStat_BackendFunctionEntry;
355 /* ----------
356 * PgStat_FunctionEntry Per-function info in a MsgFuncstat
357 * ----------
359 typedef struct PgStat_FunctionEntry
361 Oid f_id;
362 PgStat_Counter f_numcalls;
363 PgStat_Counter f_time; /* times in microseconds */
364 PgStat_Counter f_time_self;
365 } PgStat_FunctionEntry;
367 /* ----------
368 * PgStat_MsgFuncstat Sent by the backend to report function
369 * usage statistics.
370 * ----------
372 #define PGSTAT_NUM_FUNCENTRIES \
373 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
374 / sizeof(PgStat_FunctionEntry))
376 typedef struct PgStat_MsgFuncstat
378 PgStat_MsgHdr m_hdr;
379 Oid m_databaseid;
380 int m_nentries;
381 PgStat_FunctionEntry m_entry[PGSTAT_NUM_FUNCENTRIES];
382 } PgStat_MsgFuncstat;
384 /* ----------
385 * PgStat_MsgFuncpurge Sent by the backend to tell the collector
386 * about dead functions.
387 * ----------
389 #define PGSTAT_NUM_FUNCPURGE \
390 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
391 / sizeof(Oid))
393 typedef struct PgStat_MsgFuncpurge
395 PgStat_MsgHdr m_hdr;
396 Oid m_databaseid;
397 int m_nentries;
398 Oid m_functionid[PGSTAT_NUM_FUNCPURGE];
399 } PgStat_MsgFuncpurge;
402 /* ----------
403 * PgStat_Msg Union over all possible messages.
404 * ----------
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;
421 } PgStat_Msg;
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
434 /* ----------
435 * PgStat_StatDBEntry The collector's data per database
436 * ----------
438 typedef struct PgStat_StatDBEntry
440 Oid databaseid;
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
454 * write the pointers out to the stats file.
456 HTAB *tables;
457 HTAB *functions;
458 } PgStat_StatDBEntry;
461 /* ----------
462 * PgStat_StatTabEntry The collector's data per table (or index)
463 * ----------
465 typedef struct PgStat_StatTabEntry
467 Oid tableid;
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;
493 /* ----------
494 * PgStat_StatFuncEntry The collector's data per function
495 * ----------
497 typedef struct PgStat_StatFuncEntry
499 Oid functionid;
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;
524 /* ----------
525 * Shared-memory data structures
526 * ----------
529 /* ----------
530 * PgBackendStatus
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.
536 * ----------
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.
549 int st_changecount;
551 /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
552 int st_procpid;
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 */
560 Oid st_databaseid;
561 Oid st_userid;
562 SockAddr st_clientaddr;
564 /* Is backend currently waiting on an lmgr lock? */
565 bool st_waiting;
567 /* current command string; MUST be null-terminated */
568 char *st_activity;
569 } PgBackendStatus;
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 */
584 instr_time f_start;
585 } PgStat_FunctionCallUsage;
588 /* ----------
589 * GUC parameters
590 * ----------
592 extern bool pgstat_track_activities;
593 extern bool pgstat_track_counts;
594 extern int pgstat_track_functions;
595 extern 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;
604 /* ----------
605 * Functions called from postmaster
606 * ----------
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);
616 #ifdef EXEC_BACKEND
617 extern void PgstatCollectorMain(int argc, char *argv[]);
618 #endif
621 /* ----------
622 * Functions called from backends
623 * ----------
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) \
654 do { \
655 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
656 (rel)->pgstat_info->t_counts.t_numscans++; \
657 } while (0)
658 #define pgstat_count_heap_getnext(rel) \
659 do { \
660 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
661 (rel)->pgstat_info->t_counts.t_tuples_returned++; \
662 } while (0)
663 #define pgstat_count_heap_fetch(rel) \
664 do { \
665 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
666 (rel)->pgstat_info->t_counts.t_tuples_fetched++; \
667 } while (0)
668 #define pgstat_count_index_scan(rel) \
669 do { \
670 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
671 (rel)->pgstat_info->t_counts.t_numscans++; \
672 } while (0)
673 #define pgstat_count_index_tuples(rel, n) \
674 do { \
675 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
676 (rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
677 } while (0)
678 #define pgstat_count_buffer_read(rel) \
679 do { \
680 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
681 (rel)->pgstat_info->t_counts.t_blocks_fetched++; \
682 } while (0)
683 #define pgstat_count_buffer_hit(rel) \
684 do { \
685 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
686 (rel)->pgstat_info->t_counts.t_blocks_hit++; \
687 } while (0)
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,
697 bool finalize);
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);
712 /* ----------
713 * Support functions for the SQL-callable functions to
714 * generate the pgstat* views.
715 * ----------
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 */