pgstat.c 63.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* ----------
 * pgstat.c
 *
 *	All the statistics collector stuff hacked up in one big, ugly file.
 *
 *	TODO:	- Separate collector, postmaster and backend stuff
 *			  into different files.
 *
 *			- Add some automatic call for pgstat vacuuming.
 *
 *			- Add a pgstat config column to pg_database, so this
12
 *			  entire thing can be enabled/disabled on a per db basis.
13
 *
14
 *	Copyright (c) 2001-2007, PostgreSQL Global Development Group
15
 *
16
 *	$PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.145 2007/02/07 23:11:29 tgl Exp $
17 18
 * ----------
 */
P
Peter Eisentraut 已提交
19 20
#include "postgres.h"

21 22 23 24 25
#include <unistd.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/socket.h>
B
Bruce Momjian 已提交
26
#include <netdb.h>
27 28 29
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
30
#include <time.h>
31 32 33 34 35 36
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_SYS_POLL_H
#include <sys/poll.h>
#endif
37

38 39
#include "pgstat.h"

40
#include "access/heapam.h"
41
#include "access/transam.h"
42
#include "access/xact.h"
43
#include "catalog/pg_database.h"
44
#include "libpq/ip.h"
B
Bruce Momjian 已提交
45
#include "libpq/libpq.h"
46
#include "libpq/pqsignal.h"
47
#include "mb/pg_wchar.h"
48
#include "miscadmin.h"
49
#include "postmaster/autovacuum.h"
50
#include "postmaster/fork_process.h"
51
#include "postmaster/postmaster.h"
52
#include "storage/backendid.h"
53
#include "storage/fd.h"
54
#include "storage/ipc.h"
55
#include "storage/pg_shmem.h"
56 57
#include "storage/pmsignal.h"
#include "utils/memutils.h"
58
#include "utils/ps_status.h"
59 60


61
/* ----------
62
 * Paths for the statistics files (relative to installation's $PGDATA).
63 64
 * ----------
 */
65 66
#define PGSTAT_STAT_FILENAME	"global/pgstat.stat"
#define PGSTAT_STAT_TMPFILE		"global/pgstat.tmp"
67 68 69 70 71

/* ----------
 * Timer definitions.
 * ----------
 */
72 73
#define PGSTAT_STAT_INTERVAL	500		/* How often to write the status file;
										 * in milliseconds. */
74

75 76 77
#define PGSTAT_RESTART_INTERVAL 60		/* How often to attempt to restart a
										 * failed statistics collector; in
										 * seconds. */
78

79 80 81
#define PGSTAT_SELECT_TIMEOUT	2		/* How often to check for postmaster
										 * death; in seconds. */

82 83 84 85 86 87 88 89 90

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


91
/* ----------
92
 * GUC parameters
93 94
 * ----------
 */
95
bool		pgstat_collect_startcollector = true;
96
bool		pgstat_collect_resetonpmstart = false;
97 98
bool		pgstat_collect_tuplelevel = false;
bool		pgstat_collect_blocklevel = false;
99
bool		pgstat_collect_querystring = false;
100 101 102 103 104

/* ----------
 * Local data
 * ----------
 */
B
Bruce Momjian 已提交
105
NON_EXEC_STATIC int pgStatSock = -1;
106

B
Bruce Momjian 已提交
107
static struct sockaddr_storage pgStatAddr;
108

109
static time_t last_pgstat_start_time;
110

111
static bool pgStatRunningInCollector = false;
112

113 114 115 116 117 118 119
/*
 * Place where backends store per-table info to be sent to the collector.
 * We store shared relations separately from non-shared ones, to be able to
 * send them in separate messages.
 */
typedef struct TabStatArray
{
B
Bruce Momjian 已提交
120 121
	int			tsa_alloc;		/* num allocated */
	int			tsa_used;		/* num actually used */
122 123
	PgStat_MsgTabstat **tsa_messages;	/* the array itself */
} TabStatArray;
B
Bruce Momjian 已提交
124

125 126
#define TABSTAT_QUANTUM		4	/* we alloc this many at a time */

B
Bruce Momjian 已提交
127 128
static TabStatArray RegularTabStat = {0, 0, NULL};
static TabStatArray SharedTabStat = {0, 0, NULL};
129

130 131
static int	pgStatXactCommit = 0;
static int	pgStatXactRollback = 0;
132

133
static MemoryContext pgStatLocalContext = NULL;
134
static HTAB *pgStatDBHash = NULL;
135 136
static PgBackendStatus *localBackendStatusTable = NULL;
static int	localNumBackends = 0;
137

B
Bruce Momjian 已提交
138 139
static volatile bool need_exit = false;
static volatile bool need_statwrite = false;
140

141

142 143 144 145
/* ----------
 * Local function forward declarations
 * ----------
 */
146
#ifdef EXEC_BACKEND
147
static pid_t pgstat_forkexec(void);
148
#endif
149 150

NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]);
151
static void pgstat_exit(SIGNAL_ARGS);
152
static void force_statwrite(SIGNAL_ARGS);
153
static void pgstat_beshutdown_hook(int code, Datum arg);
154

155
static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
156 157
static void pgstat_drop_database(Oid databaseid);
static void pgstat_write_statsfile(void);
158
static HTAB *pgstat_read_statsfile(Oid onlydb);
159
static void backend_read_statsfile(void);
160
static void pgstat_read_current_status(void);
161
static HTAB *pgstat_collect_oids(Oid catalogid);
162

163 164
static void pgstat_setup_memcxt(void);

165
static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype);
166 167 168 169 170 171
static void pgstat_send(void *msg, int len);

static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
172 173 174
static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
175 176 177 178 179 180 181 182 183 184 185


/* ------------------------------------------------------------
 * Public functions called from postmaster follow
 * ------------------------------------------------------------
 */

/* ----------
 * pgstat_init() -
 *
 *	Called from postmaster at startup. Create the resources required
186 187 188
 *	by the statistics collector process.  If unable to do so, do not
 *	fail --- better to let the postmaster start with stats collection
 *	disabled.
189 190
 * ----------
 */
191
void
192 193
pgstat_init(void)
{
B
Bruce Momjian 已提交
194 195 196 197
	ACCEPT_TYPE_ARG3 alen;
	struct addrinfo *addrs = NULL,
			   *addr,
				hints;
B
Bruce Momjian 已提交
198
	int			ret;
B
Bruce Momjian 已提交
199
	fd_set		rset;
200
	struct timeval tv;
B
Bruce Momjian 已提交
201 202
	char		test_byte;
	int			sel_res;
203
	int			tries = 0;
B
Bruce Momjian 已提交
204

205
#define TESTBYTEVAL ((char) 199)
206

207
	/*
208
	 * Force start of collector daemon if something to collect.  Note that
B
Bruce Momjian 已提交
209 210
	 * pgstat_collect_querystring is now an independent facility that does not
	 * require the collector daemon.
211
	 */
212
	if (pgstat_collect_tuplelevel ||
213
		pgstat_collect_blocklevel)
214 215 216
		pgstat_collect_startcollector = true;

	/*
217
	 * If we don't have to start a collector or should reset the collected
218
	 * statistics on postmaster start, simply remove the stats file.
219 220
	 */
	if (!pgstat_collect_startcollector || pgstat_collect_resetonpmstart)
221
		pgstat_reset_all();
222 223 224 225 226

	/*
	 * Nothing else required if collector will not get started
	 */
	if (!pgstat_collect_startcollector)
227
		return;
228

229
	/*
230
	 * Create the UDP socket for sending and receiving statistic messages
231
	 */
B
Bruce Momjian 已提交
232 233 234 235 236 237 238 239
	hints.ai_flags = AI_PASSIVE;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_protocol = 0;
	hints.ai_addrlen = 0;
	hints.ai_addr = NULL;
	hints.ai_canonname = NULL;
	hints.ai_next = NULL;
240
	ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
241
	if (ret || !addrs)
B
Bruce Momjian 已提交
242
	{
243
		ereport(LOG,
244
				(errmsg("could not resolve \"localhost\": %s",
245
						gai_strerror(ret))));
B
Bruce Momjian 已提交
246 247
		goto startup_failed;
	}
B
Bruce Momjian 已提交
248

249
	/*
250 251 252
	 * On some platforms, pg_getaddrinfo_all() may return multiple addresses
	 * only one of which will actually work (eg, both IPv6 and IPv4 addresses
	 * when kernel will reject IPv6).  Worse, the failure may occur at the
253
	 * bind() or perhaps even connect() stage.	So we must loop through the
254 255
	 * results till we find a working combination. We will generate LOG
	 * messages, but no error, for bogus combinations.
256
	 */
257 258 259 260 261 262 263
	for (addr = addrs; addr; addr = addr->ai_next)
	{
#ifdef HAVE_UNIX_SOCKETS
		/* Ignore AF_UNIX sockets, if any are returned. */
		if (addr->ai_family == AF_UNIX)
			continue;
#endif
B
Bruce Momjian 已提交
264

265 266
		if (++tries > 1)
			ereport(LOG,
B
Bruce Momjian 已提交
267 268
			(errmsg("trying another address for the statistics collector")));

269 270 271 272 273 274 275
		/*
		 * Create the socket.
		 */
		if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) < 0)
		{
			ereport(LOG,
					(errcode_for_socket_access(),
B
Bruce Momjian 已提交
276
			errmsg("could not create socket for statistics collector: %m")));
277 278 279 280
			continue;
		}

		/*
B
Bruce Momjian 已提交
281 282
		 * Bind it to a kernel assigned port on localhost and get the assigned
		 * port via getsockname().
283 284 285 286 287
		 */
		if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
		{
			ereport(LOG,
					(errcode_for_socket_access(),
B
Bruce Momjian 已提交
288
			  errmsg("could not bind socket for statistics collector: %m")));
289 290 291 292 293 294
			closesocket(pgStatSock);
			pgStatSock = -1;
			continue;
		}

		alen = sizeof(pgStatAddr);
B
Bruce Momjian 已提交
295
		if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
296 297 298 299 300 301 302 303 304 305
		{
			ereport(LOG,
					(errcode_for_socket_access(),
					 errmsg("could not get address of socket for statistics collector: %m")));
			closesocket(pgStatSock);
			pgStatSock = -1;
			continue;
		}

		/*
B
Bruce Momjian 已提交
306 307 308 309
		 * Connect the socket to its own address.  This saves a few cycles by
		 * not having to respecify the target address on every send. This also
		 * provides a kernel-level check that only packets from this same
		 * address will be received.
310
		 */
B
Bruce Momjian 已提交
311
		if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
312 313 314
		{
			ereport(LOG,
					(errcode_for_socket_access(),
B
Bruce Momjian 已提交
315
			errmsg("could not connect socket for statistics collector: %m")));
316 317 318 319
			closesocket(pgStatSock);
			pgStatSock = -1;
			continue;
		}
B
Bruce Momjian 已提交
320

