pgstat.h 12.9 KB
Newer Older
1 2 3 4 5
/* ----------
 *	pgstat.h
 *
 *	Definitions for the PostgreSQL statistics collector daemon.
 *
6
 *	Copyright (c) 2001-2005, PostgreSQL Global Development Group
7
 *
8
 *	$PostgreSQL: pgsql/src/include/pgstat.h,v 1.41 2006/01/18 20:35:06 tgl Exp $
9 10 11 12 13
 * ----------
 */
#ifndef PGSTAT_H
#define PGSTAT_H

14
#include "libpq/pqcomm.h"
15 16
#include "utils/hsearch.h"
#include "utils/rel.h"
17
#include "utils/timestamp.h"
18

19 20 21 22
/* ----------
 * The types of backend/postmaster -> collector messages
 * ----------
 */
23 24 25 26 27 28 29 30 31 32 33 34 35 36
typedef enum StatMsgType
{
	PGSTAT_MTYPE_DUMMY,
	PGSTAT_MTYPE_BESTART,
	PGSTAT_MTYPE_BETERM,
	PGSTAT_MTYPE_ACTIVITY,
	PGSTAT_MTYPE_TABSTAT,
	PGSTAT_MTYPE_TABPURGE,
	PGSTAT_MTYPE_DROPDB,
	PGSTAT_MTYPE_RESETCOUNTER,
	PGSTAT_MTYPE_AUTOVAC_START,
	PGSTAT_MTYPE_VACUUM,
	PGSTAT_MTYPE_ANALYZE
} StatMsgType;
37 38 39 40 41

/* ----------
 * The data type used for counters.
 * ----------
 */
42
typedef int64 PgStat_Counter;
43 44 45 46 47 48 49 50 51 52 53 54


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


/* ----------
 * PgStat_MsgHdr				The common message header
 * ----------
 */
55
typedef struct PgStat_MsgHdr
56
{
57
	StatMsgType m_type;
58 59 60
	int			m_size;
	int			m_backendid;
	int			m_procpid;
61 62 63
} PgStat_MsgHdr;

/* ----------
64 65 66 67 68 69 70 71 72
 * Space available in a message.  This will keep the UDP packets below 1K,
 * which should fit unfragmented into the MTU of the lo interface on most
 * platforms. Does anybody care for platforms where it doesn't?
 * ----------
 */
#define PGSTAT_MSG_PAYLOAD	(1000 - sizeof(PgStat_MsgHdr))

/* ----------
 * PgStat_TableEntry			Per-table info in a MsgTabstat
73 74 75 76 77 78 79
 *
 * Note: for a table, tuples_returned is the number of tuples successfully
 * fetched by heap_getnext, while tuples_fetched is the number of tuples
 * successfully fetched by heap_fetch under the control of bitmap indexscans.
 * For an index, tuples_returned is the number of index entries returned by
 * the index AM, while tuples_fetched is the number of tuples successfully
 * fetched by heap_fetch under the control of simple indexscans for this index.
80 81
 * ----------
 */
82
typedef struct PgStat_TableEntry
83
{
84
	Oid			t_id;
85

86
	PgStat_Counter t_numscans;
87

88 89
	PgStat_Counter t_tuples_returned;
	PgStat_Counter t_tuples_fetched;
90

91 92 93
	PgStat_Counter t_tuples_inserted;
	PgStat_Counter t_tuples_updated;
	PgStat_Counter t_tuples_deleted;
94

95 96
	PgStat_Counter t_blocks_fetched;
	PgStat_Counter t_blocks_hit;
97 98 99 100 101 102 103
} PgStat_TableEntry;


/* ----------
 * PgStat_MsgDummy				A dummy message, ignored by the collector
 * ----------
 */
104
typedef struct PgStat_MsgDummy
105
{
106 107
	PgStat_MsgHdr m_hdr;
	char		m_dummy[512];
108 109 110 111 112 113
} PgStat_MsgDummy;

/* ----------
 * PgStat_MsgBestart			Sent by the backend on startup
 * ----------
 */
114
typedef struct PgStat_MsgBestart
115
{
116 117 118 119
	PgStat_MsgHdr m_hdr;
	Oid			m_databaseid;
	Oid			m_userid;
	SockAddr	m_clientaddr;
120 121 122 123 124 125
} PgStat_MsgBestart;

