~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/include/pgstat.h

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.27 2005-01-01 05:43:08 momjian 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
 * 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
        Oid                     m_databaseid;
 
56
        AclId           m_userid;
 
57
} PgStat_MsgHdr;
 
58
 
 
59
/* ----------
 
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?
 
63
 * ----------
 
64
 */
 
65
#define PGSTAT_MSG_PAYLOAD      (1000 - sizeof(PgStat_MsgHdr))
 
66
 
 
67
/* ----------
 
68
 * PgStat_TableEntry                    Per-table info in a MsgTabstat
 
69
 * ----------
 
70
 */
 
71
typedef struct PgStat_TableEntry
 
72
{
 
73
        Oid                     t_id;
 
74
 
 
75
        PgStat_Counter t_numscans;
 
76
 
 
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;
 
82
 
 
83
        PgStat_Counter t_blocks_fetched;
 
84
        PgStat_Counter t_blocks_hit;
 
85
} PgStat_TableEntry;
 
86
 
 
87
 
 
88
/* ----------
 
89
 * PgStat_MsgDummy                              A dummy message, ignored by the collector
 
90
 * ----------
 
91
 */
 
92
typedef struct PgStat_MsgDummy
 
93
{
 
94
        PgStat_MsgHdr m_hdr;
 
95
        char            m_dummy[512];
 
96
} PgStat_MsgDummy;
 
97
 
 
98
/* ----------
 
99
 * PgStat_MsgBestart                    Sent by the backend on startup
 
100
 * ----------
 
101
 */
 
102
typedef struct PgStat_MsgBestart
 
103
{
 
104
        PgStat_MsgHdr m_hdr;
 
105
} PgStat_MsgBestart;
 
106
 
 
107
/* ----------
 
108
 * PgStat_MsgBeterm                             Sent by the postmaster after backend exit
 
109
 * ----------
 
110
 */
 
111
typedef struct PgStat_MsgBeterm
 
112
{
 
113
        PgStat_MsgHdr m_hdr;
 
114
} PgStat_MsgBeterm;
 
115
 
 
116
/* ----------
 
117
 * PgStat_MsgActivity                   Sent by the backends when they start
 
118
 *                                                              to parse a query.
 
119
 * ----------
 
120
 */
 
121
#define PGSTAT_ACTIVITY_SIZE    PGSTAT_MSG_PAYLOAD
 
122
 
 
123
typedef struct PgStat_MsgActivity
 
124
{
 
125
        PgStat_MsgHdr m_hdr;
 
126
        char            m_what[PGSTAT_ACTIVITY_SIZE];
 
127
} PgStat_MsgActivity;
 
128
 
 
129
/* ----------
 
130
 * PgStat_MsgTabstat                    Sent by the backend to report table
 
131
 *                                                              and buffer access statistics.
 
132
 * ----------
 
133
 */
 
134
#define PGSTAT_NUM_TABENTRIES   ((PGSTAT_MSG_PAYLOAD - 3 * sizeof(int))         \
 
135
                                                                / sizeof(PgStat_TableEntry))
 
136
 
 
137
typedef struct PgStat_MsgTabstat
 
138
{
 
139
        PgStat_MsgHdr m_hdr;
 
140
        int                     m_nentries;
 
141
        int                     m_xact_commit;
 
142
        int                     m_xact_rollback;
 
143
        PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
 
144
} PgStat_MsgTabstat;
 
145
 
 
146
/* ----------
 
147
 * PgStat_MsgTabpurge                   Sent by the backend to tell the collector
 
148
 *                                                              about dead tables.
 
149
 * ----------
 
150
 */
 
151
#define PGSTAT_NUM_TABPURGE             ((PGSTAT_MSG_PAYLOAD - sizeof(int))             \
 
152
                                                                / sizeof(Oid))
 
153
 
 
154
typedef struct PgStat_MsgTabpurge
 