321
		/*
B
Bruce Momjian 已提交
322 323 324 325
		 * Try to send and receive a one-byte test message on the socket. This
		 * is to catch situations where the socket can be created but will not
		 * actually pass data (for instance, because kernel packet filtering
		 * rules prevent it).
326 327
		 */
		test_byte = TESTBYTEVAL;
328 329

retry1:
330 331
		if (send(pgStatSock, &test_byte, 1, 0) != 1)
		{
332 333
			if (errno == EINTR)
				goto retry1;	/* if interrupted, just retry */
334 335 336 337 338 339 340 341 342
			ereport(LOG,
					(errcode_for_socket_access(),
					 errmsg("could not send test message on socket for statistics collector: %m")));
			closesocket(pgStatSock);
			pgStatSock = -1;
			continue;
		}

		/*
B
Bruce Momjian 已提交
343 344 345
		 * There could possibly be a little delay before the message can be
		 * received.  We arbitrarily allow up to half a second before deciding
		 * it's broken.
346 347 348 349 350 351 352
		 */
		for (;;)				/* need a loop to handle EINTR */
		{
			FD_ZERO(&rset);
			FD_SET(pgStatSock, &rset);
			tv.tv_sec = 0;
			tv.tv_usec = 500000;
B
Bruce Momjian 已提交
353
			sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
354 355 356 357 358 359 360
			if (sel_res >= 0 || errno != EINTR)
				break;
		}
		if (sel_res < 0)
		{
			ereport(LOG,
					(errcode_for_socket_access(),
B
Bruce Momjian 已提交
361
					 errmsg("select() failed in statistics collector: %m")));
362 363 364 365 366 367 368
			closesocket(pgStatSock);
			pgStatSock = -1;
			continue;
		}
		if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
		{
			/*
B
Bruce Momjian 已提交
369 370
			 * This is the case we actually think is likely, so take pains to
			 * give a specific message for it.
371 372 373 374
			 *
			 * errno will not be set meaningfully here, so don't use it.
			 */
			ereport(LOG,
375
					(errcode(ERRCODE_CONNECTION_FAILURE),
376 377 378 379 380 381 382 383
					 errmsg("test message did not get through on socket for statistics collector")));
			closesocket(pgStatSock);
			pgStatSock = -1;
			continue;
		}

		test_byte++;			/* just make sure variable is changed */

384
retry2:
385 386
		if (recv(pgStatSock, &test_byte, 1, 0) != 1)
		{
387 388
			if (errno == EINTR)
				goto retry2;	/* if interrupted, just retry */
389 390 391 392 393 394 395 396
			ereport(LOG,
					(errcode_for_socket_access(),
					 errmsg("could not receive test message on socket for statistics collector: %m")));
			closesocket(pgStatSock);
			pgStatSock = -1;
			continue;
		}

B
Bruce Momjian 已提交
397
		if (test_byte != TESTBYTEVAL)	/* strictly paranoia ... */
398 399
		{
			ereport(LOG,
400
					(errcode(ERRCODE_INTERNAL_ERROR),
401 402 403 404 405 406
					 errmsg("incorrect test message transmission on socket for statistics collector")));
			closesocket(pgStatSock);
			pgStatSock = -1;
			continue;
		}

407 408
		/* If we get here, we have a working socket */
		break;
409 410
	}

411 412
	/* Did we find a working address? */
	if (!addr || pgStatSock < 0)
413
		goto startup_failed;
414 415

	/*
B
Bruce Momjian 已提交
416
	 * Set the socket to non-blocking IO.  This ensures that if the collector
417 418
	 * falls behind, statistics messages will be discarded; backends won't
	 * block waiting to send messages to the collector.
419
	 */
420
	if (!pg_set_noblock(pgStatSock))
421
	{
422 423
		ereport(LOG,
				(errcode_for_socket_access(),
B
Bruce Momjian 已提交
424
				 errmsg("could not set statistics collector socket to nonblocking mode: %m")));
425
		goto startup_failed;
426 427
	}

428
	pg_freeaddrinfo_all(hints.ai_family, addrs);
429

430
	return;
431 432

startup_failed:
433
	ereport(LOG,
B
Bruce Momjian 已提交
434
	  (errmsg("disabling statistics collector for lack of working socket")));
435

436
	if (addrs)
437
		pg_freeaddrinfo_all(hints.ai_family, addrs);
B
Bruce Momjian 已提交
438

439
	if (pgStatSock >= 0)
440
		closesocket(pgStatSock);
441 442 443
	pgStatSock = -1;

	/* Adjust GUC variables to suppress useless activity */
444
	pgstat_collect_startcollector = false;
445 446
	pgstat_collect_tuplelevel = false;
	pgstat_collect_blocklevel = false;
447 448
}

449 450 451
/*
 * pgstat_reset_all() -
 *
B
Bruce Momjian 已提交
452
 * Remove the stats file.  This is used on server start if the
453 454 455 456 457 458 459 460
 * stats_reset_on_server_start feature is enabled, or if WAL
 * recovery is needed after a crash.
 */
void
pgstat_reset_all(void)
{
	unlink(PGSTAT_STAT_FILENAME);
}
461

462 463
#ifdef EXEC_BACKEND

464
/*
465
 * pgstat_forkexec() -
466
 *
467
 * Format up the arglist for, then fork and exec, statistics collector process
468
 */
469
static pid_t
470
pgstat_forkexec(void)
471
{
B
Bruce Momjian 已提交
472
	char	   *av[10];
473
	int			ac = 0;
474 475

	av[ac++] = "postgres";
476
	av[ac++] = "--forkcol";
477 478 479 480
	av[ac++] = NULL;			/* filled in by postmaster_forkexec */

	av[ac] = NULL;
	Assert(ac < lengthof(av));
481

482
	return postmaster_forkexec(ac, av);
483
}
B
Bruce Momjian 已提交
484
#endif   /* EXEC_BACKEND */
485

486

487 488 489 490
/* ----------
 * pgstat_start() -
 *
 *	Called from postmaster at startup or after an existing collector
491
 *	died.  Attempt to fire up a fresh statistics collector.
492
 *
493 494
 *	Returns PID of child process, or 0 if fail.
 *
495
 *	Note: if fail, we will be called again from the postmaster main loop.
496 497
 * ----------
 */
498
int
499
pgstat_start(void)
500
{
501
	time_t		curtime;
502
	pid_t		pgStatPid;
503

504 505 506
	/*
	 * Do nothing if no collector needed
	 */
507 508
	if (!pgstat_collect_startcollector)
		return 0;
509

510
	/*
B
Bruce Momjian 已提交
511 512 513 514
	 * Do nothing if too soon since last collector start.  This is a safety
	 * valve to protect against continuous respawn attempts if the collector
	 * is dying immediately at launch.	Note that since we will be re-called
	 * from the postmaster main loop, we will get another chance later.
515 516 517 518
	 */
	curtime = time(NULL);
	if ((unsigned int) (curtime - last_pgstat_start_time) <
		(unsigned int) PGSTAT_RESTART_INTERVAL)
519
		return 0;
520 521 522 523
	last_pgstat_start_time = curtime;

	/*
	 * Check that the socket is there, else pgstat_init failed.
524 525 526
	 */
	if (pgStatSock < 0)
	{
527 528
		ereport(LOG,
				(errmsg("statistics collector startup skipped")));
B
Bruce Momjian 已提交
529

530 531 532 533 534
		/*
		 * We can only get here if someone tries to manually turn
		 * pgstat_collect_startcollector on after it had been off.
		 */
		pgstat_collect_startcollector = false;
535
		return 0;
536 537 538
	}

	/*
539
	 * Okay, fork off the collector.
540
	 */
541
#ifdef EXEC_BACKEND
542
	switch ((pgStatPid = pgstat_forkexec()))
543
#else
544
	switch ((pgStatPid = fork_process()))
545
#endif
546 547
	{
		case -1:
548
			ereport(LOG,
549
					(errmsg("could not fork statistics collector: %m")));
550
			return 0;
551

552
#ifndef EXEC_BACKEND
553
		case 0:
554
			/* in postmaster child ... */
555
			/* Close the postmaster's sockets */
556
			ClosePostmasterPorts(false);
557

558 559 560
			/* Lose the postmaster's on-exit routines */
			on_exit_reset();

561 562 563
			/* Drop our connection to postmaster's shared memory, as well */
			PGSharedMemoryDetach();

564
			PgstatCollectorMain(0, NULL);
565
			break;
566
#endif
567 568

		default:
569
			return (int) pgStatPid;
570 571
	}

572 573
	/* shouldn't get here */
	return 0;
574 575 576 577 578
}


/* ------------------------------------------------------------
 * Public functions used by backends follow
579
 *------------------------------------------------------------
580 581 582 583 584 585 586 587 588 589 590 591 592
 */


/* ----------
 * pgstat_report_tabstat() -
 *
 *	Called from tcop/postgres.c to send the so far collected
 *	per table access statistics to the collector.
 * ----------
 */
void
pgstat_report_tabstat(void)
{
593
	int			i;
594

595
	if (pgStatSock < 0 ||
596
		(!pgstat_collect_tuplelevel &&
597
		 !pgstat_collect_blocklevel))
598 599
	{
		/* Not reporting stats, so just flush whatever we have */
600 601
		RegularTabStat.tsa_used = 0;
		SharedTabStat.tsa_used = 0;
602
		return;
603
	}
604 605

	/*
606 607
	 * For each message buffer used during the last query set the header
	 * fields and send it out.
608
	 */
609
	for (i = 0; i < RegularTabStat.tsa_used; i++)
610
	{
611
		PgStat_MsgTabstat *tsmsg = RegularTabStat.tsa_messages[i];
612 613 614 615
		int			n;
		int			len;

		n = tsmsg->m_nentries;
616 617
		len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
			n * sizeof(PgStat_TableEntry);
618

619 620
		tsmsg->m_xact_commit = pgStatXactCommit;
		tsmsg->m_xact_rollback = pgStatXactRollback;
621
		pgStatXactCommit = 0;
622 623
		pgStatXactRollback = 0;

624
		pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
625
		tsmsg->m_databaseid = MyDatabaseId;
626
		pgstat_send(tsmsg, len);
627
	}
628 629 630 631 632 633 634 635 636 637 638 639
	RegularTabStat.tsa_used = 0;

	/* Ditto, for shared relations */
	for (i = 0; i < SharedTabStat.tsa_used; i++)
	{
		PgStat_MsgTabstat *tsmsg = SharedTabStat.tsa_messages[i];
		int			n;
		int			len;

		n = tsmsg->m_nentries;
		len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
			n * sizeof(PgStat_TableEntry);
640

641 642 643 644 645 646 647 648 649
		/* We don't report transaction commit/abort here */
		tsmsg->m_xact_commit = 0;
		tsmsg->m_xact_rollback = 0;

		pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
		tsmsg->m_databaseid = InvalidOid;
		pgstat_send(tsmsg, len);
	}
	SharedTabStat.tsa_used = 0;
650 651 652 653 654 655 656 657 658
}


