4 * All the statistics collector stuff hacked up in one big, ugly file.
6 * TODO: - Separate collector, postmaster and backend stuff
7 * into different files.
9 * - Add some automatic call for pgstat vacuuming.
11 * - Add a pgstat config column to pg_database, so this
12 * entire thing can be enabled/disabled on a per db basis.
14 * Copyright (c) 2001-2006, PostgreSQL Global Development Group
16 * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.141 2006/12/06 18:06:47 neilc Exp $
23 #include <sys/param.h>
25 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
34 #ifdef HAVE_SYS_POLL_H
40 #include "access/heapam.h"
41 #include "access/transam.h"
42 #include "access/xact.h"
43 #include "catalog/pg_database.h"
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 #include "storage/pmsignal.h"
57 #include "utils/memutils.h"
58 #include "utils/ps_status.h"
62 * Paths for the statistics files (relative to installation's $PGDATA).
65 #define PGSTAT_STAT_FILENAME "global/pgstat.stat"
66 #define PGSTAT_STAT_TMPFILE "global/pgstat.tmp"
72 #define PGSTAT_STAT_INTERVAL 500 /* How often to write the status file;
75 #define PGSTAT_RESTART_INTERVAL 60 /* How often to attempt to restart a
76 * failed statistics collector; in
79 #define PGSTAT_SELECT_TIMEOUT 2 /* How often to check for postmaster
80 * death; in seconds. */
84 * The initial size hints for the hash tables used in the collector.
87 #define PGSTAT_DB_HASH_SIZE 16
88 #define PGSTAT_TAB_HASH_SIZE 512
95 bool pgstat_collect_startcollector = true;
96 bool pgstat_collect_resetonpmstart = false;
97 bool pgstat_collect_tuplelevel = false;
98 bool pgstat_collect_blocklevel = false;
99 bool pgstat_collect_querystring = false;
105 NON_EXEC_STATIC int pgStatSock = -1;
107 static struct sockaddr_storage pgStatAddr;
109 static time_t last_pgstat_start_time;
111 static bool pgStatRunningInCollector = false;
114 * Place where backends store per-table info to be sent to the collector.
115 * We store shared relations separately from non-shared ones, to be able to
116 * send them in separate messages.
118 typedef struct TabStatArray
120 int tsa_alloc; /* num allocated */
121 int tsa_used; /* num actually used */
122 PgStat_MsgTabstat **tsa_messages; /* the array itself */
125 #define TABSTAT_QUANTUM 4 /* we alloc this many at a time */
127 static TabStatArray RegularTabStat = {0, 0, NULL};
128 static TabStatArray SharedTabStat = {0, 0, NULL};
130 static int pgStatXactCommit = 0;
131 static int pgStatXactRollback = 0;
133 static TransactionId pgStatDBHashXact = InvalidTransactionId;
134 static HTAB *pgStatDBHash = NULL;
135 static TransactionId pgStatLocalStatusXact = InvalidTransactionId;
136 static PgBackendStatus *localBackendStatusTable = NULL;
137 static int localNumBackends = 0;
139 static volatile bool need_exit = false;
140 static volatile bool need_statwrite = false;
144 * Local function forward declarations
148 static pid_t pgstat_forkexec(void);
151 NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]);
152 static void pgstat_exit(SIGNAL_ARGS);
153 static void force_statwrite(SIGNAL_ARGS);
154 static void pgstat_beshutdown_hook(int code, Datum arg);
156 static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
157 static void pgstat_drop_database(Oid databaseid);
158 static void pgstat_write_statsfile(void);
159 static void pgstat_read_statsfile(HTAB **dbhash, Oid onlydb);
160 static void backend_read_statsfile(void);
161 static void pgstat_read_current_status(void);
163 static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype);
164 static void pgstat_send(void *msg, int len);
166 static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
167 static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
168 static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
169 static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
170 static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
171 static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
172 static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
175 /* ------------------------------------------------------------
176 * Public functions called from postmaster follow
177 * ------------------------------------------------------------
183 * Called from postmaster at startup. Create the resources required
184 * by the statistics collector process. If unable to do so, do not
185 * fail --- better to let the postmaster start with stats collection
192 ACCEPT_TYPE_ARG3 alen;
193 struct addrinfo *addrs = NULL,
203 #define TESTBYTEVAL ((char) 199)
206 * Force start of collector daemon if something to collect. Note that
207 * pgstat_collect_querystring is now an independent facility that does not
208 * require the collector daemon.
210 if (pgstat_collect_tuplelevel ||
211 pgstat_collect_blocklevel)
212 pgstat_collect_startcollector = true;
215 * If we don't have to start a collector or should reset the collected
216 * statistics on postmaster start, simply remove the stats file.
218 if (!pgstat_collect_startcollector || pgstat_collect_resetonpmstart)
222 * Nothing else required if collector will not get started
224 if (!pgstat_collect_startcollector)
228 * Create the UDP socket for sending and receiving statistic messages
230 hints.ai_flags = AI_PASSIVE;
231 hints.ai_family = PF_UNSPEC;
232 hints.ai_socktype = SOCK_DGRAM;
233 hints.ai_protocol = 0;
234 hints.ai_addrlen = 0;
235 hints.ai_addr = NULL;
236 hints.ai_canonname = NULL;
237 hints.ai_next = NULL;
238 ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
242 (errmsg("could not resolve \"localhost\": %s",
243 gai_strerror(ret))));
248 * On some platforms, pg_getaddrinfo_all() may return multiple addresses
249 * only one of which will actually work (eg, both IPv6 and IPv4 addresses
250 * when kernel will reject IPv6). Worse, the failure may occur at the
251 * bind() or perhaps even connect() stage. So we must loop through the
252 * results till we find a working combination. We will generate LOG
253 * messages, but no error, for bogus combinations.
255 for (addr = addrs; addr; addr = addr->ai_next)
257 #ifdef HAVE_UNIX_SOCKETS
258 /* Ignore AF_UNIX sockets, if any are returned. */
259 if (addr->ai_family == AF_UNIX)
265 (errmsg("trying another address for the statistics collector")));
270 if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) < 0)
273 (errcode_for_socket_access(),
274 errmsg("could not create socket for statistics collector: %m")));
279 * Bind it to a kernel assigned port on localhost and get the assigned
280 * port via getsockname().
282 if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
285 (errcode_for_socket_access(),
286 errmsg("could not bind socket for statistics collector: %m")));
287 closesocket(pgStatSock);
292 alen = sizeof(pgStatAddr);
293 if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
296 (errcode_for_socket_access(),
297 errmsg("could not get address of socket for statistics collector: %m")));
298 closesocket(pgStatSock);
304 * Connect the socket to its own address. This saves a few cycles by
305 * not having to respecify the target address on every send. This also
306 * provides a kernel-level check that only packets from this same
307 * address will be received.
309 if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
312 (errcode_for_socket_access(),
313 errmsg("could not connect socket for statistics collector: %m")));
314 closesocket(pgStatSock);
320 * Try to send and receive a one-byte test message on the socket. This
321 * is to catch situations where the socket can be created but will not
322 * actually pass data (for instance, because kernel packet filtering
325 test_byte = TESTBYTEVAL;
328 if (send(pgStatSock, &test_byte, 1, 0) != 1)
331 goto retry1; /* if interrupted, just retry */
333 (errcode_for_socket_access(),
334 errmsg("could not send test message on socket for statistics collector: %m")));
335 closesocket(pgStatSock);
341 * There could possibly be a little delay before the message can be
342 * received. We arbitrarily allow up to half a second before deciding
345 for (;;) /* need a loop to handle EINTR */
348 FD_SET(pgStatSock, &rset);
351 sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
352 if (sel_res >= 0 || errno != EINTR)
358 (errcode_for_socket_access(),
359 errmsg("select() failed in statistics collector: %m")));
360 closesocket(pgStatSock);
364 if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
367 * This is the case we actually think is likely, so take pains to
368 * give a specific message for it.
370 * errno will not be set meaningfully here, so don't use it.
373 (errcode(ERRCODE_CONNECTION_FAILURE),
374 errmsg("test message did not get through on socket for statistics collector")));
375 closesocket(pgStatSock);
380 test_byte++; /* just make sure variable is changed */
383 if (recv(pgStatSock, &test_byte, 1, 0) != 1)
386 goto retry2; /* if interrupted, just retry */
388 (errcode_for_socket_access(),
389 errmsg("could not receive test message on socket for statistics collector: %m")));
390 closesocket(pgStatSock);
395 if (test_byte != TESTBYTEVAL) /* strictly paranoia ... */
398 (errcode(ERRCODE_INTERNAL_ERROR),
399 errmsg("incorrect test message transmission on socket for statistics collector")));
400 closesocket(pgStatSock);
405 /* If we get here, we have a working socket */
409 /* Did we find a working address? */
410 if (!addr || pgStatSock < 0)
414 * Set the socket to non-blocking IO. This ensures that if the collector
415 * falls behind, statistics messages will be discarded; backends won't
416 * block waiting to send messages to the collector.
418 if (!pg_set_noblock(pgStatSock))
421 (errcode_for_socket_access(),
422 errmsg("could not set statistics collector socket to nonblocking mode: %m")));
426 pg_freeaddrinfo_all(hints.ai_family, addrs);
432 (errmsg("disabling statistics collector for lack of working socket")));
435 pg_freeaddrinfo_all(hints.ai_family, addrs);
438 closesocket(pgStatSock);
441 /* Adjust GUC variables to suppress useless activity */
442 pgstat_collect_startcollector = false;
443 pgstat_collect_tuplelevel = false;
444 pgstat_collect_blocklevel = false;
448 * pgstat_reset_all() -
450 * Remove the stats file. This is used on server start if the
451 * stats_reset_on_server_start feature is enabled, or if WAL
452 * recovery is needed after a crash.
455 pgstat_reset_all(void)
457 unlink(PGSTAT_STAT_FILENAME);
463 * pgstat_forkexec() -
465 * Format up the arglist for, then fork and exec, statistics collector process
468 pgstat_forkexec(void)
473 av[ac++] = "postgres";
474 av[ac++] = "--forkcol";
475 av[ac++] = NULL; /* filled in by postmaster_forkexec */
478 Assert(ac < lengthof(av));
480 return postmaster_forkexec(ac, av);
482 #endif /* EXEC_BACKEND */
488 * Called from postmaster at startup or after an existing collector
489 * died. Attempt to fire up a fresh statistics collector.
491 * Returns PID of child process, or 0 if fail.
493 * Note: if fail, we will be called again from the postmaster main loop.
503 * Do nothing if no collector needed
505 if (!pgstat_collect_startcollector)
509 * Do nothing if too soon since last collector start. This is a safety
510 * valve to protect against continuous respawn attempts if the collector
511 * is dying immediately at launch. Note that since we will be re-called
512 * from the postmaster main loop, we will get another chance later.
514 curtime = time(NULL);
515 if ((unsigned int) (curtime - last_pgstat_start_time) <
516 (unsigned int) PGSTAT_RESTART_INTERVAL)
518 last_pgstat_start_time = curtime;
521 * Check that the socket is there, else pgstat_init failed.
526 (errmsg("statistics collector startup skipped")));
529 * We can only get here if someone tries to manually turn
530 * pgstat_collect_startcollector on after it had been off.
532 pgstat_collect_startcollector = false;
537 * Okay, fork off the collector.
540 switch ((pgStatPid = pgstat_forkexec()))
542 switch ((pgStatPid = fork_process()))
547 (errmsg("could not fork statistics collector: %m")));
552 /* in postmaster child ... */
553 /* Close the postmaster's sockets */
554 ClosePostmasterPorts(false);
556 /* Lose the postmaster's on-exit routines */
559 /* Drop our connection to postmaster's shared memory, as well */
560 PGSharedMemoryDetach();
562 PgstatCollectorMain(0, NULL);
567 return (int) pgStatPid;
570 /* shouldn't get here */
575 /* ------------------------------------------------------------
576 * Public functions used by backends follow
577 *------------------------------------------------------------
582 * pgstat_report_tabstat() -
584 * Called from tcop/postgres.c to send the so far collected
585 * per table access statistics to the collector.
589 pgstat_report_tabstat(void)
593 if (pgStatSock < 0 ||
594 (!pgstat_collect_tuplelevel &&
595 !pgstat_collect_blocklevel))
597 /* Not reporting stats, so just flush whatever we have */
598 RegularTabStat.tsa_used = 0;
599 SharedTabStat.tsa_used = 0;
604 * For each message buffer used during the last query set the header
605 * fields and send it out.
607 for (i = 0; i < RegularTabStat.tsa_used; i++)
609 PgStat_MsgTabstat *tsmsg = RegularTabStat.tsa_messages[i];
613 n = tsmsg->m_nentries;
614 len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
615 n * sizeof(PgStat_TableEntry);
617 tsmsg->m_xact_commit = pgStatXactCommit;
618 tsmsg->m_xact_rollback = pgStatXactRollback;
619 pgStatXactCommit = 0;
620 pgStatXactRollback = 0;
622 pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
623 tsmsg->m_databaseid = MyDatabaseId;
624 pgstat_send(tsmsg, len);
626 RegularTabStat.tsa_used = 0;
628 /* Ditto, for shared relations */
629 for (i = 0; i < SharedTabStat.tsa_used; i++)
631 PgStat_MsgTabstat *tsmsg = SharedTabStat.tsa_messages[i];
635 n = tsmsg->m_nentries;
636 len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
637 n * sizeof(PgStat_TableEntry);
639 /* We don't report transaction commit/abort here */
640 tsmsg->m_xact_commit = 0;
641 tsmsg->m_xact_rollback = 0;
643 pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
644 tsmsg->m_databaseid = InvalidOid;
645 pgstat_send(tsmsg, len);
647 SharedTabStat.tsa_used = 0;
652 * pgstat_vacuum_tabstat() -
654 * Will tell the collector about objects he can get rid of.
658 pgstat_vacuum_tabstat(void)
664 PgStat_MsgTabpurge msg;
665 HASH_SEQ_STATUS hstat;
666 PgStat_StatDBEntry *dbentry;
667 PgStat_StatTabEntry *tabentry;
674 * If not done for this transaction, read the statistics collector stats
675 * file into some hash tables.
677 backend_read_statsfile();
680 * Read pg_database and make a list of OIDs of all existing databases
683 rel = heap_open(DatabaseRelationId, AccessShareLock);
684 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
685 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
687 oidlist = lappend_oid(oidlist, HeapTupleGetOid(tup));
690 heap_close(rel, AccessShareLock);
693 * Search the database hash table for dead databases and tell the
694 * collector to drop them.
696 hash_seq_init(&hstat, pgStatDBHash);
697 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
699 Oid dbid = dbentry->databaseid;
701 if (!list_member_oid(oidlist, dbid))
702 pgstat_drop_database(dbid);
709 * Lookup our own database entry; if not found, nothing more to do.
711 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
712 (void *) &MyDatabaseId,
714 if (dbentry == NULL || dbentry->tables == NULL)
718 * Similarly to above, make a list of all known relations in this DB.
721 rel = heap_open(RelationRelationId, AccessShareLock);
722 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
723 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
725 oidlist = lappend_oid(oidlist, HeapTupleGetOid(tup));
728 heap_close(rel, AccessShareLock);
731 * Initialize our messages table counter to zero
736 * Check for all tables listed in stats hashtable if they still exist.
738 hash_seq_init(&hstat, dbentry->tables);
739 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
741 if (list_member_oid(oidlist, tabentry->tableid))
745 * Not there, so add this table's Oid to the message
747 msg.m_tableid[msg.m_nentries++] = tabentry->tableid;
750 * If the message is full, send it out and reinitialize to empty
752 if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
754 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
755 +msg.m_nentries * sizeof(Oid);
757 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
758 msg.m_databaseid = MyDatabaseId;
759 pgstat_send(&msg, len);
768 if (msg.m_nentries > 0)
770 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
771 +msg.m_nentries * sizeof(Oid);
773 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
774 msg.m_databaseid = MyDatabaseId;
775 pgstat_send(&msg, len);
784 * pgstat_drop_database() -
786 * Tell the collector that we just dropped a database.
787 * (If the message gets lost, we will still clean the dead DB eventually
788 * via future invocations of pgstat_vacuum_tabstat().)
792 pgstat_drop_database(Oid databaseid)
794 PgStat_MsgDropdb msg;
799 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
800 msg.m_databaseid = databaseid;
801 pgstat_send(&msg, sizeof(msg));
806 * pgstat_drop_relation() -
808 * Tell the collector that we just dropped a relation.
809 * (If the message gets lost, we will still clean the dead entry eventually
810 * via future invocations of pgstat_vacuum_tabstat().)
814 pgstat_drop_relation(Oid relid)
816 PgStat_MsgTabpurge msg;
822 msg.m_tableid[0] = relid;
825 len = offsetof(PgStat_MsgTabpurge, m_tableid[0]) +sizeof(Oid);
827 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
828 msg.m_databaseid = MyDatabaseId;
829 pgstat_send(&msg, len);
834 * pgstat_reset_counters() -
836 * Tell the statistics collector to reset counters for our database.
840 pgstat_reset_counters(void)
842 PgStat_MsgResetcounter msg;
849 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
850 errmsg("must be superuser to reset statistics counters")));
852 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
853 msg.m_databaseid = MyDatabaseId;
854 pgstat_send(&msg, sizeof(msg));
859 * pgstat_report_autovac() -
861 * Called from autovacuum.c to report startup of an autovacuum process.
862 * We are called before InitPostgres is done, so can't rely on MyDatabaseId;
863 * the db OID must be passed in, instead.
867 pgstat_report_autovac(Oid dboid)
869 PgStat_MsgAutovacStart msg;
874 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_AUTOVAC_START);
875 msg.m_databaseid = dboid;
876 msg.m_start_time = GetCurrentTimestamp();
878 pgstat_send(&msg, sizeof(msg));
883 * pgstat_report_vacuum() -
885 * Tell the collector about the table we just vacuumed.
889 pgstat_report_vacuum(Oid tableoid, bool shared,
890 bool analyze, PgStat_Counter tuples)
892 PgStat_MsgVacuum msg;
894 if (pgStatSock < 0 ||
895 !pgstat_collect_tuplelevel)
898 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_VACUUM);
899 msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
900 msg.m_tableoid = tableoid;
901 msg.m_analyze = analyze;
902 msg.m_autovacuum = IsAutoVacuumProcess(); /* is this autovacuum? */
903 msg.m_vacuumtime = GetCurrentTimestamp();
904 msg.m_tuples = tuples;
905 pgstat_send(&msg, sizeof(msg));
909 * pgstat_report_analyze() -
911 * Tell the collector about the table we just analyzed.
915 pgstat_report_analyze(Oid tableoid, bool shared, PgStat_Counter livetuples,
916 PgStat_Counter deadtuples)
918 PgStat_MsgAnalyze msg;
920 if (pgStatSock < 0 ||
921 !pgstat_collect_tuplelevel)
924 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ANALYZE);
925 msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
926 msg.m_tableoid = tableoid;
927 msg.m_autovacuum = IsAutoVacuumProcess(); /* is this autovacuum? */
928 msg.m_analyzetime = GetCurrentTimestamp();
929 msg.m_live_tuples = livetuples;
930 msg.m_dead_tuples = deadtuples;
931 pgstat_send(&msg, sizeof(msg));
938 * Send some junk data to the collector to increase traffic.
949 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DUMMY);
950 pgstat_send(&msg, sizeof(msg));
954 * Enlarge a TabStatArray
957 more_tabstat_space(TabStatArray *tsarr)
959 PgStat_MsgTabstat *newMessages;
960 PgStat_MsgTabstat **msgArray;
964 AssertArg(PointerIsValid(tsarr));
966 newAlloc = tsarr->tsa_alloc + TABSTAT_QUANTUM;
968 /* Create (another) quantum of message buffers */
969 newMessages = (PgStat_MsgTabstat *)
970 MemoryContextAllocZero(TopMemoryContext,
971 sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
973 /* Create or enlarge the pointer array */
974 if (tsarr->tsa_messages == NULL)
975 msgArray = (PgStat_MsgTabstat **)
976 MemoryContextAlloc(TopMemoryContext,
977 sizeof(PgStat_MsgTabstat *) * newAlloc);
979 msgArray = (PgStat_MsgTabstat **)
980 repalloc(tsarr->tsa_messages,
981 sizeof(PgStat_MsgTabstat *) * newAlloc);
983 for (i = 0; i < TABSTAT_QUANTUM; i++)
984 msgArray[tsarr->tsa_alloc + i] = newMessages++;
985 tsarr->tsa_messages = msgArray;
986 tsarr->tsa_alloc = newAlloc;
988 Assert(tsarr->tsa_used < tsarr->tsa_alloc);
992 * pgstat_initstats() -
994 * Called from various places usually dealing with initialization
995 * of Relation or Scan structures. The data placed into these
996 * structures from here tell where later to count for buffer reads,
997 * scans and tuples fetched.
1001 pgstat_initstats(PgStat_Info *stats, Relation rel)
1003 Oid rel_id = rel->rd_id;
1004 PgStat_TableEntry *useent;
1005 TabStatArray *tsarr;
1006 PgStat_MsgTabstat *tsmsg;
1011 * Initialize data not to count at all.
1013 stats->tabentry = NULL;
1015 if (pgStatSock < 0 ||
1016 !(pgstat_collect_tuplelevel ||
1017 pgstat_collect_blocklevel))
1020 tsarr = rel->rd_rel->relisshared ? &SharedTabStat : &RegularTabStat;
1023 * Search the already-used message slots for this relation.
1025 for (mb = 0; mb < tsarr->tsa_used; mb++)
1027 tsmsg = tsarr->tsa_messages[mb];
1029 for (i = tsmsg->m_nentries; --i >= 0;)
1031 if (tsmsg->m_entry[i].t_id == rel_id)
1033 stats->tabentry = (void *) &(tsmsg->m_entry[i]);
1038 if (tsmsg->m_nentries >= PGSTAT_NUM_TABENTRIES)
1042 * Not found, but found a message buffer with an empty slot instead.
1043 * Fine, let's use this one.
1045 i = tsmsg->m_nentries++;
1046 useent = &tsmsg->m_entry[i];
1047 MemSet(useent, 0, sizeof(PgStat_TableEntry));
1048 useent->t_id = rel_id;
1049 stats->tabentry = (void *) useent;
1054 * If we ran out of message buffers, we just allocate more.
1056 if (tsarr->tsa_used >= tsarr->tsa_alloc)
1057 more_tabstat_space(tsarr);
1060 * Use the first entry of the next message buffer.
1062 mb = tsarr->tsa_used++;
1063 tsmsg = tsarr->tsa_messages[mb];
1064 tsmsg->m_nentries = 1;
1065 useent = &tsmsg->m_entry[0];
1066 MemSet(useent, 0, sizeof(PgStat_TableEntry));
1067 useent->t_id = rel_id;
1068 stats->tabentry = (void *) useent;
1073 * pgstat_count_xact_commit() -
1075 * Called from access/transam/xact.c to count transaction commits.
1079 pgstat_count_xact_commit(void)
1081 if (!pgstat_collect_tuplelevel &&
1082 !pgstat_collect_blocklevel)
1088 * If there was no relation activity yet, just make one existing message
1089 * buffer used without slots, causing the next report to tell new
1092 if (RegularTabStat.tsa_alloc == 0)
1093 more_tabstat_space(&RegularTabStat);
1095 if (RegularTabStat.tsa_used == 0)
1097 RegularTabStat.tsa_used++;
1098 RegularTabStat.tsa_messages[0]->m_nentries = 0;
1104 * pgstat_count_xact_rollback() -
1106 * Called from access/transam/xact.c to count transaction rollbacks.
1110 pgstat_count_xact_rollback(void)
1112 if (!pgstat_collect_tuplelevel &&
1113 !pgstat_collect_blocklevel)
1116 pgStatXactRollback++;
1119 * If there was no relation activity yet, just make one existing message
1120 * buffer used without slots, causing the next report to tell new
1123 if (RegularTabStat.tsa_alloc == 0)
1124 more_tabstat_space(&RegularTabStat);
1126 if (RegularTabStat.tsa_used == 0)
1128 RegularTabStat.tsa_used++;
1129 RegularTabStat.tsa_messages[0]->m_nentries = 0;
1135 * pgstat_fetch_stat_dbentry() -
1137 * Support function for the SQL-callable pgstat* functions. Returns
1138 * the collected statistics for one database or NULL. NULL doesn't mean
1139 * that the database doesn't exist, it is just not yet known by the
1140 * collector, so the caller is better off to report ZERO instead.
1143 PgStat_StatDBEntry *
1144 pgstat_fetch_stat_dbentry(Oid dbid)
1147 * If not done for this transaction, read the statistics collector stats
1148 * file into some hash tables.
1150 backend_read_statsfile();
1153 * Lookup the requested database; return NULL if not found
1155 return (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1162 * pgstat_fetch_stat_tabentry() -
1164 * Support function for the SQL-callable pgstat* functions. Returns
1165 * the collected statistics for one table or NULL. NULL doesn't mean
1166 * that the table doesn't exist, it is just not yet known by the
1167 * collector, so the caller is better off to report ZERO instead.
1170 PgStat_StatTabEntry *
1171 pgstat_fetch_stat_tabentry(Oid relid)
1174 PgStat_StatDBEntry *dbentry;
1175 PgStat_StatTabEntry *tabentry;
1178 * If not done for this transaction, read the statistics collector stats
1179 * file into some hash tables.
1181 backend_read_statsfile();
1184 * Lookup our database, then look in its table hash table.
1186 dbid = MyDatabaseId;
1187 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1190 if (dbentry != NULL && dbentry->tables != NULL)
1192 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1200 * If we didn't find it, maybe it's a shared table.
1203 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1206 if (dbentry != NULL && dbentry->tables != NULL)
1208 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1220 * pgstat_fetch_stat_beentry() -
1222 * Support function for the SQL-callable pgstat* functions. Returns
1223 * our local copy of the current-activity entry for one backend.
1225 * NB: caller is responsible for a check if the user is permitted to see
1226 * this info (especially the querystring).
1230 pgstat_fetch_stat_beentry(int beid)
1232 pgstat_read_current_status();
1234 if (beid < 1 || beid > localNumBackends)
1237 return &localBackendStatusTable[beid - 1];
1242 * pgstat_fetch_stat_numbackends() -
1244 * Support function for the SQL-callable pgstat* functions. Returns
1245 * the maximum current backend id.
1249 pgstat_fetch_stat_numbackends(void)
1251 pgstat_read_current_status();
1253 return localNumBackends;
1257 /* ------------------------------------------------------------
1258 * Functions for management of the shared-memory PgBackendStatus array
1259 * ------------------------------------------------------------
1262 static PgBackendStatus *BackendStatusArray = NULL;
1263 static PgBackendStatus *MyBEEntry = NULL;
1267 * Report shared-memory space needed by CreateSharedBackendStatus.
1270 BackendStatusShmemSize(void)
1274 size = mul_size(sizeof(PgBackendStatus), MaxBackends);
1279 * Initialize the shared status array during postmaster startup.
1282 CreateSharedBackendStatus(void)
1284 Size size = BackendStatusShmemSize();
1287 /* Create or attach to the shared array */
1288 BackendStatusArray = (PgBackendStatus *)
1289 ShmemInitStruct("Backend Status Array", size, &found);
1294 * We're the first - initialize.
1296 MemSet(BackendStatusArray, 0, size);
1302 * pgstat_bestart() -
1304 * Initialize this backend's entry in the PgBackendStatus array,
1305 * and set up an on-proc-exit hook that will clear it again.
1306 * Called from InitPostgres. MyBackendId and MyDatabaseId must be set.
1310 pgstat_bestart(void)
1312 volatile PgBackendStatus *beentry;
1313 TimestampTz proc_start_timestamp;
1315 SockAddr clientaddr;
1317 Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
1318 MyBEEntry = &BackendStatusArray[MyBackendId - 1];
1321 * To minimize the time spent modifying the entry, fetch all the needed
1324 * If we have a MyProcPort, use its session start time (for consistency,
1325 * and to save a kernel call).
1328 proc_start_timestamp = MyProcPort->SessionStartTime;
1330 proc_start_timestamp = GetCurrentTimestamp();
1331 userid = GetSessionUserId();
1334 * We may not have a MyProcPort (eg, if this is the autovacuum process).
1335 * If so, use all-zeroes client address, which is dealt with specially in
1336 * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
1339 memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
1341 MemSet(&clientaddr, 0, sizeof(clientaddr));
1344 * Initialize my status entry, following the protocol of bumping
1345 * st_changecount before and after; and make sure it's even afterwards. We
1346 * use a volatile pointer here to ensure the compiler doesn't try to get
1349 beentry = MyBEEntry;
1352 beentry->st_changecount++;
1353 } while ((beentry->st_changecount & 1) == 0);
1355 beentry->st_procpid = MyProcPid;
1356 beentry->st_proc_start_timestamp = proc_start_timestamp;
1357 beentry->st_activity_start_timestamp = 0;
1358 beentry->st_txn_start_timestamp = 0;
1359 beentry->st_databaseid = MyDatabaseId;
1360 beentry->st_userid = userid;
1361 beentry->st_clientaddr = clientaddr;
1362 beentry->st_waiting = false;
1363 beentry->st_activity[0] = '\0';
1364 /* Also make sure the last byte in the string area is always 0 */
1365 beentry->st_activity[PGBE_ACTIVITY_SIZE - 1] = '\0';
1367 beentry->st_changecount++;
1368 Assert((beentry->st_changecount & 1) == 0);
1371 * Set up a process-exit hook to clean up.
1373 on_shmem_exit(pgstat_beshutdown_hook, 0);
1377 * Shut down a single backend's statistics reporting at process exit.
1379 * Flush any remaining statistics counts out to the collector.
1380 * Without this, operations triggered during backend exit (such as
1381 * temp table deletions) won't be counted.
1383 * Lastly, clear out our entry in the PgBackendStatus array.
1386 pgstat_beshutdown_hook(int code, Datum arg)
1388 volatile PgBackendStatus *beentry = MyBEEntry;
1390 pgstat_report_tabstat();
1393 * Clear my status entry, following the protocol of bumping st_changecount
1394 * before and after. We use a volatile pointer here to ensure the
1395 * compiler doesn't try to get cute.
1397 beentry->st_changecount++;
1399 beentry->st_procpid = 0; /* mark invalid */
1401 beentry->st_changecount++;
1402 Assert((beentry->st_changecount & 1) == 0);
1407 * pgstat_report_activity() -
1409 * Called from tcop/postgres.c to report what the backend is actually doing
1410 * (usually "<IDLE>" or the start of the query to be executed).
1414 pgstat_report_activity(const char *cmd_str)
1416 volatile PgBackendStatus *beentry = MyBEEntry;
1417 TimestampTz start_timestamp;
1420 if (!pgstat_collect_querystring || !beentry)
1424 * To minimize the time spent modifying the entry, fetch all the needed
1427 start_timestamp = GetCurrentStatementStartTimestamp();
1429 len = strlen(cmd_str);
1430 len = pg_mbcliplen(cmd_str, len, PGBE_ACTIVITY_SIZE - 1);
1433 * Update my status entry, following the protocol of bumping
1434 * st_changecount before and after. We use a volatile pointer here to
1435 * ensure the compiler doesn't try to get cute.
1437 beentry->st_changecount++;
1439 beentry->st_activity_start_timestamp = start_timestamp;
1440 memcpy((char *) beentry->st_activity, cmd_str, len);
1441 beentry->st_activity[len] = '\0';
1443 beentry->st_changecount++;
1444 Assert((beentry->st_changecount & 1) == 0);
1448 * Set the current transaction start timestamp to the specified
1449 * value. If there is no current active transaction, this is signified
1453 pgstat_report_txn_timestamp(TimestampTz tstamp)
1455 volatile PgBackendStatus *beentry = MyBEEntry;
1457 if (!pgstat_collect_querystring || !beentry)
1461 * Update my status entry, following the protocol of bumping
1462 * st_changecount before and after. We use a volatile pointer
1463 * here to ensure the compiler doesn't try to get cute.
1465 beentry->st_changecount++;
1466 beentry->st_txn_start_timestamp = tstamp;
1467 beentry->st_changecount++;
1468 Assert((beentry->st_changecount & 1) == 0);
1472 * pgstat_report_waiting() -
1474 * Called from lock manager to report beginning or end of a lock wait.
1476 * NB: this *must* be able to survive being called before MyBEEntry has been
1481 pgstat_report_waiting(bool waiting)
1483 volatile PgBackendStatus *beentry = MyBEEntry;
1485 if (!pgstat_collect_querystring || !beentry)
1489 * Since this is a single-byte field in a struct that only this process
1490 * may modify, there seems no need to bother with the st_changecount
1491 * protocol. The update must appear atomic in any case.
1493 beentry->st_waiting = waiting;
1498 * pgstat_read_current_status() -
1500 * Copy the current contents of the PgBackendStatus array to local memory,
1501 * if not already done in this transaction.
1505 pgstat_read_current_status(void)
1507 TransactionId topXid = GetTopTransactionId();
1508 volatile PgBackendStatus *beentry;
1509 PgBackendStatus *localentry;
1512 Assert(!pgStatRunningInCollector);
1513 if (TransactionIdEquals(pgStatLocalStatusXact, topXid))
1514 return; /* already done */
1516 localBackendStatusTable = (PgBackendStatus *)
1517 MemoryContextAlloc(TopTransactionContext,
1518 sizeof(PgBackendStatus) * MaxBackends);
1519 localNumBackends = 0;
1521 beentry = BackendStatusArray;
1522 localentry = localBackendStatusTable;
1523 for (i = 1; i <= MaxBackends; i++)
1526 * Follow the protocol of retrying if st_changecount changes while we
1527 * copy the entry, or if it's odd. (The check for odd is needed to
1528 * cover the case where we are able to completely copy the entry while
1529 * the source backend is between increment steps.) We use a volatile
1530 * pointer here to ensure the compiler doesn't try to get cute.
1534 int save_changecount = beentry->st_changecount;
1537 * XXX if PGBE_ACTIVITY_SIZE is really large, it might be best to
1538 * use strcpy not memcpy for copying the activity string?
1540 memcpy(localentry, (char *) beentry, sizeof(PgBackendStatus));
1542 if (save_changecount == beentry->st_changecount &&
1543 (save_changecount & 1) == 0)
1546 /* Make sure we can break out of loop if stuck... */
1547 CHECK_FOR_INTERRUPTS();
1551 /* Only valid entries get included into the local array */
1552 if (localentry->st_procpid > 0)
1559 pgStatLocalStatusXact = topXid;
1563 /* ------------------------------------------------------------
1564 * Local support functions follow
1565 * ------------------------------------------------------------
1570 * pgstat_setheader() -
1572 * Set common header fields in a statistics message
1576 pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
1578 hdr->m_type = mtype;
1585 * Send out one statistics message to the collector
1589 pgstat_send(void *msg, int len)
1596 ((PgStat_MsgHdr *) msg)->m_size = len;
1598 /* We'll retry after EINTR, but ignore all other failures */
1601 rc = send(pgStatSock, msg, len, 0);
1602 } while (rc < 0 && errno == EINTR);
1604 #ifdef USE_ASSERT_CHECKING
1605 /* In debug builds, log send failures ... */
1607 elog(LOG, "could not send to statistics collector: %m");
1613 * PgstatCollectorMain() -
1615 * Start up the statistics collector process. This is the body of the
1616 * postmaster child process.
1618 * The argc/argv parameters are valid only in EXEC_BACKEND case.
1621 NON_EXEC_STATIC void
1622 PgstatCollectorMain(int argc, char *argv[])
1624 struct itimerval write_timeout;
1625 bool need_timer = false;
1630 struct pollfd input_fd;
1632 struct timeval sel_timeout;
1636 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
1638 MyProcPid = getpid(); /* reset MyProcPid */
1641 * If possible, make this process a group leader, so that the postmaster
1642 * can signal any child processes too. (pgstat probably never has
1643 * any child processes, but for consistency we make all postmaster
1644 * child processes do this.)
1648 elog(FATAL, "setsid() failed: %m");
1652 * Ignore all signals usually bound to some action in the postmaster,
1653 * except SIGQUIT and SIGALRM.
1655 pqsignal(SIGHUP, SIG_IGN);
1656 pqsignal(SIGINT, SIG_IGN);
1657 pqsignal(SIGTERM, SIG_IGN);
1658 pqsignal(SIGQUIT, pgstat_exit);
1659 pqsignal(SIGALRM, force_statwrite);
1660 pqsignal(SIGPIPE, SIG_IGN);
1661 pqsignal(SIGUSR1, SIG_IGN);
1662 pqsignal(SIGUSR2, SIG_IGN);
1663 pqsignal(SIGCHLD, SIG_DFL);
1664 pqsignal(SIGTTIN, SIG_DFL);
1665 pqsignal(SIGTTOU, SIG_DFL);
1666 pqsignal(SIGCONT, SIG_DFL);
1667 pqsignal(SIGWINCH, SIG_DFL);
1668 PG_SETMASK(&UnBlockSig);
1671 * Identify myself via ps
1673 init_ps_display("stats collector process", "", "", "");
1676 * Arrange to write the initial status file right away
1678 need_statwrite = true;
1680 /* Preset the delay between status file writes */
1681 MemSet(&write_timeout, 0, sizeof(struct itimerval));
1682 write_timeout.it_value.tv_sec = PGSTAT_STAT_INTERVAL / 1000;
1683 write_timeout.it_value.tv_usec = PGSTAT_STAT_INTERVAL % 1000;
1686 * Read in an existing statistics stats file or initialize the stats to
1689 pgStatRunningInCollector = true;
1690 pgstat_read_statsfile(&pgStatDBHash, InvalidOid);
1693 * Setup the descriptor set for select(2). Since only one bit in the set
1694 * ever changes, we need not repeat FD_ZERO each time.
1701 * Loop to process messages until we get SIGQUIT or detect ungraceful
1702 * death of our parent postmaster.
1704 * For performance reasons, we don't want to do a PostmasterIsAlive() test
1705 * after every message; instead, do it at statwrite time and if
1706 * select()/poll() is interrupted by timeout.
1713 * Quit if we get SIGQUIT from the postmaster.
1719 * If time to write the stats file, do so. Note that the alarm
1720 * interrupt isn't re-enabled immediately, but only after we next
1721 * receive a stats message; so no cycles are wasted when there is
1726 /* Check for postmaster death; if so we'll write file below */
1727 if (!PostmasterIsAlive(true))
1730 pgstat_write_statsfile();
1731 need_statwrite = false;
1736 * Wait for a message to arrive; but not for more than
1737 * PGSTAT_SELECT_TIMEOUT seconds. (This determines how quickly we will
1738 * shut down after an ungraceful postmaster termination; so it needn't
1739 * be very fast. However, on some systems SIGQUIT won't interrupt the
1740 * poll/select call, so this also limits speed of response to SIGQUIT,
1741 * which is more important.)
1743 * We use poll(2) if available, otherwise select(2)
1746 input_fd.fd = pgStatSock;
1747 input_fd.events = POLLIN | POLLERR;
1748 input_fd.revents = 0;
1750 if (poll(&input_fd, 1, PGSTAT_SELECT_TIMEOUT * 1000) < 0)
1755 (errcode_for_socket_access(),
1756 errmsg("poll() failed in statistics collector: %m")));
1759 got_data = (input_fd.revents != 0);
1760 #else /* !HAVE_POLL */
1762 FD_SET(pgStatSock, &rfds);
1765 * timeout struct is modified by select() on some operating systems,
1766 * so re-fill it each time.
1768 sel_timeout.tv_sec = PGSTAT_SELECT_TIMEOUT;
1769 sel_timeout.tv_usec = 0;
1771 if (select(pgStatSock + 1, &rfds, NULL, NULL, &sel_timeout) < 0)
1776 (errcode_for_socket_access(),
1777 errmsg("select() failed in statistics collector: %m")));
1780 got_data = FD_ISSET(pgStatSock, &rfds);
1781 #endif /* HAVE_POLL */
1784 * If there is a message on the socket, read it and check for
1789 len = recv(pgStatSock, (char *) &msg,
1790 sizeof(PgStat_Msg), 0);
1796 (errcode_for_socket_access(),
1797 errmsg("could not read statistics message: %m")));
1801 * We ignore messages that are smaller than our common header
1803 if (len < sizeof(PgStat_MsgHdr))
1807 * The received length must match the length in the header
1809 if (msg.msg_hdr.m_size != len)
1813 * O.K. - we accept this message. Process it.
1815 switch (msg.msg_hdr.m_type)
1817 case PGSTAT_MTYPE_DUMMY:
1820 case PGSTAT_MTYPE_TABSTAT:
1821 pgstat_recv_tabstat((PgStat_MsgTabstat *) &msg, len);
1824 case PGSTAT_MTYPE_TABPURGE:
1825 pgstat_recv_tabpurge((PgStat_MsgTabpurge *) &msg, len);
1828 case PGSTAT_MTYPE_DROPDB:
1829 pgstat_recv_dropdb((PgStat_MsgDropdb *) &msg, len);
1832 case PGSTAT_MTYPE_RESETCOUNTER:
1833 pgstat_recv_resetcounter((PgStat_MsgResetcounter *) &msg,
1837 case PGSTAT_MTYPE_AUTOVAC_START:
1838 pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
1841 case PGSTAT_MTYPE_VACUUM:
1842 pgstat_recv_vacuum((PgStat_MsgVacuum *) &msg, len);
1845 case PGSTAT_MTYPE_ANALYZE:
1846 pgstat_recv_analyze((PgStat_MsgAnalyze *) &msg, len);
1854 * If this is the first message after we wrote the stats file the
1855 * last time, enable the alarm interrupt to make it be written
1860 if (setitimer(ITIMER_REAL, &write_timeout, NULL))
1862 (errmsg("could not set statistics collector timer: %m")));
1869 * We can only get here if the select/poll timeout elapsed. Check
1870 * for postmaster death.
1872 if (!PostmasterIsAlive(true))
1875 } /* end of message-processing loop */
1878 * Save the final stats to reuse at next startup.
1880 pgstat_write_statsfile();
1886 /* SIGQUIT signal handler for collector process */
1888 pgstat_exit(SIGNAL_ARGS)
1893 /* SIGALRM signal handler for collector process */
1895 force_statwrite(SIGNAL_ARGS)
1897 need_statwrite = true;
1902 * Lookup the hash table entry for the specified database. If no hash
1903 * table entry exists, initialize it, if the create parameter is true.
1904 * Else, return NULL.
1906 static PgStat_StatDBEntry *
1907 pgstat_get_db_entry(Oid databaseid, bool create)
1909 PgStat_StatDBEntry *result;
1911 HASHACTION action = (create ? HASH_ENTER : HASH_FIND);
1913 /* Lookup or create the hash table entry for this database */
1914 result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1918 if (!create && !found)
1921 /* If not found, initialize the new one. */
1926 result->tables = NULL;
1927 result->n_xact_commit = 0;
1928 result->n_xact_rollback = 0;
1929 result->n_blocks_fetched = 0;
1930 result->n_blocks_hit = 0;
1931 result->last_autovac_time = 0;
1933 memset(&hash_ctl, 0, sizeof(hash_ctl));
1934 hash_ctl.keysize = sizeof(Oid);
1935 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
1936 hash_ctl.hash = oid_hash;
1937 result->tables = hash_create("Per-database table",
1938 PGSTAT_TAB_HASH_SIZE,
1940 HASH_ELEM | HASH_FUNCTION);
1948 * pgstat_write_statsfile() -
1954 pgstat_write_statsfile(void)
1956 HASH_SEQ_STATUS hstat;
1957 HASH_SEQ_STATUS tstat;
1958 PgStat_StatDBEntry *dbentry;
1959 PgStat_StatTabEntry *tabentry;
1964 * Open the statistics temp file to write out the current values.
1966 fpout = fopen(PGSTAT_STAT_TMPFILE, PG_BINARY_W);
1970 (errcode_for_file_access(),
1971 errmsg("could not open temporary statistics file \"%s\": %m",
1972 PGSTAT_STAT_TMPFILE)));
1977 * Write the file header --- currently just a format ID.
1979 format_id = PGSTAT_FILE_FORMAT_ID;
1980 fwrite(&format_id, sizeof(format_id), 1, fpout);
1983 * Walk through the database table.
1985 hash_seq_init(&hstat, pgStatDBHash);
1986 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
1989 * Write out the DB entry including the number of live backends. We
1990 * don't write the tables pointer since it's of no use to any other
1994 fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
1997 * Walk through the database's access stats per table.
1999 hash_seq_init(&tstat, dbentry->tables);
2000 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
2003 fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
2007 * Mark the end of this DB
2013 * No more output to be done. Close the temp file and replace the old
2014 * pgstat.stat with it. The ferror() check replaces testing for error
2015 * after each individual fputc or fwrite above.
2022 (errcode_for_file_access(),
2023 errmsg("could not write temporary statistics file \"%s\": %m",
2024 PGSTAT_STAT_TMPFILE)));
2026 unlink(PGSTAT_STAT_TMPFILE);
2028 else if (fclose(fpout) < 0)
2031 (errcode_for_file_access(),
2032 errmsg("could not close temporary statistics file \"%s\": %m",
2033 PGSTAT_STAT_TMPFILE)));
2034 unlink(PGSTAT_STAT_TMPFILE);
2036 else if (rename(PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME) < 0)
2039 (errcode_for_file_access(),
2040 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
2041 PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME)));
2042 unlink(PGSTAT_STAT_TMPFILE);
2048 * pgstat_read_statsfile() -
2050 * Reads in an existing statistics collector file and initializes the
2051 * databases' hash table (whose entries point to the tables' hash tables).
2055 pgstat_read_statsfile(HTAB **dbhash, Oid onlydb)
2057 PgStat_StatDBEntry *dbentry;
2058 PgStat_StatDBEntry dbbuf;
2059 PgStat_StatTabEntry *tabentry;
2060 PgStat_StatTabEntry tabbuf;
2062 HTAB *tabhash = NULL;
2066 MemoryContext use_mcxt;
2070 * If running in the collector or the autovacuum process, we use the
2071 * DynaHashCxt memory context. If running in a backend, we use the
2072 * TopTransactionContext instead, so the caller must only know the last
2073 * XactId when this call happened to know if his tables are still valid or
2076 if (pgStatRunningInCollector || IsAutoVacuumProcess())
2083 use_mcxt = TopTransactionContext;
2084 mcxt_flags = HASH_CONTEXT;
2088 * Create the DB hashtable
2090 memset(&hash_ctl, 0, sizeof(hash_ctl));
2091 hash_ctl.keysize = sizeof(Oid);
2092 hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
2093 hash_ctl.hash = oid_hash;
2094 hash_ctl.hcxt = use_mcxt;
2095 *dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
2096 HASH_ELEM | HASH_FUNCTION | mcxt_flags);
2099 * Try to open the status file. If it doesn't exist, the backends simply
2100 * return zero for anything and the collector simply starts from scratch
2101 * with empty counters.
2103 if ((fpin = AllocateFile(PGSTAT_STAT_FILENAME, PG_BINARY_R)) == NULL)
2107 * Verify it's of the expected format.
2109 if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id)
2110 || format_id != PGSTAT_FILE_FORMAT_ID)
2112 ereport(pgStatRunningInCollector ? LOG : WARNING,
2113 (errmsg("corrupted pgstat.stat file")));
2118 * We found an existing collector stats file. Read it and put all the
2119 * hashtable entries into place.
2123 switch (fgetc(fpin))
2126 * 'D' A PgStat_StatDBEntry struct describing a database
2127 * follows. Subsequently, zero to many 'T' entries will follow
2128 * until a 'd' is encountered.
2131 if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
2132 fpin) != offsetof(PgStat_StatDBEntry, tables))
2134 ereport(pgStatRunningInCollector ? LOG : WARNING,
2135 (errmsg("corrupted pgstat.stat file")));
2140 * Add to the DB hash
2142 dbentry = (PgStat_StatDBEntry *) hash_search(*dbhash,
2143 (void *) &dbbuf.databaseid,
2148 ereport(pgStatRunningInCollector ? LOG : WARNING,
2149 (errmsg("corrupted pgstat.stat file")));
2153 memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
2154 dbentry->tables = NULL;
2157 * Don't collect tables if not the requested DB (or the
2158 * shared-table info)
2160 if (onlydb != InvalidOid)
2162 if (dbbuf.databaseid != onlydb &&
2163 dbbuf.databaseid != InvalidOid)
2167 memset(&hash_ctl, 0, sizeof(hash_ctl));
2168 hash_ctl.keysize = sizeof(Oid);
2169 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2170 hash_ctl.hash = oid_hash;
2171 hash_ctl.hcxt = use_mcxt;
2172 dbentry->tables = hash_create("Per-database table",
2173 PGSTAT_TAB_HASH_SIZE,
2175 HASH_ELEM | HASH_FUNCTION | mcxt_flags);
2178 * Arrange that following 'T's add entries to this database's
2179 * tables hash table.
2181 tabhash = dbentry->tables;
2185 * 'd' End of this database.
2192 * 'T' A PgStat_StatTabEntry follows.
2195 if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
2196 fpin) != sizeof(PgStat_StatTabEntry))
2198 ereport(pgStatRunningInCollector ? LOG : WARNING,
2199 (errmsg("corrupted pgstat.stat file")));
2204 * Skip if table belongs to a not requested database.
2206 if (tabhash == NULL)
2209 tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
2210 (void *) &tabbuf.tableid,
2211 HASH_ENTER, &found);
2215 ereport(pgStatRunningInCollector ? LOG : WARNING,
2216 (errmsg("corrupted pgstat.stat file")));
2220 memcpy(tabentry, &tabbuf, sizeof(tabbuf));
2224 * 'E' The EOF marker of a complete stats file.
2230 ereport(pgStatRunningInCollector ? LOG : WARNING,
2231 (errmsg("corrupted pgstat.stat file")));
2241 * If not done for this transaction, read the statistics collector
2242 * stats file into some hash tables.
2244 * Because we store the tables in TopTransactionContext, the result
2245 * is good for the entire current main transaction.
2247 * Inside the autovacuum process, the statfile is assumed to be valid
2248 * "forever", that is one iteration, within one database. This means
2249 * we only consider the statistics as they were when the autovacuum
2250 * iteration started.
2253 backend_read_statsfile(void)
2255 if (IsAutoVacuumProcess())
2257 /* already read it? */
2260 Assert(!pgStatRunningInCollector);
2261 pgstat_read_statsfile(&pgStatDBHash, InvalidOid);
2265 TransactionId topXid = GetTopTransactionId();
2267 if (!TransactionIdEquals(pgStatDBHashXact, topXid))
2269 Assert(!pgStatRunningInCollector);
2270 pgstat_read_statsfile(&pgStatDBHash, MyDatabaseId);
2271 pgStatDBHashXact = topXid;
2277 * pgstat_recv_tabstat() -
2279 * Count what the backend has done.
2283 pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
2285 PgStat_TableEntry *tabmsg = &(msg->m_entry[0]);
2286 PgStat_StatDBEntry *dbentry;
2287 PgStat_StatTabEntry *tabentry;
2291 dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
2294 * Update database-wide stats.
2296 dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
2297 dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
2300 * Process all table entries in the message.
2302 for (i = 0; i < msg->m_nentries; i++)
2304 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2305 (void *) &(tabmsg[i].t_id),
2306 HASH_ENTER, &found);
2311 * If it's a new table entry, initialize counters to the values we
2314 tabentry->numscans = tabmsg[i].t_numscans;
2315 tabentry->tuples_returned = tabmsg[i].t_tuples_returned;
2316 tabentry->tuples_fetched = tabmsg[i].t_tuples_fetched;
2317 tabentry->tuples_inserted = tabmsg[i].t_tuples_inserted;
2318 tabentry->tuples_updated = tabmsg[i].t_tuples_updated;
2319 tabentry->tuples_deleted = tabmsg[i].t_tuples_deleted;
2321 tabentry->n_live_tuples = tabmsg[i].t_tuples_inserted;
2322 tabentry->n_dead_tuples = tabmsg[i].t_tuples_updated +
2323 tabmsg[i].t_tuples_deleted;
2324 tabentry->last_anl_tuples = 0;
2325 tabentry->vacuum_timestamp = 0;
2326 tabentry->autovac_vacuum_timestamp = 0;
2327 tabentry->analyze_timestamp = 0;
2328 tabentry->autovac_analyze_timestamp = 0;
2330 tabentry->blocks_fetched = tabmsg[i].t_blocks_fetched;
2331 tabentry->blocks_hit = tabmsg[i].t_blocks_hit;
2336 * Otherwise add the values to the existing entry.
2338 tabentry->numscans += tabmsg[i].t_numscans;
2339 tabentry->tuples_returned += tabmsg[i].t_tuples_returned;
2340 tabentry->tuples_fetched += tabmsg[i].t_tuples_fetched;
2341 tabentry->tuples_inserted += tabmsg[i].t_tuples_inserted;
2342 tabentry->tuples_updated += tabmsg[i].t_tuples_updated;
2343 tabentry->tuples_deleted += tabmsg[i].t_tuples_deleted;
2345 tabentry->n_live_tuples += tabmsg[i].t_tuples_inserted -
2346 tabmsg[i].t_tuples_deleted;
2347 tabentry->n_dead_tuples += tabmsg[i].t_tuples_updated +
2348 tabmsg[i].t_tuples_deleted;
2350 tabentry->blocks_fetched += tabmsg[i].t_blocks_fetched;
2351 tabentry->blocks_hit += tabmsg[i].t_blocks_hit;
2355 * And add the block IO to the database entry.
2357 dbentry->n_blocks_fetched += tabmsg[i].t_blocks_fetched;
2358 dbentry->n_blocks_hit += tabmsg[i].t_blocks_hit;
2364 * pgstat_recv_tabpurge() -
2366 * Arrange for dead table removal.
2370 pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
2372 PgStat_StatDBEntry *dbentry;
2375 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2378 * No need to purge if we don't even know the database.
2380 if (!dbentry || !dbentry->tables)
2384 * Process all table entries in the message.
2386 for (i = 0; i < msg->m_nentries; i++)
2388 /* Remove from hashtable if present; we don't care if it's not. */
2389 (void) hash_search(dbentry->tables,
2390 (void *) &(msg->m_tableid[i]),
2397 * pgstat_recv_dropdb() -
2399 * Arrange for dead database removal
2403 pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len)
2405 PgStat_StatDBEntry *dbentry;
2408 * Lookup the database in the hashtable.
2410 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2413 * If found, remove it.
2417 if (dbentry->tables != NULL)
2418 hash_destroy(dbentry->tables);
2420 if (hash_search(pgStatDBHash,
2421 (void *) &(dbentry->databaseid),
2422 HASH_REMOVE, NULL) == NULL)
2424 (errmsg("database hash table corrupted "
2425 "during cleanup --- abort")));
2431 * pgstat_recv_resetcounter() -
2433 * Reset the statistics for the specified database.
2437 pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
2440 PgStat_StatDBEntry *dbentry;
2443 * Lookup the database in the hashtable. Nothing to do if not there.
2445 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2451 * We simply throw away all the database's table entries by recreating a
2452 * new hash table for them.
2454 if (dbentry->tables != NULL)
2455 hash_destroy(dbentry->tables);
2457 dbentry->tables = NULL;
2458 dbentry->n_xact_commit = 0;
2459 dbentry->n_xact_rollback = 0;
2460 dbentry->n_blocks_fetched = 0;
2461 dbentry->n_blocks_hit = 0;
2463 memset(&hash_ctl, 0, sizeof(hash_ctl));
2464 hash_ctl.keysize = sizeof(Oid);
2465 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2466 hash_ctl.hash = oid_hash;
2467 dbentry->tables = hash_create("Per-database table",
2468 PGSTAT_TAB_HASH_SIZE,
2470 HASH_ELEM | HASH_FUNCTION);
2474 * pgstat_recv_autovac() -
2476 * Process an autovacuum signalling message.
2480 pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len)
2482 PgStat_StatDBEntry *dbentry;
2485 * Lookup the database in the hashtable. Don't create the entry if it
2486 * doesn't exist, because autovacuum may be processing a template
2487 * database. If this isn't the case, the database is most likely to have
2488 * an entry already. (If it doesn't, not much harm is done anyway --
2489 * it'll get created as soon as somebody actually uses the database.)
2491 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2492 if (dbentry == NULL)
2496 * Store the last autovacuum time in the database entry.
2498 dbentry->last_autovac_time = msg->m_start_time;
2502 * pgstat_recv_vacuum() -
2504 * Process a VACUUM message.
2508 pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len)
2510 PgStat_StatDBEntry *dbentry;
2511 PgStat_StatTabEntry *tabentry;
2514 * Don't create either the database or table entry if it doesn't already
2515 * exist. This avoids bloating the stats with entries for stuff that is
2516 * only touched by vacuum and not by live operations.
2518 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2519 if (dbentry == NULL)
2522 tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
2524 if (tabentry == NULL)
2527 if (msg->m_autovacuum)
2528 tabentry->autovac_vacuum_timestamp = msg->m_vacuumtime;
2530 tabentry->vacuum_timestamp = msg->m_vacuumtime;
2531 tabentry->n_live_tuples = msg->m_tuples;
2532 tabentry->n_dead_tuples = 0;
2535 tabentry->last_anl_tuples = msg->m_tuples;
2536 if (msg->m_autovacuum)
2537 tabentry->autovac_analyze_timestamp = msg->m_vacuumtime;
2539 tabentry->analyze_timestamp = msg->m_vacuumtime;
2543 /* last_anl_tuples must never exceed n_live_tuples */
2544 tabentry->last_anl_tuples = Min(tabentry->last_anl_tuples,
2550 * pgstat_recv_analyze() -
2552 * Process an ANALYZE message.
2556 pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
2558 PgStat_StatDBEntry *dbentry;
2559 PgStat_StatTabEntry *tabentry;
2562 * Don't create either the database or table entry if it doesn't already
2563 * exist. This avoids bloating the stats with entries for stuff that is
2564 * only touched by analyze and not by live operations.
2566 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2567 if (dbentry == NULL)
2570 tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
2572 if (tabentry == NULL)
2575 if (msg->m_autovacuum)
2576 tabentry->autovac_analyze_timestamp = msg->m_analyzetime;
2578 tabentry->analyze_timestamp = msg->m_analyzetime;
2579 tabentry->n_live_tuples = msg->m_live_tuples;
2580 tabentry->n_dead_tuples = msg->m_dead_tuples;
2581 tabentry->last_anl_tuples = msg->m_live_tuples + msg->m_dead_tuples;