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-2007, PostgreSQL Global Development Group
16 * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.147 2007/02/15 23:23:23 alvherre 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 MemoryContext pgStatLocalContext = NULL;
134 static HTAB *pgStatDBHash = NULL;
135 static PgBackendStatus *localBackendStatusTable = NULL;
136 static int localNumBackends = 0;
138 static volatile bool need_exit = false;
139 static volatile bool need_statwrite = false;
143 * Local function forward declarations
147 static pid_t pgstat_forkexec(void);
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);
155 static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
156 static void pgstat_write_statsfile(void);
157 static HTAB *pgstat_read_statsfile(Oid onlydb);
158 static void backend_read_statsfile(void);
159 static void pgstat_read_current_status(void);
160 static HTAB *pgstat_collect_oids(Oid catalogid);
162 static void pgstat_setup_memcxt(void);
164 static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype);
165 static void pgstat_send(void *msg, int len);
167 static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
168 static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
169 static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
170 static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
171 static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
172 static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
173 static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
176 /* ------------------------------------------------------------
177 * Public functions called from postmaster follow
178 * ------------------------------------------------------------
184 * Called from postmaster at startup. Create the resources required
185 * by the statistics collector process. If unable to do so, do not
186 * fail --- better to let the postmaster start with stats collection
193 ACCEPT_TYPE_ARG3 alen;
194 struct addrinfo *addrs = NULL,
204 #define TESTBYTEVAL ((char) 199)
207 * Force start of collector daemon if something to collect. Note that
208 * pgstat_collect_querystring is now an independent facility that does not
209 * require the collector daemon.
211 if (pgstat_collect_tuplelevel ||
212 pgstat_collect_blocklevel)
213 pgstat_collect_startcollector = true;
216 * If we don't have to start a collector or should reset the collected
217 * statistics on postmaster start, simply remove the stats file.
219 if (!pgstat_collect_startcollector || pgstat_collect_resetonpmstart)
223 * Nothing else required if collector will not get started
225 if (!pgstat_collect_startcollector)
229 * Create the UDP socket for sending and receiving statistic messages
231 hints.ai_flags = AI_PASSIVE;
232 hints.ai_family = PF_UNSPEC;
233 hints.ai_socktype = SOCK_DGRAM;
234 hints.ai_protocol = 0;
235 hints.ai_addrlen = 0;
236 hints.ai_addr = NULL;
237 hints.ai_canonname = NULL;
238 hints.ai_next = NULL;
239 ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
243 (errmsg("could not resolve \"localhost\": %s",
244 gai_strerror(ret))));
249 * On some platforms, pg_getaddrinfo_all() may return multiple addresses
250 * only one of which will actually work (eg, both IPv6 and IPv4 addresses
251 * when kernel will reject IPv6). Worse, the failure may occur at the
252 * bind() or perhaps even connect() stage. So we must loop through the
253 * results till we find a working combination. We will generate LOG
254 * messages, but no error, for bogus combinations.
256 for (addr = addrs; addr; addr = addr->ai_next)
258 #ifdef HAVE_UNIX_SOCKETS
259 /* Ignore AF_UNIX sockets, if any are returned. */
260 if (addr->ai_family == AF_UNIX)
266 (errmsg("trying another address for the statistics collector")));
271 if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) < 0)
274 (errcode_for_socket_access(),
275 errmsg("could not create socket for statistics collector: %m")));
280 * Bind it to a kernel assigned port on localhost and get the assigned
281 * port via getsockname().
283 if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
286 (errcode_for_socket_access(),
287 errmsg("could not bind socket for statistics collector: %m")));
288 closesocket(pgStatSock);
293 alen = sizeof(pgStatAddr);
294 if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
297 (errcode_for_socket_access(),
298 errmsg("could not get address of socket for statistics collector: %m")));
299 closesocket(pgStatSock);
305 * Connect the socket to its own address. This saves a few cycles by
306 * not having to respecify the target address on every send. This also
307 * provides a kernel-level check that only packets from this same
308 * address will be received.
310 if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
313 (errcode_for_socket_access(),
314 errmsg("could not connect socket for statistics collector: %m")));
315 closesocket(pgStatSock);
321 * Try to send and receive a one-byte test message on the socket. This
322 * is to catch situations where the socket can be created but will not
323 * actually pass data (for instance, because kernel packet filtering
326 test_byte = TESTBYTEVAL;
329 if (send(pgStatSock, &test_byte, 1, 0) != 1)
332 goto retry1; /* if interrupted, just retry */
334 (errcode_for_socket_access(),
335 errmsg("could not send test message on socket for statistics collector: %m")));
336 closesocket(pgStatSock);
342 * There could possibly be a little delay before the message can be
343 * received. We arbitrarily allow up to half a second before deciding
346 for (;;) /* need a loop to handle EINTR */
349 FD_SET(pgStatSock, &rset);
352 sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
353 if (sel_res >= 0 || errno != EINTR)
359 (errcode_for_socket_access(),
360 errmsg("select() failed in statistics collector: %m")));
361 closesocket(pgStatSock);
365 if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
368 * This is the case we actually think is likely, so take pains to
369 * give a specific message for it.
371 * errno will not be set meaningfully here, so don't use it.
374 (errcode(ERRCODE_CONNECTION_FAILURE),
375 errmsg("test message did not get through on socket for statistics collector")));
376 closesocket(pgStatSock);
381 test_byte++; /* just make sure variable is changed */
384 if (recv(pgStatSock, &test_byte, 1, 0) != 1)
387 goto retry2; /* if interrupted, just retry */
389 (errcode_for_socket_access(),
390 errmsg("could not receive test message on socket for statistics collector: %m")));
391 closesocket(pgStatSock);
396 if (test_byte != TESTBYTEVAL) /* strictly paranoia ... */
399 (errcode(ERRCODE_INTERNAL_ERROR),
400 errmsg("incorrect test message transmission on socket for statistics collector")));
401 closesocket(pgStatSock);
406 /* If we get here, we have a working socket */
410 /* Did we find a working address? */
411 if (!addr || pgStatSock < 0)
415 * Set the socket to non-blocking IO. This ensures that if the collector
416 * falls behind, statistics messages will be discarded; backends won't
417 * block waiting to send messages to the collector.
419 if (!pg_set_noblock(pgStatSock))
422 (errcode_for_socket_access(),
423 errmsg("could not set statistics collector socket to nonblocking mode: %m")));
427 pg_freeaddrinfo_all(hints.ai_family, addrs);
433 (errmsg("disabling statistics collector for lack of working socket")));
436 pg_freeaddrinfo_all(hints.ai_family, addrs);
439 closesocket(pgStatSock);
442 /* Adjust GUC variables to suppress useless activity */
443 pgstat_collect_startcollector = false;
444 pgstat_collect_tuplelevel = false;
445 pgstat_collect_blocklevel = false;
449 * pgstat_reset_all() -
451 * Remove the stats file. This is used on server start if the
452 * stats_reset_on_server_start feature is enabled, or if WAL
453 * recovery is needed after a crash.
456 pgstat_reset_all(void)
458 unlink(PGSTAT_STAT_FILENAME);
464 * pgstat_forkexec() -
466 * Format up the arglist for, then fork and exec, statistics collector process
469 pgstat_forkexec(void)
474 av[ac++] = "postgres";
475 av[ac++] = "--forkcol";
476 av[ac++] = NULL; /* filled in by postmaster_forkexec */
479 Assert(ac < lengthof(av));
481 return postmaster_forkexec(ac, av);
483 #endif /* EXEC_BACKEND */
489 * Called from postmaster at startup or after an existing collector
490 * died. Attempt to fire up a fresh statistics collector.
492 * Returns PID of child process, or 0 if fail.
494 * Note: if fail, we will be called again from the postmaster main loop.
504 * Do nothing if no collector needed
506 if (!pgstat_collect_startcollector)
510 * Do nothing if too soon since last collector start. This is a safety
511 * valve to protect against continuous respawn attempts if the collector
512 * is dying immediately at launch. Note that since we will be re-called
513 * from the postmaster main loop, we will get another chance later.
515 curtime = time(NULL);
516 if ((unsigned int) (curtime - last_pgstat_start_time) <
517 (unsigned int) PGSTAT_RESTART_INTERVAL)
519 last_pgstat_start_time = curtime;
522 * Check that the socket is there, else pgstat_init failed.
527 (errmsg("statistics collector startup skipped")));
530 * We can only get here if someone tries to manually turn
531 * pgstat_collect_startcollector on after it had been off.
533 pgstat_collect_startcollector = false;
538 * Okay, fork off the collector.
541 switch ((pgStatPid = pgstat_forkexec()))
543 switch ((pgStatPid = fork_process()))
548 (errmsg("could not fork statistics collector: %m")));
553 /* in postmaster child ... */
554 /* Close the postmaster's sockets */
555 ClosePostmasterPorts(false);
557 /* Lose the postmaster's on-exit routines */
560 /* Drop our connection to postmaster's shared memory, as well */
561 PGSharedMemoryDetach();
563 PgstatCollectorMain(0, NULL);
568 return (int) pgStatPid;
571 /* shouldn't get here */
576 /* ------------------------------------------------------------
577 * Public functions used by backends follow
578 *------------------------------------------------------------
583 * pgstat_report_tabstat() -
585 * Called from tcop/postgres.c to send the so far collected
586 * per table access statistics to the collector.
590 pgstat_report_tabstat(void)
594 if (pgStatSock < 0 ||
595 (!pgstat_collect_tuplelevel &&
596 !pgstat_collect_blocklevel))
598 /* Not reporting stats, so just flush whatever we have */
599 RegularTabStat.tsa_used = 0;
600 SharedTabStat.tsa_used = 0;
605 * For each message buffer used during the last query set the header
606 * fields and send it out.
608 for (i = 0; i < RegularTabStat.tsa_used; i++)
610 PgStat_MsgTabstat *tsmsg = RegularTabStat.tsa_messages[i];
614 n = tsmsg->m_nentries;
615 len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
616 n * sizeof(PgStat_TableEntry);
618 tsmsg->m_xact_commit = pgStatXactCommit;
619 tsmsg->m_xact_rollback = pgStatXactRollback;
620 pgStatXactCommit = 0;
621 pgStatXactRollback = 0;
623 pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
624 tsmsg->m_databaseid = MyDatabaseId;
625 pgstat_send(tsmsg, len);
627 RegularTabStat.tsa_used = 0;
629 /* Ditto, for shared relations */
630 for (i = 0; i < SharedTabStat.tsa_used; i++)
632 PgStat_MsgTabstat *tsmsg = SharedTabStat.tsa_messages[i];
636 n = tsmsg->m_nentries;
637 len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
638 n * sizeof(PgStat_TableEntry);
640 /* We don't report transaction commit/abort here */
641 tsmsg->m_xact_commit = 0;
642 tsmsg->m_xact_rollback = 0;
644 pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
645 tsmsg->m_databaseid = InvalidOid;
646 pgstat_send(tsmsg, len);
648 SharedTabStat.tsa_used = 0;
653 * pgstat_vacuum_tabstat() -
655 * Will tell the collector about objects he can get rid of.
659 pgstat_vacuum_tabstat(void)
662 PgStat_MsgTabpurge msg;
663 HASH_SEQ_STATUS hstat;
664 PgStat_StatDBEntry *dbentry;
665 PgStat_StatTabEntry *tabentry;
672 * If not done for this transaction, read the statistics collector stats
673 * file into some hash tables.
675 backend_read_statsfile();
678 * Read pg_database and make a list of OIDs of all existing databases
680 htab = pgstat_collect_oids(DatabaseRelationId);
683 * Search the database hash table for dead databases and tell the
684 * collector to drop them.
686 hash_seq_init(&hstat, pgStatDBHash);
687 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
689 Oid dbid = dbentry->databaseid;
691 CHECK_FOR_INTERRUPTS();
693 if (hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
694 pgstat_drop_database(dbid);
701 * Lookup our own database entry; if not found, nothing more to do.
703 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
704 (void *) &MyDatabaseId,
706 if (dbentry == NULL || dbentry->tables == NULL)
710 * Similarly to above, make a list of all known relations in this DB.
712 htab = pgstat_collect_oids(RelationRelationId);
715 * Initialize our messages table counter to zero
720 * Check for all tables listed in stats hashtable if they still exist.
722 hash_seq_init(&hstat, dbentry->tables);
723 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
725 Oid tabid = tabentry->tableid;
727 CHECK_FOR_INTERRUPTS();
729 if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
733 * Not there, so add this table's Oid to the message
735 msg.m_tableid[msg.m_nentries++] = tabid;
738 * If the message is full, send it out and reinitialize to empty
740 if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
742 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
743 +msg.m_nentries * sizeof(Oid);
745 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
746 msg.m_databaseid = MyDatabaseId;
747 pgstat_send(&msg, len);
756 if (msg.m_nentries > 0)
758 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
759 +msg.m_nentries * sizeof(Oid);
761 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
762 msg.m_databaseid = MyDatabaseId;
763 pgstat_send(&msg, len);
772 * pgstat_collect_oids() -
774 * Collect the OIDs of either all databases or all tables, according to
775 * the parameter, into a temporary hash table. Caller should hash_destroy
776 * the result when done with it.
780 pgstat_collect_oids(Oid catalogid)
788 memset(&hash_ctl, 0, sizeof(hash_ctl));
789 hash_ctl.keysize = sizeof(Oid);
790 hash_ctl.entrysize = sizeof(Oid);
791 hash_ctl.hash = oid_hash;
792 htab = hash_create("Temporary table of OIDs",
793 PGSTAT_TAB_HASH_SIZE,
795 HASH_ELEM | HASH_FUNCTION);
797 rel = heap_open(catalogid, AccessShareLock);
798 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
799 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
801 Oid thisoid = HeapTupleGetOid(tup);
803 CHECK_FOR_INTERRUPTS();
805 (void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
808 heap_close(rel, AccessShareLock);
815 * pgstat_drop_database() -
817 * Tell the collector that we just dropped a database.
818 * (If the message gets lost, we will still clean the dead DB eventually
819 * via future invocations of pgstat_vacuum_tabstat().)
823 pgstat_drop_database(Oid databaseid)
825 PgStat_MsgDropdb msg;
830 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
831 msg.m_databaseid = databaseid;
832 pgstat_send(&msg, sizeof(msg));
837 * pgstat_drop_relation() -
839 * Tell the collector that we just dropped a relation.
840 * (If the message gets lost, we will still clean the dead entry eventually
841 * via future invocations of pgstat_vacuum_tabstat().)
845 pgstat_drop_relation(Oid relid)
847 PgStat_MsgTabpurge msg;
853 msg.m_tableid[0] = relid;
856 len = offsetof(PgStat_MsgTabpurge, m_tableid[0]) +sizeof(Oid);
858 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
859 msg.m_databaseid = MyDatabaseId;
860 pgstat_send(&msg, len);
865 * pgstat_reset_counters() -
867 * Tell the statistics collector to reset counters for our database.
871 pgstat_reset_counters(void)
873 PgStat_MsgResetcounter msg;
880 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
881 errmsg("must be superuser to reset statistics counters")));
883 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
884 msg.m_databaseid = MyDatabaseId;
885 pgstat_send(&msg, sizeof(msg));
890 * pgstat_report_autovac() -
892 * Called from autovacuum.c to report startup of an autovacuum process.
893 * We are called before InitPostgres is done, so can't rely on MyDatabaseId;
894 * the db OID must be passed in, instead.
898 pgstat_report_autovac(Oid dboid)
900 PgStat_MsgAutovacStart msg;
905 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_AUTOVAC_START);
906 msg.m_databaseid = dboid;
907 msg.m_start_time = GetCurrentTimestamp();
909 pgstat_send(&msg, sizeof(msg));
914 * pgstat_report_vacuum() -
916 * Tell the collector about the table we just vacuumed.
920 pgstat_report_vacuum(Oid tableoid, bool shared,
921 bool analyze, PgStat_Counter tuples)
923 PgStat_MsgVacuum msg;
925 if (pgStatSock < 0 ||
926 !pgstat_collect_tuplelevel)
929 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_VACUUM);
930 msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
931 msg.m_tableoid = tableoid;
932 msg.m_analyze = analyze;
933 msg.m_autovacuum = IsAutoVacuumWorkerProcess(); /* is this autovacuum? */
934 msg.m_vacuumtime = GetCurrentTimestamp();
935 msg.m_tuples = tuples;
936 pgstat_send(&msg, sizeof(msg));
940 * pgstat_report_analyze() -
942 * Tell the collector about the table we just analyzed.
946 pgstat_report_analyze(Oid tableoid, bool shared, PgStat_Counter livetuples,
947 PgStat_Counter deadtuples)
949 PgStat_MsgAnalyze msg;
951 if (pgStatSock < 0 ||
952 !pgstat_collect_tuplelevel)
955 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ANALYZE);
956 msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
957 msg.m_tableoid = tableoid;
958 msg.m_autovacuum = IsAutoVacuumWorkerProcess(); /* is this autovacuum? */
959 msg.m_analyzetime = GetCurrentTimestamp();
960 msg.m_live_tuples = livetuples;
961 msg.m_dead_tuples = deadtuples;
962 pgstat_send(&msg, sizeof(msg));
969 * Send some junk data to the collector to increase traffic.
980 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DUMMY);
981 pgstat_send(&msg, sizeof(msg));
985 * Enlarge a TabStatArray
988 more_tabstat_space(TabStatArray *tsarr)
990 PgStat_MsgTabstat *newMessages;
991 PgStat_MsgTabstat **msgArray;
995 AssertArg(PointerIsValid(tsarr));
997 newAlloc = tsarr->tsa_alloc + TABSTAT_QUANTUM;
999 /* Create (another) quantum of message buffers */
1000 newMessages = (PgStat_MsgTabstat *)
1001 MemoryContextAllocZero(TopMemoryContext,
1002 sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
1004 /* Create or enlarge the pointer array */
1005 if (tsarr->tsa_messages == NULL)
1006 msgArray = (PgStat_MsgTabstat **)
1007 MemoryContextAlloc(TopMemoryContext,
1008 sizeof(PgStat_MsgTabstat *) * newAlloc);
1010 msgArray = (PgStat_MsgTabstat **)
1011 repalloc(tsarr->tsa_messages,
1012 sizeof(PgStat_MsgTabstat *) * newAlloc);
1014 for (i = 0; i < TABSTAT_QUANTUM; i++)
1015 msgArray[tsarr->tsa_alloc + i] = newMessages++;
1016 tsarr->tsa_messages = msgArray;
1017 tsarr->tsa_alloc = newAlloc;
1019 Assert(tsarr->tsa_used < tsarr->tsa_alloc);
1023 * pgstat_initstats() -
1025 * Called from various places usually dealing with initialization
1026 * of Relation or Scan structures. The data placed into these
1027 * structures from here tell where later to count for buffer reads,
1028 * scans and tuples fetched.
1032 pgstat_initstats(PgStat_Info *stats, Relation rel)
1034 Oid rel_id = rel->rd_id;
1035 PgStat_TableEntry *useent;
1036 TabStatArray *tsarr;
1037 PgStat_MsgTabstat *tsmsg;
1042 * Initialize data not to count at all.
1044 stats->tabentry = NULL;
1046 if (pgStatSock < 0 ||
1047 !(pgstat_collect_tuplelevel ||
1048 pgstat_collect_blocklevel))
1051 tsarr = rel->rd_rel->relisshared ? &SharedTabStat : &RegularTabStat;
1054 * Search the already-used message slots for this relation.
1056 for (mb = 0; mb < tsarr->tsa_used; mb++)
1058 tsmsg = tsarr->tsa_messages[mb];
1060 for (i = tsmsg->m_nentries; --i >= 0;)
1062 if (tsmsg->m_entry[i].t_id == rel_id)
1064 stats->tabentry = (void *) &(tsmsg->m_entry[i]);
1069 if (tsmsg->m_nentries >= PGSTAT_NUM_TABENTRIES)
1073 * Not found, but found a message buffer with an empty slot instead.
1074 * Fine, let's use this one.
1076 i = tsmsg->m_nentries++;
1077 useent = &tsmsg->m_entry[i];
1078 MemSet(useent, 0, sizeof(PgStat_TableEntry));
1079 useent->t_id = rel_id;
1080 stats->tabentry = (void *) useent;
1085 * If we ran out of message buffers, we just allocate more.
1087 if (tsarr->tsa_used >= tsarr->tsa_alloc)
1088 more_tabstat_space(tsarr);
1091 * Use the first entry of the next message buffer.
1093 mb = tsarr->tsa_used++;
1094 tsmsg = tsarr->tsa_messages[mb];
1095 tsmsg->m_nentries = 1;
1096 useent = &tsmsg->m_entry[0];
1097 MemSet(useent, 0, sizeof(PgStat_TableEntry));
1098 useent->t_id = rel_id;
1099 stats->tabentry = (void *) useent;
1104 * pgstat_count_xact_commit() -
1106 * Called from access/transam/xact.c to count transaction commits.
1110 pgstat_count_xact_commit(void)
1112 if (!pgstat_collect_tuplelevel &&
1113 !pgstat_collect_blocklevel)
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_count_xact_rollback() -
1137 * Called from access/transam/xact.c to count transaction rollbacks.
1141 pgstat_count_xact_rollback(void)
1143 if (!pgstat_collect_tuplelevel &&
1144 !pgstat_collect_blocklevel)
1147 pgStatXactRollback++;
1150 * If there was no relation activity yet, just make one existing message
1151 * buffer used without slots, causing the next report to tell new
1154 if (RegularTabStat.tsa_alloc == 0)
1155 more_tabstat_space(&RegularTabStat);
1157 if (RegularTabStat.tsa_used == 0)
1159 RegularTabStat.tsa_used++;
1160 RegularTabStat.tsa_messages[0]->m_nentries = 0;
1166 * pgstat_fetch_stat_dbentry() -
1168 * Support function for the SQL-callable pgstat* functions. Returns
1169 * the collected statistics for one database or NULL. NULL doesn't mean
1170 * that the database doesn't exist, it is just not yet known by the
1171 * collector, so the caller is better off to report ZERO instead.
1174 PgStat_StatDBEntry *
1175 pgstat_fetch_stat_dbentry(Oid dbid)
1178 * If not done for this transaction, read the statistics collector stats
1179 * file into some hash tables.
1181 backend_read_statsfile();
1184 * Lookup the requested database; return NULL if not found
1186 return (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1193 * pgstat_fetch_stat_tabentry() -
1195 * Support function for the SQL-callable pgstat* functions. Returns
1196 * the collected statistics for one table or NULL. NULL doesn't mean
1197 * that the table doesn't exist, it is just not yet known by the
1198 * collector, so the caller is better off to report ZERO instead.
1201 PgStat_StatTabEntry *
1202 pgstat_fetch_stat_tabentry(Oid relid)
1205 PgStat_StatDBEntry *dbentry;
1206 PgStat_StatTabEntry *tabentry;
1209 * If not done for this transaction, read the statistics collector stats
1210 * file into some hash tables.
1212 backend_read_statsfile();
1215 * Lookup our database, then look in its table hash table.
1217 dbid = MyDatabaseId;
1218 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1221 if (dbentry != NULL && dbentry->tables != NULL)
1223 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1231 * If we didn't find it, maybe it's a shared table.
1234 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1237 if (dbentry != NULL && dbentry->tables != NULL)
1239 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1251 * pgstat_fetch_stat_beentry() -
1253 * Support function for the SQL-callable pgstat* functions. Returns
1254 * our local copy of the current-activity entry for one backend.
1256 * NB: caller is responsible for a check if the user is permitted to see
1257 * this info (especially the querystring).
1261 pgstat_fetch_stat_beentry(int beid)
1263 pgstat_read_current_status();
1265 if (beid < 1 || beid > localNumBackends)
1268 return &localBackendStatusTable[beid - 1];
1273 * pgstat_fetch_stat_numbackends() -
1275 * Support function for the SQL-callable pgstat* functions. Returns
1276 * the maximum current backend id.
1280 pgstat_fetch_stat_numbackends(void)
1282 pgstat_read_current_status();
1284 return localNumBackends;
1288 /* ------------------------------------------------------------
1289 * Functions for management of the shared-memory PgBackendStatus array
1290 * ------------------------------------------------------------
1293 static PgBackendStatus *BackendStatusArray = NULL;
1294 static PgBackendStatus *MyBEEntry = NULL;
1298 * Report shared-memory space needed by CreateSharedBackendStatus.
1301 BackendStatusShmemSize(void)
1305 size = mul_size(sizeof(PgBackendStatus), MaxBackends);
1310 * Initialize the shared status array during postmaster startup.
1313 CreateSharedBackendStatus(void)
1315 Size size = BackendStatusShmemSize();
1318 /* Create or attach to the shared array */
1319 BackendStatusArray = (PgBackendStatus *)
1320 ShmemInitStruct("Backend Status Array", size, &found);
1325 * We're the first - initialize.
1327 MemSet(BackendStatusArray, 0, size);
1333 * pgstat_bestart() -
1335 * Initialize this backend's entry in the PgBackendStatus array,
1336 * and set up an on-proc-exit hook that will clear it again.
1337 * Called from InitPostgres. MyBackendId and MyDatabaseId must be set.
1341 pgstat_bestart(void)
1343 volatile PgBackendStatus *beentry;
1344 TimestampTz proc_start_timestamp;
1346 SockAddr clientaddr;
1348 Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
1349 MyBEEntry = &BackendStatusArray[MyBackendId - 1];
1352 * To minimize the time spent modifying the entry, fetch all the needed
1355 * If we have a MyProcPort, use its session start time (for consistency,
1356 * and to save a kernel call).
1359 proc_start_timestamp = MyProcPort->SessionStartTime;
1361 proc_start_timestamp = GetCurrentTimestamp();
1362 userid = GetSessionUserId();
1365 * We may not have a MyProcPort (eg, if this is the autovacuum process).
1366 * If so, use all-zeroes client address, which is dealt with specially in
1367 * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
1370 memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
1372 MemSet(&clientaddr, 0, sizeof(clientaddr));
1375 * Initialize my status entry, following the protocol of bumping
1376 * st_changecount before and after; and make sure it's even afterwards. We
1377 * use a volatile pointer here to ensure the compiler doesn't try to get
1380 beentry = MyBEEntry;
1383 beentry->st_changecount++;
1384 } while ((beentry->st_changecount & 1) == 0);
1386 beentry->st_procpid = MyProcPid;
1387 beentry->st_proc_start_timestamp = proc_start_timestamp;
1388 beentry->st_activity_start_timestamp = 0;
1389 beentry->st_txn_start_timestamp = 0;
1390 beentry->st_databaseid = MyDatabaseId;
1391 beentry->st_userid = userid;
1392 beentry->st_clientaddr = clientaddr;
1393 beentry->st_waiting = false;
1394 beentry->st_activity[0] = '\0';
1395 /* Also make sure the last byte in the string area is always 0 */
1396 beentry->st_activity[PGBE_ACTIVITY_SIZE - 1] = '\0';
1398 beentry->st_changecount++;
1399 Assert((beentry->st_changecount & 1) == 0);
1402 * Set up a process-exit hook to clean up.
1404 on_shmem_exit(pgstat_beshutdown_hook, 0);
1408 * Shut down a single backend's statistics reporting at process exit.
1410 * Flush any remaining statistics counts out to the collector.
1411 * Without this, operations triggered during backend exit (such as
1412 * temp table deletions) won't be counted.
1414 * Lastly, clear out our entry in the PgBackendStatus array.
1417 pgstat_beshutdown_hook(int code, Datum arg)
1419 volatile PgBackendStatus *beentry = MyBEEntry;
1421 pgstat_report_tabstat();
1424 * Clear my status entry, following the protocol of bumping st_changecount
1425 * before and after. We use a volatile pointer here to ensure the
1426 * compiler doesn't try to get cute.
1428 beentry->st_changecount++;
1430 beentry->st_procpid = 0; /* mark invalid */
1432 beentry->st_changecount++;
1433 Assert((beentry->st_changecount & 1) == 0);
1438 * pgstat_report_activity() -
1440 * Called from tcop/postgres.c to report what the backend is actually doing
1441 * (usually "<IDLE>" or the start of the query to be executed).
1445 pgstat_report_activity(const char *cmd_str)
1447 volatile PgBackendStatus *beentry = MyBEEntry;
1448 TimestampTz start_timestamp;
1451 if (!pgstat_collect_querystring || !beentry)
1455 * To minimize the time spent modifying the entry, fetch all the needed
1458 start_timestamp = GetCurrentStatementStartTimestamp();
1460 len = strlen(cmd_str);
1461 len = pg_mbcliplen(cmd_str, len, PGBE_ACTIVITY_SIZE - 1);
1464 * Update my status entry, following the protocol of bumping
1465 * st_changecount before and after. We use a volatile pointer here to
1466 * ensure the compiler doesn't try to get cute.
1468 beentry->st_changecount++;
1470 beentry->st_activity_start_timestamp = start_timestamp;
1471 memcpy((char *) beentry->st_activity, cmd_str, len);
1472 beentry->st_activity[len] = '\0';
1474 beentry->st_changecount++;
1475 Assert((beentry->st_changecount & 1) == 0);
1479 * Set the current transaction start timestamp to the specified
1480 * value. If there is no current active transaction, this is signified
1484 pgstat_report_txn_timestamp(TimestampTz tstamp)
1486 volatile PgBackendStatus *beentry = MyBEEntry;
1488 if (!pgstat_collect_querystring || !beentry)
1492 * Update my status entry, following the protocol of bumping
1493 * st_changecount before and after. We use a volatile pointer
1494 * here to ensure the compiler doesn't try to get cute.
1496 beentry->st_changecount++;
1497 beentry->st_txn_start_timestamp = tstamp;
1498 beentry->st_changecount++;
1499 Assert((beentry->st_changecount & 1) == 0);
1503 * pgstat_report_waiting() -
1505 * Called from lock manager to report beginning or end of a lock wait.
1507 * NB: this *must* be able to survive being called before MyBEEntry has been
1512 pgstat_report_waiting(bool waiting)
1514 volatile PgBackendStatus *beentry = MyBEEntry;
1516 if (!pgstat_collect_querystring || !beentry)
1520 * Since this is a single-byte field in a struct that only this process
1521 * may modify, there seems no need to bother with the st_changecount
1522 * protocol. The update must appear atomic in any case.
1524 beentry->st_waiting = waiting;
1529 * pgstat_read_current_status() -
1531 * Copy the current contents of the PgBackendStatus array to local memory,
1532 * if not already done in this transaction.
1536 pgstat_read_current_status(void)
1538 volatile PgBackendStatus *beentry;
1539 PgBackendStatus *localtable;
1540 PgBackendStatus *localentry;
1543 Assert(!pgStatRunningInCollector);
1544 if (localBackendStatusTable)
1545 return; /* already done */
1547 pgstat_setup_memcxt();
1549 localtable = (PgBackendStatus *)
1550 MemoryContextAlloc(pgStatLocalContext,
1551 sizeof(PgBackendStatus) * MaxBackends);
1552 localNumBackends = 0;
1554 beentry = BackendStatusArray;
1555 localentry = localtable;
1556 for (i = 1; i <= MaxBackends; i++)
1559 * Follow the protocol of retrying if st_changecount changes while we
1560 * copy the entry, or if it's odd. (The check for odd is needed to
1561 * cover the case where we are able to completely copy the entry while
1562 * the source backend is between increment steps.) We use a volatile
1563 * pointer here to ensure the compiler doesn't try to get cute.
1567 int save_changecount = beentry->st_changecount;
1570 * XXX if PGBE_ACTIVITY_SIZE is really large, it might be best to
1571 * use strcpy not memcpy for copying the activity string?
1573 memcpy(localentry, (char *) beentry, sizeof(PgBackendStatus));
1575 if (save_changecount == beentry->st_changecount &&
1576 (save_changecount & 1) == 0)
1579 /* Make sure we can break out of loop if stuck... */
1580 CHECK_FOR_INTERRUPTS();
1584 /* Only valid entries get included into the local array */
1585 if (localentry->st_procpid > 0)
1592 /* Set the pointer only after completion of a valid table */
1593 localBackendStatusTable = localtable;
1597 /* ------------------------------------------------------------
1598 * Local support functions follow
1599 * ------------------------------------------------------------
1604 * pgstat_setheader() -
1606 * Set common header fields in a statistics message
1610 pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
1612 hdr->m_type = mtype;
1619 * Send out one statistics message to the collector
1623 pgstat_send(void *msg, int len)
1630 ((PgStat_MsgHdr *) msg)->m_size = len;
1632 /* We'll retry after EINTR, but ignore all other failures */
1635 rc = send(pgStatSock, msg, len, 0);
1636 } while (rc < 0 && errno == EINTR);
1638 #ifdef USE_ASSERT_CHECKING
1639 /* In debug builds, log send failures ... */
1641 elog(LOG, "could not send to statistics collector: %m");
1647 * PgstatCollectorMain() -
1649 * Start up the statistics collector process. This is the body of the
1650 * postmaster child process.
1652 * The argc/argv parameters are valid only in EXEC_BACKEND case.
1655 NON_EXEC_STATIC void
1656 PgstatCollectorMain(int argc, char *argv[])
1658 struct itimerval write_timeout;
1659 bool need_timer = false;
1665 struct pollfd input_fd;
1667 struct timeval sel_timeout;
1672 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
1674 MyProcPid = getpid(); /* reset MyProcPid */
1677 * If possible, make this process a group leader, so that the postmaster
1678 * can signal any child processes too. (pgstat probably never has
1679 * any child processes, but for consistency we make all postmaster
1680 * child processes do this.)
1684 elog(FATAL, "setsid() failed: %m");
1688 * Ignore all signals usually bound to some action in the postmaster,
1689 * except SIGQUIT and SIGALRM.
1691 pqsignal(SIGHUP, SIG_IGN);
1692 pqsignal(SIGINT, SIG_IGN);
1693 pqsignal(SIGTERM, SIG_IGN);
1694 pqsignal(SIGQUIT, pgstat_exit);
1695 pqsignal(SIGALRM, force_statwrite);
1696 pqsignal(SIGPIPE, SIG_IGN);
1697 pqsignal(SIGUSR1, SIG_IGN);
1698 pqsignal(SIGUSR2, SIG_IGN);
1699 pqsignal(SIGCHLD, SIG_DFL);
1700 pqsignal(SIGTTIN, SIG_DFL);
1701 pqsignal(SIGTTOU, SIG_DFL);
1702 pqsignal(SIGCONT, SIG_DFL);
1703 pqsignal(SIGWINCH, SIG_DFL);
1704 PG_SETMASK(&UnBlockSig);
1707 * Identify myself via ps
1709 init_ps_display("stats collector process", "", "", "");
1712 * Arrange to write the initial status file right away
1714 need_statwrite = true;
1716 /* Preset the delay between status file writes */
1717 MemSet(&write_timeout, 0, sizeof(struct itimerval));
1718 write_timeout.it_value.tv_sec = PGSTAT_STAT_INTERVAL / 1000;
1719 write_timeout.it_value.tv_usec = PGSTAT_STAT_INTERVAL % 1000;
1722 * Read in an existing statistics stats file or initialize the stats to
1725 pgStatRunningInCollector = true;
1726 pgStatDBHash = pgstat_read_statsfile(InvalidOid);
1729 * Setup the descriptor set for select(2). Since only one bit in the set
1730 * ever changes, we need not repeat FD_ZERO each time.
1732 #if !defined(HAVE_POLL) && !defined(WIN32)
1737 * Loop to process messages until we get SIGQUIT or detect ungraceful
1738 * death of our parent postmaster.
1740 * For performance reasons, we don't want to do a PostmasterIsAlive() test
1741 * after every message; instead, do it at statwrite time and if
1742 * select()/poll() is interrupted by timeout.
1749 * Quit if we get SIGQUIT from the postmaster.
1755 * If time to write the stats file, do so. Note that the alarm
1756 * interrupt isn't re-enabled immediately, but only after we next
1757 * receive a stats message; so no cycles are wasted when there is
1762 /* Check for postmaster death; if so we'll write file below */
1763 if (!PostmasterIsAlive(true))
1766 pgstat_write_statsfile();
1767 need_statwrite = false;
1772 * Wait for a message to arrive; but not for more than
1773 * PGSTAT_SELECT_TIMEOUT seconds. (This determines how quickly we will
1774 * shut down after an ungraceful postmaster termination; so it needn't
1775 * be very fast. However, on some systems SIGQUIT won't interrupt the
1776 * poll/select call, so this also limits speed of response to SIGQUIT,
1777 * which is more important.)
1779 * We use poll(2) if available, otherwise select(2).
1780 * Win32 has its own implementation.
1784 input_fd.fd = pgStatSock;
1785 input_fd.events = POLLIN | POLLERR;
1786 input_fd.revents = 0;
1788 if (poll(&input_fd, 1, PGSTAT_SELECT_TIMEOUT * 1000) < 0)
1793 (errcode_for_socket_access(),
1794 errmsg("poll() failed in statistics collector: %m")));
1797 got_data = (input_fd.revents != 0);
1798 #else /* !HAVE_POLL */
1800 FD_SET(pgStatSock, &rfds);
1803 * timeout struct is modified by select() on some operating systems,
1804 * so re-fill it each time.
1806 sel_timeout.tv_sec = PGSTAT_SELECT_TIMEOUT;
1807 sel_timeout.tv_usec = 0;
1809 if (select(pgStatSock + 1, &rfds, NULL, NULL, &sel_timeout) < 0)
1814 (errcode_for_socket_access(),
1815 errmsg("select() failed in statistics collector: %m")));
1818 got_data = FD_ISSET(pgStatSock, &rfds);
1819 #endif /* HAVE_POLL */
1821 got_data = pgwin32_waitforsinglesocket(pgStatSock, FD_READ,
1822 PGSTAT_SELECT_TIMEOUT*1000);
1826 * If there is a message on the socket, read it and check for
1831 len = recv(pgStatSock, (char *) &msg,
1832 sizeof(PgStat_Msg), 0);
1838 (errcode_for_socket_access(),
1839 errmsg("could not read statistics message: %m")));
1843 * We ignore messages that are smaller than our common header
1845 if (len < sizeof(PgStat_MsgHdr))
1849 * The received length must match the length in the header
1851 if (msg.msg_hdr.m_size != len)
1855 * O.K. - we accept this message. Process it.
1857 switch (msg.msg_hdr.m_type)
1859 case PGSTAT_MTYPE_DUMMY:
1862 case PGSTAT_MTYPE_TABSTAT:
1863 pgstat_recv_tabstat((PgStat_MsgTabstat *) &msg, len);
1866 case PGSTAT_MTYPE_TABPURGE:
1867 pgstat_recv_tabpurge((PgStat_MsgTabpurge *) &msg, len);
1870 case PGSTAT_MTYPE_DROPDB:
1871 pgstat_recv_dropdb((PgStat_MsgDropdb *) &msg, len);
1874 case PGSTAT_MTYPE_RESETCOUNTER:
1875 pgstat_recv_resetcounter((PgStat_MsgResetcounter *) &msg,
1879 case PGSTAT_MTYPE_AUTOVAC_START:
1880 pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
1883 case PGSTAT_MTYPE_VACUUM:
1884 pgstat_recv_vacuum((PgStat_MsgVacuum *) &msg, len);
1887 case PGSTAT_MTYPE_ANALYZE:
1888 pgstat_recv_analyze((PgStat_MsgAnalyze *) &msg, len);
1896 * If this is the first message after we wrote the stats file the
1897 * last time, enable the alarm interrupt to make it be written
1902 if (setitimer(ITIMER_REAL, &write_timeout, NULL))
1904 (errmsg("could not set statistics collector timer: %m")));
1911 * We can only get here if the select/poll timeout elapsed. Check
1912 * for postmaster death.
1914 if (!PostmasterIsAlive(true))
1917 } /* end of message-processing loop */
1920 * Save the final stats to reuse at next startup.
1922 pgstat_write_statsfile();
1928 /* SIGQUIT signal handler for collector process */
1930 pgstat_exit(SIGNAL_ARGS)
1935 /* SIGALRM signal handler for collector process */
1937 force_statwrite(SIGNAL_ARGS)
1939 need_statwrite = true;
1944 * Lookup the hash table entry for the specified database. If no hash
1945 * table entry exists, initialize it, if the create parameter is true.
1946 * Else, return NULL.
1948 static PgStat_StatDBEntry *
1949 pgstat_get_db_entry(Oid databaseid, bool create)
1951 PgStat_StatDBEntry *result;
1953 HASHACTION action = (create ? HASH_ENTER : HASH_FIND);
1955 /* Lookup or create the hash table entry for this database */
1956 result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1960 if (!create && !found)
1963 /* If not found, initialize the new one. */
1968 result->tables = NULL;
1969 result->n_xact_commit = 0;
1970 result->n_xact_rollback = 0;
1971 result->n_blocks_fetched = 0;
1972 result->n_blocks_hit = 0;
1973 result->last_autovac_time = 0;
1975 memset(&hash_ctl, 0, sizeof(hash_ctl));
1976 hash_ctl.keysize = sizeof(Oid);
1977 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
1978 hash_ctl.hash = oid_hash;
1979 result->tables = hash_create("Per-database table",
1980 PGSTAT_TAB_HASH_SIZE,
1982 HASH_ELEM | HASH_FUNCTION);
1990 * pgstat_write_statsfile() -
1996 pgstat_write_statsfile(void)
1998 HASH_SEQ_STATUS hstat;
1999 HASH_SEQ_STATUS tstat;
2000 PgStat_StatDBEntry *dbentry;
2001 PgStat_StatTabEntry *tabentry;
2006 * Open the statistics temp file to write out the current values.
2008 fpout = fopen(PGSTAT_STAT_TMPFILE, PG_BINARY_W);
2012 (errcode_for_file_access(),
2013 errmsg("could not open temporary statistics file \"%s\": %m",
2014 PGSTAT_STAT_TMPFILE)));
2019 * Write the file header --- currently just a format ID.
2021 format_id = PGSTAT_FILE_FORMAT_ID;
2022 fwrite(&format_id, sizeof(format_id), 1, fpout);
2025 * Walk through the database table.
2027 hash_seq_init(&hstat, pgStatDBHash);
2028 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
2031 * Write out the DB entry including the number of live backends. We
2032 * don't write the tables pointer since it's of no use to any other
2036 fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
2039 * Walk through the database's access stats per table.
2041 hash_seq_init(&tstat, dbentry->tables);
2042 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
2045 fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
2049 * Mark the end of this DB
2055 * No more output to be done. Close the temp file and replace the old
2056 * pgstat.stat with it. The ferror() check replaces testing for error
2057 * after each individual fputc or fwrite above.
2064 (errcode_for_file_access(),
2065 errmsg("could not write temporary statistics file \"%s\": %m",
2066 PGSTAT_STAT_TMPFILE)));
2068 unlink(PGSTAT_STAT_TMPFILE);
2070 else if (fclose(fpout) < 0)
2073 (errcode_for_file_access(),
2074 errmsg("could not close temporary statistics file \"%s\": %m",
2075 PGSTAT_STAT_TMPFILE)));
2076 unlink(PGSTAT_STAT_TMPFILE);
2078 else if (rename(PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME) < 0)
2081 (errcode_for_file_access(),
2082 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
2083 PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME)));
2084 unlink(PGSTAT_STAT_TMPFILE);
2090 * pgstat_read_statsfile() -
2092 * Reads in an existing statistics collector file and initializes the
2093 * databases' hash table (whose entries point to the tables' hash tables).
2097 pgstat_read_statsfile(Oid onlydb)
2099 PgStat_StatDBEntry *dbentry;
2100 PgStat_StatDBEntry dbbuf;
2101 PgStat_StatTabEntry *tabentry;
2102 PgStat_StatTabEntry tabbuf;
2105 HTAB *tabhash = NULL;
2111 * The tables will live in pgStatLocalContext.
2113 pgstat_setup_memcxt();
2116 * Create the DB hashtable
2118 memset(&hash_ctl, 0, sizeof(hash_ctl));
2119 hash_ctl.keysize = sizeof(Oid);
2120 hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
2121 hash_ctl.hash = oid_hash;
2122 hash_ctl.hcxt = pgStatLocalContext;
2123 dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
2124 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
2127 * Try to open the status file. If it doesn't exist, the backends simply
2128 * return zero for anything and the collector simply starts from scratch
2129 * with empty counters.
2131 if ((fpin = AllocateFile(PGSTAT_STAT_FILENAME, PG_BINARY_R)) == NULL)
2135 * Verify it's of the expected format.
2137 if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id)
2138 || format_id != PGSTAT_FILE_FORMAT_ID)
2140 ereport(pgStatRunningInCollector ? LOG : WARNING,
2141 (errmsg("corrupted pgstat.stat file")));
2146 * We found an existing collector stats file. Read it and put all the
2147 * hashtable entries into place.
2151 switch (fgetc(fpin))
2154 * 'D' A PgStat_StatDBEntry struct describing a database
2155 * follows. Subsequently, zero to many 'T' entries will follow
2156 * until a 'd' is encountered.
2159 if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
2160 fpin) != offsetof(PgStat_StatDBEntry, tables))
2162 ereport(pgStatRunningInCollector ? LOG : WARNING,
2163 (errmsg("corrupted pgstat.stat file")));
2168 * Add to the DB hash
2170 dbentry = (PgStat_StatDBEntry *) hash_search(dbhash,
2171 (void *) &dbbuf.databaseid,
2176 ereport(pgStatRunningInCollector ? LOG : WARNING,
2177 (errmsg("corrupted pgstat.stat file")));
2181 memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
2182 dbentry->tables = NULL;
2185 * Don't collect tables if not the requested DB (or the
2186 * shared-table info)
2188 if (onlydb != InvalidOid)
2190 if (dbbuf.databaseid != onlydb &&
2191 dbbuf.databaseid != InvalidOid)
2195 memset(&hash_ctl, 0, sizeof(hash_ctl));
2196 hash_ctl.keysize = sizeof(Oid);
2197 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2198 hash_ctl.hash = oid_hash;
2199 hash_ctl.hcxt = pgStatLocalContext;
2200 dbentry->tables = hash_create("Per-database table",
2201 PGSTAT_TAB_HASH_SIZE,
2203 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
2206 * Arrange that following 'T's add entries to this database's
2207 * tables hash table.
2209 tabhash = dbentry->tables;
2213 * 'd' End of this database.
2220 * 'T' A PgStat_StatTabEntry follows.
2223 if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
2224 fpin) != sizeof(PgStat_StatTabEntry))
2226 ereport(pgStatRunningInCollector ? LOG : WARNING,
2227 (errmsg("corrupted pgstat.stat file")));
2232 * Skip if table belongs to a not requested database.
2234 if (tabhash == NULL)
2237 tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
2238 (void *) &tabbuf.tableid,
2239 HASH_ENTER, &found);
2243 ereport(pgStatRunningInCollector ? LOG : WARNING,
2244 (errmsg("corrupted pgstat.stat file")));
2248 memcpy(tabentry, &tabbuf, sizeof(tabbuf));
2252 * 'E' The EOF marker of a complete stats file.
2258 ereport(pgStatRunningInCollector ? LOG : WARNING,
2259 (errmsg("corrupted pgstat.stat file")));
2271 * If not already done, read the statistics collector stats file into
2272 * some hash tables. The results will be kept until pgstat_clear_snapshot()
2273 * is called (typically, at end of transaction).
2276 backend_read_statsfile(void)
2278 /* already read it? */
2281 Assert(!pgStatRunningInCollector);
2283 /* Autovacuum launcher wants stats about all databases */
2284 if (IsAutoVacuumLauncherProcess())
2285 pgStatDBHash = pgstat_read_statsfile(InvalidOid);
2287 pgStatDBHash = pgstat_read_statsfile(MyDatabaseId);
2292 * pgstat_setup_memcxt() -
2294 * Create pgStatLocalContext, if not already done.
2298 pgstat_setup_memcxt(void)
2300 if (!pgStatLocalContext)
2301 pgStatLocalContext = AllocSetContextCreate(TopMemoryContext,
2302 "Statistics snapshot",
2303 ALLOCSET_SMALL_MINSIZE,
2304 ALLOCSET_SMALL_INITSIZE,
2305 ALLOCSET_SMALL_MAXSIZE);
2310 * pgstat_clear_snapshot() -
2312 * Discard any data collected in the current transaction. Any subsequent
2313 * request will cause new snapshots to be read.
2315 * This is also invoked during transaction commit or abort to discard
2316 * the no-longer-wanted snapshot.
2320 pgstat_clear_snapshot(void)
2322 /* In an autovacuum worker process we keep the stats forever */
2323 if (IsAutoVacuumWorkerProcess())
2326 /* Release memory, if any was allocated */
2327 if (pgStatLocalContext)
2328 MemoryContextDelete(pgStatLocalContext);
2330 /* Reset variables */
2331 pgStatLocalContext = NULL;
2332 pgStatDBHash = NULL;
2333 localBackendStatusTable = NULL;
2334 localNumBackends = 0;
2339 * pgstat_recv_tabstat() -
2341 * Count what the backend has done.
2345 pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
2347 PgStat_TableEntry *tabmsg = &(msg->m_entry[0]);
2348 PgStat_StatDBEntry *dbentry;
2349 PgStat_StatTabEntry *tabentry;
2353 dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
2356 * Update database-wide stats.
2358 dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
2359 dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
2362 * Process all table entries in the message.
2364 for (i = 0; i < msg->m_nentries; i++)
2366 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2367 (void *) &(tabmsg[i].t_id),
2368 HASH_ENTER, &found);
2373 * If it's a new table entry, initialize counters to the values we
2376 tabentry->numscans = tabmsg[i].t_numscans;
2377 tabentry->tuples_returned = tabmsg[i].t_tuples_returned;
2378 tabentry->tuples_fetched = tabmsg[i].t_tuples_fetched;
2379 tabentry->tuples_inserted = tabmsg[i].t_tuples_inserted;
2380 tabentry->tuples_updated = tabmsg[i].t_tuples_updated;
2381 tabentry->tuples_deleted = tabmsg[i].t_tuples_deleted;
2383 tabentry->n_live_tuples = tabmsg[i].t_tuples_inserted;
2384 tabentry->n_dead_tuples = tabmsg[i].t_tuples_updated +
2385 tabmsg[i].t_tuples_deleted;
2386 tabentry->last_anl_tuples = 0;
2387 tabentry->vacuum_timestamp = 0;
2388 tabentry->autovac_vacuum_timestamp = 0;
2389 tabentry->analyze_timestamp = 0;
2390 tabentry->autovac_analyze_timestamp = 0;
2392 tabentry->blocks_fetched = tabmsg[i].t_blocks_fetched;
2393 tabentry->blocks_hit = tabmsg[i].t_blocks_hit;
2398 * Otherwise add the values to the existing entry.
2400 tabentry->numscans += tabmsg[i].t_numscans;
2401 tabentry->tuples_returned += tabmsg[i].t_tuples_returned;
2402 tabentry->tuples_fetched += tabmsg[i].t_tuples_fetched;
2403 tabentry->tuples_inserted += tabmsg[i].t_tuples_inserted;
2404 tabentry->tuples_updated += tabmsg[i].t_tuples_updated;
2405 tabentry->tuples_deleted += tabmsg[i].t_tuples_deleted;
2407 tabentry->n_live_tuples += tabmsg[i].t_tuples_inserted -
2408 tabmsg[i].t_tuples_deleted;
2409 tabentry->n_dead_tuples += tabmsg[i].t_tuples_updated +
2410 tabmsg[i].t_tuples_deleted;
2412 tabentry->blocks_fetched += tabmsg[i].t_blocks_fetched;
2413 tabentry->blocks_hit += tabmsg[i].t_blocks_hit;
2417 * And add the block IO to the database entry.
2419 dbentry->n_blocks_fetched += tabmsg[i].t_blocks_fetched;
2420 dbentry->n_blocks_hit += tabmsg[i].t_blocks_hit;
2426 * pgstat_recv_tabpurge() -
2428 * Arrange for dead table removal.
2432 pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
2434 PgStat_StatDBEntry *dbentry;
2437 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2440 * No need to purge if we don't even know the database.
2442 if (!dbentry || !dbentry->tables)
2446 * Process all table entries in the message.
2448 for (i = 0; i < msg->m_nentries; i++)
2450 /* Remove from hashtable if present; we don't care if it's not. */
2451 (void) hash_search(dbentry->tables,
2452 (void *) &(msg->m_tableid[i]),
2459 * pgstat_recv_dropdb() -
2461 * Arrange for dead database removal
2465 pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len)
2467 PgStat_StatDBEntry *dbentry;
2470 * Lookup the database in the hashtable.
2472 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2475 * If found, remove it.
2479 if (dbentry->tables != NULL)
2480 hash_destroy(dbentry->tables);
2482 if (hash_search(pgStatDBHash,
2483 (void *) &(dbentry->databaseid),
2484 HASH_REMOVE, NULL) == NULL)
2486 (errmsg("database hash table corrupted "
2487 "during cleanup --- abort")));
2493 * pgstat_recv_resetcounter() -
2495 * Reset the statistics for the specified database.
2499 pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
2502 PgStat_StatDBEntry *dbentry;
2505 * Lookup the database in the hashtable. Nothing to do if not there.
2507 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2513 * We simply throw away all the database's table entries by recreating a
2514 * new hash table for them.
2516 if (dbentry->tables != NULL)
2517 hash_destroy(dbentry->tables);
2519 dbentry->tables = NULL;
2520 dbentry->n_xact_commit = 0;
2521 dbentry->n_xact_rollback = 0;
2522 dbentry->n_blocks_fetched = 0;
2523 dbentry->n_blocks_hit = 0;
2525 memset(&hash_ctl, 0, sizeof(hash_ctl));
2526 hash_ctl.keysize = sizeof(Oid);
2527 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2528 hash_ctl.hash = oid_hash;
2529 dbentry->tables = hash_create("Per-database table",
2530 PGSTAT_TAB_HASH_SIZE,
2532 HASH_ELEM | HASH_FUNCTION);
2536 * pgstat_recv_autovac() -
2538 * Process an autovacuum signalling message.
2542 pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len)
2544 PgStat_StatDBEntry *dbentry;
2547 * Lookup the database in the hashtable. Don't create the entry if it
2548 * doesn't exist, because autovacuum may be processing a template
2549 * database. If this isn't the case, the database is most likely to have
2550 * an entry already. (If it doesn't, not much harm is done anyway --
2551 * it'll get created as soon as somebody actually uses the database.)
2553 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2554 if (dbentry == NULL)
2558 * Store the last autovacuum time in the database entry.
2560 dbentry->last_autovac_time = msg->m_start_time;
2564 * pgstat_recv_vacuum() -
2566 * Process a VACUUM message.
2570 pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len)
2572 PgStat_StatDBEntry *dbentry;
2573 PgStat_StatTabEntry *tabentry;
2576 * Don't create either the database or table entry if it doesn't already
2577 * exist. This avoids bloating the stats with entries for stuff that is
2578 * only touched by vacuum and not by live operations.
2580 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2581 if (dbentry == NULL)
2584 tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
2586 if (tabentry == NULL)
2589 if (msg->m_autovacuum)
2590 tabentry->autovac_vacuum_timestamp = msg->m_vacuumtime;
2592 tabentry->vacuum_timestamp = msg->m_vacuumtime;
2593 tabentry->n_live_tuples = msg->m_tuples;
2594 tabentry->n_dead_tuples = 0;
2597 tabentry->last_anl_tuples = msg->m_tuples;
2598 if (msg->m_autovacuum)
2599 tabentry->autovac_analyze_timestamp = msg->m_vacuumtime;
2601 tabentry->analyze_timestamp = msg->m_vacuumtime;
2605 /* last_anl_tuples must never exceed n_live_tuples */
2606 tabentry->last_anl_tuples = Min(tabentry->last_anl_tuples,
2612 * pgstat_recv_analyze() -
2614 * Process an ANALYZE message.
2618 pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
2620 PgStat_StatDBEntry *dbentry;
2621 PgStat_StatTabEntry *tabentry;
2624 * Don't create either the database or table entry if it doesn't already
2625 * exist. This avoids bloating the stats with entries for stuff that is
2626 * only touched by analyze and not by live operations.
2628 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2629 if (dbentry == NULL)
2632 tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
2634 if (tabentry == NULL)
2637 if (msg->m_autovacuum)
2638 tabentry->autovac_analyze_timestamp = msg->m_analyzetime;
2640 tabentry->analyze_timestamp = msg->m_analyzetime;
2641 tabentry->n_live_tuples = msg->m_live_tuples;
2642 tabentry->n_dead_tuples = msg->m_dead_tuples;
2643 tabentry->last_anl_tuples = msg->m_live_tuples + msg->m_dead_tuples;