155
{
 
156
        PgStat_MsgHdr m_hdr;
 
157
        int                     m_nentries;
 
158
        Oid                     m_tableid[PGSTAT_NUM_TABPURGE];
 
159
} PgStat_MsgTabpurge;
 
160
 
 
161
 
 
162
/* ----------
 
163
 * PgStat_MsgDropdb                             Sent by the backend to tell the collector
 
164
 *                                                              about dropped database
 
165
 * ----------
 
166
 */
 
167
typedef struct PgStat_MsgDropdb
 
168
{
 
169
        PgStat_MsgHdr m_hdr;
 
170
        Oid                     m_databaseid;
 
171
} PgStat_MsgDropdb;
 
172
 
 
173
 
 
174
/* ----------
 
175
 * PgStat_MsgResetcounter               Sent by the backend to tell the collector
 
176
 *                                                              to reset counters
 
177
 * ----------
 
178
 */
 
179
typedef struct PgStat_MsgResetcounter
 
180
{
 
181
        PgStat_MsgHdr m_hdr;
 
182
} PgStat_MsgResetcounter;
 
183
 
 
184
 
 
185
/* ----------
 
186
 * PgStat_Msg                                   Union over all possible messages.
 
187
 * ----------
 
188
 */
 
189
typedef union PgStat_Msg
 
190
{
 
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;
 
199
} PgStat_Msg;
 
200
 
 
201
 
 
202
/* ------------------------------------------------------------
 
203
 * Statistic collector data structures follow
 
204
 * ------------------------------------------------------------
 
205
 */
 
206
 
 
207
/* ----------
 
208
 * PgStat_StatDBEntry                   The collectors data per database
 
209
 * ----------
 
210
 */
 
211
typedef struct PgStat_StatDBEntry
 
212
{
 
213
        Oid                     databaseid;
 
214
        HTAB       *tables;
 
215
        int                     n_backends;
 
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;
 
221
        int                     destroy;
 
222
} PgStat_StatDBEntry;
 
223
 
 
224
 
 
225
/* ----------
 
226
 * PgStat_StatBeEntry                   The collectors data per backend
 
227
 * ----------
 
228
 */
 
229
typedef struct PgStat_StatBeEntry
 
230
{
 
231
        Oid                     databaseid;
 
232
        Oid                     userid;
 
233
        int                     procpid;
 
234
        AbsoluteTime activity_start_sec;
 
235
        int                     activity_start_usec;
 
236
        char            activity[PGSTAT_ACTIVITY_SIZE];
 
237
} PgStat_StatBeEntry;
 
238
 
 
239
 
 
240
/* ----------
 
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.
 
246
 * ----------
 
247
 */
 
248
typedef struct PgStat_StatBeDead
 
249
{
 
250
        int                     procpid;
 
251
        int                     backendid;
 
252
        int                     destroy;
 
253
} PgStat_StatBeDead;
 
254
 
 
255
 
 
256
/* ----------
 
257
 * PgStat_StatTabEntry                  The collectors data table data
 
258
 * ----------
 
259
 */
 
260
typedef struct PgStat_StatTabEntry
 
261
{
 
262
        Oid                     tableid;
 
263
 
 
264
        PgStat_Counter numscans;
 
265
 
 
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;
 
271
 
 
272
        PgStat_Counter blocks_fetched;
 
273
        PgStat_Counter blocks_hit;
 
274
 
 
275
        int                     destroy;
 
276
} PgStat_StatTabEntry;
 
277
 
 
278
 
 
279
/* ----------
 
280
 * GUC parameters
 
281
 * ----------
 
282
 */
 
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;
 
288
 
 
289
 
 
290
/* ----------
 
291
 * Functions called from postmaster
 
292
 * ----------
 
293
 */
 
294
extern void pgstat_init(void);
 
295
extern int      pgstat_start(void);
 
296
extern void pgstat_beterm(int pid);
 