/* ----------
 * pgstat_vacuum_tabstat() -
 *
 *	Will tell the collector about objects he can get rid of.
 * ----------
 */
659
void
660 661
pgstat_vacuum_tabstat(void)
{
662
	HTAB	   *htab;
663
	PgStat_MsgTabpurge msg;
664 665 666 667
	HASH_SEQ_STATUS hstat;
	PgStat_StatDBEntry *dbentry;
	PgStat_StatTabEntry *tabentry;
	int			len;
668 669

	if (pgStatSock < 0)
670
		return;
671 672

	/*
B
Bruce Momjian 已提交
673 674
	 * If not done for this transaction, read the statistics collector stats
	 * file into some hash tables.
675
	 */
676
	backend_read_statsfile();
677 678

	/*
679 680
	 * Read pg_database and make a list of OIDs of all existing databases
	 */
681
	htab = pgstat_collect_oids(DatabaseRelationId);
682 683 684 685 686 687 688 689 690 691

	/*
	 * Search the database hash table for dead databases and tell the
	 * collector to drop them.
	 */
	hash_seq_init(&hstat, pgStatDBHash);
	while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
	{
		Oid			dbid = dbentry->databaseid;

692 693 694
		CHECK_FOR_INTERRUPTS();

		if (hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
695 696 697 698
			pgstat_drop_database(dbid);
	}

	/* Clean up */
699
	hash_destroy(htab);
700 701 702

	/*
	 * Lookup our own database entry; if not found, nothing more to do.
703
	 */
704 705 706
	dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
												 (void *) &MyDatabaseId,
												 HASH_FIND, NULL);
707 708 709 710 711 712
	if (dbentry == NULL || dbentry->tables == NULL)
		return;

	/*
	 * Similarly to above, make a list of all known relations in this DB.
	 */
713
	htab = pgstat_collect_oids(RelationRelationId);
714 715 716 717 718 719 720

	/*
	 * Initialize our messages table counter to zero
	 */
	msg.m_nentries = 0;

	/*
721
	 * Check for all tables listed in stats hashtable if they still exist.
722
	 */
723
	hash_seq_init(&hstat, dbentry->tables);
724
	while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
725
	{
726 727 728 729 730
		Oid			tabid = tabentry->tableid;

		CHECK_FOR_INTERRUPTS();

		if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
731 732 733
			continue;

		/*
734
		 * Not there, so add this table's Oid to the message
735
		 */
736
		msg.m_tableid[msg.m_nentries++] = tabid;
737 738

		/*
739
		 * If the message is full, send it out and reinitialize to empty
740 741 742
		 */
		if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
		{
743
			len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
B
Bruce Momjian 已提交
744
				+msg.m_nentries * sizeof(Oid);
745 746

			pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
747
			msg.m_databaseid = MyDatabaseId;
748 749 750 751 752 753 754 755 756 757 758
			pgstat_send(&msg, len);

			msg.m_nentries = 0;
		}
	}

	/*
	 * Send the rest
	 */
	if (msg.m_nentries > 0)
	{
759
		len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
B
Bruce Momjian 已提交
760
			+msg.m_nentries * sizeof(Oid);
761 762

		pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
763
		msg.m_databaseid = MyDatabaseId;
764 765 766
		pgstat_send(&msg, len);
	}

767
	/* Clean up */
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
	hash_destroy(htab);
}


/* ----------
 * pgstat_collect_oids() -
 *
 *	Collect the OIDs of either all databases or all tables, according to
 *	the parameter, into a temporary hash table.  Caller should hash_destroy
 *	the result when done with it.
 * ----------
 */
static HTAB *
pgstat_collect_oids(Oid catalogid)
{
	HTAB	   *htab;
	HASHCTL		hash_ctl;
	Relation	rel;
	HeapScanDesc scan;
	HeapTuple	tup;

	memset(&hash_ctl, 0, sizeof(hash_ctl));
	hash_ctl.keysize = sizeof(Oid);
	hash_ctl.entrysize = sizeof(Oid);
	hash_ctl.hash = oid_hash;
	htab = hash_create("Temporary table of OIDs",
					   PGSTAT_TAB_HASH_SIZE,
					   &hash_ctl,
					   HASH_ELEM | HASH_FUNCTION);

	rel = heap_open(catalogid, AccessShareLock);
	scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
	while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
	{
		Oid		thisoid = HeapTupleGetOid(tup);

		CHECK_FOR_INTERRUPTS();

		(void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
	}
	heap_endscan(scan);
	heap_close(rel, AccessShareLock);

	return htab;
812 813 814 815 816 817 818
}


/* ----------
 * pgstat_drop_database() -
 *
 *	Tell the collector that we just dropped a database.
819 820
 *	(If the message gets lost, we will still clean the dead DB eventually
 *	via future invocations of pgstat_vacuum_tabstat().)
821 822 823 824 825
 * ----------
 */
static void
pgstat_drop_database(Oid databaseid)
{
826
	PgStat_MsgDropdb msg;
827 828 829 830 831

	if (pgStatSock < 0)
		return;

	pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
832
	msg.m_databaseid = databaseid;
833 834 835 836
	pgstat_send(&msg, sizeof(msg));
}


837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
/* ----------
 * pgstat_drop_relation() -
 *
 *	Tell the collector that we just dropped a relation.
 *	(If the message gets lost, we will still clean the dead entry eventually
 *	via future invocations of pgstat_vacuum_tabstat().)
 * ----------
 */
void
pgstat_drop_relation(Oid relid)
{
	PgStat_MsgTabpurge msg;
	int			len;

	if (pgStatSock < 0)
		return;

	msg.m_tableid[0] = relid;
	msg.m_nentries = 1;

B
Bruce Momjian 已提交
857
	len = offsetof(PgStat_MsgTabpurge, m_tableid[0]) +sizeof(Oid);
858 859 860 861 862 863 864

	pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
	msg.m_databaseid = MyDatabaseId;
	pgstat_send(&msg, len);
}


865 866 867 868 869 870 871 872 873
/* ----------
 * pgstat_reset_counters() -
 *
 *	Tell the statistics collector to reset counters for our database.
 * ----------
 */
void
pgstat_reset_counters(void)
{
874
	PgStat_MsgResetcounter msg;
875 876 877 878 879

	if (pgStatSock < 0)
		return;

	if (!superuser())
880 881
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
B
Bruce Momjian 已提交
882
				 errmsg("must be superuser to reset statistics counters")));
883 884

	pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
885
	msg.m_databaseid = MyDatabaseId;
886 887 888 889
	pgstat_send(&msg, sizeof(msg));
}


890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933
/* ----------
 * pgstat_report_autovac() -
 *
 *	Called from autovacuum.c to report startup of an autovacuum process.
 *	We are called before InitPostgres is done, so can't rely on MyDatabaseId;
 *	the db OID must be passed in, instead.
 * ----------
 */
void
pgstat_report_autovac(Oid dboid)
{
	PgStat_MsgAutovacStart msg;

	if (pgStatSock < 0)
		return;

	pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_AUTOVAC_START);
	msg.m_databaseid = dboid;
	msg.m_start_time = GetCurrentTimestamp();

	pgstat_send(&msg, sizeof(msg));
}


/* ---------
 * pgstat_report_vacuum() -
 *
 *	Tell the collector about the table we just vacuumed.
 * ---------
 */
void
pgstat_report_vacuum(Oid tableoid, bool shared,
					 bool analyze, PgStat_Counter tuples)
{
	PgStat_MsgVacuum msg;

	if (pgStatSock < 0 ||
		!pgstat_collect_tuplelevel)
		return;

	pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_VACUUM);
	msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
	msg.m_tableoid = tableoid;
	msg.m_analyze = analyze;
B
Bruce Momjian 已提交
934
	msg.m_autovacuum = IsAutoVacuumProcess();	/* is this autovacuum? */
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
	msg.m_vacuumtime = GetCurrentTimestamp();
	msg.m_tuples = tuples;
	pgstat_send(&msg, sizeof(msg));
}

/* --------
 * pgstat_report_analyze() -
 *
 *	Tell the collector about the table we just analyzed.
 * --------
 */
void
pgstat_report_analyze(Oid tableoid, bool shared, PgStat_Counter livetuples,
					  PgStat_Counter deadtuples)
{
	PgStat_MsgAnalyze msg;

	if (pgStatSock < 0 ||
		!pgstat_collect_tuplelevel)
		return;

	pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ANALYZE);
	msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
	msg.m_tableoid = tableoid;
B
Bruce Momjian 已提交
959
	msg.m_autovacuum = IsAutoVacuumProcess();	/* is this autovacuum? */
960 961 962 963 964 965 966
	msg.m_analyzetime = GetCurrentTimestamp();
	msg.m_live_tuples = livetuples;
	msg.m_dead_tuples = deadtuples;
	pgstat_send(&msg, sizeof(msg));
}


967 968 969 970 971 972 973 974 975
/* ----------
 * pgstat_ping() -
 *
 *	Send some junk data to the collector to increase traffic.
 * ----------
 */
void
pgstat_ping(void)
{
976
	PgStat_MsgDummy msg;
977 978 979 980 981 982 983 984

	if (pgStatSock < 0)
		return;

	pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DUMMY);
	pgstat_send(&msg, sizeof(msg));
}

985
/*
986
 * Enlarge a TabStatArray
987
 */
