]> granicus.if.org Git - postgresql/blob - src/include/pgstat.h
A visit from the message-style police ...
[postgresql] / src / include / pgstat.h
1 /* ----------
2  *      pgstat.h
3  *
4  *      Definitions for the PostgreSQL statistics collector daemon.
5  *
6  *      Copyright (c) 2001-2003, PostgreSQL Global Development Group
7  *
8  *      $Id: pgstat.h,v 1.15 2003/04/27 20:09:44 tgl Exp $
9  * ----------
10  */
11 #ifndef PGSTAT_H
12 #define PGSTAT_H
13
14 #include "utils/hsearch.h"
15 #include "utils/nabstime.h"
16 #include "utils/rel.h"
17
18
19 /* ----------
20  * Paths for the statistics files. The %s is replaced with the
21  * installations $PGDATA.
22  * ----------
23  */
24 #define PGSTAT_STAT_FILENAME    "%s/global/pgstat.stat"
25 #define PGSTAT_STAT_TMPFILE             "%s/global/pgstat.tmp.%d"
26
27 /* ----------
28  * Timer definitions.
29  * ----------
30  */
31 #define PGSTAT_STAT_INTERVAL    500             /* How often to write the status        */
32  /* file; in milliseconds.                       */
33
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.                             */
38
39 #define PGSTAT_DESTROY_COUNT    (PGSTAT_DESTROY_DELAY / PGSTAT_STAT_INTERVAL)
40
41 #define PGSTAT_RESTART_INTERVAL 60              /* How often to attempt to restart */
42  /* a failed statistics collector; in seconds. */
43
44 /* ----------
45  * How much of the actual query string to send to the collector.
46  * ----------
47  */
48 #define PGSTAT_ACTIVITY_SIZE    256
49
50
51 /* ----------
52  * The types of backend/postmaster -> collector messages
53  * ----------
54  */
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
63
64 /* ----------
65  * Amount of space reserved in pgstat_recvbuffer().
66  * ----------
67  */
68 #define PGSTAT_RECVBUFFERSZ             ((int) (1024 * sizeof(PgStat_Msg)))
69
70
71 /* ----------
72  * The initial size hints for the hash tables used in the
73  * collector.
74  * ----------
75  */
76 #define PGSTAT_DB_HASH_SIZE             16
77 #define PGSTAT_BE_HASH_SIZE             512
78 #define PGSTAT_TAB_HASH_SIZE    512
79
80
81 /* ----------
82  * The data type used for counters.
83  * ----------
84  */
85 typedef int64 PgStat_Counter;
86
87
88 /* ------------------------------------------------------------
89  * Statistic collector data structures follow
90  * ------------------------------------------------------------
91  */
92 /* ----------
93  * PgStat_StatDBEntry                   The collectors data per database
94  * ----------
95  */
96 typedef struct PgStat_StatDBEntry
97 {
98         Oid                     databaseid;
99         HTAB       *tables;
100         int                     n_backends;
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;
106         int                     destroy;
107 } PgStat_StatDBEntry;
108
109
110 /* ----------
111  * PgStat_StatBeEntry                   The collectors data per backend
112  * ----------
113  */
114 typedef struct PgStat_StatBeEntry
115 {
116         Oid                     databaseid;
117         Oid                     userid;
118         int                     procpid;
119         char            activity[PGSTAT_ACTIVITY_SIZE];
120         AbsoluteTime activity_start_sec;
121         int                     activity_start_usec;
122 } PgStat_StatBeEntry;
123
124
125 /* ----------
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.
131  * ----------
132  */
133 typedef struct PgStat_StatBeDead
134 {
135         int                     procpid;
136         int                     backendid;
137         int                     destroy;
138 } PgStat_StatBeDead;
139
140
141 /* ----------
142  * PgStat_StatTabEntry                  The collectors data table data
143  * ----------
144  */
145 typedef struct PgStat_StatTabEntry
146 {
147         Oid                     tableid;
148
149         PgStat_Counter numscans;
150
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;
156
157         PgStat_Counter blocks_fetched;
158         PgStat_Counter blocks_hit;
159
160         int                     destroy;
161 } PgStat_StatTabEntry;
162
163
164 /* ------------------------------------------------------------
165  * Message formats follow
166  * ------------------------------------------------------------
167  */
168
169
170 /* ----------
171  * PgStat_MsgHdr                                The common message header
172  * ----------
173  */
174 typedef struct PgStat_MsgHdr
175 {
176         int                     m_type;
177         int                     m_size;
178         int                     m_backendid;
179         int                     m_procpid;
180         Oid                     m_databaseid;
181         AclId           m_userid;
182 } PgStat_MsgHdr;
183
184 /* ----------
185  * PgStat_TabEntry                              A table slot in a MsgTabstat
186  * ----------
187  */
188 typedef struct PgStat_TableEntry
189 {
190         Oid                     t_id;
191
192         PgStat_Counter t_numscans;
193
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;
199
200         PgStat_Counter t_blocks_fetched;
201         PgStat_Counter t_blocks_hit;
202 } PgStat_TableEntry;
203
204
205 /* ----------
206  * PgStat_MsgDummy                              A dummy message, ignored by the collector
207  * ----------
208  */
209 typedef struct PgStat_MsgDummy
210 {
211         PgStat_MsgHdr m_hdr;
212         char            m_dummy[512];
213 } PgStat_MsgDummy;
214
215 /* ----------
216  * PgStat_MsgBestart                    Sent by the backend on startup
217  * ----------
218  */
219 typedef struct PgStat_MsgBestart
220 {
221         PgStat_MsgHdr m_hdr;
222 } PgStat_MsgBestart;
223
224 /* ----------
225  * PgStat_MsgBeterm                             Sent by the postmaster after backend exit
226  * ----------
227  */
228 typedef struct PgStat_MsgBeterm
229 {
230         PgStat_MsgHdr m_hdr;
231 } PgStat_MsgBeterm;
232
233 /* ----------
234  * PgStat_MsgActivity                   Sent by the backends when they start
235  *                                                              to parse a query.
236  * ----------
237  */
238 typedef struct PgStat_MsgActivity
239 {
240         PgStat_MsgHdr m_hdr;
241         char            m_what[PGSTAT_ACTIVITY_SIZE];
242 } PgStat_MsgActivity;
243
244 /* ----------
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?
249  * ----------
250  */
251 #define PGSTAT_NUM_TABENTRIES   ((1000 - sizeof(PgStat_MsgHdr))                 \
252                                                                 / sizeof(PgStat_TableEntry))
253
254 /* ----------
255  * PgStat_MsgTabstat                    Sent by the backend to report table
256  *                                                              and buffer access statistics.
257  * ----------
258  */
259 typedef struct PgStat_MsgTabstat
260 {
261         PgStat_MsgHdr m_hdr;
262         int                     m_nentries;
263         int                     m_xact_commit;
264         int                     m_xact_rollback;
265         PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
266 } PgStat_MsgTabstat;
267
268
269 /* ----------
270  * How many Oid entries fit into a MsgTabpurge.
271  * ----------
272  */
273 #define PGSTAT_NUM_TABPURGE             ((1000 - sizeof(PgStat_MsgHdr))                 \
274                                                                 / sizeof(PgStat_TableEntry))
275
276 /* ----------
277  * PgStat_MsgTabpurge                   Sent by the backend to tell the collector
278  *                                                              about dead tables.
279  * ----------
280  */
281 typedef struct PgStat_MsgTabpurge
282 {
283         PgStat_MsgHdr m_hdr;
284         int                     m_nentries;
285         Oid                     m_tableid[PGSTAT_NUM_TABPURGE];
286 } PgStat_MsgTabpurge;
287
288
289 /* ----------
290  * PgStat_MsgDropdb                             Sent by the backend to tell the collector
291  *                                                              about dropped database
292  * ----------
293  */
294 typedef struct PgStat_MsgDropdb
295 {
296         PgStat_MsgHdr m_hdr;
297         Oid                     m_databaseid;
298 } PgStat_MsgDropdb;
299
300
301 /* ----------
302  * PgStat_MsgResetcounter               Sent by the backend to tell the collector
303  *                                                              to reset counters
304  * ----------
305  */
306 typedef struct PgStat_MsgResetcounter
307 {
308         PgStat_MsgHdr m_hdr;
309 } PgStat_MsgResetcounter;
310
311
312 /* ----------
313  * PgStat_Msg                                   Union over all possible messages.
314  * ----------
315  */
316 typedef union PgStat_Msg
317 {
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;
326 } PgStat_Msg;
327
328
329 /* ----------
330  * GUC parameters
331  * ----------
332  */
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;
338
339 /* ----------
340  * Other global variables
341  * ----------
342  */
343 extern bool pgstat_is_running;
344
345 /* ----------
346  * Functions called from postmaster
347  * ----------
348  */
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);
354
355 /* ----------
356  * Functions called from backends
357  * ----------
358  */
359 extern void pgstat_bestart(void);
360
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);
365
366 extern void pgstat_reset_counters(void);
367
368 extern void pgstat_initstats(PgStat_Info *stats, Relation rel);
369
370
371 #define pgstat_reset_heap_scan(s)                                                                               \
372         do {                                                                                                                            \
373                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
374                         (s)->heap_scan_counted = FALSE;                                                         \
375         } while (0)
376 #define pgstat_count_heap_scan(s)                                                                               \
377         do {                                                                                                                            \
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;                                                          \
382                 }                                                                                                                               \
383         } while (0)
384 #define pgstat_count_heap_getnext(s)                                                                    \
385         do {                                                                                                                            \
386                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
387                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
388         } while (0)
389 #define pgstat_count_heap_fetch(s)                                                                              \
390         do {                                                                                                                            \
391                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
392                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_fetched++; \
393         } while (0)
394 #define pgstat_count_heap_insert(s)                                                                             \
395         do {                                                                                                                            \
396                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
397                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
398         } while (0)
399 #define pgstat_count_heap_update(s)                                                                             \
400         do {                                                                                                                            \
401                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
402                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_updated++; \
403         } while (0)
404 #define pgstat_count_heap_delete(s)                                                                             \
405         do {                                                                                                                            \
406                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
407                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_deleted++; \
408         } while (0)
409 #define pgstat_reset_index_scan(s)                                                                              \
410         do {                                                                                                                            \
411                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
412                         (s)->index_scan_counted = FALSE;                                                        \
413         } while (0)
414 #define pgstat_count_index_scan(s)                                                                              \
415         do {                                                                                                                            \
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;                                                         \
420                 }                                                                                                                               \
421         } while (0)
422 #define pgstat_count_index_getnext(s)                                                                   \
423         do {                                                                                                                            \
424                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
425                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
426         } while (0)
427 #define pgstat_count_buffer_read(s,r)                                                                   \
428         do {                                                                                                                            \
429                 if (pgstat_collect_blocklevel && (s)->tabentry != NULL)                 \
430                         ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
431                 else {                                                                                                                  \
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++; \
436                         }                                                                                                                       \
437                 }                                                                                                                               \
438         } while (0)
439 #define pgstat_count_buffer_hit(s,r)                                                                    \
440         do {                                                                                                                            \
441                 if (pgstat_collect_blocklevel && (s)->tabentry != NULL)                 \
442                         ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++;         \
443                 else {                                                                                                                  \
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++; \
448                         }                                                                                                                       \
449                 }                                                                                                                               \
450         } while (0)
451
452
453 extern void pgstat_count_xact_commit(void);
454 extern void pgstat_count_xact_rollback(void);
455
456 /* ----------
457  * Support functions for the SQL-callable functions to
458  * generate the pgstat* views.
459  * ----------
460  */
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);
465
466 #endif   /* PGSTAT_H */