/* ----------
 * PgStat_MsgBeterm				Sent by the postmaster after backend exit
 * ----------
 */
126
typedef struct PgStat_MsgBeterm
127
{
128
	PgStat_MsgHdr m_hdr;
129 130
} PgStat_MsgBeterm;

131 132
/* ----------
 * PgStat_MsgAutovacStart		Sent by the autovacuum daemon to signal
133
 *								that a database is going to be processed
134 135 136 137 138 139
 * ----------
 */
typedef struct PgStat_MsgAutovacStart
{
	PgStat_MsgHdr m_hdr;
	Oid			m_databaseid;
140
	TimestampTz m_start_time;
141 142 143 144
} PgStat_MsgAutovacStart;

/* ----------
 * PgStat_MsgVacuum				Sent by the backend or autovacuum daemon
145
 *								after VACUUM or VACUUM ANALYZE
146 147 148 149 150 151 152 153 154 155 156 157 158
 * ----------
 */
typedef struct PgStat_MsgVacuum
{
	PgStat_MsgHdr m_hdr;
	Oid			m_databaseid;
	Oid			m_tableoid;
	bool		m_analyze;
	PgStat_Counter m_tuples;
} PgStat_MsgVacuum;

/* ----------
 * PgStat_MsgAnalyze			Sent by the backend or autovacuum daemon
159
 *								after ANALYZE
160 161 162 163 164 165 166
 * ----------
 */
typedef struct PgStat_MsgAnalyze
{
	PgStat_MsgHdr m_hdr;
	Oid			m_databaseid;
	Oid			m_tableoid;
167 168
	PgStat_Counter m_live_tuples;
	PgStat_Counter m_dead_tuples;
169 170 171
} PgStat_MsgAnalyze;


172 173 174 175 176
/* ----------
 * PgStat_MsgActivity			Sent by the backends when they start
 *								to parse a query.
 * ----------
 */
177 178
#define PGSTAT_ACTIVITY_SIZE	PGSTAT_MSG_PAYLOAD

179
typedef struct PgStat_MsgActivity
180
{
181
	PgStat_MsgHdr m_hdr;
Bruce Momjian's avatar
Bruce Momjian committed
182
	char		m_cmd_str[PGSTAT_ACTIVITY_SIZE];
183 184 185 186 187 188 189
} PgStat_MsgActivity;

/* ----------
 * PgStat_MsgTabstat			Sent by the backend to report table
 *								and buffer access statistics.
 * ----------
 */
190 191 192
#define PGSTAT_NUM_TABENTRIES  \
	((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - 3 * sizeof(int))  \
	 / sizeof(PgStat_TableEntry))
193

194
typedef struct PgStat_MsgTabstat
195
{
196
	PgStat_MsgHdr m_hdr;
197
	Oid			m_databaseid;
198 199 200 201
	int			m_nentries;
	int			m_xact_commit;
	int			m_xact_rollback;
	PgStat_TableEntry m_entry[PGSTAT_NUM_TABENTRIES];
202 203 204 205 206 207 208
} PgStat_MsgTabstat;

/* ----------
 * PgStat_MsgTabpurge			Sent by the backend to tell the collector
 *								about dead tables.
 * ----------
 */
209 210 211
#define PGSTAT_NUM_TABPURGE  \
	((PGSTAT_MSG_PAYLOAD - sizeof(Oid) - sizeof(int))  \
	 / sizeof(Oid))
212

213
typedef struct PgStat_MsgTabpurge
214
{
215
	PgStat_MsgHdr m_hdr;
216
	Oid			m_databaseid;
217 218
	int			m_nentries;
	Oid			m_tableid[PGSTAT_NUM_TABPURGE];
219 220 221 222 223
} PgStat_MsgTabpurge;


/* ----------
 * PgStat_MsgDropdb				Sent by the backend to tell the collector
224
 *								about a dropped database
225 226
 * ----------
 */
227
typedef struct PgStat_MsgDropdb
228
{
229 230
	PgStat_MsgHdr m_hdr;
	Oid			m_databaseid;
231 232 233 234 235 236 237 238
} PgStat_MsgDropdb;


/* ----------
 * PgStat_MsgResetcounter		Sent by the backend to tell the collector
 *								to reset counters
 * ----------
 */