988
static void
989
more_tabstat_space(TabStatArray *tsarr)
990 991 992
{
	PgStat_MsgTabstat *newMessages;
	PgStat_MsgTabstat **msgArray;
993
	int			newAlloc;
994 995
	int			i;

996 997 998 999
	AssertArg(PointerIsValid(tsarr));

	newAlloc = tsarr->tsa_alloc + TABSTAT_QUANTUM;

1000 1001
	/* Create (another) quantum of message buffers */
	newMessages = (PgStat_MsgTabstat *)
1002 1003
		MemoryContextAllocZero(TopMemoryContext,
							   sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
1004 1005

	/* Create or enlarge the pointer array */
1006
	if (tsarr->tsa_messages == NULL)
1007
		msgArray = (PgStat_MsgTabstat **)
1008 1009
			MemoryContextAlloc(TopMemoryContext,
							   sizeof(PgStat_MsgTabstat *) * newAlloc);
1010 1011
	else
		msgArray = (PgStat_MsgTabstat **)
1012
			repalloc(tsarr->tsa_messages,
1013
					 sizeof(PgStat_MsgTabstat *) * newAlloc);
1014 1015

	for (i = 0; i < TABSTAT_QUANTUM; i++)
1016 1017 1018
		msgArray[tsarr->tsa_alloc + i] = newMessages++;
	tsarr->tsa_messages = msgArray;
	tsarr->tsa_alloc = newAlloc;
1019

1020
	Assert(tsarr->tsa_used < tsarr->tsa_alloc);
1021
}
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

/* ----------
 * pgstat_initstats() -
 *
 *	Called from various places usually dealing with initialization
 *	of Relation or Scan structures. The data placed into these
 *	structures from here tell where later to count for buffer reads,
 *	scans and tuples fetched.
 * ----------
 */
void
pgstat_initstats(PgStat_Info *stats, Relation rel)
{
1035
	Oid			rel_id = rel->rd_id;
1036
	PgStat_TableEntry *useent;
B
Bruce Momjian 已提交
1037
	TabStatArray *tsarr;
1038
	PgStat_MsgTabstat *tsmsg;
1039 1040
	int			mb;
	int			i;
1041 1042 1043 1044

	/*
	 * Initialize data not to count at all.
	 */
1045
	stats->tabentry = NULL;
1046

1047 1048 1049
	if (pgStatSock < 0 ||
		!(pgstat_collect_tuplelevel ||
		  pgstat_collect_blocklevel))
1050 1051
		return;

1052 1053
	tsarr = rel->rd_rel->relisshared ? &SharedTabStat : &RegularTabStat;

1054
	/*
1055
	 * Search the already-used message slots for this relation.
1056
	 */
1057
	for (mb = 0; mb < tsarr->tsa_used; mb++)
1058
	{
1059
		tsmsg = tsarr->tsa_messages[mb];
1060

B
Bruce Momjian 已提交
1061
		for (i = tsmsg->m_nentries; --i >= 0;)
1062
		{
1063
			if (tsmsg->m_entry[i].t_id == rel_id)
1064
			{
1065
				stats->tabentry = (void *) &(tsmsg->m_entry[i]);
1066 1067 1068 1069
				return;
			}
		}

1070
		if (tsmsg->m_nentries >= PGSTAT_NUM_TABENTRIES)
1071
			continue;
1072

1073
		/*
B
Bruce Momjian 已提交
1074 1075
		 * Not found, but found a message buffer with an empty slot instead.
		 * Fine, let's use this one.
1076
		 */
1077 1078
		i = tsmsg->m_nentries++;
		useent = &tsmsg->m_entry[i];
1079
		MemSet(useent, 0, sizeof(PgStat_TableEntry));
1080
		useent->t_id = rel_id;
1081
		stats->tabentry = (void *) useent;
1082 1083 1084 1085 1086 1087
		return;
	}

	/*
	 * If we ran out of message buffers, we just allocate more.
	 */
1088 1089
	if (tsarr->tsa_used >= tsarr->tsa_alloc)
		more_tabstat_space(tsarr);
1090 1091 1092 1093

	/*
	 * Use the first entry of the next message buffer.
	 */
1094 1095
	mb = tsarr->tsa_used++;
	tsmsg = tsarr->tsa_messages[mb];
1096 1097
	tsmsg->m_nentries = 1;
	useent = &tsmsg->m_entry[0];
1098
	MemSet(useent, 0, sizeof(PgStat_TableEntry));
1099
	useent->t_id = rel_id;
1100
	stats->tabentry = (void *) useent;
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
}


/* ----------
 * pgstat_count_xact_commit() -
 *
 *	Called from access/transam/xact.c to count transaction commits.
 * ----------
 */
void
pgstat_count_xact_commit(void)
{
B
Bruce Momjian 已提交
1113 1114
	if (!pgstat_collect_tuplelevel &&
		!pgstat_collect_blocklevel)
1115 1116
		return;

1117 1118 1119
	pgStatXactCommit++;

	/*
B
Bruce Momjian 已提交
1120 1121 1122
	 * If there was no relation activity yet, just make one existing message
	 * buffer used without slots, causing the next report to tell new
	 * xact-counters.
1123
	 */
1124 1125
	if (RegularTabStat.tsa_alloc == 0)
		more_tabstat_space(&RegularTabStat);
1126

1127
	if (RegularTabStat.tsa_used == 0)
1128
	{
1129 1130
		RegularTabStat.tsa_used++;
		RegularTabStat.tsa_messages[0]->m_nentries = 0;
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	}
}


/* ----------
 * pgstat_count_xact_rollback() -
 *
 *	Called from access/transam/xact.c to count transaction rollbacks.
 * ----------
 */
void
pgstat_count_xact_rollback(void)
{
B
Bruce Momjian 已提交
1144 1145
	if (!pgstat_collect_tuplelevel &&
		!pgstat_collect_blocklevel)
1146 1147
		return;

1148 1149 1150
	pgStatXactRollback++;

	/*
B
Bruce Momjian 已提交
1151 1152 1153
	 * If there was no relation activity yet, just make one existing message
	 * buffer used without slots, causing the next report to tell new
	 * xact-counters.
1154
	 */
1155 1156
	if (RegularTabStat.tsa_alloc == 0)
		more_tabstat_space(&RegularTabStat);
1157

1158
	if (RegularTabStat.tsa_used == 0)
1159
	{
1160 1161
		RegularTabStat.tsa_used++;
		RegularTabStat.tsa_messages[0]->m_nentries = 0;
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	}
}


/* ----------
 * pgstat_fetch_stat_dbentry() -
 *
 *	Support function for the SQL-callable pgstat* functions. Returns
 *	the collected statistics for one database or NULL. NULL doesn't mean
 *	that the database doesn't exist, it is just not yet known by the
 *	collector, so the caller is better off to report ZERO instead.
 * ----------
 */
PgStat_StatDBEntry *
pgstat_fetch_stat_dbentry(Oid dbid)
{
	/*
B
Bruce Momjian 已提交
1179 1180
	 * If not done for this transaction, read the statistics collector stats
	 * file into some hash tables.
1181
	 */
1182
	backend_read_statsfile();
1183 1184

	/*
1185
	 * Lookup the requested database; return NULL if not found
1186
	 */
1187 1188 1189
	return (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
											  (void *) &dbid,
											  HASH_FIND, NULL);
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
}


/* ----------
 * pgstat_fetch_stat_tabentry() -
 *
 *	Support function for the SQL-callable pgstat* functions. Returns
 *	the collected statistics for one table or NULL. NULL doesn't mean
 *	that the table doesn't exist, it is just not yet known by the
 *	collector, so the caller is better off to report ZERO instead.
 * ----------
 */
PgStat_StatTabEntry *
pgstat_fetch_stat_tabentry(Oid relid)
{
1205
	Oid			dbid;
1206 1207
	PgStat_StatDBEntry *dbentry;
	PgStat_StatTabEntry *tabentry;
1208 1209

	/*
B
Bruce Momjian 已提交
1210 1211
	 * If not done for this transaction, read the statistics collector stats
	 * file into some hash tables.
1212
	 */
1213
	backend_read_statsfile();
1214 1215

	/*
1216
	 * Lookup our database, then look in its table hash table.
1217
	 */
1218
	dbid = MyDatabaseId;
1219
	dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1220
												 (void *) &dbid,
1221
												 HASH_FIND, NULL);
1222 1223 1224 1225 1226 1227 1228 1229
	if (dbentry != NULL && dbentry->tables != NULL)
	{
		tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
													   (void *) &relid,
													   HASH_FIND, NULL);
		if (tabentry)
			return tabentry;
	}
1230 1231

	/*
1232
	 * If we didn't find it, maybe it's a shared table.
1233
	 */
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	dbid = InvalidOid;
	dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
												 (void *) &dbid,
												 HASH_FIND, NULL);
	if (dbentry != NULL && dbentry->tables != NULL)
	{
		tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
													   (void *) &relid,
													   HASH_FIND, NULL);
		if (tabentry)
			return tabentry;
	}
1246

1247
	return NULL;
1248 1249 1250 1251 1252 1253 1254
}


/* ----------
 * pgstat_fetch_stat_beentry() -
 *
 *	Support function for the SQL-callable pgstat* functions. Returns
1255 1256 1257 1258
 *	our local copy of the current-activity entry for one backend.
 *
 *	NB: caller is responsible for a check if the user is permitted to see
 *	this info (especially the querystring).
1259 1260
 * ----------
 */
1261
PgBackendStatus *
1262 1263
pgstat_fetch_stat_beentry(int beid)
{
1264
	pgstat_read_current_status();
1265

1266
	if (beid < 1 || beid > localNumBackends)
1267 1268
		return NULL;

1269
	return &localBackendStatusTable[beid - 1];
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
}


/* ----------
 * pgstat_fetch_stat_numbackends() -
 *
 *	Support function for the SQL-callable pgstat* functions. Returns
 *	the maximum current backend id.
 * ----------
 */
int
pgstat_fetch_stat_numbackends(void)
{
1283
	pgstat_read_current_status();
1284

1285
	return localNumBackends;
1286 1287 1288 1289
}


/* ------------------------------------------------------------
1290
 * Functions for management of the shared-memory PgBackendStatus array
1291 1292 1293
 * ------------------------------------------------------------
 */

1294 1295
static PgBackendStatus *BackendStatusArray = NULL;
static PgBackendStatus *MyBEEntry = NULL;
1296

1297 1298 1299

/*
 * Report shared-memory space needed by CreateSharedBackendStatus.
1300
 */
1301 1302
Size
BackendStatusShmemSize(void)
1303
{
1304
	Size		size;
1305

1306 1307 1308
	size = mul_size(sizeof(PgBackendStatus), MaxBackends);
	return size;
}
1309

1310 1311
/*
 * Initialize the shared status array during postmaster startup.
1312
 */
1313 1314
void
CreateSharedBackendStatus(void)
1315
{
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	Size		size = BackendStatusShmemSize();
	bool		found;

	/* Create or attach to the shared array */
	BackendStatusArray = (PgBackendStatus *)
		ShmemInitStruct("Backend Status Array", size, &found);

	if (!found)
	{
		/*
		 * We're the first - initialize.
		 */
		MemSet(BackendStatusArray, 0, size);
	}
}


/* ----------
 * pgstat_bestart() -
 *
 *	Initialize this backend's entry in the PgBackendStatus array,
 *	and set up an on-proc-exit hook that will clear it again.
 *	Called from InitPostgres.  MyBackendId and MyDatabaseId must be set.
 * ----------
 */
