]> granicus.if.org Git - postgresql/blob - src/include/pgstat.h
Ensure 64bit arithmetic when calculating tapeSpace
[postgresql] / src / include / pgstat.h
1 /* ----------
2  *      pgstat.h
3  *
4  *      Definitions for the PostgreSQL statistics collector daemon.
5  *
6  *      Copyright (c) 2001-2013, PostgreSQL Global Development Group
7  *
8  *      src/include/pgstat.h
9  * ----------
10  */
11 #ifndef PGSTAT_H
12 #define PGSTAT_H
13
14 #include "datatype/timestamp.h"
15 #include "fmgr.h"
16 #include "libpq/pqcomm.h"
17 #include "portability/instr_time.h"
18 #include "utils/hsearch.h"
19 #include "utils/relcache.h"
20
21
22 /* Values for track_functions GUC variable --- order is significant! */
23 typedef enum TrackFunctionsLevel
24 {
25         TRACK_FUNC_OFF,
26         TRACK_FUNC_PL,
27         TRACK_FUNC_ALL
28 }       TrackFunctionsLevel;
29
30 /* ----------
31  * The types of backend -> collector messages
32  * ----------
33  */
34 typedef enum StatMsgType
35 {
36         PGSTAT_MTYPE_DUMMY,
37         PGSTAT_MTYPE_INQUIRY,
38         PGSTAT_MTYPE_TABSTAT,
39         PGSTAT_MTYPE_TABPURGE,
40         PGSTAT_MTYPE_DROPDB,
41         PGSTAT_MTYPE_RESETCOUNTER,
42         PGSTAT_MTYPE_RESETSHAREDCOUNTER,
43         PGSTAT_MTYPE_RESETSINGLECOUNTER,
44         PGSTAT_MTYPE_AUTOVAC_START,
45         PGSTAT_MTYPE_VACUUM,
46         PGSTAT_MTYPE_ANALYZE,
47         PGSTAT_MTYPE_BGWRITER,
48         PGSTAT_MTYPE_FUNCSTAT,
49         PGSTAT_MTYPE_FUNCPURGE,
50         PGSTAT_MTYPE_RECOVERYCONFLICT,
51         PGSTAT_MTYPE_TEMPFILE,
52         PGSTAT_MTYPE_DEADLOCK
53 } StatMsgType;
54
55 /* ----------
56  * The data type used for counters.
57  * ----------
58  */
59 typedef int64 PgStat_Counter;
60
61 /* ----------
62  * PgStat_TableCounts                   The actual per-table counts kept by a backend
63  *
64  * This struct should contain only actual event counters, because we memcmp
65  * it against zeroes to detect whether there are any counts to transmit.
66  * It is a component of PgStat_TableStatus (within-backend state) and
67  * PgStat_TableEntry (the transmitted message format).
68  *
69  * Note: for a table, tuples_returned is the number of tuples successfully
70  * fetched by heap_getnext, while tuples_fetched is the number of tuples
71  * successfully fetched by heap_fetch under the control of bitmap indexscans.
72  * For an index, tuples_returned is the number of index entries returned by
73  * the index AM, while tuples_fetched is the number of tuples successfully
74  * fetched by heap_fetch under the control of simple indexscans for this index.
75  *
76  * tuples_inserted/updated/deleted/hot_updated count attempted actions,
77  * regardless of whether the transaction committed.  delta_live_tuples,
78  * delta_dead_tuples, and changed_tuples are set depending on commit or abort.
79  * Note that delta_live_tuples and delta_dead_tuples can be negative!
80  * ----------
81  */
82 typedef struct PgStat_TableCounts
83 {
84         PgStat_Counter t_numscans;
85
86         PgStat_Counter t_tuples_returned;
87         PgStat_Counter t_tuples_fetched;
88
89         PgStat_Counter t_tuples_inserted;
90         PgStat_Counter t_tuples_updated;
91         PgStat_Counter t_tuples_deleted;
92         PgStat_Counter t_tuples_hot_updated;
93
94         PgStat_Counter t_delta_live_tuples;
95         PgStat_Counter t_delta_dead_tuples;
96         PgStat_Counter t_changed_tuples;
97
98         PgStat_Counter t_blocks_fetched;
99         PgStat_Counter t_blocks_hit;
100 } PgStat_TableCounts;
101
102 /* Possible targets for resetting cluster-wide shared values */
103 typedef enum PgStat_Shared_Reset_Target
104 {
105         RESET_BGWRITER
106 } PgStat_Shared_Reset_Target;
107
108 /* Possible object types for resetting single counters */
109 typedef enum PgStat_Single_Reset_Type
110 {
111         RESET_TABLE,
112         RESET_FUNCTION
113 } PgStat_Single_Reset_Type;
114
115 /* ------------------------------------------------------------
116  * Structures kept in backend local memory while accumulating counts
117  * ------------------------------------------------------------
118  */
119
120
121 /* ----------
122  * PgStat_TableStatus                   Per-table status within a backend
123  *
124  * Many of the event counters are nontransactional, ie, we count events
125  * in committed and aborted transactions alike.  For these, we just count
126  * directly in the PgStat_TableStatus.  However, delta_live_tuples,
127  * delta_dead_tuples, and changed_tuples must be derived from event counts
128  * with awareness of whether the transaction or subtransaction committed or
129  * aborted.  Hence, we also keep a stack of per-(sub)transaction status
130  * records for every table modified in the current transaction.  At commit
131  * or abort, we propagate tuples_inserted/updated/deleted up to the
132  * parent subtransaction level, or out to the parent PgStat_TableStatus,
133  * as appropriate.
134  * ----------
135  */
136 typedef struct PgStat_TableStatus
137 {
138         Oid                     t_id;                   /* table's OID */
139         bool            t_shared;               /* is it a shared catalog? */
140         struct PgStat_TableXactStatus *trans;           /* lowest subxact's counts */
141         PgStat_TableCounts t_counts;    /* event counts to be sent */
142 } PgStat_TableStatus;
143
144 /* ----------
145  * PgStat_TableXactStatus               Per-table, per-subtransaction status
146  * ----------
147  */
148 typedef struct PgStat_TableXactStatus
149 {
150         PgStat_Counter tuples_inserted;         /* tuples inserted in (sub)xact */
151         PgStat_Counter tuples_updated;          /* tuples updated in (sub)xact */
152         PgStat_Counter tuples_deleted;          /* tuples deleted in (sub)xact */
153         int                     nest_level;             /* subtransaction nest level */
154         /* links to other structs for same relation: */
155         struct PgStat_TableXactStatus *upper;           /* next higher subxact if any */
156         PgStat_TableStatus *parent; /* per-table status */
157         /* structs of same subxact level are linked here: */
158         struct PgStat_TableXactStatus *next;            /* next of same subxact */
159 } PgStat_TableXactStatus;
160
161
162 /* ------------------------------------------------------------
163  * Message formats follow
164  * ------------------------------------------------------------
165  */
166
167
168 /* ----------
169  * PgStat_MsgHdr                                The common message header
170  * ----------
171  */
172 typedef struct PgStat_MsgHdr
173 {
174         StatMsgType m_type;
175         int                     m_size;
176 } PgStat_MsgHdr;
177
178 /* ----------
179  * Space available in a message.  This will keep the UDP packets below 1K,
180  * which should fit unfragmented into the MTU of the lo interface on most
181  * platforms. Does anybody care for platforms where it doesn't?
182  * ----------
183  */
184 #define PGSTAT_MSG_PAYLOAD      (1000 - sizeof(PgStat_MsgHdr))
185
186
187 /* ----------
188  * PgStat_MsgDummy                              A dummy message, ignored by the collector
189  * ----------
190  */
191 typedef struct PgStat_MsgDummy
192 {
193         PgStat_MsgHdr m_hdr;
194 } PgStat_MsgDummy;
195
196
197 /* ----------
198  * PgStat_MsgInquiry                    Sent by a backend to ask the collector
199  *                                                              to write the stats file.
200  * ----------
201  */
202
203 typedef struct PgStat_MsgInquiry
204 {
205         PgStat_MsgHdr m_hdr;
206         TimestampTz clock_time;         /* observed local clock time */
207         TimestampTz cutoff_time;        /* minimum acceptable file timestamp */
208         Oid                     databaseid;             /* requested DB (InvalidOid => all DBs) */
209 } PgStat_MsgInquiry;
210
211
212 /* ----------
213  * PgStat_TableEntry                    Per-table info in a MsgTabstat
214  * ----------
215  */
216 typedef struct PgStat_TableEntry
217 {
218         Oid                     t_id;
219         PgStat_TableCounts t_counts;
220 } PgStat_TableEntry;
221
222 /* ----------
223  * PgStat_MsgTabstat                    Sent by the backend to report table
224  *                                                              and buffer access statistics.
225  * ----------
226  */
227 #define PGSTAT_NUM_TABENTRIES  \
228         ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int))  \
229          / sizeof(PgStat_TableEntry))
230
231 typedef struct PgStat_MsgTabstat
232 {
233         PgStat_MsgHdr m_hdr;
234         Oid                     m_databaseid;
235         int                     m_nentries;
236         int                     m_xact_commit;
237         int                     m_xact_rollback;
238         PgStat_Counter m_block_read_time;       /* times in microseconds */
239         PgStat_Counter m_block_write_time;
240         PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
241 } PgStat_MsgTabstat;
242
243
244 /* ----------
245  * PgStat_MsgTabpurge                   Sent by the backend to tell the collector
246  *                                                              about dead tables.
247  * ----------
248  */
249 #define PGSTAT_NUM_TABPURGE  \
250         ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int))  \
251          / sizeof(Oid))
252
253 typedef struct PgStat_MsgTabpurge
254 {
255         PgStat_MsgHdr m_hdr;
256         Oid                     m_databaseid;
257         int                     m_nentries;
258         Oid                     m_tableid[PGSTAT_NUM_TABPURGE];
259 } PgStat_MsgTabpurge;
260
261
262 /* ----------
263  * PgStat_MsgDropdb                             Sent by the backend to tell the collector
264  *                                                              about a dropped database
265  * ----------
266  */
267 typedef struct PgStat_MsgDropdb
268 {
269         PgStat_MsgHdr m_hdr;
270         Oid                     m_databaseid;
271 } PgStat_MsgDropdb;
272
273
274 /* ----------
275  * PgStat_MsgResetcounter               Sent by the backend to tell the collector
276  *                                                              to reset counters
277  * ----------
278  */
279 typedef struct PgStat_MsgResetcounter
280 {
281         PgStat_MsgHdr m_hdr;
282         Oid                     m_databaseid;
283 } PgStat_MsgResetcounter;
284
285 /* ----------
286  * PgStat_MsgResetsharedcounter Sent by the backend to tell the collector
287  *                                                              to reset a shared counter
288  * ----------
289  */
290 typedef struct PgStat_MsgResetsharedcounter
291 {
292         PgStat_MsgHdr m_hdr;
293         PgStat_Shared_Reset_Target m_resettarget;
294 } PgStat_MsgResetsharedcounter;
295
296 /* ----------
297  * PgStat_MsgResetsinglecounter Sent by the backend to tell the collector
298  *                                                              to reset a single counter
299  * ----------
300  */
301 typedef struct PgStat_MsgResetsinglecounter
302 {
303         PgStat_MsgHdr m_hdr;
304         Oid                     m_databaseid;
305         PgStat_Single_Reset_Type m_resettype;
306         Oid                     m_objectid;
307 } PgStat_MsgResetsinglecounter;
308
309 /* ----------
310  * PgStat_MsgAutovacStart               Sent by the autovacuum daemon to signal
311  *                                                              that a database is going to be processed
312  * ----------
313  */
314 typedef struct PgStat_MsgAutovacStart
315 {
316         PgStat_MsgHdr m_hdr;
317         Oid                     m_databaseid;
318         TimestampTz m_start_time;
319 } PgStat_MsgAutovacStart;
320
321
322 /* ----------
323  * PgStat_MsgVacuum                             Sent by the backend or autovacuum daemon
324  *                                                              after VACUUM
325  * ----------
326  */
327 typedef struct PgStat_MsgVacuum
328 {
329         PgStat_MsgHdr m_hdr;
330         Oid                     m_databaseid;
331         Oid                     m_tableoid;
332         bool            m_autovacuum;
333         TimestampTz m_vacuumtime;
334         PgStat_Counter m_tuples;
335 } PgStat_MsgVacuum;
336
337
338 /* ----------
339  * PgStat_MsgAnalyze                    Sent by the backend or autovacuum daemon
340  *                                                              after ANALYZE
341  * ----------
342  */
343 typedef struct PgStat_MsgAnalyze
344 {
345         PgStat_MsgHdr m_hdr;
346         Oid                     m_databaseid;
347         Oid                     m_tableoid;
348         bool            m_autovacuum;
349         TimestampTz m_analyzetime;
350         PgStat_Counter m_live_tuples;
351         PgStat_Counter m_dead_tuples;
352 } PgStat_MsgAnalyze;
353
354
355 /* ----------
356  * PgStat_MsgBgWriter                   Sent by the bgwriter to update statistics.
357  * ----------
358  */
359 typedef struct PgStat_MsgBgWriter
360 {
361         PgStat_MsgHdr m_hdr;
362
363         PgStat_Counter m_timed_checkpoints;
364         PgStat_Counter m_requested_checkpoints;
365         PgStat_Counter m_buf_written_checkpoints;
366         PgStat_Counter m_buf_written_clean;
367         PgStat_Counter m_maxwritten_clean;
368         PgStat_Counter m_buf_written_backend;
369         PgStat_Counter m_buf_fsync_backend;
370         PgStat_Counter m_buf_alloc;
371         PgStat_Counter m_checkpoint_write_time;         /* times in milliseconds */
372         PgStat_Counter m_checkpoint_sync_time;
373 } PgStat_MsgBgWriter;
374
375 /* ----------
376  * PgStat_MsgRecoveryConflict   Sent by the backend upon recovery conflict
377  * ----------
378  */
379 typedef struct PgStat_MsgRecoveryConflict
380 {
381         PgStat_MsgHdr m_hdr;
382
383         Oid                     m_databaseid;
384         int                     m_reason;
385 } PgStat_MsgRecoveryConflict;
386
387 /* ----------
388  * PgStat_MsgTempFile   Sent by the backend upon creating a temp file
389  * ----------
390  */
391 typedef struct PgStat_MsgTempFile
392 {
393         PgStat_MsgHdr m_hdr;
394
395         Oid                     m_databaseid;
396         size_t          m_filesize;
397 } PgStat_MsgTempFile;
398
399 /* ----------
400  * PgStat_FunctionCounts        The actual per-function counts kept by a backend
401  *
402  * This struct should contain only actual event counters, because we memcmp
403  * it against zeroes to detect whether there are any counts to transmit.
404  *
405  * Note that the time counters are in instr_time format here.  We convert to
406  * microseconds in PgStat_Counter format when transmitting to the collector.
407  * ----------
408  */
409 typedef struct PgStat_FunctionCounts
410 {
411         PgStat_Counter f_numcalls;
412         instr_time      f_total_time;
413         instr_time      f_self_time;
414 } PgStat_FunctionCounts;
415
416 /* ----------
417  * PgStat_BackendFunctionEntry  Entry in backend's per-function hash table
418  * ----------
419  */
420 typedef struct PgStat_BackendFunctionEntry
421 {
422         Oid                     f_id;
423         PgStat_FunctionCounts f_counts;
424 } PgStat_BackendFunctionEntry;
425
426 /* ----------
427  * PgStat_FunctionEntry                 Per-function info in a MsgFuncstat
428  * ----------
429  */
430 typedef struct PgStat_FunctionEntry
431 {
432         Oid                     f_id;
433         PgStat_Counter f_numcalls;
434         PgStat_Counter f_total_time;    /* times in microseconds */
435         PgStat_Counter f_self_time;
436 } PgStat_FunctionEntry;
437
438 /* ----------
439  * PgStat_MsgFuncstat                   Sent by the backend to report function
440  *                                                              usage statistics.
441  * ----------
442  */
443 #define PGSTAT_NUM_FUNCENTRIES  \
444         ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int))  \
445          / sizeof(PgStat_FunctionEntry))
446
447 typedef struct PgStat_MsgFuncstat
448 {
449         PgStat_MsgHdr m_hdr;
450         Oid                     m_databaseid;
451         int                     m_nentries;
452         PgStat_FunctionEntry m_entry[PGSTAT_NUM_FUNCENTRIES];
453 } PgStat_MsgFuncstat;
454
455 /* ----------
456  * PgStat_MsgFuncpurge                  Sent by the backend to tell the collector
457  *                                                              about dead functions.
458  * ----------
459  */
460 #define PGSTAT_NUM_FUNCPURGE  \
461         ((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int))  \
462          / sizeof(Oid))
463
464 typedef struct PgStat_MsgFuncpurge
465 {
466         PgStat_MsgHdr m_hdr;
467         Oid                     m_databaseid;
468         int                     m_nentries;
469         Oid                     m_functionid[PGSTAT_NUM_FUNCPURGE];
470 } PgStat_MsgFuncpurge;
471
472 /* ----------
473  * PgStat_MsgDeadlock                   Sent by the backend to tell the collector
474  *                                                              about a deadlock that occurred.
475  * ----------
476  */
477 typedef struct PgStat_MsgDeadlock
478 {
479         PgStat_MsgHdr m_hdr;
480         Oid                     m_databaseid;
481 } PgStat_MsgDeadlock;
482
483
484 /* ----------
485  * PgStat_Msg                                   Union over all possible messages.
486  * ----------
487  */
488 typedef union PgStat_Msg
489 {
490         PgStat_MsgHdr msg_hdr;
491         PgStat_MsgDummy msg_dummy;
492         PgStat_MsgInquiry msg_inquiry;
493         PgStat_MsgTabstat msg_tabstat;
494         PgStat_MsgTabpurge msg_tabpurge;
495         PgStat_MsgDropdb msg_dropdb;
496         PgStat_MsgResetcounter msg_resetcounter;
497         PgStat_MsgResetsharedcounter msg_resetsharedcounter;
498         PgStat_MsgResetsinglecounter msg_resetsinglecounter;
499         PgStat_MsgAutovacStart msg_autovacuum;
500         PgStat_MsgVacuum msg_vacuum;
501         PgStat_MsgAnalyze msg_analyze;
502         PgStat_MsgBgWriter msg_bgwriter;
503         PgStat_MsgFuncstat msg_funcstat;
504         PgStat_MsgFuncpurge msg_funcpurge;
505         PgStat_MsgRecoveryConflict msg_recoveryconflict;
506         PgStat_MsgDeadlock msg_deadlock;
507 } PgStat_Msg;
508
509
510 /* ------------------------------------------------------------
511  * Statistic collector data structures follow
512  *
513  * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
514  * data structures change.
515  * ------------------------------------------------------------
516  */
517
518 #define PGSTAT_FILE_FORMAT_ID   0x01A5BC9B
519
520 /* ----------
521  * PgStat_StatDBEntry                   The collector's data per database
522  * ----------
523  */
524 typedef struct PgStat_StatDBEntry
525 {
526         Oid                     databaseid;
527         PgStat_Counter n_xact_commit;
528         PgStat_Counter n_xact_rollback;
529         PgStat_Counter n_blocks_fetched;
530         PgStat_Counter n_blocks_hit;
531         PgStat_Counter n_tuples_returned;
532         PgStat_Counter n_tuples_fetched;
533         PgStat_Counter n_tuples_inserted;
534         PgStat_Counter n_tuples_updated;
535         PgStat_Counter n_tuples_deleted;
536         TimestampTz last_autovac_time;
537         PgStat_Counter n_conflict_tablespace;
538         PgStat_Counter n_conflict_lock;
539         PgStat_Counter n_conflict_snapshot;
540         PgStat_Counter n_conflict_bufferpin;
541         PgStat_Counter n_conflict_startup_deadlock;
542         PgStat_Counter n_temp_files;
543         PgStat_Counter n_temp_bytes;
544         PgStat_Counter n_deadlocks;
545         PgStat_Counter n_block_read_time;       /* times in microseconds */
546         PgStat_Counter n_block_write_time;
547
548         TimestampTz stat_reset_timestamp;
549         TimestampTz stats_timestamp;    /* time of db stats file update */
550
551         /*
552          * tables and functions must be last in the struct, because we don't write
553          * the pointers out to the stats file.
554          */
555         HTAB       *tables;
556         HTAB       *functions;
557 } PgStat_StatDBEntry;
558
559
560 /* ----------
561  * PgStat_StatTabEntry                  The collector's data per table (or index)
562  * ----------
563  */
564 typedef struct PgStat_StatTabEntry
565 {
566         Oid                     tableid;
567
568         PgStat_Counter numscans;
569
570         PgStat_Counter tuples_returned;
571         PgStat_Counter tuples_fetched;
572
573         PgStat_Counter tuples_inserted;
574         PgStat_Counter tuples_updated;
575         PgStat_Counter tuples_deleted;
576         PgStat_Counter tuples_hot_updated;
577
578         PgStat_Counter n_live_tuples;
579         PgStat_Counter n_dead_tuples;
580         PgStat_Counter changes_since_analyze;
581
582         PgStat_Counter blocks_fetched;
583         PgStat_Counter blocks_hit;
584
585         TimestampTz vacuum_timestamp;           /* user initiated vacuum */
586         PgStat_Counter vacuum_count;
587         TimestampTz autovac_vacuum_timestamp;           /* autovacuum initiated */
588         PgStat_Counter autovac_vacuum_count;
589         TimestampTz analyze_timestamp;          /* user initiated */
590         PgStat_Counter analyze_count;
591         TimestampTz autovac_analyze_timestamp;          /* autovacuum initiated */
592         PgStat_Counter autovac_analyze_count;
593 } PgStat_StatTabEntry;
594
595
596 /* ----------
597  * PgStat_StatFuncEntry                 The collector's data per function
598  * ----------
599  */
600 typedef struct PgStat_StatFuncEntry
601 {
602         Oid                     functionid;
603
604         PgStat_Counter f_numcalls;
605
606         PgStat_Counter f_total_time;    /* times in microseconds */
607         PgStat_Counter f_self_time;
608 } PgStat_StatFuncEntry;
609
610
611 /*
612  * Global statistics kept in the stats collector
613  */
614 typedef struct PgStat_GlobalStats
615 {
616         TimestampTz stats_timestamp;    /* time of stats file update */
617         PgStat_Counter timed_checkpoints;
618         PgStat_Counter requested_checkpoints;
619         PgStat_Counter checkpoint_write_time;           /* times in milliseconds */
620         PgStat_Counter checkpoint_sync_time;
621         PgStat_Counter buf_written_checkpoints;
622         PgStat_Counter buf_written_clean;
623         PgStat_Counter maxwritten_clean;
624         PgStat_Counter buf_written_backend;
625         PgStat_Counter buf_fsync_backend;
626         PgStat_Counter buf_alloc;
627         TimestampTz stat_reset_timestamp;
628 } PgStat_GlobalStats;
629
630
631 /* ----------
632  * Backend states
633  * ----------
634  */
635 typedef enum BackendState
636 {
637         STATE_UNDEFINED,
638         STATE_IDLE,
639         STATE_RUNNING,
640         STATE_IDLEINTRANSACTION,
641         STATE_FASTPATH,
642         STATE_IDLEINTRANSACTION_ABORTED,
643         STATE_DISABLED,
644 } BackendState;
645
646 /* ----------
647  * Shared-memory data structures
648  * ----------
649  */
650
651
652 /* ----------
653  * PgBackendStatus
654  *
655  * Each live backend maintains a PgBackendStatus struct in shared memory
656  * showing its current activity.  (The structs are allocated according to
657  * BackendId, but that is not critical.)  Note that the collector process
658  * has no involvement in, or even access to, these structs.
659  * ----------
660  */
661 typedef struct PgBackendStatus
662 {
663         /*
664          * To avoid locking overhead, we use the following protocol: a backend
665          * increments st_changecount before modifying its entry, and again after
666          * finishing a modification.  A would-be reader should note the value of
667          * st_changecount, copy the entry into private memory, then check
668          * st_changecount again.  If the value hasn't changed, and if it's even,
669          * the copy is valid; otherwise start over.  This makes updates cheap
670          * while reads are potentially expensive, but that's the tradeoff we want.
671          */
672         int                     st_changecount;
673
674         /* The entry is valid iff st_procpid > 0, unused if st_procpid == 0 */
675         int                     st_procpid;
676
677         /* Times when current backend, transaction, and activity started */
678         TimestampTz st_proc_start_timestamp;
679         TimestampTz st_xact_start_timestamp;
680         TimestampTz st_activity_start_timestamp;
681         TimestampTz st_state_start_timestamp;
682
683         /* Database OID, owning user's OID, connection client address */
684         Oid                     st_databaseid;
685         Oid                     st_userid;
686         SockAddr        st_clientaddr;
687         char       *st_clienthostname;          /* MUST be null-terminated */
688
689         /* Is backend currently waiting on an lmgr lock? */
690         bool            st_waiting;
691
692         /* current state */
693         BackendState st_state;
694
695         /* application name; MUST be null-terminated */
696         char       *st_appname;
697
698         /* current command string; MUST be null-terminated */
699         char       *st_activity;
700 } PgBackendStatus;
701
702 /*
703  * Working state needed to accumulate per-function-call timing statistics.
704  */
705 typedef struct PgStat_FunctionCallUsage
706 {
707         /* Link to function's hashtable entry (must still be there at exit!) */
708         /* NULL means we are not tracking the current function call */
709         PgStat_FunctionCounts *fs;
710         /* Total time previously charged to function, as of function start */
711         instr_time      save_f_total_time;
712         /* Backend-wide total time as of function start */
713         instr_time      save_total;
714         /* system clock as of function start */
715         instr_time      f_start;
716 } PgStat_FunctionCallUsage;
717
718
719 /* ----------
720  * GUC parameters
721  * ----------
722  */
723 extern bool pgstat_track_activities;
724 extern bool pgstat_track_counts;
725 extern int      pgstat_track_functions;
726 extern PGDLLIMPORT int pgstat_track_activity_query_size;
727 extern char *pgstat_stat_directory;
728 extern char *pgstat_stat_tmpname;
729 extern char *pgstat_stat_filename;
730
731 /*
732  * BgWriter statistics counters are updated directly by bgwriter and bufmgr
733  */
734 extern PgStat_MsgBgWriter BgWriterStats;
735
736 /*
737  * Updated by pgstat_count_buffer_*_time macros
738  */
739 extern PgStat_Counter pgStatBlockReadTime;
740 extern PgStat_Counter pgStatBlockWriteTime;
741
742 /* ----------
743  * Functions called from postmaster
744  * ----------
745  */
746 extern Size BackendStatusShmemSize(void);
747 extern void CreateSharedBackendStatus(void);
748
749 extern void pgstat_init(void);
750 extern int      pgstat_start(void);
751 extern void pgstat_reset_all(void);
752 extern void allow_immediate_pgstat_restart(void);
753
754 #ifdef EXEC_BACKEND
755 extern void PgstatCollectorMain(int argc, char *argv[]) __attribute__((noreturn));
756 #endif
757
758
759 /* ----------
760  * Functions called from backends
761  * ----------
762  */
763 extern void pgstat_ping(void);
764
765 extern void pgstat_report_stat(bool force);
766 extern void pgstat_vacuum_stat(void);
767 extern void pgstat_drop_database(Oid databaseid);
768
769 extern void pgstat_clear_snapshot(void);
770 extern void pgstat_reset_counters(void);
771 extern void pgstat_reset_shared_counters(const char *);
772 extern void pgstat_reset_single_counter(Oid objectid, PgStat_Single_Reset_Type type);
773
774 extern void pgstat_report_autovac(Oid dboid);
775 extern void pgstat_report_vacuum(Oid tableoid, bool shared,
776                                          PgStat_Counter tuples);
777 extern void pgstat_report_analyze(Relation rel,
778                                           PgStat_Counter livetuples, PgStat_Counter deadtuples);
779
780 extern void pgstat_report_recovery_conflict(int reason);
781 extern void pgstat_report_deadlock(void);
782
783 extern void pgstat_initialize(void);
784 extern void pgstat_bestart(void);
785
786 extern void pgstat_report_activity(BackendState state, const char *cmd_str);
787 extern void pgstat_report_tempfile(size_t filesize);
788 extern void pgstat_report_appname(const char *appname);
789 extern void pgstat_report_xact_timestamp(TimestampTz tstamp);
790 extern void pgstat_report_waiting(bool waiting);
791 extern const char *pgstat_get_backend_current_activity(int pid, bool checkUser);
792 extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer,
793                                                                         int buflen);
794
795 extern PgStat_TableStatus *find_tabstat_entry(Oid rel_id);
796 extern PgStat_BackendFunctionEntry *find_funcstat_entry(Oid func_id);
797
798 extern void pgstat_initstats(Relation rel);
799
800 /* nontransactional event counts are simple enough to inline */
801
802 #define pgstat_count_heap_scan(rel)                                                                     \
803         do {                                                                                                                    \
804                 if ((rel)->pgstat_info != NULL)                                                         \
805                         (rel)->pgstat_info->t_counts.t_numscans++;                              \
806         } while (0)
807 #define pgstat_count_heap_getnext(rel)                                                          \
808         do {                                                                                                                    \
809                 if ((rel)->pgstat_info != NULL)                                                         \
810                         (rel)->pgstat_info->t_counts.t_tuples_returned++;               \
811         } while (0)
812 #define pgstat_count_heap_fetch(rel)                                                            \
813         do {                                                                                                                    \
814                 if ((rel)->pgstat_info != NULL)                                                         \
815                         (rel)->pgstat_info->t_counts.t_tuples_fetched++;                \
816         } while (0)
817 #define pgstat_count_index_scan(rel)                                                            \
818         do {                                                                                                                    \
819                 if ((rel)->pgstat_info != NULL)                                                         \
820                         (rel)->pgstat_info->t_counts.t_numscans++;                              \
821         } while (0)
822 #define pgstat_count_index_tuples(rel, n)                                                       \
823         do {                                                                                                                    \
824                 if ((rel)->pgstat_info != NULL)                                                         \
825                         (rel)->pgstat_info->t_counts.t_tuples_returned += (n);  \
826         } while (0)
827 #define pgstat_count_buffer_read(rel)                                                           \
828         do {                                                                                                                    \
829                 if ((rel)->pgstat_info != NULL)                                                         \
830                         (rel)->pgstat_info->t_counts.t_blocks_fetched++;                \
831         } while (0)
832 #define pgstat_count_buffer_hit(rel)                                                            \
833         do {                                                                                                                    \
834                 if ((rel)->pgstat_info != NULL)                                                         \
835                         (rel)->pgstat_info->t_counts.t_blocks_hit++;                    \
836         } while (0)
837 #define pgstat_count_buffer_read_time(n)                                                        \
838         (pgStatBlockReadTime += (n))
839 #define pgstat_count_buffer_write_time(n)                                                       \
840         (pgStatBlockWriteTime += (n))
841
842 extern void pgstat_count_heap_insert(Relation rel, int n);
843 extern void pgstat_count_heap_update(Relation rel, bool hot);
844 extern void pgstat_count_heap_delete(Relation rel);
845 extern void pgstat_update_heap_dead_tuples(Relation rel, int delta);
846
847 extern void pgstat_init_function_usage(FunctionCallInfoData *fcinfo,
848                                                    PgStat_FunctionCallUsage *fcu);
849 extern void pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu,
850                                                   bool finalize);
851
852 extern void AtEOXact_PgStat(bool isCommit);
853 extern void AtEOSubXact_PgStat(bool isCommit, int nestDepth);
854
855 extern void AtPrepare_PgStat(void);
856 extern void PostPrepare_PgStat(void);
857
858 extern void pgstat_twophase_postcommit(TransactionId xid, uint16 info,
859                                                    void *recdata, uint32 len);
860 extern void pgstat_twophase_postabort(TransactionId xid, uint16 info,
861                                                   void *recdata, uint32 len);
862
863 extern void pgstat_send_bgwriter(void);
864
865 /* ----------
866  * Support functions for the SQL-callable functions to
867  * generate the pgstat* views.
868  * ----------
869  */
870 extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
871 extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
872 extern PgBackendStatus *pgstat_fetch_stat_beentry(int beid);
873 extern PgStat_StatFuncEntry *pgstat_fetch_stat_funcentry(Oid funcid);
874 extern int      pgstat_fetch_stat_numbackends(void);
875 extern PgStat_GlobalStats *pgstat_fetch_global(void);
876
877 #endif   /* PGSTAT_H */