]> granicus.if.org Git - postgresql/blob - src/include/pgstat.h
ad1239b985c1db69d50c7bb12aa821e9cf500089
[postgresql] / src / include / pgstat.h
1 /* ----------
2  *      pgstat.h
3  *
4  *      Definitions for the PostgreSQL statistics collector daemon.
5  *
6  *      Copyright (c) 2001-2005, PostgreSQL Global Development Group
7  *
8  *      $PostgreSQL: pgsql/src/include/pgstat.h,v 1.29 2005/05/11 01:41:41 neilc Exp $
9  * ----------
10  */
11 #ifndef PGSTAT_H
12 #define PGSTAT_H
13
14 #include "libpq/pqcomm.h"
15 #include "utils/hsearch.h"
16 #include "utils/nabstime.h"
17 #include "utils/rel.h"
18
19 /* ----------
20  * The types of backend/postmaster -> collector messages
21  * ----------
22  */
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
31
32 /* ----------
33  * The data type used for counters.
34  * ----------
35  */
36 typedef int64 PgStat_Counter;
37
38
39 /* ------------------------------------------------------------
40  * Message formats follow
41  * ------------------------------------------------------------
42  */
43
44
45 /* ----------
46  * PgStat_MsgHdr                                The common message header
47  * ----------
48  */
49 typedef struct PgStat_MsgHdr
50 {
51         int                     m_type;
52         int                     m_size;
53         int                     m_backendid;
54         int                     m_procpid;
55 } PgStat_MsgHdr;
56
57 /* ----------
58  * Space available in a message.  This will keep the UDP packets below 1K,
59  * which should fit unfragmented into the MTU of the lo interface on most
60  * platforms. Does anybody care for platforms where it doesn't?
61  * ----------
62  */
63 #define PGSTAT_MSG_PAYLOAD      (1000 - sizeof(PgStat_MsgHdr))
64
65 /* ----------
66  * PgStat_TableEntry                    Per-table info in a MsgTabstat
67  * ----------
68  */
69 typedef struct PgStat_TableEntry
70 {
71         Oid                     t_id;
72
73         PgStat_Counter t_numscans;
74
75         PgStat_Counter t_tuples_returned;
76         PgStat_Counter t_tuples_fetched;
77         PgStat_Counter t_tuples_inserted;
78         PgStat_Counter t_tuples_updated;
79         PgStat_Counter t_tuples_deleted;
80
81         PgStat_Counter t_blocks_fetched;
82         PgStat_Counter t_blocks_hit;
83 } PgStat_TableEntry;
84
85
86 /* ----------
87  * PgStat_MsgDummy                              A dummy message, ignored by the collector
88  * ----------
89  */
90 typedef struct PgStat_MsgDummy
91 {
92         PgStat_MsgHdr m_hdr;
93         char            m_dummy[512];
94 } PgStat_MsgDummy;
95
96 /* ----------
97  * PgStat_MsgBestart                    Sent by the backend on startup
98  * ----------
99  */
100 typedef struct PgStat_MsgBestart
101 {
102         PgStat_MsgHdr   m_hdr;
103         Oid                             m_databaseid;
104         AclId                   m_userid;
105         SockAddr                m_clientaddr;
106 } PgStat_MsgBestart;
107
108 /* ----------
109  * PgStat_MsgBeterm                             Sent by the postmaster after backend exit
110  * ----------
111  */
112 typedef struct PgStat_MsgBeterm
113 {
114         PgStat_MsgHdr m_hdr;
115 } PgStat_MsgBeterm;
116
117 /* ----------
118  * PgStat_MsgActivity                   Sent by the backends when they start
119  *                                                              to parse a query.
120  * ----------
121  */
122 #define PGSTAT_ACTIVITY_SIZE    PGSTAT_MSG_PAYLOAD
123
124 typedef struct PgStat_MsgActivity
125 {
126         PgStat_MsgHdr m_hdr;
127         char            m_what[PGSTAT_ACTIVITY_SIZE];
128 } PgStat_MsgActivity;
129
130 /* ----------
131  * PgStat_MsgTabstat                    Sent by the backend to report table
132  *                                                              and buffer access statistics.
133  * ----------
134  */
135 #define PGSTAT_NUM_TABENTRIES   ((PGSTAT_MSG_PAYLOAD - 3 * sizeof(int))         \
136                                                                 / sizeof(PgStat_TableEntry))
137
138 typedef struct PgStat_MsgTabstat
139 {
140         PgStat_MsgHdr m_hdr;
141         int                     m_databaseid;
142         int                     m_nentries;
143         int                     m_xact_commit;
144         int                     m_xact_rollback;
145         PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
146 } PgStat_MsgTabstat;
147
148 /* ----------
149  * PgStat_MsgTabpurge                   Sent by the backend to tell the collector
150  *                                                              about dead tables.
151  * ----------
152  */
153 #define PGSTAT_NUM_TABPURGE             ((PGSTAT_MSG_PAYLOAD - sizeof(int))             \
154                                                                 / sizeof(Oid))
155
156 typedef struct PgStat_MsgTabpurge
157 {
158         PgStat_MsgHdr m_hdr;
159         Oid                     m_databaseid;
160         int                     m_nentries;
161         Oid                     m_tableid[PGSTAT_NUM_TABPURGE];
162 } PgStat_MsgTabpurge;
163
164
165 /* ----------
166  * PgStat_MsgDropdb                             Sent by the backend to tell the collector
167  *                                                              about dropped database
168  * ----------
169  */
170 typedef struct PgStat_MsgDropdb
171 {
172         PgStat_MsgHdr m_hdr;
173         Oid                     m_databaseid;
174 } PgStat_MsgDropdb;
175
176
177 /* ----------
178  * PgStat_MsgResetcounter               Sent by the backend to tell the collector
179  *                                                              to reset counters
180  * ----------
181  */
182 typedef struct PgStat_MsgResetcounter
183 {
184         PgStat_MsgHdr m_hdr;
185         Oid                     m_databaseid;
186 } PgStat_MsgResetcounter;
187
188
189 /* ----------
190  * PgStat_Msg                                   Union over all possible messages.
191  * ----------
192  */
193 typedef union PgStat_Msg
194 {
195         PgStat_MsgHdr msg_hdr;
196         PgStat_MsgDummy msg_dummy;
197         PgStat_MsgBestart msg_bestart;
198         PgStat_MsgActivity msg_activity;
199         PgStat_MsgTabstat msg_tabstat;
200         PgStat_MsgTabpurge msg_tabpurge;
201         PgStat_MsgDropdb msg_dropdb;
202         PgStat_MsgResetcounter msg_resetcounter;
203 } PgStat_Msg;
204
205
206 /* ------------------------------------------------------------
207  * Statistic collector data structures follow
208  * ------------------------------------------------------------
209  */
210
211 /* ----------
212  * PgStat_StatDBEntry                   The collectors data per database
213  * ----------
214  */
215 typedef struct PgStat_StatDBEntry
216 {
217         Oid                     databaseid;
218         HTAB       *tables;
219         int                     n_backends;
220         PgStat_Counter n_xact_commit;
221         PgStat_Counter n_xact_rollback;
222         PgStat_Counter n_blocks_fetched;
223         PgStat_Counter n_blocks_hit;
224         int                     destroy;
225 } PgStat_StatDBEntry;
226
227
228 /* ----------
229  * PgStat_StatBeEntry                   The collectors data per backend
230  * ----------
231  */
232 typedef struct PgStat_StatBeEntry
233 {
234         /* An entry is non-empty iff procpid > 0 */
235         int                     procpid;
236         AbsoluteTime start_sec;
237         int         start_usec;
238         AbsoluteTime activity_start_sec;
239         int                     activity_start_usec;
240         char            activity[PGSTAT_ACTIVITY_SIZE];
241
242         /*
243          * The following fields are initialized by the BESTART message. If
244          * we have received messages from a backend before we have
245          * received its BESTART, these fields will be uninitialized:
246          * userid and databaseid will be InvalidOid, and clientaddr will
247          * be undefined.
248          */
249         Oid                     userid;
250         Oid                     databaseid;
251         SockAddr    clientaddr;
252 } PgStat_StatBeEntry;
253
254
255 /* ----------
256  * PgStat_StatBeDead                    Because UDP packets can arrive out of
257  *                                                              order, we need to keep some information
258  *                                                              about backends that are known to be
259  *                                                              dead for some seconds. This info is held
260  *                                                              in a hash table of these structs.
261  * ----------
262  */
263 typedef struct PgStat_StatBeDead
264 {
265         int                     procpid;
266         int                     backendid;
267         int                     destroy;
268 } PgStat_StatBeDead;
269
270
271 /* ----------
272  * PgStat_StatTabEntry                  The collectors data table data
273  * ----------
274  */
275 typedef struct PgStat_StatTabEntry
276 {
277         Oid                     tableid;
278
279         PgStat_Counter numscans;
280
281         PgStat_Counter tuples_returned;
282         PgStat_Counter tuples_fetched;
283         PgStat_Counter tuples_inserted;
284         PgStat_Counter tuples_updated;
285         PgStat_Counter tuples_deleted;
286
287         PgStat_Counter blocks_fetched;
288         PgStat_Counter blocks_hit;
289
290         int                     destroy;
291 } PgStat_StatTabEntry;
292
293
294 /* ----------
295  * GUC parameters
296  * ----------
297  */
298 extern bool pgstat_collect_startcollector;
299 extern bool pgstat_collect_resetonpmstart;
300 extern bool pgstat_collect_querystring;
301 extern bool pgstat_collect_tuplelevel;
302 extern bool pgstat_collect_blocklevel;
303
304
305 /* ----------
306  * Functions called from postmaster
307  * ----------
308  */
309 extern void pgstat_init(void);
310 extern int      pgstat_start(void);
311 extern void pgstat_beterm(int pid);
312
313 #ifdef EXEC_BACKEND
314 extern void PgstatBufferMain(int argc, char *argv[]);
315 extern void PgstatCollectorMain(int argc, char *argv[]);
316 #endif
317
318
319 /* ----------
320  * Functions called from backends
321  * ----------
322  */
323 extern void pgstat_bestart(void);
324
325 extern void pgstat_ping(void);
326 extern void pgstat_report_activity(const char *what);
327 extern void pgstat_report_tabstat(void);
328 extern int      pgstat_vacuum_tabstat(void);
329
330 extern void pgstat_reset_counters(void);
331
332 extern void pgstat_initstats(PgStat_Info *stats, Relation rel);
333
334
335 #define pgstat_reset_heap_scan(s)                                                                               \
336         do {                                                                                                                            \
337                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
338                         (s)->heap_scan_counted = FALSE;                                                         \
339         } while (0)
340 #define pgstat_count_heap_scan(s)                                                                               \
341         do {                                                                                                                            \
342                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL &&               \
343                                 !(s)->heap_scan_counted) {                                                              \
344                         ((PgStat_TableEntry *)((s)->tabentry))->t_numscans++;           \
345                         (s)->heap_scan_counted = TRUE;                                                          \
346                 }                                                                                                                               \
347         } while (0)
348 #define pgstat_count_heap_getnext(s)                                                                    \
349         do {                                                                                                                            \
350                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
351                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
352         } while (0)
353 #define pgstat_count_heap_fetch(s)                                                                              \
354         do {                                                                                                                            \
355                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
356                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_fetched++; \
357         } while (0)
358 #define pgstat_count_heap_insert(s)                                                                             \
359         do {                                                                                                                            \
360                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
361                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
362         } while (0)
363 #define pgstat_count_heap_update(s)                                                                             \
364         do {                                                                                                                            \
365                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
366                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_updated++; \
367         } while (0)
368 #define pgstat_count_heap_delete(s)                                                                             \
369         do {                                                                                                                            \
370                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
371                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_deleted++; \
372         } while (0)
373 #define pgstat_reset_index_scan(s)                                                                              \
374         do {                                                                                                                            \
375                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
376                         (s)->index_scan_counted = FALSE;                                                        \
377         } while (0)
378 #define pgstat_count_index_scan(s)                                                                              \
379         do {                                                                                                                            \
380                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL &&               \
381                                 !(s)->index_scan_counted) {                                                             \
382                         ((PgStat_TableEntry *)((s)->tabentry))->t_numscans++;           \
383                         (s)->index_scan_counted = TRUE;                                                         \
384                 }                                                                                                                               \
385         } while (0)
386 #define pgstat_count_index_getnext(s)                                                                   \
387         do {                                                                                                                            \
388                 if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
389                         ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
390         } while (0)
391 #define pgstat_count_buffer_read(s,r)                                                                   \
392         do {                                                                                                                            \
393                 if (pgstat_collect_blocklevel && (s)->tabentry != NULL)                 \
394                         ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
395                 else {                                                                                                                  \
396                         if (pgstat_collect_blocklevel && !(s)->no_stats) {                      \
397                                 pgstat_initstats((s), (r));                                                             \
398                                 if ((s)->tabentry != NULL)                                                              \
399                                         ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
400                         }                                                                                                                       \
401                 }                                                                                                                               \
402         } while (0)
403 #define pgstat_count_buffer_hit(s,r)                                                                    \
404         do {                                                                                                                            \
405                 if (pgstat_collect_blocklevel && (s)->tabentry != NULL)                 \
406                         ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++;         \
407                 else {                                                                                                                  \
408                         if (pgstat_collect_blocklevel && !(s)->no_stats) {                      \
409                                 pgstat_initstats((s), (r));                                                             \
410                                 if ((s)->tabentry != NULL)                                                              \
411                                         ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
412                         }                                                                                                                       \
413                 }                                                                                                                               \
414         } while (0)
415
416
417 extern void pgstat_count_xact_commit(void);
418 extern void pgstat_count_xact_rollback(void);
419
420 /* ----------
421  * Support functions for the SQL-callable functions to
422  * generate the pgstat* views.
423  * ----------
424  */
425 extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
426 extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
427 extern PgStat_StatBeEntry *pgstat_fetch_stat_beentry(int beid);
428 extern int      pgstat_fetch_stat_numbackends(void);
429
430 #endif   /* PGSTAT_H */