void
pgstat_bestart(void)
{
	volatile PgBackendStatus *beentry;
	TimestampTz proc_start_timestamp;
	Oid			userid;
	SockAddr	clientaddr;

	Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
	MyBEEntry = &BackendStatusArray[MyBackendId - 1];

	/*
B
Bruce Momjian 已提交
1353 1354
	 * To minimize the time spent modifying the entry, fetch all the needed
	 * data first.
1355 1356 1357
	 *
	 * If we have a MyProcPort, use its session start time (for consistency,
	 * and to save a kernel call).
1358
	 */
1359 1360 1361 1362
	if (MyProcPort)
		proc_start_timestamp = MyProcPort->SessionStartTime;
	else
		proc_start_timestamp = GetCurrentTimestamp();
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376
	userid = GetSessionUserId();

	/*
	 * We may not have a MyProcPort (eg, if this is the autovacuum process).
	 * If so, use all-zeroes client address, which is dealt with specially in
	 * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
	 */
	if (MyProcPort)
		memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
	else
		MemSet(&clientaddr, 0, sizeof(clientaddr));

	/*
	 * Initialize my status entry, following the protocol of bumping
B
Bruce Momjian 已提交
1377 1378 1379
	 * st_changecount before and after; and make sure it's even afterwards. We
	 * use a volatile pointer here to ensure the compiler doesn't try to get
	 * cute.
1380 1381
	 */
	beentry = MyBEEntry;
B
Bruce Momjian 已提交
1382 1383
	do
	{
1384 1385 1386 1387 1388 1389
		beentry->st_changecount++;
	} while ((beentry->st_changecount & 1) == 0);

	beentry->st_procpid = MyProcPid;
	beentry->st_proc_start_timestamp = proc_start_timestamp;
	beentry->st_activity_start_timestamp = 0;
1390
	beentry->st_txn_start_timestamp = 0;
1391 1392 1393
	beentry->st_databaseid = MyDatabaseId;
	beentry->st_userid = userid;
	beentry->st_clientaddr = clientaddr;
1394
	beentry->st_waiting = false;
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
	beentry->st_activity[0] = '\0';
	/* Also make sure the last byte in the string area is always 0 */
	beentry->st_activity[PGBE_ACTIVITY_SIZE - 1] = '\0';

	beentry->st_changecount++;
	Assert((beentry->st_changecount & 1) == 0);

	/*
	 * Set up a process-exit hook to clean up.
	 */
	on_shmem_exit(pgstat_beshutdown_hook, 0);
}

/*
 * Shut down a single backend's statistics reporting at process exit.
 *
 * Flush any remaining statistics counts out to the collector.
 * Without this, operations triggered during backend exit (such as
 * temp table deletions) won't be counted.
 *
 * Lastly, clear out our entry in the PgBackendStatus array.
 */
static void
pgstat_beshutdown_hook(int code, Datum arg)
{
1420
	volatile PgBackendStatus *beentry = MyBEEntry;
1421 1422 1423 1424

	pgstat_report_tabstat();

	/*
B
Bruce Momjian 已提交
1425 1426 1427
	 * Clear my status entry, following the protocol of bumping st_changecount
	 * before and after.  We use a volatile pointer here to ensure the
	 * compiler doesn't try to get cute.
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
	 */
	beentry->st_changecount++;

	beentry->st_procpid = 0;	/* mark invalid */

	beentry->st_changecount++;
	Assert((beentry->st_changecount & 1) == 0);
}


/* ----------
 * pgstat_report_activity() -
 *
 *	Called from tcop/postgres.c to report what the backend is actually doing
 *	(usually "<IDLE>" or the start of the query to be executed).
 * ----------
 */
void
pgstat_report_activity(const char *cmd_str)
{
1448
	volatile PgBackendStatus *beentry = MyBEEntry;
1449 1450 1451
	TimestampTz start_timestamp;
	int			len;

1452
	if (!pgstat_collect_querystring || !beentry)
1453 1454 1455
		return;

	/*
B
Bruce Momjian 已提交
1456 1457
	 * To minimize the time spent modifying the entry, fetch all the needed
	 * data first.
1458
	 */
1459
	start_timestamp = GetCurrentStatementStartTimestamp();
1460 1461 1462 1463 1464 1465

	len = strlen(cmd_str);
	len = pg_mbcliplen(cmd_str, len, PGBE_ACTIVITY_SIZE - 1);

	/*
	 * Update my status entry, following the protocol of bumping
B
Bruce Momjian 已提交
1466 1467
	 * st_changecount before and after.  We use a volatile pointer here to
	 * ensure the compiler doesn't try to get cute.
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	 */
	beentry->st_changecount++;

	beentry->st_activity_start_timestamp = start_timestamp;
	memcpy((char *) beentry->st_activity, cmd_str, len);
	beentry->st_activity[len] = '\0';

	beentry->st_changecount++;
	Assert((beentry->st_changecount & 1) == 0);
}

1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
/*
 * Set the current transaction start timestamp to the specified
 * value. If there is no current active transaction, this is signified
 * by 0.
 */
void
pgstat_report_txn_timestamp(TimestampTz tstamp)
{
	volatile PgBackendStatus *beentry = MyBEEntry;

	if (!pgstat_collect_querystring || !beentry)
		return;

	/*
	 * Update my status entry, following the protocol of bumping
	 * st_changecount before and after.  We use a volatile pointer
	 * here to ensure the compiler doesn't try to get cute.
	 */
	beentry->st_changecount++;
	beentry->st_txn_start_timestamp = tstamp;
	beentry->st_changecount++;
	Assert((beentry->st_changecount & 1) == 0);
}
1502

1503 1504 1505 1506
/* ----------
 * pgstat_report_waiting() -
 *
 *	Called from lock manager to report beginning or end of a lock wait.
1507 1508 1509
 *
 * NB: this *must* be able to survive being called before MyBEEntry has been
 * initialized.
1510 1511 1512 1513 1514
 * ----------
 */
void
pgstat_report_waiting(bool waiting)
{
1515
	volatile PgBackendStatus *beentry = MyBEEntry;
1516

1517
	if (!pgstat_collect_querystring || !beentry)
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
		return;

	/*
	 * Since this is a single-byte field in a struct that only this process
	 * may modify, there seems no need to bother with the st_changecount
	 * protocol.  The update must appear atomic in any case.
	 */
	beentry->st_waiting = waiting;
}


1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
/* ----------
 * pgstat_read_current_status() -
 *
 *	Copy the current contents of the PgBackendStatus array to local memory,
 *	if not already done in this transaction.
 * ----------
 */
static void
pgstat_read_current_status(void)
{
	volatile PgBackendStatus *beentry;
1540
	PgBackendStatus *localtable;
1541 1542 1543 1544
	PgBackendStatus *localentry;
	int			i;

	Assert(!pgStatRunningInCollector);
1545
	if (localBackendStatusTable)
1546 1547
		return;					/* already done */

1548 1549 1550 1551
	pgstat_setup_memcxt();

	localtable = (PgBackendStatus *)
		MemoryContextAlloc(pgStatLocalContext,
1552 1553 1554 1555
						   sizeof(PgBackendStatus) * MaxBackends);
	localNumBackends = 0;

	beentry = BackendStatusArray;
1556
	localentry = localtable;
1557 1558 1559
	for (i = 1; i <= MaxBackends; i++)
	{
		/*
B
Bruce Momjian 已提交
1560 1561 1562 1563 1564
		 * Follow the protocol of retrying if st_changecount changes while we
		 * copy the entry, or if it's odd.  (The check for odd is needed to
		 * cover the case where we are able to completely copy the entry while
		 * the source backend is between increment steps.)	We use a volatile
		 * pointer here to ensure the compiler doesn't try to get cute.
1565 1566 1567
		 */
		for (;;)
		{
B
Bruce Momjian 已提交
1568
			int			save_changecount = beentry->st_changecount;
1569 1570

			/*
B
Bruce Momjian 已提交
1571 1572
			 * XXX if PGBE_ACTIVITY_SIZE is really large, it might be best to
			 * use strcpy not memcpy for copying the activity string?
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
			 */
			memcpy(localentry, (char *) beentry, sizeof(PgBackendStatus));

			if (save_changecount == beentry->st_changecount &&
				(save_changecount & 1) == 0)
				break;

			/* Make sure we can break out of loop if stuck... */
			CHECK_FOR_INTERRUPTS();
		}

		beentry++;
		/* Only valid entries get included into the local array */
		if (localentry->st_procpid > 0)
		{
			localentry++;
			localNumBackends++;
		}
	}

1593 1594
	/* Set the pointer only after completion of a valid table */
	localBackendStatusTable = localtable;
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
}


/* ------------------------------------------------------------
 * Local support functions follow
 * ------------------------------------------------------------
 */


/* ----------
 * pgstat_setheader() -
 *
 *		Set common header fields in a statistics message
 * ----------
 */
static void
pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
{
	hdr->m_type = mtype;
}


/* ----------
 * pgstat_send() -
 *
 *		Send out one statistics message to the collector
 * ----------
 */
static void
pgstat_send(void *msg, int len)
{
1626 1627
	int			rc;

1628 1629
	if (pgStatSock < 0)
		return;
1630

1631
	((PgStat_MsgHdr *) msg)->m_size = len;
1632

1633 1634 1635 1636 1637 1638
	/* We'll retry after EINTR, but ignore all other failures */
	do
	{
		rc = send(pgStatSock, msg, len, 0);
	} while (rc < 0 && errno == EINTR);

1639
#ifdef USE_ASSERT_CHECKING
1640 1641
	/* In debug builds, log send failures ... */
	if (rc < 0)
1642 1643
		elog(LOG, "could not send to statistics collector: %m");
#endif
1644 1645 1646
}


1647 1648 1649
/* ----------
 * PgstatCollectorMain() -
 *
B
Bruce Momjian 已提交
1650
 *	Start up the statistics collector process.	This is the body of the
1651
 *	postmaster child process.
1652 1653 1654 1655
 *
 *	The argc/argv parameters are valid only in EXEC_BACKEND case.
 * ----------
 */