239
typedef struct PgStat_MsgResetcounter
240
{
241
	PgStat_MsgHdr m_hdr;
242
	Oid			m_databaseid;
243 244 245 246 247 248 249
} PgStat_MsgResetcounter;


/* ----------
 * PgStat_Msg					Union over all possible messages.
 * ----------
 */
250
typedef union PgStat_Msg
251
{
252 253 254 255 256 257 258 259
	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;
260 261 262
	PgStat_MsgAutovacStart msg_autovacuum;
	PgStat_MsgVacuum msg_vacuum;
	PgStat_MsgAnalyze msg_analyze;
263 264 265
} PgStat_Msg;


266 267
/* ------------------------------------------------------------
 * Statistic collector data structures follow
268 269 270
 *
 * PGSTAT_FILE_FORMAT_ID should be changed whenever any of these
 * data structures change.
271 272 273
 * ------------------------------------------------------------
 */

274
#define PGSTAT_FILE_FORMAT_ID	0x01A5BC94
275

276
/* ----------
277
 * PgStat_StatDBEntry			The collector's data per database
278 279 280 281 282
 * ----------
 */
typedef struct PgStat_StatDBEntry
{
	Oid			databaseid;
283
	int			destroy;
284 285 286 287 288
	int			n_backends;
	PgStat_Counter n_xact_commit;
	PgStat_Counter n_xact_rollback;
	PgStat_Counter n_blocks_fetched;
	PgStat_Counter n_blocks_hit;
289
	TimestampTz last_autovac_time;
290 291 292 293 294 295

	/*
	 * tables must be last in the struct, because we don't write the pointer
	 * out to the stats file.
	 */
	HTAB	   *tables;
296 297 298 299
} PgStat_StatDBEntry;


/* ----------
300
 * PgStat_StatBeEntry			The collector's data per backend
301 302 303 304
 * ----------
 */
typedef struct PgStat_StatBeEntry
{
305
	/* An entry is non-empty iff procpid > 0 */
306
	int			procpid;
307 308
	TimestampTz start_timestamp;
	TimestampTz activity_start_timestamp;
309 310

	/*
311
	 * These fields are initialized by the BESTART message. If we have
312 313 314
	 * received messages from a backend before we have received its BESTART,
	 * these fields will be uninitialized: userid and databaseid will be
	 * InvalidOid, and clientaddr will be undefined.
315 316 317
	 */
	Oid			userid;
	Oid			databaseid;
318
	SockAddr	clientaddr;
319 320 321 322 323 324

	/*
	 * activity[] must be last in the struct, because we only write as much
	 * of it as needed to the stats file.
	 */
	char		activity[PGSTAT_ACTIVITY_SIZE];
325 326 327 328 329 330 331 332 333
} 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.
334 335
 *
 * (This struct is not used in the stats file.)
336 337 338 339 340 341 342 343 344 345 346
 * ----------
 */
typedef struct PgStat_StatBeDead
{
	int			procpid;
	int			backendid;
	int			destroy;
} PgStat_StatBeDead;


/* ----------
347
 * PgStat_StatTabEntry			The collector's data per table (or index)
348 349 350 351 352
 * ----------
 */
typedef struct PgStat_StatTabEntry
{
	Oid			tableid;
353
	int			destroy;
354 355 356 357 358

	PgStat_Counter numscans;

	PgStat_Counter tuples_returned;
	PgStat_Counter tuples_fetched;
359

360 361 362 363
	PgStat_Counter tuples_inserted;
	PgStat_Counter tuples_updated;
	PgStat_Counter tuples_deleted;

364 365 366 367
	PgStat_Counter n_live_tuples;
	PgStat_Counter n_dead_tuples;
	PgStat_Counter last_anl_tuples;

368 369 370 371 372
	PgStat_Counter blocks_fetched;
	PgStat_Counter blocks_hit;
} PgStat_StatTabEntry;


373
/* ----------
374
 * GUC parameters
375 376
 * ----------
 */
377 378 379 380 381
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;
382

383

384 385 386 387
/* ----------
 * Functions called from postmaster
 * ----------
 */
388
extern void pgstat_init(void);
389
extern int	pgstat_start(void);
390
extern void pgstat_beterm(int pid);
391
extern void pgstat_reset_all(void);
392

