4
* Definitions for the PostgreSQL statistics collector daemon.
6
* Copyright (c) 2001-2005, PostgreSQL Global Development Group
8
* $PostgreSQL: pgsql/src/include/pgstat.h,v 1.27 2005-01-01 05:43:08 momjian Exp $
14
#include "utils/hsearch.h"
15
#include "utils/nabstime.h"
16
#include "utils/rel.h"
20
* The types of backend/postmaster -> collector messages
23
#define PGSTAT_MTYPE_DUMMY 0
24
#define PGSTAT_MTYPE_BESTART 1
25
#define PGSTAT_MTYPE_BETERM 2
26
#define PGSTAT_MTYPE_ACTIVITY 3
27
#define PGSTAT_MTYPE_TABSTAT 4
28
#define PGSTAT_MTYPE_TABPURGE 5
29
#define PGSTAT_MTYPE_DROPDB 6
30
#define PGSTAT_MTYPE_RESETCOUNTER 7
33
* The data type used for counters.
36
typedef int64 PgStat_Counter;
39
/* ------------------------------------------------------------
40
* Message formats follow
41
* ------------------------------------------------------------
46
* PgStat_MsgHdr The common message header
49
typedef struct PgStat_MsgHdr
60
* Space available in a message. This will keep the UDP packets below 1K,
61
* which should fit unfragmented into the MTU of the lo interface on most
62
* platforms. Does anybody care for platforms where it doesn't?
65
#define PGSTAT_MSG_PAYLOAD (1000 - sizeof(PgStat_MsgHdr))
68
* PgStat_TableEntry Per-table info in a MsgTabstat
71
typedef struct PgStat_TableEntry
75
PgStat_Counter t_numscans;
77
PgStat_Counter t_tuples_returned;
78
PgStat_Counter t_tuples_fetched;
79
PgStat_Counter t_tuples_inserted;
80
PgStat_Counter t_tuples_updated;
81
PgStat_Counter t_tuples_deleted;
83
PgStat_Counter t_blocks_fetched;
84
PgStat_Counter t_blocks_hit;
89
* PgStat_MsgDummy A dummy message, ignored by the collector
92
typedef struct PgStat_MsgDummy
99
* PgStat_MsgBestart Sent by the backend on startup
102
typedef struct PgStat_MsgBestart
108
* PgStat_MsgBeterm Sent by the postmaster after backend exit
111
typedef struct PgStat_MsgBeterm
117
* PgStat_MsgActivity Sent by the backends when they start
121
#define PGSTAT_ACTIVITY_SIZE PGSTAT_MSG_PAYLOAD
123
typedef struct PgStat_MsgActivity
126
char m_what[PGSTAT_ACTIVITY_SIZE];
127
} PgStat_MsgActivity;
130
* PgStat_MsgTabstat Sent by the backend to report table
131
* and buffer access statistics.
134
#define PGSTAT_NUM_TABENTRIES ((PGSTAT_MSG_PAYLOAD - 3 * sizeof(int)) \
135
/ sizeof(PgStat_TableEntry))
137
typedef struct PgStat_MsgTabstat
143
PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
147
* PgStat_MsgTabpurge Sent by the backend to tell the collector
151
#define PGSTAT_NUM_TABPURGE ((PGSTAT_MSG_PAYLOAD - sizeof(int)) \
154
typedef struct PgStat_MsgTabpurge
158
Oid m_tableid[PGSTAT_NUM_TABPURGE];
159
} PgStat_MsgTabpurge;
163
* PgStat_MsgDropdb Sent by the backend to tell the collector
164
* about dropped database
167
typedef struct PgStat_MsgDropdb
175
* PgStat_MsgResetcounter Sent by the backend to tell the collector
179
typedef struct PgStat_MsgResetcounter
182
} PgStat_MsgResetcounter;
186
* PgStat_Msg Union over all possible messages.
189
typedef union PgStat_Msg
191
PgStat_MsgHdr msg_hdr;
192
PgStat_MsgDummy msg_dummy;
193
PgStat_MsgBestart msg_bestart;
194
PgStat_MsgActivity msg_activity;
195
PgStat_MsgTabstat msg_tabstat;
196
PgStat_MsgTabpurge msg_tabpurge;
197
PgStat_MsgDropdb msg_dropdb;
198
PgStat_MsgResetcounter msg_resetcounter;
202
/* ------------------------------------------------------------
203
* Statistic collector data structures follow
204
* ------------------------------------------------------------
208
* PgStat_StatDBEntry The collectors data per database
211
typedef struct PgStat_StatDBEntry
216
PgStat_Counter n_connects;
217
PgStat_Counter n_xact_commit;
218
PgStat_Counter n_xact_rollback;
219
PgStat_Counter n_blocks_fetched;
220
PgStat_Counter n_blocks_hit;
222
} PgStat_StatDBEntry;
226
* PgStat_StatBeEntry The collectors data per backend
229
typedef struct PgStat_StatBeEntry
234
AbsoluteTime activity_start_sec;
235
int activity_start_usec;
236
char activity[PGSTAT_ACTIVITY_SIZE];
237
} PgStat_StatBeEntry;
241
* PgStat_StatBeDead Because UDP packets can arrive out of
242
* order, we need to keep some information
243
* about backends that are known to be
244
* dead for some seconds. This info is held
245
* in a hash table of these structs.
248
typedef struct PgStat_StatBeDead
257
* PgStat_StatTabEntry The collectors data table data
260
typedef struct PgStat_StatTabEntry
264
PgStat_Counter numscans;
266
PgStat_Counter tuples_returned;
267
PgStat_Counter tuples_fetched;
268
PgStat_Counter tuples_inserted;
269
PgStat_Counter tuples_updated;
270
PgStat_Counter tuples_deleted;
272
PgStat_Counter blocks_fetched;
273
PgStat_Counter blocks_hit;
276
} PgStat_StatTabEntry;
283
extern bool pgstat_collect_startcollector;
284
extern bool pgstat_collect_resetonpmstart;
285
extern bool pgstat_collect_querystring;
286
extern bool pgstat_collect_tuplelevel;
287
extern bool pgstat_collect_blocklevel;
291
* Functions called from postmaster
294
extern void pgstat_init(void);
295
extern int pgstat_start(void);
296
extern void pgstat_beterm(int pid);
299
extern void PgstatBufferMain(int argc, char *argv[]);
300
extern void PgstatCollectorMain(int argc, char *argv[]);
305
* Functions called from backends
308
extern void pgstat_bestart(void);
310
extern void pgstat_ping(void);
311
extern void pgstat_report_activity(const char *what);
312
extern void pgstat_report_tabstat(void);
313
extern int pgstat_vacuum_tabstat(void);
315
extern void pgstat_reset_counters(void);
317
extern void pgstat_initstats(PgStat_Info *stats, Relation rel);
320
#define pgstat_reset_heap_scan(s) \
322
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
323
(s)->heap_scan_counted = FALSE; \
325
#define pgstat_count_heap_scan(s) \
327
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL && \
328
!(s)->heap_scan_counted) { \
329
((PgStat_TableEntry *)((s)->tabentry))->t_numscans++; \
330
(s)->heap_scan_counted = TRUE; \
333
#define pgstat_count_heap_getnext(s) \
335
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
336
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
338
#define pgstat_count_heap_fetch(s) \
340
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
341
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_fetched++; \
343
#define pgstat_count_heap_insert(s) \
345
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
346
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
348
#define pgstat_count_heap_update(s) \
350
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
351
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_updated++; \
353
#define pgstat_count_heap_delete(s) \
355
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
356
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_deleted++; \
358
#define pgstat_reset_index_scan(s) \
360
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
361
(s)->index_scan_counted = FALSE; \
363
#define pgstat_count_index_scan(s) \
365
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL && \
366
!(s)->index_scan_counted) { \
367
((PgStat_TableEntry *)((s)->tabentry))->t_numscans++; \
368
(s)->index_scan_counted = TRUE; \
371
#define pgstat_count_index_getnext(s) \
373
if (pgstat_collect_tuplelevel && (s)->tabentry != NULL) \
374
((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
376
#define pgstat_count_buffer_read(s,r) \
378
if (pgstat_collect_blocklevel && (s)->tabentry != NULL) \
379
((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
381
if (pgstat_collect_blocklevel && !(s)->no_stats) { \
382
pgstat_initstats((s), (r)); \
383
if ((s)->tabentry != NULL) \
384
((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
388
#define pgstat_count_buffer_hit(s,r) \
390
if (pgstat_collect_blocklevel && (s)->tabentry != NULL) \
391
((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
393
if (pgstat_collect_blocklevel && !(s)->no_stats) { \
394
pgstat_initstats((s), (r)); \
395
if ((s)->tabentry != NULL) \
396
((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
402
extern void pgstat_count_xact_commit(void);
403
extern void pgstat_count_xact_rollback(void);
406
* Support functions for the SQL-callable functions to
407
* generate the pgstat* views.
410
extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
411
extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
412
extern PgStat_StatBeEntry *pgstat_fetch_stat_beentry(int beid);
413
extern int pgstat_fetch_stat_numbackends(void);
415
#endif /* PGSTAT_H */