1656
NON_EXEC_STATIC void
1657
PgstatCollectorMain(int argc, char *argv[])
1658
{
1659 1660 1661
	struct itimerval write_timeout;
	bool		need_timer = false;
	int			len;
1662
	PgStat_Msg	msg;
B
Bruce Momjian 已提交
1663

1664
#ifndef WIN32
1665 1666 1667 1668
#ifdef HAVE_POLL
	struct pollfd input_fd;
#else
	struct timeval sel_timeout;
1669
	fd_set		rfds;
1670
#endif
1671 1672 1673
#endif

	IsUnderPostmaster = true;	/* we are a postmaster subprocess now */
1674

1675 1676
	MyProcPid = getpid();		/* reset MyProcPid */

1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
	/*
	 * If possible, make this process a group leader, so that the postmaster
	 * can signal any child processes too.  (pgstat probably never has
	 * any child processes, but for consistency we make all postmaster
	 * child processes do this.)
	 */
#ifdef HAVE_SETSID
	if (setsid() < 0)
		elog(FATAL, "setsid() failed: %m");
#endif

1688
	/*
1689 1690
	 * Ignore all signals usually bound to some action in the postmaster,
	 * except SIGQUIT and SIGALRM.
1691 1692 1693 1694
	 */
	pqsignal(SIGHUP, SIG_IGN);
	pqsignal(SIGINT, SIG_IGN);
	pqsignal(SIGTERM, SIG_IGN);
1695
	pqsignal(SIGQUIT, pgstat_exit);
1696
	pqsignal(SIGALRM, force_statwrite);
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
	pqsignal(SIGPIPE, SIG_IGN);
	pqsignal(SIGUSR1, SIG_IGN);
	pqsignal(SIGUSR2, SIG_IGN);
	pqsignal(SIGCHLD, SIG_DFL);
	pqsignal(SIGTTIN, SIG_DFL);
	pqsignal(SIGTTOU, SIG_DFL);
	pqsignal(SIGCONT, SIG_DFL);
	pqsignal(SIGWINCH, SIG_DFL);
	PG_SETMASK(&UnBlockSig);

1707 1708 1709
	/*
	 * Identify myself via ps
	 */
1710
	init_ps_display("stats collector process", "", "", "");
1711

1712 1713 1714
	/*
	 * Arrange to write the initial status file right away
	 */
1715 1716
	need_statwrite = true;

1717
	/* Preset the delay between status file writes */
1718 1719 1720
	MemSet(&write_timeout, 0, sizeof(struct itimerval));
	write_timeout.it_value.tv_sec = PGSTAT_STAT_INTERVAL / 1000;
	write_timeout.it_value.tv_usec = PGSTAT_STAT_INTERVAL % 1000;
1721

1722
	/*
B
Bruce Momjian 已提交
1723 1724
	 * Read in an existing statistics stats file or initialize the stats to
	 * zero.
1725
	 */
1726
	pgStatRunningInCollector = true;
1727
	pgStatDBHash = pgstat_read_statsfile(InvalidOid);
1728

1729
	/*
B
Bruce Momjian 已提交
1730 1731
	 * Setup the descriptor set for select(2).	Since only one bit in the set
	 * ever changes, we need not repeat FD_ZERO each time.
1732
	 */
1733
#if !defined(HAVE_POLL) && !defined(WIN32)
1734 1735
	FD_ZERO(&rfds);
#endif
1736

1737
	/*
1738 1739 1740
	 * Loop to process messages until we get SIGQUIT or detect ungraceful
	 * death of our parent postmaster.
	 *
B
Bruce Momjian 已提交
1741 1742
	 * For performance reasons, we don't want to do a PostmasterIsAlive() test
	 * after every message; instead, do it at statwrite time and if
1743
	 * select()/poll() is interrupted by timeout.
1744 1745 1746
	 */
	for (;;)
	{
B
Bruce Momjian 已提交
1747
		int			got_data;
1748 1749 1750 1751 1752 1753 1754

		/*
		 * Quit if we get SIGQUIT from the postmaster.
		 */
		if (need_exit)
			break;

1755
		/*
B
Bruce Momjian 已提交
1756
		 * If time to write the stats file, do so.	Note that the alarm
1757 1758 1759 1760
		 * interrupt isn't re-enabled immediately, but only after we next
		 * receive a stats message; so no cycles are wasted when there is
		 * nothing going on.
		 */
1761 1762
		if (need_statwrite)
		{
1763 1764 1765 1766
			/* Check for postmaster death; if so we'll write file below */
			if (!PostmasterIsAlive(true))
				break;

1767 1768 1769
			pgstat_write_statsfile();
			need_statwrite = false;
			need_timer = true;
1770 1771 1772
		}

		/*
1773 1774 1775
		 * Wait for a message to arrive; but not for more than
		 * PGSTAT_SELECT_TIMEOUT seconds. (This determines how quickly we will
		 * shut down after an ungraceful postmaster termination; so it needn't
B
Bruce Momjian 已提交
1776 1777 1778
		 * be very fast.  However, on some systems SIGQUIT won't interrupt the
		 * poll/select call, so this also limits speed of response to SIGQUIT,
		 * which is more important.)
1779
		 *
1780 1781
		 * We use poll(2) if available, otherwise select(2).
		 * Win32 has its own implementation.
1782
		 */
1783
#ifndef WIN32
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
#ifdef HAVE_POLL
		input_fd.fd = pgStatSock;
		input_fd.events = POLLIN | POLLERR;
		input_fd.revents = 0;

		if (poll(&input_fd, 1, PGSTAT_SELECT_TIMEOUT * 1000) < 0)
		{
			if (errno == EINTR)
				continue;
			ereport(ERROR,
					(errcode_for_socket_access(),
					 errmsg("poll() failed in statistics collector: %m")));
		}

		got_data = (input_fd.revents != 0);
#else							/* !HAVE_POLL */

		FD_SET(pgStatSock, &rfds);
1802 1803

		/*
1804 1805
		 * timeout struct is modified by select() on some operating systems,
		 * so re-fill it each time.
1806
		 */
1807 1808 1809 1810
		sel_timeout.tv_sec = PGSTAT_SELECT_TIMEOUT;
		sel_timeout.tv_usec = 0;

		if (select(pgStatSock + 1, &rfds, NULL, NULL, &sel_timeout) < 0)
1811
		{
1812 1813
			if (errno == EINTR)
				continue;
1814
			ereport(ERROR,
1815
					(errcode_for_socket_access(),
B
Bruce Momjian 已提交
1816
					 errmsg("select() failed in statistics collector: %m")));
1817 1818
		}

1819 1820
		got_data = FD_ISSET(pgStatSock, &rfds);
#endif   /* HAVE_POLL */
1821 1822 1823 1824
#else /* WIN32 */
		got_data = pgwin32_waitforsinglesocket(pgStatSock, FD_READ,
											   PGSTAT_SELECT_TIMEOUT*1000);
#endif
1825

1826
		/*
1827 1828
		 * If there is a message on the socket, read it and check for
		 * validity.
1829
		 */
1830
		if (got_data)
1831
		{
1832 1833 1834
			len = recv(pgStatSock, (char *) &msg,
					   sizeof(PgStat_Msg), 0);
			if (len < 0)
1835 1836 1837
			{
				if (errno == EINTR)
					continue;
1838 1839 1840
				ereport(ERROR,
						(errcode_for_socket_access(),
						 errmsg("could not read statistics message: %m")));
1841
			}
1842

1843
			/*
1844
			 * We ignore messages that are smaller than our common header
1845
			 */
1846 1847
			if (len < sizeof(PgStat_MsgHdr))
				continue;
1848

1849
			/*
1850
			 * The received length must match the length in the header
1851
			 */
1852 1853
			if (msg.msg_hdr.m_size != len)
				continue;
1854 1855

			/*
1856
			 * O.K. - we accept this message.  Process it.
1857 1858 1859 1860 1861 1862 1863
			 */
			switch (msg.msg_hdr.m_type)
			{
				case PGSTAT_MTYPE_DUMMY:
					break;

				case PGSTAT_MTYPE_TABSTAT:
1864
					pgstat_recv_tabstat((PgStat_MsgTabstat *) &msg, len);
1865 1866 1867
					break;

				case PGSTAT_MTYPE_TABPURGE:
1868
					pgstat_recv_tabpurge((PgStat_MsgTabpurge *) &msg, len);
1869 1870 1871
					break;

				case PGSTAT_MTYPE_DROPDB:
1872
					pgstat_recv_dropdb((PgStat_MsgDropdb *) &msg, len);
1873 1874 1875
					break;

				case PGSTAT_MTYPE_RESETCOUNTER:
1876
					pgstat_recv_resetcounter((PgStat_MsgResetcounter *) &msg,
1877
											 len);
1878 1879
					break;

1880
				case PGSTAT_MTYPE_AUTOVAC_START:
1881
					pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
1882 1883 1884
					break;

				case PGSTAT_MTYPE_VACUUM:
1885
					pgstat_recv_vacuum((PgStat_MsgVacuum *) &msg, len);
1886 1887 1888
					break;

				case PGSTAT_MTYPE_ANALYZE:
1889
					pgstat_recv_analyze((PgStat_MsgAnalyze *) &msg, len);
1890 1891
					break;

1892 1893 1894 1895
				default:
					break;
			}

1896 1897 1898 1899 1900
			/*
			 * If this is the first message after we wrote the stats file the
			 * last time, enable the alarm interrupt to make it be written
			 * again later.
			 */
1901
			if (need_timer)
1902
			{
1903
				if (setitimer(ITIMER_REAL, &write_timeout, NULL))
1904
					ereport(ERROR,
B
Bruce Momjian 已提交
1905
					(errmsg("could not set statistics collector timer: %m")));
1906
				need_timer = false;
1907
			}
1908 1909 1910 1911
		}
		else
		{
			/*
B
Bruce Momjian 已提交
1912 1913
			 * We can only get here if the select/poll timeout elapsed. Check
			 * for postmaster death.
1914
			 */
1915 1916
			if (!PostmasterIsAlive(true))
				break;
1917
		}
B
Bruce Momjian 已提交
1918
	}							/* end of message-processing loop */
1919

1920 1921 1922 1923
	/*
	 * Save the final stats to reuse at next startup.
	 */
	pgstat_write_statsfile();
1924

1925
	exit(0);
1926 1927
}

1928 1929

/* SIGQUIT signal handler for collector process */
1930 1931 1932
static void
pgstat_exit(SIGNAL_ARGS)
{
1933
	need_exit = true;
1934 1935
}

1936
/* SIGALRM signal handler for collector process */
1937
static void
1938
force_statwrite(SIGNAL_ARGS)
1939
{
1940
	need_statwrite = true;
1941 1942
}

1943

1944 1945
/*
 * Lookup the hash table entry for the specified database. If no hash
1946 1947
 * table entry exists, initialize it, if the create parameter is true.
 * Else, return NULL.
1948 1949
 */
static PgStat_StatDBEntry *
1950
pgstat_get_db_entry(Oid databaseid, bool create)
1951 1952
{
	PgStat_StatDBEntry *result;
B
Bruce Momjian 已提交
1953 1954
	bool		found;
	HASHACTION	action = (create ? HASH_ENTER : HASH_FIND);
1955 1956 1957 1958

	/* Lookup or create the hash table entry for this database */
	result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
												&databaseid,
1959 1960 1961 1962
												action, &found);

	if (!create && !found)
		return NULL;
1963

1964
	/* If not found, initialize the new one. */
1965 1966
	if (!found)
	{
1967
		HASHCTL		hash_ctl;
1968

1969 1970 1971 1972 1973
		result->tables = NULL;
		result->n_xact_commit = 0;
		result->n_xact_rollback = 0;
		result->n_blocks_fetched = 0;
		result->n_blocks_hit = 0;
1974
		result->last_autovac_time = 0;
1975 1976

		memset(&hash_ctl, 0, sizeof(hash_ctl));
1977
		hash_ctl.keysize = sizeof(Oid);
1978
		hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
1979
		hash_ctl.hash = oid_hash;
1980
		result->tables = hash_create("Per-database table",
B
Bruce Momjian 已提交
1981 1982 1983
									 PGSTAT_TAB_HASH_SIZE,
									 &hash_ctl,
									 HASH_ELEM | HASH_FUNCTION);
1984 1985
	}

1986
	return result;
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
}


/* ----------
 * pgstat_write_statsfile() -
 *
 *	Tell the news.
 * ----------
 */
