pgstat.h 11.7 KB
Newer Older
1 2 3 4 5
/* ----------
 *	pgstat.h
 *
 *	Definitions for the PostgreSQL statistics collector daemon.
 *
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 12 13
 * ----------
 */
#ifndef PGSTAT_H
#define PGSTAT_H

14
#include "utils/hsearch.h"
15
#include "utils/nabstime.h"
16 17
#include "utils/rel.h"

18

19 20 21 22 23
/* ----------
 * Paths for the statistics files. The %s is replaced with the
 * installations $PGDATA.
 * ----------
 */
24 25
#define PGSTAT_STAT_FILENAME	"%s/global/pgstat.stat"
#define PGSTAT_STAT_TMPFILE		"%s/global/pgstat.tmp.%d"
26 27 28 29 30 31

/* ----------
 * Timer definitions.
 * ----------
 */
#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 36
 /* objects known, to give delayed	 */
 /* UDP packets time to arrive;		 */
37
 /* in milliseconds.				 */
38

39
#define PGSTAT_DESTROY_COUNT	(PGSTAT_DESTROY_DELAY / PGSTAT_STAT_INTERVAL)
40

41 42
#define PGSTAT_RESTART_INTERVAL	60		/* How often to attempt to restart */
 /* a failed statistics collector; in seconds. */
43 44

/* ----------
45
 * How much of the actual query string to send to the collector.
46 47 48 49 50 51 52 53 54
 * ----------
 */
#define PGSTAT_ACTIVITY_SIZE	256


/* ----------
 * The types of backend/postmaster -> collector messages
 * ----------
 */
55 56 57 58 59 60 61 62
#define PGSTAT_MTYPE_DUMMY			0
#define PGSTAT_MTYPE_BESTART		1
#define PGSTAT_MTYPE_BETERM			2
#define PGSTAT_MTYPE_ACTIVITY		3
#define PGSTAT_MTYPE_TABSTAT		4
#define PGSTAT_MTYPE_TABPURGE		5
#define PGSTAT_MTYPE_DROPDB			6
#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 73 74 75 76 77 78 79 80 81 82 83 84


/* ----------
 * The initial size hints for the hash tables used in the
 * collector.
 * ----------
 */
#define PGSTAT_DB_HASH_SIZE		16
#define PGSTAT_BE_HASH_SIZE		512
#define PGSTAT_TAB_HASH_SIZE	512


/* ----------
 * The data type used for counters.
 * ----------
 */
85
typedef int64 PgStat_Counter;
86 87 88 89 90 91 92 93 94 95


/* ------------------------------------------------------------
 * Statistic collector data structures follow
 * ------------------------------------------------------------
 */
/* ----------
 * PgStat_StatDBEntry			The collectors data per database
 * ----------
 */
96
typedef struct PgStat_StatDBEntry
97
{
98 99 100 101 102 103 104 105 106
	Oid			databaseid;
	HTAB	   *tables;
	int			n_backends;
	PgStat_Counter n_connects;
	PgStat_Counter n_xact_commit;
	PgStat_Counter n_xact_rollback;
	PgStat_Counter n_blocks_fetched;
	PgStat_Counter n_blocks_hit;
	int			destroy;
107 108 109 110 111 112 113
} PgStat_StatDBEntry;


/* ----------
 * PgStat_StatBeEntry			The collectors data per backend
 * ----------
 */
114
typedef struct PgStat_StatBeEntry
115
{
116 117 118 119
	Oid			databaseid;
	Oid			userid;
	int			procpid;
	char		activity[PGSTAT_ACTIVITY_SIZE];
120 121
	AbsoluteTime activity_start_sec;
	int			activity_start_usec;
122 123 124 125 126 127 128 129 130 131 132
} PgStat_StatBeEntry;


/* ----------
 * PgStat_StatBeDead			Because UDP packets can arrive out of
 *								order, we need to keep some information
 *								about backends that are known to be
 *								dead for some seconds. This info is held
 *								in a hash table of these structs.
 * ----------
 */
133
typedef struct PgStat_StatBeDead
134
{
135 136 137
	int			procpid;
	int			backendid;
	int			destroy;
138 139 140 141 142 143 144
} PgStat_StatBeDead;


/* ----------
 * PgStat_StatTabEntry			The collectors data table data
 * ----------
 */
