4 * Definitions for the PostgreSQL statistics collector daemon.
6 * Copyright (c) 2001-2003, PostgreSQL Global Development Group
8 * $Id: pgstat.h,v 1.15 2003/04/27 20:09:44 tgl Exp $
14 #include "utils/hsearch.h"
15 #include "utils/nabstime.h"
16 #include "utils/rel.h"
20 * Paths for the statistics files. The %s is replaced with the
21 * installations $PGDATA.
24 #define PGSTAT_STAT_FILENAME "%s/global/pgstat.stat"
25 #define PGSTAT_STAT_TMPFILE "%s/global/pgstat.tmp.%d"
31 #define PGSTAT_STAT_INTERVAL 500 /* How often to write the status */
32 /* file; in milliseconds. */
34 #define PGSTAT_DESTROY_DELAY 10000 /* How long to keep destroyed */
35 /* objects known, to give delayed */
36 /* UDP packets time to arrive; */
37 /* in milliseconds. */
39 #define PGSTAT_DESTROY_COUNT (PGSTAT_DESTROY_DELAY / PGSTAT_STAT_INTERVAL)
41 #define PGSTAT_RESTART_INTERVAL 60 /* How often to attempt to restart */
42 /* a failed statistics collector; in seconds. */
45 * How much of the actual query string to send to the collector.
48 #define PGSTAT_ACTIVITY_SIZE 256
52 * The types of backend/postmaster -> collector messages
55 #define PGSTAT_MTYPE_DUMMY 0
56 #define PGSTAT_MTYPE_BESTART 1
57 #define PGSTAT_MTYPE_BETERM 2
58 #define PGSTAT_MTYPE_ACTIVITY 3
59 #define PGSTAT_MTYPE_TABSTAT 4
60 #define PGSTAT_MTYPE_TABPURGE 5
61 #define PGSTAT_MTYPE_DROPDB 6
62 #define PGSTAT_MTYPE_RESETCOUNTER 7
65 * Amount of space reserved in pgstat_recvbuffer().
68 #define PGSTAT_RECVBUFFERSZ ((int) (1024 * sizeof(PgStat_Msg)))
72 * The initial size hints for the hash tables used in the
76 #define PGSTAT_DB_HASH_SIZE 16
77 #define PGSTAT_BE_HASH_SIZE 512
78 #define PGSTAT_TAB_HASH_SIZE 512
82 * The data type used for counters.
85 typedef int64 PgStat_Counter;
88 /* ------------------------------------------------------------
89 * Statistic collector data structures follow
90 * ------------------------------------------------------------
93 * PgStat_StatDBEntry The collectors data per database
96 typedef struct PgStat_StatDBEntry
101 PgStat_Counter n_connects;
102 PgStat_Counter n_xact_commit;
103 PgStat_Counter n_xact_rollback;
104 PgStat_Counter n_blocks_fetched;
105 PgStat_Counter n_blocks_hit;
107 } PgStat_StatDBEntry;
111 * PgStat_StatBeEntry The collectors data per backend
114 typedef struct PgStat_StatBeEntry
119 char activity[PGSTAT_ACTIVITY_SIZE];
120 AbsoluteTime activity_start_sec;
121 int activity_start_usec;
122 } PgStat_StatBeEntry;
126 * PgStat_StatBeDead Because UDP packets can arrive out of
127 * order, we need to keep some information
128 * about backends that are known to be
129 * dead for some seconds. This info is held
130 * in a hash table of these structs.
133 typedef struct PgStat_StatBeDead
142 * PgStat_StatTabEntry The collectors data table data
145 typedef struct PgStat_StatTabEntry
149 PgStat_Counter numscans;
151 PgStat_Counter tuples_returned;
152 PgStat_Counter tuples_fetched;
153 PgStat_Counter tuples_inserted;
154 PgStat_Counter tuples_updated;
155 PgStat_Counter tuples_deleted;
157 PgStat_Counter blocks_fetched;
158 PgStat_Counter blocks_hit;
161 } PgStat_StatTabEntry;
164 /* ------------------------------------------------------------
165 * Message formats follow
166 * ------------------------------------------------------------
171 * PgStat_MsgHdr The common message header
174 typedef struct PgStat_MsgHdr
185 * PgStat_TabEntry A table slot in a MsgTabstat
188 typedef struct PgStat_TableEntry
192 PgStat_Counter t_numscans;
194 PgStat_Counter t_tuples_returned;
195 PgStat_Counter t_tuples_fetched;
196 PgStat_Counter t_tuples_inserted;
197 PgStat_Counter t_tuples_updated;
198 PgStat_Counter t_tuples_deleted;
200 PgStat_Counter t_blocks_fetched;
201 PgStat_Counter t_blocks_hit;
206 * PgStat_MsgDummy A dummy message, ignored by the collector
209 typedef struct PgStat_MsgDummy
216 * PgStat_MsgBestart Sent by the backend on startup
219 typedef struct PgStat_MsgBestart
225 * PgStat_MsgBeterm Sent by the postmaster after backend exit
228 typedef struct PgStat_MsgBeterm
234 * PgStat_MsgActivity Sent by the backends when they start
238 typedef struct PgStat_MsgActivity
241 char m_what[PGSTAT_ACTIVITY_SIZE];
242 } PgStat_MsgActivity;
245 * How many table entries fit into a MsgTabstat. Actually,
246 * this will keep the UDP packets below 1K, what should fit
247 * unfragmented into the MTU of the lo interface on most
248 * platforms. Does anybody care for platforms where it doesn't?
251 #define PGSTAT_NUM_TABENTRIES ((1000 - sizeof(PgStat_MsgHdr)) \
252 / sizeof(PgStat_TableEntry))
255 * PgStat_MsgTabstat Sent by the backend to report table
256 * and buffer access statistics.
259 typedef struct PgStat_MsgTabstat
265 PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
270 * How many Oid entries fit into a MsgTabpurge.
273 #define PGSTAT_NUM_TABPURGE ((1000 - sizeof(PgStat_MsgHdr)) \
274 / sizeof(PgStat_TableEntry))
277 * PgStat_MsgTabpurge Sent by the backend to tell the collector
281 typedef struct PgStat_MsgTabpurge
285 Oid m_tableid[PGSTAT_NUM_TABPURGE];
286 } PgStat_MsgTabpurge;
290 * PgStat_MsgDropdb Sent by the backend to tell the collector
291 * about dropped database
294 typedef struct PgStat_MsgDropdb
302 * PgStat_MsgResetcounter Sent by the backend to tell the collector
306 typedef struct PgStat_MsgResetcounter
309 } PgStat_MsgResetcounter;
313 * PgStat_Msg Union over all possible messages.
316 typedef union PgStat_Msg
318 PgStat_MsgHdr msg_hdr;
319 PgStat_MsgDummy msg_dummy;
320 PgStat_MsgBestart msg_bestart;
321 PgStat_MsgActivity msg_activity;
322 PgStat_MsgTabstat msg_tabstat;
323 PgStat_MsgTabpurge msg_tabpurge;
324 PgStat_MsgDropdb msg_dropdb;
325 PgStat_MsgResetcounter msg_resetcounter;
333 extern bool pgstat_collect_startcollector;
334 extern bool pgstat_collect_resetonpmstart;
335 extern bool pgstat_collect_querystring;
336 extern bool pgstat_collect_tuplelevel;
337 extern bool pgstat_collect_blocklevel;
340 * Other global variables
343 extern bool pgstat_is_running;
346 * Functions called from postmaster
349 extern void pgstat_init(void);
350 extern void pgstat_start(void);
351 extern bool pgstat_ispgstat(int pid);
352 extern void pgstat_close_sockets(void);
353 extern void pgstat_beterm(int pid);
356 * Functions called from backends
359 extern void pgstat_bestart(void);
361 extern void pgstat_ping(void);
362 extern void pgstat_report_activity(const char *what);
363 extern void pgstat_report_tabstat(void);
364 extern int pgstat_vacuum_tabstat(void);
366 extern void pgstat_reset_counters(void);
368 extern void pgstat_initstats(PgStat_Info *stats, Relation rel);
371 #define pgstat_reset_heap_scan(s) \
373 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
374 (s)->heap_scan_counted = FALSE; \
376 #define pgstat_count_heap_scan(s) \
378 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL && \
379 !(s)->heap_scan_counted) { \
380 ((PgStat_TableEntry *)((s)->tabentry))->t_numscans++; \
381 (s)->heap_scan_counted = TRUE; \
384 #define pgstat_count_heap_getnext(s) \
386 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
387 ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
389 #define pgstat_count_heap_fetch(s) \
391 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
392 ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_fetched++; \
394 #define pgstat_count_heap_insert(s) \
396 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
397 ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
399 #define pgstat_count_heap_update(s) \
401 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
402 ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_updated++; \
404 #define pgstat_count_heap_delete(s) \
406 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
407 ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_deleted++; \
409 #define pgstat_reset_index_scan(s) \
411 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
412 (s)->index_scan_counted = FALSE; \
414 #define pgstat_count_index_scan(s) \
416 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL && \
417 !(s)->index_scan_counted) { \
418 ((PgStat_TableEntry *)((s)->tabentry))->t_numscans++; \
419 (s)->index_scan_counted = TRUE; \
422 #define pgstat_count_index_getnext(s) \
424 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
425 ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
427 #define pgstat_count_buffer_read(s,r) \
429 if (pgstat_collect_blocklevel && (s)->tabentry != NULL) \
430 ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
432 if (pgstat_collect_blocklevel && !(s)->no_stats) { \
433 pgstat_initstats((s), (r)); \
434 if ((s)->tabentry != NULL) \
435 ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
439 #define pgstat_count_buffer_hit(s,r) \
441 if (pgstat_collect_blocklevel && (s)->tabentry != NULL) \
442 ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
444 if (pgstat_collect_blocklevel && !(s)->no_stats) { \
445 pgstat_initstats((s), (r)); \
446 if ((s)->tabentry != NULL) \
447 ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
453 extern void pgstat_count_xact_commit(void);
454 extern void pgstat_count_xact_rollback(void);
457 * Support functions for the SQL-callable functions to
458 * generate the pgstat* views.
461 extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
462 extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
463 extern PgStat_StatBeEntry *pgstat_fetch_stat_beentry(int beid);
464 extern int pgstat_fetch_stat_numbackends(void);
466 #endif /* PGSTAT_H */