static void
pgstat_write_statsfile(void)
{
1999 2000 2001 2002 2003
	HASH_SEQ_STATUS hstat;
	HASH_SEQ_STATUS tstat;
	PgStat_StatDBEntry *dbentry;
	PgStat_StatTabEntry *tabentry;
	FILE	   *fpout;
2004
	int32		format_id;
2005 2006

	/*
2007
	 * Open the statistics temp file to write out the current values.
2008
	 */
2009
	fpout = fopen(PGSTAT_STAT_TMPFILE, PG_BINARY_W);
2010 2011
	if (fpout == NULL)
	{
2012 2013
		ereport(LOG,
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
2014 2015
				 errmsg("could not open temporary statistics file \"%s\": %m",
						PGSTAT_STAT_TMPFILE)));
2016 2017 2018
		return;
	}

2019 2020 2021 2022 2023 2024
	/*
	 * Write the file header --- currently just a format ID.
	 */
	format_id = PGSTAT_FILE_FORMAT_ID;
	fwrite(&format_id, sizeof(format_id), 1, fpout);

2025 2026 2027 2028
	/*
	 * Walk through the database table.
	 */
	hash_seq_init(&hstat, pgStatDBHash);
2029
	while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
2030 2031
	{
		/*
B
Bruce Momjian 已提交
2032 2033 2034
		 * Write out the DB entry including the number of live backends. We
		 * don't write the tables pointer since it's of no use to any other
		 * process.
2035 2036
		 */
		fputc('D', fpout);
2037
		fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
2038 2039

		/*
2040
		 * Walk through the database's access stats per table.
2041 2042
		 */
		hash_seq_init(&tstat, dbentry->tables);
2043
		while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
2044 2045 2046 2047
		{
			fputc('T', fpout);
			fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
		}
2048

2049 2050 2051 2052 2053 2054 2055
		/*
		 * Mark the end of this DB
		 */
		fputc('d', fpout);
	}

	/*
2056
	 * No more output to be done. Close the temp file and replace the old
2057 2058
	 * pgstat.stat with it.  The ferror() check replaces testing for error
	 * after each individual fputc or fwrite above.
2059 2060
	 */
	fputc('E', fpout);
2061 2062 2063 2064 2065

	if (ferror(fpout))
	{
		ereport(LOG,
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
2066 2067
			   errmsg("could not write temporary statistics file \"%s\": %m",
					  PGSTAT_STAT_TMPFILE)));
2068 2069 2070 2071
		fclose(fpout);
		unlink(PGSTAT_STAT_TMPFILE);
	}
	else if (fclose(fpout) < 0)
2072
	{
2073 2074
		ereport(LOG,
				(errcode_for_file_access(),
B
Bruce Momjian 已提交
2075 2076
			   errmsg("could not close temporary statistics file \"%s\": %m",
					  PGSTAT_STAT_TMPFILE)));
2077
		unlink(PGSTAT_STAT_TMPFILE);
2078
	}
2079
	else if (rename(PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME) < 0)
2080
	{
2081 2082 2083 2084 2085
		ereport(LOG,
				(errcode_for_file_access(),
				 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
						PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME)));
		unlink(PGSTAT_STAT_TMPFILE);
2086 2087 2088 2089 2090 2091 2092
	}
}


/* ----------
 * pgstat_read_statsfile() -
 *
2093 2094
 *	Reads in an existing statistics collector file and initializes the
 *	databases' hash table (whose entries point to the tables' hash tables).
2095 2096
 * ----------
 */
2097 2098
static HTAB *
pgstat_read_statsfile(Oid onlydb)
2099
{
2100 2101 2102 2103 2104
	PgStat_StatDBEntry *dbentry;
	PgStat_StatDBEntry dbbuf;
	PgStat_StatTabEntry *tabentry;
	PgStat_StatTabEntry tabbuf;
	HASHCTL		hash_ctl;
2105
	HTAB	   *dbhash;
2106 2107
	HTAB	   *tabhash = NULL;
	FILE	   *fpin;
2108
	int32		format_id;
2109 2110 2111
	bool		found;

	/*
2112
	 * The tables will live in pgStatLocalContext.
2113
	 */
2114
	pgstat_setup_memcxt();
2115 2116 2117 2118 2119

	/*
	 * Create the DB hashtable
	 */
	memset(&hash_ctl, 0, sizeof(hash_ctl));
2120
	hash_ctl.keysize = sizeof(Oid);
2121
	hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
2122
	hash_ctl.hash = oid_hash;
2123 2124 2125
	hash_ctl.hcxt = pgStatLocalContext;
	dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
						 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
2126 2127

	/*
B
Bruce Momjian 已提交
2128 2129 2130
	 * Try to open the status file. If it doesn't exist, the backends simply
	 * return zero for anything and the collector simply starts from scratch
	 * with empty counters.
2131
	 */
2132
	if ((fpin = AllocateFile(PGSTAT_STAT_FILENAME, PG_BINARY_R)) == NULL)
2133
		return dbhash;
2134

2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145
	/*
	 * Verify it's of the expected format.
	 */
	if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id)
		|| format_id != PGSTAT_FILE_FORMAT_ID)
	{
		ereport(pgStatRunningInCollector ? LOG : WARNING,
				(errmsg("corrupted pgstat.stat file")));
		goto done;
	}

2146
	/*
2147 2148
	 * We found an existing collector stats file. Read it and put all the
	 * hashtable entries into place.
2149 2150 2151 2152 2153
	 */
	for (;;)
	{
		switch (fgetc(fpin))
		{
2154 2155
				/*
				 * 'D'	A PgStat_StatDBEntry struct describing a database
B
Bruce Momjian 已提交
2156 2157
				 * follows. Subsequently, zero to many 'T' entries will follow
				 * until a 'd' is encountered.
2158
				 */
2159
			case 'D':
2160 2161
				if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
						  fpin) != offsetof(PgStat_StatDBEntry, tables))
2162
				{
2163 2164
					ereport(pgStatRunningInCollector ? LOG : WARNING,
							(errmsg("corrupted pgstat.stat file")));
2165
					goto done;
2166 2167 2168 2169 2170
				}

				/*
				 * Add to the DB hash
				 */
2171
				dbentry = (PgStat_StatDBEntry *) hash_search(dbhash,
B
Bruce Momjian 已提交
2172
												  (void *) &dbbuf.databaseid,
2173 2174
															 HASH_ENTER,
															 &found);
2175 2176
				if (found)
				{
2177 2178
					ereport(pgStatRunningInCollector ? LOG : WARNING,
							(errmsg("corrupted pgstat.stat file")));
2179
					goto done;
2180 2181 2182
				}

				memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
2183
				dbentry->tables = NULL;
2184 2185

				/*
2186 2187
				 * Don't collect tables if not the requested DB (or the
				 * shared-table info)
2188
				 */
2189 2190 2191 2192
				if (onlydb != InvalidOid)
				{
					if (dbbuf.databaseid != onlydb &&
						dbbuf.databaseid != InvalidOid)
B
Bruce Momjian 已提交
2193
						break;
2194
				}
2195 2196

				memset(&hash_ctl, 0, sizeof(hash_ctl));
2197
				hash_ctl.keysize = sizeof(Oid);
2198
				hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2199
				hash_ctl.hash = oid_hash;
2200
				hash_ctl.hcxt = pgStatLocalContext;
2201 2202 2203
				dbentry->tables = hash_create("Per-database table",
											  PGSTAT_TAB_HASH_SIZE,
											  &hash_ctl,
2204
									 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
2205 2206

				/*
2207
				 * Arrange that following 'T's add entries to this database's
B
Bruce Momjian 已提交
2208
				 * tables hash table.
2209 2210 2211 2212
				 */
				tabhash = dbentry->tables;
				break;

2213 2214 2215
				/*
				 * 'd'	End of this database.
				 */
2216 2217 2218 2219
			case 'd':
				tabhash = NULL;
				break;

2220 2221 2222
				/*
				 * 'T'	A PgStat_StatTabEntry follows.
				 */
2223
			case 'T':
2224 2225
				if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
						  fpin) != sizeof(PgStat_StatTabEntry))
2226
				{
2227 2228
					ereport(pgStatRunningInCollector ? LOG : WARNING,
							(errmsg("corrupted pgstat.stat file")));
2229
					goto done;
2230 2231 2232 2233 2234 2235 2236 2237
				}

				/*
				 * Skip if table belongs to a not requested database.
				 */
				if (tabhash == NULL)
					break;

2238
				tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
B
Bruce Momjian 已提交
2239 2240
													(void *) &tabbuf.tableid,
														 HASH_ENTER, &found);
2241 2242 2243

				if (found)
				{
2244 2245
					ereport(pgStatRunningInCollector ? LOG : WARNING,
							(errmsg("corrupted pgstat.stat file")));
2246
					goto done;
2247 2248 2249 2250 2251
				}

				memcpy(tabentry, &tabbuf, sizeof(tabbuf));
				break;

2252
				/*
2253
				 * 'E'	The EOF marker of a complete stats file.
2254
				 */
2255 2256
			case 'E':
				goto done;
2257

2258 2259 2260 2261 2262 2263
			default:
				ereport(pgStatRunningInCollector ? LOG : WARNING,
						(errmsg("corrupted pgstat.stat file")));
				goto done;
		}
	}
2264

2265 2266
done:
	FreeFile(fpin);
2267 2268

	return dbhash;
2269
}
2270

2271
/*
2272 2273 2274
 * If not already done, read the statistics collector stats file into
 * some hash tables.  The results will be kept until pgstat_clear_snapshot()
 * is called (typically, at end of transaction).
2275 2276 2277 2278
 */
static void
backend_read_statsfile(void)
{
2279 2280 2281 2282 2283 2284
	/* already read it? */
	if (pgStatDBHash)
		return;
	Assert(!pgStatRunningInCollector);

	/* Autovacuum wants stats about all databases */
2285
	if (IsAutoVacuumProcess())
2286
		pgStatDBHash = pgstat_read_statsfile(InvalidOid);
2287
	else
2288 2289
		pgStatDBHash = pgstat_read_statsfile(MyDatabaseId);
}
2290

2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306

/* ----------
 * pgstat_setup_memcxt() -
 *
 *	Create pgStatLocalContext, if not already done.
 * ----------
 */
static void
pgstat_setup_memcxt(void)
{
	if (!pgStatLocalContext)
		pgStatLocalContext = AllocSetContextCreate(TopMemoryContext,
												   "Statistics snapshot",
												   ALLOCSET_SMALL_MINSIZE,
												   ALLOCSET_SMALL_INITSIZE,
												   ALLOCSET_SMALL_MAXSIZE);
2307 2308
}

2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338

/* ----------
 * pgstat_clear_snapshot() -
 *
 *	Discard any data collected in the current transaction.  Any subsequent
 *	request will cause new snapshots to be read.
 *
 *	This is also invoked during transaction commit or abort to discard
 *	the no-longer-wanted snapshot.
 * ----------
 */