145
typedef struct PgStat_StatTabEntry
146
{
147
	Oid			tableid;
148

149
	PgStat_Counter numscans;
150

151 152 153 154 155
	PgStat_Counter tuples_returned;
	PgStat_Counter tuples_fetched;
	PgStat_Counter tuples_inserted;
	PgStat_Counter tuples_updated;
	PgStat_Counter tuples_deleted;
156

157 158
	PgStat_Counter blocks_fetched;
	PgStat_Counter blocks_hit;
159

160
	int			destroy;
161 162 163 164 165 166 167 168 169 170 171 172 173
} PgStat_StatTabEntry;


/* ------------------------------------------------------------
 * Message formats follow
 * ------------------------------------------------------------
 */


/* ----------
 * PgStat_MsgHdr				The common message header
 * ----------
 */
174
typedef struct PgStat_MsgHdr
175
{
176 177 178 179 180
	int			m_type;
	int			m_size;
	int			m_backendid;
	int			m_procpid;
	Oid			m_databaseid;
181
	AclId		m_userid;
182 183 184 185 186 187
} PgStat_MsgHdr;

/* ----------
 * PgStat_TabEntry				A table slot in a MsgTabstat
 * ----------
 */
188
typedef struct PgStat_TableEntry
189
{
190
	Oid			t_id;
191

192
	PgStat_Counter t_numscans;
193

194 195 196 197 198
	PgStat_Counter t_tuples_returned;
	PgStat_Counter t_tuples_fetched;
	PgStat_Counter t_tuples_inserted;
	PgStat_Counter t_tuples_updated;
	PgStat_Counter t_tuples_deleted;
199

200 201
	PgStat_Counter t_blocks_fetched;
	PgStat_Counter t_blocks_hit;
202 203 204 205 206 207 208
} PgStat_TableEntry;


/* ----------
 * PgStat_MsgDummy				A dummy message, ignored by the collector
 * ----------
 */
209
typedef struct PgStat_MsgDummy
210
{
211 212
	PgStat_MsgHdr m_hdr;
	char		m_dummy[512];
213 214 215 216 217 218
} PgStat_MsgDummy;

/* ----------
 * PgStat_MsgBestart			Sent by the backend on startup
 * ----------
 */
219
typedef struct PgStat_MsgBestart
220
{
221
	PgStat_MsgHdr m_hdr;
222 223 224 225 226 227
} PgStat_MsgBestart;

/* ----------
 * PgStat_MsgBeterm				Sent by the postmaster after backend exit
 * ----------
 */
228
typedef struct PgStat_MsgBeterm
229
{
230
	PgStat_MsgHdr m_hdr;
231 232 233 234 235 236 237
} PgStat_MsgBeterm;

/* ----------
 * PgStat_MsgActivity			Sent by the backends when they start
 *								to parse a query.
 * ----------
 */
238
typedef struct PgStat_MsgActivity
239
{
240 241
	PgStat_MsgHdr m_hdr;
	char		m_what[PGSTAT_ACTIVITY_SIZE];
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
} PgStat_MsgActivity;

/* ----------
 * How many table entries fit into a MsgTabstat. Actually,
 * this will keep the UDP packets below 1K, what should fit
 * unfragmented into the MTU of the lo interface on most
 * platforms. Does anybody care for platforms where it doesn't?
 * ----------
 */
#define PGSTAT_NUM_TABENTRIES	((1000 - sizeof(PgStat_MsgHdr))			\
								/ sizeof(PgStat_TableEntry))

/* ----------
 * PgStat_MsgTabstat			Sent by the backend to report table
 *								and buffer access statistics.
 * ----------
 */
259
typedef struct PgStat_MsgTabstat
260
{
261 262 263 264 265
	PgStat_MsgHdr m_hdr;
	int			m_nentries;
	int			m_xact_commit;
	int			m_xact_rollback;
	PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
} PgStat_MsgTabstat;


/* ----------
 * How many Oid entries fit into a MsgTabpurge.
 * ----------
 */
#define PGSTAT_NUM_TABPURGE		((1000 - sizeof(PgStat_MsgHdr))			\
								/ sizeof(PgStat_TableEntry))

/* ----------
 * PgStat_MsgTabpurge			Sent by the backend to tell the collector
 *								about dead tables.
 * ----------
 */
281
typedef struct PgStat_MsgTabpurge
282
{
283 284 285
	PgStat_MsgHdr m_hdr;
	int			m_nentries;
	Oid			m_tableid[PGSTAT_NUM_TABPURGE];
286 287 288 289 290 291 292 293
} PgStat_MsgTabpurge;