297
 
 
298
#ifdef EXEC_BACKEND
 
299
extern void PgstatBufferMain(int argc, char *argv[]);
 
300
extern void PgstatCollectorMain(int argc, char *argv[]);
 
301
#endif
 
302
 
 
303
 
 
304
/* ----------
 
305
 * Functions called from backends
 
306
 * ----------
 
307
 */
 
308
extern void pgstat_bestart(void);
 
309
 
 
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);
 
314
 
 
315
extern void pgstat_reset_counters(void);
 
316
 
 
317
extern void pgstat_initstats(PgStat_Info *stats, Relation rel);
 
318
 
 
319
 
 
320
#define pgstat_reset_heap_scan(s)                                                                               \
 
321
        do {                                                                                                                            \
 
322
                if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
 
323
                        (s)->heap_scan_counted = FALSE;                                                         \
 
324
        } while (0)
 
325
#define pgstat_count_heap_scan(s)                                                                               \
 
326
        do {                                                                                                                            \
 
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;                                                          \
 
331
                }                                                                                                                               \
 
332
        } while (0)
 
333
#define pgstat_count_heap_getnext(s)                                                                    \
 
334
        do {                                                                                                                            \
 
335
                if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
 
336
                        ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
 
337
        } while (0)
 
338
#define pgstat_count_heap_fetch(s)                                                                              \
 
339
        do {                                                                                                                            \
 
340
                if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
 
341
                        ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_fetched++; \
 
342
        } while (0)
 
343
#define pgstat_count_heap_insert(s)                                                                             \
 
344
        do {                                                                                                                            \
 
345
                if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
 
346
                        ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
 
347
        } while (0)
 
348
#define pgstat_count_heap_update(s)                                                                             \
 
349
        do {                                                                                                                            \
 
350
                if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
 
351
                        ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_updated++; \
 
352
        } while (0)
 
353
#define pgstat_count_heap_delete(s)                                                                             \
 
354
        do {                                                                                                                            \
 
355
                if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
 
356
                        ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_deleted++; \
 
357
        } while (0)
 
358
#define pgstat_reset_index_scan(s)                                                                              \
 
359
        do {                                                                                                                            \
 
360
                if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
 
361
                        (s)->index_scan_counted = FALSE;                                                        \
 
362
        } while (0)
 
363
#define pgstat_count_index_scan(s)                                                                              \
 
364
        do {                                                                                                                            \
 
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;                                                         \
 
369
                }                                                                                                                               \
 
370
        } while (0)
 
371
#define pgstat_count_index_getnext(s)                                                                   \
 
372
        do {                                                                                                                            \
 
373
                if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)                 \
 
374
                        ((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
 
375
        } while (0)
 
376
#define pgstat_count_buffer_read(s,r)                                                                   \
 
377
        do {                                                                                                                            \
 
378
                if (pgstat_collect_blocklevel && (s)->tabentry != NULL)                 \
 
379
                        ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
 
380
                else {                                                                                                                  \
 
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++; \
 
385
                        }                                                                                                                       \
 
386
                }                                                                                                                               \
 
387
        } while (0)
 
388
#define pgstat_count_buffer_hit(s,r)                                                                    \
 
389
        do {                                                                                                                            \
 
390
                if (pgstat_collect_blocklevel && (s)->tabentry != NULL)                 \
 
391
                        ((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++;         \
 
392
                else {                                                                                                                  \
 
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++; \
 
397
                        }                                                                                                                       \
 
398
                }                                                                                                                               \
 
399
        } while (0)
 
400
 
 
401
 
 
402
extern void pgstat_count_xact_commit(void);
 
403
extern void pgstat_count_xact_rollback(void);
 
404
 
 
405
/* ----------
 
406
 * Support functions for the SQL-callable functions to
 
407
 * generate the pgstat* views.
 
408
 * ----------
 
409
 */
 
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);
 
414
 
 
415
#endif   /* PGSTAT_H */