Fix pg_dump bug in the database-level collation patch. "datcollate" and
[PostgreSQL.git] / src / include / pgstat.h
blobe6b1ef01092647903dd2fcf80bcd92d481d2332a
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_TABSTAT,
37 PGSTAT_MTYPE_TABPURGE,
38 PGSTAT_MTYPE_DROPDB,
39 PGSTAT_MTYPE_RESETCOUNTER,
40 PGSTAT_MTYPE_AUTOVAC_START,
41 PGSTAT_MTYPE_VACUUM,
42 PGSTAT_MTYPE_ANALYZE,
43 PGSTAT_MTYPE_BGWRITER,
44 PGSTAT_MTYPE_FUNCSTAT,
45 PGSTAT_MTYPE_FUNCPURGE
46 } StatMsgType;
48 /* ----------
49 * The data type used for counters.
50 * ----------
52 typedef int64 PgStat_Counter;
54 /* ----------
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!
73 * ----------
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;
92 } PgStat_TableCounts;
95 /* ------------------------------------------------------------
96 * Structures kept in backend local memory while accumulating counts
97 * ------------------------------------------------------------
101 /* ----------
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,
113 * as appropriate.
114 * ----------
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;
124 /* ----------
125 * PgStat_TableXactStatus Per-table, per-subtransaction status
126 * ----------
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 * ------------------------------------------------------------
147 /* ----------
148 * PgStat_MsgHdr The common message header
149 * ----------
151 typedef struct PgStat_MsgHdr
153 StatMsgType m_type;
154 int m_size;
155 } PgStat_MsgHdr;
157 /* ----------
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?
161 * ----------
163 #define PGSTAT_MSG_PAYLOAD (1000 - sizeof(PgStat_MsgHdr))
166 /* ----------
167 * PgStat_MsgDummy A dummy message, ignored by the collector
168 * ----------
170 typedef struct PgStat_MsgDummy
172 PgStat_MsgHdr m_hdr;
173 } PgStat_MsgDummy;
176 /* ----------
177 * PgStat_TableEntry Per-table info in a MsgTabstat
178 * ----------
180 typedef struct PgStat_TableEntry
182 Oid t_id;
183 PgStat_TableCounts t_counts;
184 } PgStat_TableEntry;
186 /* ----------
187 * PgStat_MsgTabstat Sent by the backend to report table
188 * and buffer access statistics.
189 * ----------
191 #define PGSTAT_NUM_TABENTRIES \
192 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int)) \
193 / sizeof(PgStat_TableEntry))
195 typedef struct PgStat_MsgTabstat
197 PgStat_MsgHdr m_hdr;
198 Oid m_databaseid;
199 int m_nentries;
200 int m_xact_commit;
201 int m_xact_rollback;
202 PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
203 } PgStat_MsgTabstat;
206 /* ----------
207 * PgStat_MsgTabpurge Sent by the backend to tell the collector
208 * about dead tables.
209 * ----------
211 #define PGSTAT_NUM_TABPURGE \
212 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
213 / sizeof(Oid))
215 typedef struct PgStat_MsgTabpurge
217 PgStat_MsgHdr m_hdr;
218 Oid m_databaseid;
219 int m_nentries;
220 Oid m_tableid[PGSTAT_NUM_TABPURGE];
221 } PgStat_MsgTabpurge;
224 /* ----------
225 * PgStat_MsgDropdb Sent by the backend to tell the collector
226 * about a dropped database
227 * ----------
229 typedef struct PgStat_MsgDropdb
231 PgStat_MsgHdr m_hdr;
232 Oid m_databaseid;
233 } PgStat_MsgDropdb;
236 /* ----------
237 * PgStat_MsgResetcounter Sent by the backend to tell the collector
238 * to reset counters
239 * ----------
241 typedef struct PgStat_MsgResetcounter
243 PgStat_MsgHdr m_hdr;
244 Oid m_databaseid;
245 } PgStat_MsgResetcounter;
248 /* ----------
249 * PgStat_MsgAutovacStart Sent by the autovacuum daemon to signal
250 * that a database is going to be processed
251 * ----------
253 typedef struct PgStat_MsgAutovacStart
255 PgStat_MsgHdr m_hdr;
256 Oid m_databaseid;
257 TimestampTz m_start_time;
258 } PgStat_MsgAutovacStart;
261 /* ----------
262 * PgStat_MsgVacuum Sent by the backend or autovacuum daemon
263 * after VACUUM or VACUUM ANALYZE
264 * ----------
266 typedef struct PgStat_MsgVacuum
268 PgStat_MsgHdr m_hdr;
269 Oid m_databaseid;
270 Oid m_tableoid;
271 bool m_analyze;
272 bool m_autovacuum;
273 TimestampTz m_vacuumtime;
274 PgStat_Counter m_tuples;
275 } PgStat_MsgVacuum;
278 /* ----------
279 * PgStat_MsgAnalyze Sent by the backend or autovacuum daemon
280 * after ANALYZE
281 * ----------
283 typedef struct PgStat_MsgAnalyze
285 PgStat_MsgHdr m_hdr;
286 Oid m_databaseid;
287 Oid m_tableoid;
288 bool m_autovacuum;
289 TimestampTz m_analyzetime;
290 PgStat_Counter m_live_tuples;
291 PgStat_Counter m_dead_tuples;
292 } PgStat_MsgAnalyze;
295 /* ----------
296 * PgStat_MsgBgWriter Sent by the bgwriter to update statistics.
297 * ----------
299 typedef struct PgStat_MsgBgWriter
301 PgStat_MsgHdr m_hdr;
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;
313 /* ----------
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.
321 * ----------
323 typedef struct PgStat_FunctionCounts
325 PgStat_Counter f_numcalls;
326 instr_time f_time;
327 instr_time f_time_self;
328 } PgStat_FunctionCounts;
330 /* ----------
331 * PgStat_BackendFunctionEntry Entry in backend's per-function hash table
332 * ----------
334 typedef struct PgStat_BackendFunctionEntry
336 Oid f_id;
337 PgStat_FunctionCounts f_counts;
338 } PgStat_BackendFunctionEntry;
340 /* ----------
341 * PgStat_FunctionEntry Per-function info in a MsgFuncstat
342 * ----------
344 typedef struct PgStat_FunctionEntry
346 Oid f_id;
347 PgStat_Counter f_numcalls;
348 PgStat_Counter f_time; /* times in microseconds */
349 PgStat_Counter f_time_self;
350 } PgStat_FunctionEntry;
352 /* ----------
353 * PgStat_MsgFuncstat Sent by the backend to report function
354 * usage statistics.
355 * ----------
357 #define PGSTAT_NUM_FUNCENTRIES \
358 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
359 / sizeof(PgStat_FunctionEntry))
361 typedef struct PgStat_MsgFuncstat
363 PgStat_MsgHdr m_hdr;
364 Oid m_databaseid;
365 int m_nentries;
366 PgStat_FunctionEntry m_entry[PGSTAT_NUM_FUNCENTRIES];
367 } PgStat_MsgFuncstat;
369 /* ----------
370 * PgStat_MsgFuncpurge Sent by the backend to tell the collector
371 * about dead functions.
372 * ----------
374 #define PGSTAT_NUM_FUNCPURGE \
375 ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int)) \
376 / sizeof(Oid))
378 typedef struct PgStat_MsgFuncpurge
380 PgStat_MsgHdr m_hdr;
381 Oid m_databaseid;
382 int m_nentries;
383 Oid m_functionid[PGSTAT_NUM_FUNCPURGE];
384 } PgStat_MsgFuncpurge;
387 /* ----------
388 * PgStat_Msg Union over all possible messages.
389 * ----------
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;
405 } PgStat_Msg;
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
418 /* ----------
419 * PgStat_StatDBEntry The collector's data per database
420 * ----------
422 typedef struct PgStat_StatDBEntry
424 Oid databaseid;
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.
440 HTAB *tables;
441 HTAB *functions;
442 } PgStat_StatDBEntry;
445 /* ----------
446 * PgStat_StatTabEntry The collector's data per table (or index)
447 * ----------
449 typedef struct PgStat_StatTabEntry
451 Oid tableid;
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;
477 /* ----------
478 * PgStat_StatFuncEntry The collector's data per function
479 * ----------
481 typedef struct PgStat_StatFuncEntry
483 Oid functionid;
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;
507 /* ----------
508 * Shared-memory data structures
509 * ----------
512 /* ----------
513 * PgBackendStatus
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.
519 * ----------
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.
532 int st_changecount;
534 /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
535 int st_procpid;
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 */
543 Oid st_databaseid;
544 Oid st_userid;
545 SockAddr st_clientaddr;
547 /* Is backend currently waiting on an lmgr lock? */
548 bool st_waiting;
550 /* current command string; MUST be null-terminated */
551 char *st_activity;
552 } PgBackendStatus;
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 */
567 instr_time f_start;
568 } PgStat_FunctionCallUsage;
571 /* ----------
572 * GUC parameters
573 * ----------
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;
587 /* ----------
588 * Functions called from postmaster
589 * ----------
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);
599 #ifdef EXEC_BACKEND
600 extern void PgstatCollectorMain(int argc, char *argv[]);
601 #endif
604 /* ----------
605 * Functions called from backends
606 * ----------
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) \
637 do { \
638 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
639 (rel)->pgstat_info->t_counts.t_numscans++; \
640 } while (0)
641 #define pgstat_count_heap_getnext(rel) \
642 do { \
643 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
644 (rel)->pgstat_info->t_counts.t_tuples_returned++; \
645 } while (0)
646 #define pgstat_count_heap_fetch(rel) \
647 do { \
648 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
649 (rel)->pgstat_info->t_counts.t_tuples_fetched++; \
650 } while (0)
651 #define pgstat_count_index_scan(rel) \
652 do { \
653 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
654 (rel)->pgstat_info->t_counts.t_numscans++; \
655 } while (0)
656 #define pgstat_count_index_tuples(rel, n) \
657 do { \
658 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
659 (rel)->pgstat_info->t_counts.t_tuples_returned += (n); \
660 } while (0)
661 #define pgstat_count_buffer_read(rel) \
662 do { \
663 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
664 (rel)->pgstat_info->t_counts.t_blocks_fetched++; \
665 } while (0)
666 #define pgstat_count_buffer_hit(rel) \
667 do { \
668 if (pgstat_track_counts && (rel)->pgstat_info != NULL) \
669 (rel)->pgstat_info->t_counts.t_blocks_hit++; \
670 } while (0)
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,
680 bool finalize);
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);
695 /* ----------
696 * Support functions for the SQL-callable functions to
697 * generate the pgstat* views.
698 * ----------
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 */