393 394 395 396 397 398
#ifdef EXEC_BACKEND
extern void PgstatBufferMain(int argc, char *argv[]);
extern void PgstatCollectorMain(int argc, char *argv[]);
#endif


399 400 401 402
/* ----------
 * Functions called from backends
 * ----------
 */
403
extern void pgstat_bestart(void);
404

405
extern void pgstat_ping(void);
406
extern void pgstat_report_activity(const char *what);
407
extern void pgstat_report_tabstat(void);
408
extern void pgstat_report_autovac(Oid dboid);
409
extern void pgstat_report_vacuum(Oid tableoid, bool shared,
410
					 bool analyze, PgStat_Counter tuples);
411
extern void pgstat_report_analyze(Oid tableoid, bool shared,
412 413
					  PgStat_Counter livetuples,
					  PgStat_Counter deadtuples);
414 415
extern void pgstat_vacuum_tabstat(void);
extern void pgstat_drop_relation(Oid relid);
416

417
extern void pgstat_reset_counters(void);
418

419
extern void pgstat_initstats(PgStat_Info *stats, Relation rel);
420

421

422
#define pgstat_count_heap_scan(s)										\
423
	do {																\
424
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
425
			((PgStat_TableEntry *)((s)->tabentry))->t_numscans++;		\
426
	} while (0)
427 428
/* kluge for bitmap scans: */
#define pgstat_discount_heap_scan(s)									\
429
	do {																\
430 431
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
			((PgStat_TableEntry *)((s)->tabentry))->t_numscans--;		\
432
	} while (0)
433
#define pgstat_count_heap_getnext(s)									\
434
	do {																\
435
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
436 437
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned++; \
	} while (0)
438
#define pgstat_count_heap_fetch(s)										\
439
	do {																\
440
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
441
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_fetched++; \
442
	} while (0)
443
#define pgstat_count_heap_insert(s)										\
444
	do {																\
445
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
446 447
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_inserted++; \
	} while (0)
448
#define pgstat_count_heap_update(s)										\
449
	do {																\
450
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
451
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_updated++; \
452
	} while (0)
453
#define pgstat_count_heap_delete(s)										\
454
	do {																\
455
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
456
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_deleted++; \
457
	} while (0)
458
#define pgstat_count_index_scan(s)										\
459
	do {																\
460
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
461 462
			((PgStat_TableEntry *)((s)->tabentry))->t_numscans++;		\
	} while (0)
463
#define pgstat_count_index_tuples(s, n)									\
464
	do {																\
465
		if (pgstat_collect_tuplelevel && (s)->tabentry != NULL)			\
466
			((PgStat_TableEntry *)((s)->tabentry))->t_tuples_returned += (n); \
467
	} while (0)
468
#define pgstat_count_buffer_read(s,r)									\
469
	do {																\
470 471 472 473
		if (pgstat_collect_blocklevel) {								\
			if ((s)->tabentry != NULL)									\
				((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
			else {														\
474 475 476 477
				pgstat_initstats((s), (r));								\
				if ((s)->tabentry != NULL)								\
					((PgStat_TableEntry *)((s)->tabentry))->t_blocks_fetched++; \
			}															\
478
		}																\
479
	} while (0)
480
#define pgstat_count_buffer_hit(s,r)									\
481
	do {																\
482 483 484 485
		if (pgstat_collect_blocklevel) {								\
			if ((s)->tabentry != NULL)									\
				((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
			else {														\
486 487 488 489
				pgstat_initstats((s), (r));								\
				if ((s)->tabentry != NULL)								\
					((PgStat_TableEntry *)((s)->tabentry))->t_blocks_hit++; \
			}															\
490
		}																\
491
	} while (0)
492 493


494 495
extern void pgstat_count_xact_commit(void);
extern void pgstat_count_xact_rollback(void);
496 497 498 499 500 501

/* ----------
 * Support functions for the SQL-callable functions to
 * generate the pgstat* views.
 * ----------
 */
502
extern PgStat_StatDBEntry *pgstat_fetch_stat_dbentry(Oid dbid);
503
extern PgStat_StatTabEntry *pgstat_fetch_stat_tabentry(Oid relid);
504 505
extern PgStat_StatBeEntry *pgstat_fetch_stat_beentry(int beid);
extern int	pgstat_fetch_stat_numbackends(void);
506

507
#endif   /* PGSTAT_H */