/* ----------
 * PgStat_MsgDropdb				Sent by the backend to tell the collector
 *								about dropped database
 * ----------
 */
294
typedef struct PgStat_MsgDropdb
295
{
296 297
	PgStat_MsgHdr m_hdr;
	Oid			m_databaseid;
298 299 300 301 302 303 304 305
} PgStat_MsgDropdb;


/* ----------
 * PgStat_MsgResetcounter		Sent by the backend to tell the collector
 *								to reset counters
 * ----------
 */
306
typedef struct PgStat_MsgResetcounter
307
{
308
	PgStat_MsgHdr m_hdr;
309 310 311 312 313 314 315
} PgStat_MsgResetcounter;


/* ----------
 * PgStat_Msg					Union over all possible messages.
 * ----------
 */
316
typedef union PgStat_Msg
317
{
318 319 320 321 322 323 324 325
	PgStat_MsgHdr msg_hdr;
	PgStat_MsgDummy msg_dummy;
	PgStat_MsgBestart msg_bestart;
	PgStat_MsgActivity msg_activity;
	PgStat_MsgTabstat msg_tabstat;
	PgStat_MsgTabpurge msg_tabpurge;
	PgStat_MsgDropdb msg_dropdb;
	PgStat_MsgResetcounter msg_resetcounter;
326 327 328
} PgStat_Msg;


329
/* ----------
330
 * GUC parameters
331 332
 * ----------
 */
333 334 335 336 337
extern bool pgstat_collect_startcollector;
extern bool pgstat_collect_resetonpmstart;
extern bool pgstat_collect_querystring;
extern bool pgstat_collect_tuplelevel;
extern bool pgstat_collect_blocklevel;
338

339 340 341 342 343 344
/* ----------
 * Other global variables
 * ----------
 */
extern bool pgstat_is_running;

345 346 347 348
/* ----------
 * Functions called from postmaster
 * ----------
 */
349 350 351
extern void pgstat_init(void);
extern void pgstat_start(void);
extern bool pgstat_ispgstat(int pid);
352 353
extern void pgstat_close_sockets(void);
extern void pgstat_beterm(int pid);
354 355 356 357 358

/* ----------
 * Functions called from backends
 * ----------
 */
359
extern void pgstat_bestart(void);
360

361
extern void pgstat_ping(void);
362
extern void pgstat_report_activity(const char *what);
363 364
extern void pgstat_report_tabstat(void);
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 375
			(s)->heap_scan_counted = FALSE;								\
	} 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 381 382 383
			((PgStat_TableEntry *)((s)->tabentry))->t_numscans++;		\
			(s)->heap_scan_counted = TRUE;								\
		}																\
	} while (0)
384
#define pgstat_count_heap_getnext(s)									\
385
	do {																\
386
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
387 388
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
	} 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 398
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
	} 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 413
			(s)->index_scan_counted = FALSE;							\
	} 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 419 420 421
			((PgStat_TableEntry *)((s)->tabentry))->t_numscans++;		\
			(s)->index_scan_counted = TRUE;								\
		}																\
	} while (0)
422
#define pgstat_count_index_getnext(s)									\
423
	do {																\
424
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
425 426
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
	} 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 434 435 436
				pgstat_initstats((s), (r));								\
				if ((s)->tabentry != NULL)								\
					((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
			}															\
437
		}																\
438
	} while (0)
439
#define pgstat_count_buffer_hit(s,r)									\
440
	do {																\
441
		if (pgstat_collect_blocklevel && (s)->tabentry != NULL)			\
442 443
			((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++;		\
		else {															\
444
			if (pgstat_collect_blocklevel && !(s)->no_stats) {			\
445 446 447 448
				pgstat_initstats((s), (r));								\
				if ((s)->tabentry != NULL)								\
					((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
			}															\
449
		}																\
450
	} while (0)
451 452


453 454
extern void pgstat_count_xact_commit(void);
extern void pgstat_count_xact_rollback(void);
455 456 457 458 459 460

/* ----------
 * Support functions for the SQL-callable functions to
 * 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 464
extern PgStat_StatBeEntry *pgstat_fetch_stat_beentry(int beid);
extern int	pgstat_fetch_stat_numbackends(void);
465

466
#endif   /* PGSTAT_H */