void
pgstat_clear_snapshot(void)
{
	/* In an autovacuum process we keep the stats forever */
	if (IsAutoVacuumProcess())
		return;

	/* Release memory, if any was allocated */
	if (pgStatLocalContext)
		MemoryContextDelete(pgStatLocalContext);

	/* Reset variables */
	pgStatLocalContext = NULL;
	pgStatDBHash = NULL;
	localBackendStatusTable = NULL;
	localNumBackends = 0;
}


2339 2340 2341 2342 2343 2344 2345 2346 2347
/* ----------
 * pgstat_recv_tabstat() -
 *
 *	Count what the backend has done.
 * ----------
 */
static void
pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
{
2348 2349 2350 2351 2352
	PgStat_TableEntry *tabmsg = &(msg->m_entry[0]);
	PgStat_StatDBEntry *dbentry;
	PgStat_StatTabEntry *tabentry;
	int			i;
	bool		found;
2353

2354
	dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
2355 2356

	/*
2357
	 * Update database-wide stats.
2358
	 */
2359 2360
	dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
	dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
2361 2362 2363 2364 2365 2366

	/*
	 * Process all table entries in the message.
	 */
	for (i = 0; i < msg->m_nentries; i++)
	{
2367
		tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
B
Bruce Momjian 已提交
2368 2369
												  (void *) &(tabmsg[i].t_id),
													   HASH_ENTER, &found);
2370 2371 2372 2373

		if (!found)
		{
			/*
B
Bruce Momjian 已提交
2374 2375
			 * If it's a new table entry, initialize counters to the values we
			 * just got.
2376
			 */
2377 2378 2379 2380 2381 2382
			tabentry->numscans = tabmsg[i].t_numscans;
			tabentry->tuples_returned = tabmsg[i].t_tuples_returned;
			tabentry->tuples_fetched = tabmsg[i].t_tuples_fetched;
			tabentry->tuples_inserted = tabmsg[i].t_tuples_inserted;
			tabentry->tuples_updated = tabmsg[i].t_tuples_updated;
			tabentry->tuples_deleted = tabmsg[i].t_tuples_deleted;
B
Bruce Momjian 已提交
2383

2384 2385 2386
			tabentry->n_live_tuples = tabmsg[i].t_tuples_inserted;
			tabentry->n_dead_tuples = tabmsg[i].t_tuples_updated +
				tabmsg[i].t_tuples_deleted;
2387
			tabentry->last_anl_tuples = 0;
2388 2389 2390 2391
			tabentry->vacuum_timestamp = 0;
			tabentry->autovac_vacuum_timestamp = 0;
			tabentry->analyze_timestamp = 0;
			tabentry->autovac_analyze_timestamp = 0;
2392 2393 2394

			tabentry->blocks_fetched = tabmsg[i].t_blocks_fetched;
			tabentry->blocks_hit = tabmsg[i].t_blocks_hit;
2395 2396 2397 2398 2399 2400
		}
		else
		{
			/*
			 * Otherwise add the values to the existing entry.
			 */
2401 2402 2403 2404 2405 2406
			tabentry->numscans += tabmsg[i].t_numscans;
			tabentry->tuples_returned += tabmsg[i].t_tuples_returned;
			tabentry->tuples_fetched += tabmsg[i].t_tuples_fetched;
			tabentry->tuples_inserted += tabmsg[i].t_tuples_inserted;
			tabentry->tuples_updated += tabmsg[i].t_tuples_updated;
			tabentry->tuples_deleted += tabmsg[i].t_tuples_deleted;
2407

2408 2409
			tabentry->n_live_tuples += tabmsg[i].t_tuples_inserted -
				tabmsg[i].t_tuples_deleted;
2410 2411
			tabentry->n_dead_tuples += tabmsg[i].t_tuples_updated +
				tabmsg[i].t_tuples_deleted;
2412 2413 2414

			tabentry->blocks_fetched += tabmsg[i].t_blocks_fetched;
			tabentry->blocks_hit += tabmsg[i].t_blocks_hit;
2415 2416 2417 2418 2419
		}

		/*
		 * And add the block IO to the database entry.
		 */
2420 2421
		dbentry->n_blocks_fetched += tabmsg[i].t_blocks_fetched;
		dbentry->n_blocks_hit += tabmsg[i].t_blocks_hit;
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434
	}
}


/* ----------
 * pgstat_recv_tabpurge() -
 *
 *	Arrange for dead table removal.
 * ----------
 */
static void
pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
{
2435 2436
	PgStat_StatDBEntry *dbentry;
	int			i;
2437

2438 2439 2440 2441 2442 2443 2444
	dbentry = pgstat_get_db_entry(msg->m_databaseid, false);

	/*
	 * No need to purge if we don't even know the database.
	 */
	if (!dbentry || !dbentry->tables)
		return;
2445 2446 2447 2448 2449 2450

	/*
	 * Process all table entries in the message.
	 */
	for (i = 0; i < msg->m_nentries; i++)
	{
2451 2452 2453 2454
		/* Remove from hashtable if present; we don't care if it's not. */
		(void) hash_search(dbentry->tables,
						   (void *) &(msg->m_tableid[i]),
						   HASH_REMOVE, NULL);
2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467
	}
}


/* ----------
 * pgstat_recv_dropdb() -
 *
 *	Arrange for dead database removal
 * ----------
 */
static void
pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len)
{
2468
	PgStat_StatDBEntry *dbentry;
2469 2470 2471 2472

	/*
	 * Lookup the database in the hashtable.
	 */
2473
	dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2474 2475

	/*
2476
	 * If found, remove it.
2477
	 */
2478
	if (dbentry)
2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489
	{
		if (dbentry->tables != NULL)
			hash_destroy(dbentry->tables);

		if (hash_search(pgStatDBHash,
						(void *) &(dbentry->databaseid),
						HASH_REMOVE, NULL) == NULL)
			ereport(ERROR,
					(errmsg("database hash table corrupted "
							"during cleanup --- abort")));
	}
2490 2491 2492 2493
}


/* ----------
2494
 * pgstat_recv_resetcounter() -
2495
 *
2496
 *	Reset the statistics for the specified database.
2497 2498 2499 2500 2501
 * ----------
 */
static void
pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
{
2502 2503
	HASHCTL		hash_ctl;
	PgStat_StatDBEntry *dbentry;
2504 2505

	/*
2506
	 * Lookup the database in the hashtable.  Nothing to do if not there.
2507
	 */
2508 2509 2510 2511
	dbentry = pgstat_get_db_entry(msg->m_databaseid, false);

	if (!dbentry)
		return;
2512 2513

	/*
B
Bruce Momjian 已提交
2514 2515
	 * We simply throw away all the database's table entries by recreating a
	 * new hash table for them.
2516 2517 2518 2519
	 */
	if (dbentry->tables != NULL)
		hash_destroy(dbentry->tables);

2520 2521 2522 2523 2524
	dbentry->tables = NULL;
	dbentry->n_xact_commit = 0;
	dbentry->n_xact_rollback = 0;
	dbentry->n_blocks_fetched = 0;
	dbentry->n_blocks_hit = 0;
2525 2526

	memset(&hash_ctl, 0, sizeof(hash_ctl));
2527
	hash_ctl.keysize = sizeof(Oid);
2528
	hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2529
	hash_ctl.hash = oid_hash;
2530 2531 2532 2533
	dbentry->tables = hash_create("Per-database table",
								  PGSTAT_TAB_HASH_SIZE,
								  &hash_ctl,
								  HASH_ELEM | HASH_FUNCTION);
2534
}
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589

/* ----------
 * pgstat_recv_autovac() -
 *
 *	Process an autovacuum signalling message.
 * ----------
 */
static void
pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len)
{
	PgStat_StatDBEntry *dbentry;

	/*
	 * Lookup the database in the hashtable.  Don't create the entry if it
	 * doesn't exist, because autovacuum may be processing a template
	 * database.  If this isn't the case, the database is most likely to have
	 * an entry already.  (If it doesn't, not much harm is done anyway --
	 * it'll get created as soon as somebody actually uses the database.)
	 */
	dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
	if (dbentry == NULL)
		return;

	/*
	 * Store the last autovacuum time in the database entry.
	 */
	dbentry->last_autovac_time = msg->m_start_time;
}

/* ----------
 * pgstat_recv_vacuum() -
 *
 *	Process a VACUUM message.
 * ----------
 */
static void
pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len)
{
	PgStat_StatDBEntry *dbentry;
	PgStat_StatTabEntry *tabentry;

	/*
	 * Don't create either the database or table entry if it doesn't already
	 * exist.  This avoids bloating the stats with entries for stuff that is
	 * only touched by vacuum and not by live operations.
	 */
	dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
	if (dbentry == NULL)
		return;

	tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
						   HASH_FIND, NULL);
	if (tabentry == NULL)
		return;

B
Bruce Momjian 已提交
2590
	if (msg->m_autovacuum)
2591
		tabentry->autovac_vacuum_timestamp = msg->m_vacuumtime;
B
Bruce Momjian 已提交
2592 2593
	else
		tabentry->vacuum_timestamp = msg->m_vacuumtime;
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603
	tabentry->n_live_tuples = msg->m_tuples;
	tabentry->n_dead_tuples = 0;
	if (msg->m_analyze)
	{
		tabentry->last_anl_tuples = msg->m_tuples;
		if (msg->m_autovacuum)
			tabentry->autovac_analyze_timestamp = msg->m_vacuumtime;
		else
			tabentry->analyze_timestamp = msg->m_vacuumtime;
	}
2604 2605 2606 2607 2608 2609
	else
	{
		/* last_anl_tuples must never exceed n_live_tuples */
		tabentry->last_anl_tuples = Min(tabentry->last_anl_tuples,
										msg->m_tuples);
	}
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
}

/* ----------
 * pgstat_recv_analyze() -
 *
 *	Process an ANALYZE message.
 * ----------
 */
static void
pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
{
	PgStat_StatDBEntry *dbentry;
	PgStat_StatTabEntry *tabentry;

	/*
	 * Don't create either the database or table entry if it doesn't already
	 * exist.  This avoids bloating the stats with entries for stuff that is
	 * only touched by analyze and not by live operations.
	 */
	dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
	if (dbentry == NULL)
		return;

	tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
						   HASH_FIND, NULL);
	if (tabentry == NULL)
		return;

B
Bruce Momjian 已提交
2638
	if (msg->m_autovacuum)
2639
		tabentry->autovac_analyze_timestamp = msg->m_analyzetime;
B
Bruce Momjian 已提交
2640
	else
2641 2642 2643 2644 2645
		tabentry->analyze_timestamp = msg->m_analyzetime;
	tabentry->n_live_tuples = msg->m_live_tuples;
	tabentry->n_dead_tuples = msg->m_dead_tuples;
	tabentry->last_anl_tuples = msg->m_live_tuples + msg->m_dead_tuples;
}