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-2008, PostgreSQL Global Development Group
16 * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.181 2008/08/25 18:55:43 mha 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/twophase_rmgr.h"
43 #include "access/xact.h"
44 #include "catalog/pg_database.h"
45 #include "catalog/pg_proc.h"
47 #include "libpq/libpq.h"
48 #include "libpq/pqsignal.h"
49 #include "mb/pg_wchar.h"
50 #include "miscadmin.h"
52 #include "postmaster/autovacuum.h"
53 #include "postmaster/fork_process.h"
54 #include "postmaster/postmaster.h"
55 #include "storage/backendid.h"
56 #include "storage/fd.h"
57 #include "storage/ipc.h"
58 #include "storage/pg_shmem.h"
59 #include "storage/pmsignal.h"
60 #include "utils/guc.h"
61 #include "utils/memutils.h"
62 #include "utils/ps_status.h"
63 #include "utils/rel.h"
64 #include "utils/tqual.h"
68 * Paths for the statistics files (relative to installation's $PGDATA).
71 #define PGSTAT_STAT_PERMANENT_FILENAME "global/pgstat.stat"
72 #define PGSTAT_STAT_PERMANENT_TMPFILE "global/pgstat.tmp"
78 #define PGSTAT_STAT_INTERVAL 500 /* How often to write the status file;
81 #define PGSTAT_RESTART_INTERVAL 60 /* How often to attempt to restart a
82 * failed statistics collector; in
85 #define PGSTAT_SELECT_TIMEOUT 2 /* How often to check for postmaster
86 * death; in seconds. */
90 * The initial size hints for the hash tables used in the collector.
93 #define PGSTAT_DB_HASH_SIZE 16
94 #define PGSTAT_TAB_HASH_SIZE 512
95 #define PGSTAT_FUNCTION_HASH_SIZE 512
102 bool pgstat_track_activities = false;
103 bool pgstat_track_counts = false;
104 int pgstat_track_functions = TRACK_FUNC_OFF;
105 int pgstat_track_activity_query_size = 1024;
108 * Built from GUC parameter
111 char *pgstat_stat_filename = NULL;
112 char *pgstat_stat_tmpname = NULL;
115 * BgWriter global statistics counters (unused in other processes).
116 * Stored directly in a stats message structure so it can be sent
117 * without needing to copy things around. We assume this inits to zeroes.
119 PgStat_MsgBgWriter BgWriterStats;
125 NON_EXEC_STATIC int pgStatSock = -1;
127 static struct sockaddr_storage pgStatAddr;
129 static time_t last_pgstat_start_time;
131 static bool pgStatRunningInCollector = false;
134 * Structures in which backends store per-table info that's waiting to be
135 * sent to the collector.
137 * NOTE: once allocated, TabStatusArray structures are never moved or deleted
138 * for the life of the backend. Also, we zero out the t_id fields of the
139 * contained PgStat_TableStatus structs whenever they are not actively in use.
140 * This allows relcache pgstat_info pointers to be treated as long-lived data,
141 * avoiding repeated searches in pgstat_initstats() when a relation is
142 * repeatedly opened during a transaction.
144 #define TABSTAT_QUANTUM 100 /* we alloc this many at a time */
146 typedef struct TabStatusArray
148 struct TabStatusArray *tsa_next; /* link to next array, if any */
149 int tsa_used; /* # entries currently used */
150 PgStat_TableStatus tsa_entries[TABSTAT_QUANTUM]; /* per-table data */
153 static TabStatusArray *pgStatTabList = NULL;
156 * Backends store per-function info that's waiting to be sent to the collector
157 * in this hash table (indexed by function OID).
159 static HTAB *pgStatFunctions = NULL;
162 * Tuple insertion/deletion counts for an open transaction can't be propagated
163 * into PgStat_TableStatus counters until we know if it is going to commit
164 * or abort. Hence, we keep these counts in per-subxact structs that live
165 * in TopTransactionContext. This data structure is designed on the assumption
166 * that subxacts won't usually modify very many tables.
168 typedef struct PgStat_SubXactStatus
170 int nest_level; /* subtransaction nest level */
171 struct PgStat_SubXactStatus *prev; /* higher-level subxact if any */
172 PgStat_TableXactStatus *first; /* head of list for this subxact */
173 } PgStat_SubXactStatus;
175 static PgStat_SubXactStatus *pgStatXactStack = NULL;
177 static int pgStatXactCommit = 0;
178 static int pgStatXactRollback = 0;
180 /* Record that's written to 2PC state file when pgstat state is persisted */
181 typedef struct TwoPhasePgStatRecord
183 PgStat_Counter tuples_inserted; /* tuples inserted in xact */
184 PgStat_Counter tuples_deleted; /* tuples deleted in xact */
185 Oid t_id; /* table's OID */
186 bool t_shared; /* is it a shared catalog? */
187 } TwoPhasePgStatRecord;
190 * Info about current "snapshot" of stats file
192 static MemoryContext pgStatLocalContext = NULL;
193 static HTAB *pgStatDBHash = NULL;
194 static PgBackendStatus *localBackendStatusTable = NULL;
195 static int localNumBackends = 0;
198 * Cluster wide statistics, kept in the stats collector.
199 * Contains statistics that are not collected per database
202 static PgStat_GlobalStats globalStats;
204 static volatile bool need_exit = false;
205 static volatile bool need_statwrite = false;
206 static volatile bool got_SIGHUP = false;
209 * Total time charged to functions so far in the current backend.
210 * We use this to help separate "self" and "other" time charges.
211 * (We assume this initializes to zero.)
213 static instr_time total_func_time;
217 * Local function forward declarations
221 static pid_t pgstat_forkexec(void);
224 NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]);
225 static void pgstat_exit(SIGNAL_ARGS);
226 static void force_statwrite(SIGNAL_ARGS);
227 static void pgstat_beshutdown_hook(int code, Datum arg);
228 static void pgstat_sighup_handler(SIGNAL_ARGS);
230 static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
231 static void pgstat_write_statsfile(bool permanent);
232 static HTAB *pgstat_read_statsfile(Oid onlydb, bool permanent);
233 static void backend_read_statsfile(void);
234 static void pgstat_read_current_status(void);
236 static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg);
237 static void pgstat_send_funcstats(void);
238 static HTAB *pgstat_collect_oids(Oid catalogid);
240 static PgStat_TableStatus *get_tabstat_entry(Oid rel_id, bool isshared);
242 static void pgstat_setup_memcxt(void);
244 static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype);
245 static void pgstat_send(void *msg, int len);
247 static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
248 static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
249 static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
250 static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
251 static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
252 static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
253 static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
254 static void pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len);
255 static void pgstat_recv_funcstat(PgStat_MsgFuncstat *msg, int len);
256 static void pgstat_recv_funcpurge(PgStat_MsgFuncpurge *msg, int len);
259 /* ------------------------------------------------------------
260 * Public functions called from postmaster follow
261 * ------------------------------------------------------------
267 * Called from postmaster at startup. Create the resources required
268 * by the statistics collector process. If unable to do so, do not
269 * fail --- better to let the postmaster start with stats collection
276 ACCEPT_TYPE_ARG3 alen;
277 struct addrinfo *addrs = NULL,
287 #define TESTBYTEVAL ((char) 199)
290 * Create the UDP socket for sending and receiving statistic messages
292 hints.ai_flags = AI_PASSIVE;
293 hints.ai_family = PF_UNSPEC;
294 hints.ai_socktype = SOCK_DGRAM;
295 hints.ai_protocol = 0;
296 hints.ai_addrlen = 0;
297 hints.ai_addr = NULL;
298 hints.ai_canonname = NULL;
299 hints.ai_next = NULL;
300 ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
304 (errmsg("could not resolve \"localhost\": %s",
305 gai_strerror(ret))));
310 * On some platforms, pg_getaddrinfo_all() may return multiple addresses
311 * only one of which will actually work (eg, both IPv6 and IPv4 addresses
312 * when kernel will reject IPv6). Worse, the failure may occur at the
313 * bind() or perhaps even connect() stage. So we must loop through the
314 * results till we find a working combination. We will generate LOG
315 * messages, but no error, for bogus combinations.
317 for (addr = addrs; addr; addr = addr->ai_next)
319 #ifdef HAVE_UNIX_SOCKETS
320 /* Ignore AF_UNIX sockets, if any are returned. */
321 if (addr->ai_family == AF_UNIX)
327 (errmsg("trying another address for the statistics collector")));
332 if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) < 0)
335 (errcode_for_socket_access(),
336 errmsg("could not create socket for statistics collector: %m")));
341 * Bind it to a kernel assigned port on localhost and get the assigned
342 * port via getsockname().
344 if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
347 (errcode_for_socket_access(),
348 errmsg("could not bind socket for statistics collector: %m")));
349 closesocket(pgStatSock);
354 alen = sizeof(pgStatAddr);
355 if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
358 (errcode_for_socket_access(),
359 errmsg("could not get address of socket for statistics collector: %m")));
360 closesocket(pgStatSock);
366 * Connect the socket to its own address. This saves a few cycles by
367 * not having to respecify the target address on every send. This also
368 * provides a kernel-level check that only packets from this same
369 * address will be received.
371 if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
374 (errcode_for_socket_access(),
375 errmsg("could not connect socket for statistics collector: %m")));
376 closesocket(pgStatSock);
382 * Try to send and receive a one-byte test message on the socket. This
383 * is to catch situations where the socket can be created but will not
384 * actually pass data (for instance, because kernel packet filtering
387 test_byte = TESTBYTEVAL;
390 if (send(pgStatSock, &test_byte, 1, 0) != 1)
393 goto retry1; /* if interrupted, just retry */
395 (errcode_for_socket_access(),
396 errmsg("could not send test message on socket for statistics collector: %m")));
397 closesocket(pgStatSock);
403 * There could possibly be a little delay before the message can be
404 * received. We arbitrarily allow up to half a second before deciding
407 for (;;) /* need a loop to handle EINTR */
410 FD_SET(pgStatSock, &rset);
413 sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
414 if (sel_res >= 0 || errno != EINTR)
420 (errcode_for_socket_access(),
421 errmsg("select() failed in statistics collector: %m")));
422 closesocket(pgStatSock);
426 if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
429 * This is the case we actually think is likely, so take pains to
430 * give a specific message for it.
432 * errno will not be set meaningfully here, so don't use it.
435 (errcode(ERRCODE_CONNECTION_FAILURE),
436 errmsg("test message did not get through on socket for statistics collector")));
437 closesocket(pgStatSock);
442 test_byte++; /* just make sure variable is changed */
445 if (recv(pgStatSock, &test_byte, 1, 0) != 1)
448 goto retry2; /* if interrupted, just retry */
450 (errcode_for_socket_access(),
451 errmsg("could not receive test message on socket for statistics collector: %m")));
452 closesocket(pgStatSock);
457 if (test_byte != TESTBYTEVAL) /* strictly paranoia ... */
460 (errcode(ERRCODE_INTERNAL_ERROR),
461 errmsg("incorrect test message transmission on socket for statistics collector")));
462 closesocket(pgStatSock);
467 /* If we get here, we have a working socket */
471 /* Did we find a working address? */
472 if (!addr || pgStatSock < 0)
476 * Set the socket to non-blocking IO. This ensures that if the collector
477 * falls behind, statistics messages will be discarded; backends won't
478 * block waiting to send messages to the collector.
480 if (!pg_set_noblock(pgStatSock))
483 (errcode_for_socket_access(),
484 errmsg("could not set statistics collector socket to nonblocking mode: %m")));
488 pg_freeaddrinfo_all(hints.ai_family, addrs);
494 (errmsg("disabling statistics collector for lack of working socket")));
497 pg_freeaddrinfo_all(hints.ai_family, addrs);
500 closesocket(pgStatSock);
504 * Adjust GUC variables to suppress useless activity, and for debugging
505 * purposes (seeing track_counts off is a clue that we failed here). We
506 * use PGC_S_OVERRIDE because there is no point in trying to turn it back
507 * on from postgresql.conf without a restart.
509 SetConfigOption("track_counts", "off", PGC_INTERNAL, PGC_S_OVERRIDE);
513 * pgstat_reset_all() -
515 * Remove the stats file. This is currently used only if WAL
516 * recovery is needed after a crash.
519 pgstat_reset_all(void)
521 unlink(pgstat_stat_filename);
522 unlink(PGSTAT_STAT_PERMANENT_FILENAME);
528 * pgstat_forkexec() -
530 * Format up the arglist for, then fork and exec, statistics collector process
533 pgstat_forkexec(void)
538 av[ac++] = "postgres";
539 av[ac++] = "--forkcol";
540 av[ac++] = NULL; /* filled in by postmaster_forkexec */
543 Assert(ac < lengthof(av));
545 return postmaster_forkexec(ac, av);
547 #endif /* EXEC_BACKEND */
553 * Called from postmaster at startup or after an existing collector
554 * died. Attempt to fire up a fresh statistics collector.
556 * Returns PID of child process, or 0 if fail.
558 * Note: if fail, we will be called again from the postmaster main loop.
567 * Check that the socket is there, else pgstat_init failed and we can do
574 * Do nothing if too soon since last collector start. This is a safety
575 * valve to protect against continuous respawn attempts if the collector
576 * is dying immediately at launch. Note that since we will be re-called
577 * from the postmaster main loop, we will get another chance later.
579 curtime = time(NULL);
580 if ((unsigned int) (curtime - last_pgstat_start_time) <
581 (unsigned int) PGSTAT_RESTART_INTERVAL)
583 last_pgstat_start_time = curtime;
586 * Okay, fork off the collector.
589 switch ((pgStatPid = pgstat_forkexec()))
591 switch ((pgStatPid = fork_process()))
596 (errmsg("could not fork statistics collector: %m")));
601 /* in postmaster child ... */
602 /* Close the postmaster's sockets */
603 ClosePostmasterPorts(false);
605 /* Lose the postmaster's on-exit routines */
608 /* Drop our connection to postmaster's shared memory, as well */
609 PGSharedMemoryDetach();
611 PgstatCollectorMain(0, NULL);
616 return (int) pgStatPid;
619 /* shouldn't get here */
624 allow_immediate_pgstat_restart(void)
626 last_pgstat_start_time = 0;
629 /* ------------------------------------------------------------
630 * Public functions used by backends follow
631 *------------------------------------------------------------
636 * pgstat_report_stat() -
638 * Called from tcop/postgres.c to send the so far collected per-table
639 * and function usage statistics to the collector. Note that this is
640 * called only when not within a transaction, so it is fair to use
641 * transaction stop time as an approximation of current time.
645 pgstat_report_stat(bool force)
647 /* we assume this inits to all zeroes: */
648 static const PgStat_TableCounts all_zeroes;
649 static TimestampTz last_report = 0;
652 PgStat_MsgTabstat regular_msg;
653 PgStat_MsgTabstat shared_msg;
657 /* Don't expend a clock check if nothing to do */
658 /* Note: we ignore pending function stats in this test ... OK? */
659 if (pgStatTabList == NULL ||
660 pgStatTabList->tsa_used == 0)
664 * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
665 * msec since we last sent one, or the caller wants to force stats out.
667 now = GetCurrentTransactionStopTimestamp();
669 !TimestampDifferenceExceeds(last_report, now, PGSTAT_STAT_INTERVAL))
674 * Scan through the TabStatusArray struct(s) to find tables that actually
675 * have counts, and build messages to send. We have to separate shared
676 * relations from regular ones because the databaseid field in the message
677 * header has to depend on that.
679 regular_msg.m_databaseid = MyDatabaseId;
680 shared_msg.m_databaseid = InvalidOid;
681 regular_msg.m_nentries = 0;
682 shared_msg.m_nentries = 0;
684 for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
686 for (i = 0; i < tsa->tsa_used; i++)
688 PgStat_TableStatus *entry = &tsa->tsa_entries[i];
689 PgStat_MsgTabstat *this_msg;
690 PgStat_TableEntry *this_ent;
692 /* Shouldn't have any pending transaction-dependent counts */
693 Assert(entry->trans == NULL);
696 * Ignore entries that didn't accumulate any actual counts, such
697 * as indexes that were opened by the planner but not used.
699 if (memcmp(&entry->t_counts, &all_zeroes,
700 sizeof(PgStat_TableCounts)) == 0)
704 * OK, insert data into the appropriate message, and send if full.
706 this_msg = entry->t_shared ? &shared_msg : ®ular_msg;
707 this_ent = &this_msg->m_entry[this_msg->m_nentries];
708 this_ent->t_id = entry->t_id;
709 memcpy(&this_ent->t_counts, &entry->t_counts,
710 sizeof(PgStat_TableCounts));
711 if (++this_msg->m_nentries >= PGSTAT_NUM_TABENTRIES)
713 pgstat_send_tabstat(this_msg);
714 this_msg->m_nentries = 0;
717 /* zero out TableStatus structs after use */
718 MemSet(tsa->tsa_entries, 0,
719 tsa->tsa_used * sizeof(PgStat_TableStatus));
724 * Send partial messages. If force is true, make sure that any pending
725 * xact commit/abort gets counted, even if no table stats to send.
727 if (regular_msg.m_nentries > 0 ||
728 (force && (pgStatXactCommit > 0 || pgStatXactRollback > 0)))
729 pgstat_send_tabstat(®ular_msg);
730 if (shared_msg.m_nentries > 0)
731 pgstat_send_tabstat(&shared_msg);
733 /* Now, send function statistics */
734 pgstat_send_funcstats();
738 * Subroutine for pgstat_report_stat: finish and send a tabstat message
741 pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg)
746 /* It's unlikely we'd get here with no socket, but maybe not impossible */
751 * Report accumulated xact commit/rollback whenever we send a normal
754 if (OidIsValid(tsmsg->m_databaseid))
756 tsmsg->m_xact_commit = pgStatXactCommit;
757 tsmsg->m_xact_rollback = pgStatXactRollback;
758 pgStatXactCommit = 0;
759 pgStatXactRollback = 0;
763 tsmsg->m_xact_commit = 0;
764 tsmsg->m_xact_rollback = 0;
767 n = tsmsg->m_nentries;
768 len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
769 n * sizeof(PgStat_TableEntry);
771 pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
772 pgstat_send(tsmsg, len);
776 * Subroutine for pgstat_report_stat: populate and send a function stat message
779 pgstat_send_funcstats(void)
781 /* we assume this inits to all zeroes: */
782 static const PgStat_FunctionCounts all_zeroes;
784 PgStat_MsgFuncstat msg;
785 PgStat_BackendFunctionEntry *entry;
786 HASH_SEQ_STATUS fstat;
788 if (pgStatFunctions == NULL)
791 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_FUNCSTAT);
792 msg.m_databaseid = MyDatabaseId;
795 hash_seq_init(&fstat, pgStatFunctions);
796 while ((entry = (PgStat_BackendFunctionEntry *) hash_seq_search(&fstat)) != NULL)
798 PgStat_FunctionEntry *m_ent;
800 /* Skip it if no counts accumulated since last time */
801 if (memcmp(&entry->f_counts, &all_zeroes,
802 sizeof(PgStat_FunctionCounts)) == 0)
805 /* need to convert format of time accumulators */
806 m_ent = &msg.m_entry[msg.m_nentries];
807 m_ent->f_id = entry->f_id;
808 m_ent->f_numcalls = entry->f_counts.f_numcalls;
809 m_ent->f_time = INSTR_TIME_GET_MICROSEC(entry->f_counts.f_time);
810 m_ent->f_time_self = INSTR_TIME_GET_MICROSEC(entry->f_counts.f_time_self);
812 if (++msg.m_nentries >= PGSTAT_NUM_FUNCENTRIES)
814 pgstat_send(&msg, offsetof(PgStat_MsgFuncstat, m_entry[0]) +
815 msg.m_nentries * sizeof(PgStat_FunctionEntry));
819 /* reset the entry's counts */
820 MemSet(&entry->f_counts, 0, sizeof(PgStat_FunctionCounts));
823 if (msg.m_nentries > 0)
824 pgstat_send(&msg, offsetof(PgStat_MsgFuncstat, m_entry[0]) +
825 msg.m_nentries * sizeof(PgStat_FunctionEntry));
830 * pgstat_vacuum_stat() -
832 * Will tell the collector about objects he can get rid of.
836 pgstat_vacuum_stat(void)
839 PgStat_MsgTabpurge msg;
840 PgStat_MsgFuncpurge f_msg;
841 HASH_SEQ_STATUS hstat;
842 PgStat_StatDBEntry *dbentry;
843 PgStat_StatTabEntry *tabentry;
844 PgStat_StatFuncEntry *funcentry;
851 * If not done for this transaction, read the statistics collector stats
852 * file into some hash tables.
854 backend_read_statsfile();
857 * Read pg_database and make a list of OIDs of all existing databases
859 htab = pgstat_collect_oids(DatabaseRelationId);
862 * Search the database hash table for dead databases and tell the
863 * collector to drop them.
865 hash_seq_init(&hstat, pgStatDBHash);
866 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
868 Oid dbid = dbentry->databaseid;
870 CHECK_FOR_INTERRUPTS();
872 /* the DB entry for shared tables (with InvalidOid) is never dropped */
873 if (OidIsValid(dbid) &&
874 hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
875 pgstat_drop_database(dbid);
882 * Lookup our own database entry; if not found, nothing more to do.
884 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
885 (void *) &MyDatabaseId,
887 if (dbentry == NULL || dbentry->tables == NULL)
891 * Similarly to above, make a list of all known relations in this DB.
893 htab = pgstat_collect_oids(RelationRelationId);
896 * Initialize our messages table counter to zero
901 * Check for all tables listed in stats hashtable if they still exist.
903 hash_seq_init(&hstat, dbentry->tables);
904 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
906 Oid tabid = tabentry->tableid;
908 CHECK_FOR_INTERRUPTS();
910 if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
914 * Not there, so add this table's Oid to the message
916 msg.m_tableid[msg.m_nentries++] = tabid;
919 * If the message is full, send it out and reinitialize to empty
921 if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
923 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
924 +msg.m_nentries * sizeof(Oid);
926 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
927 msg.m_databaseid = MyDatabaseId;
928 pgstat_send(&msg, len);
937 if (msg.m_nentries > 0)
939 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
940 +msg.m_nentries * sizeof(Oid);
942 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
943 msg.m_databaseid = MyDatabaseId;
944 pgstat_send(&msg, len);
951 * Now repeat the above steps for functions.
953 htab = pgstat_collect_oids(ProcedureRelationId);
955 pgstat_setheader(&f_msg.m_hdr, PGSTAT_MTYPE_FUNCPURGE);
956 f_msg.m_databaseid = MyDatabaseId;
957 f_msg.m_nentries = 0;
959 hash_seq_init(&hstat, dbentry->functions);
960 while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&hstat)) != NULL)
962 Oid funcid = funcentry->functionid;
964 CHECK_FOR_INTERRUPTS();
966 if (hash_search(htab, (void *) &funcid, HASH_FIND, NULL) != NULL)
970 * Not there, so add this function's Oid to the message
972 f_msg.m_functionid[f_msg.m_nentries++] = funcid;
975 * If the message is full, send it out and reinitialize to empty
977 if (f_msg.m_nentries >= PGSTAT_NUM_FUNCPURGE)
979 len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
980 +f_msg.m_nentries * sizeof(Oid);
982 pgstat_send(&f_msg, len);
984 f_msg.m_nentries = 0;
991 if (f_msg.m_nentries > 0)
993 len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
994 +f_msg.m_nentries * sizeof(Oid);
996 pgstat_send(&f_msg, len);
1004 * pgstat_collect_oids() -
1006 * Collect the OIDs of all objects listed in the specified system catalog
1007 * into a temporary hash table. Caller should hash_destroy the result
1008 * when done with it.
1012 pgstat_collect_oids(Oid catalogid)
1020 memset(&hash_ctl, 0, sizeof(hash_ctl));
1021 hash_ctl.keysize = sizeof(Oid);
1022 hash_ctl.entrysize = sizeof(Oid);
1023 hash_ctl.hash = oid_hash;
1024 htab = hash_create("Temporary table of OIDs",
1025 PGSTAT_TAB_HASH_SIZE,
1027 HASH_ELEM | HASH_FUNCTION);
1029 rel = heap_open(catalogid, AccessShareLock);
1030 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
1031 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
1033 Oid thisoid = HeapTupleGetOid(tup);
1035 CHECK_FOR_INTERRUPTS();
1037 (void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
1040 heap_close(rel, AccessShareLock);
1047 * pgstat_drop_database() -
1049 * Tell the collector that we just dropped a database.
1050 * (If the message gets lost, we will still clean the dead DB eventually
1051 * via future invocations of pgstat_vacuum_stat().)
1055 pgstat_drop_database(Oid databaseid)
1057 PgStat_MsgDropdb msg;
1062 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
1063 msg.m_databaseid = databaseid;
1064 pgstat_send(&msg, sizeof(msg));
1069 * pgstat_drop_relation() -
1071 * Tell the collector that we just dropped a relation.
1072 * (If the message gets lost, we will still clean the dead entry eventually
1073 * via future invocations of pgstat_vacuum_stat().)
1075 * Currently not used for lack of any good place to call it; we rely
1076 * entirely on pgstat_vacuum_stat() to clean out stats for dead rels.
1081 pgstat_drop_relation(Oid relid)
1083 PgStat_MsgTabpurge msg;
1089 msg.m_tableid[0] = relid;
1092 len = offsetof(PgStat_MsgTabpurge, m_tableid[0]) +sizeof(Oid);
1094 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
1095 msg.m_databaseid = MyDatabaseId;
1096 pgstat_send(&msg, len);
1098 #endif /* NOT_USED */
1102 * pgstat_reset_counters() -
1104 * Tell the statistics collector to reset counters for our database.
1108 pgstat_reset_counters(void)
1110 PgStat_MsgResetcounter msg;
1117 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1118 errmsg("must be superuser to reset statistics counters")));
1120 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
1121 msg.m_databaseid = MyDatabaseId;
1122 pgstat_send(&msg, sizeof(msg));
1127 * pgstat_report_autovac() -
1129 * Called from autovacuum.c to report startup of an autovacuum process.
1130 * We are called before InitPostgres is done, so can't rely on MyDatabaseId;
1131 * the db OID must be passed in, instead.
1135 pgstat_report_autovac(Oid dboid)
1137 PgStat_MsgAutovacStart msg;
1142 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_AUTOVAC_START);
1143 msg.m_databaseid = dboid;
1144 msg.m_start_time = GetCurrentTimestamp();
1146 pgstat_send(&msg, sizeof(msg));
1151 * pgstat_report_vacuum() -
1153 * Tell the collector about the table we just vacuumed.
1157 pgstat_report_vacuum(Oid tableoid, bool shared,
1158 bool analyze, PgStat_Counter tuples)
1160 PgStat_MsgVacuum msg;
1162 if (pgStatSock < 0 || !pgstat_track_counts)
1165 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_VACUUM);
1166 msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1167 msg.m_tableoid = tableoid;
1168 msg.m_analyze = analyze;
1169 msg.m_autovacuum = IsAutoVacuumWorkerProcess(); /* is this autovacuum? */
1170 msg.m_vacuumtime = GetCurrentTimestamp();
1171 msg.m_tuples = tuples;
1172 pgstat_send(&msg, sizeof(msg));
1176 * pgstat_report_analyze() -
1178 * Tell the collector about the table we just analyzed.
1182 pgstat_report_analyze(Relation rel, PgStat_Counter livetuples,
1183 PgStat_Counter deadtuples)
1185 PgStat_MsgAnalyze msg;
1187 if (pgStatSock < 0 || !pgstat_track_counts)
1191 * Unlike VACUUM, ANALYZE might be running inside a transaction that
1192 * has already inserted and/or deleted rows in the target table.
1193 * ANALYZE will have counted such rows as live or dead respectively.
1194 * Because we will report our counts of such rows at transaction end,
1195 * we should subtract off these counts from what we send to the collector
1196 * now, else they'll be double-counted after commit. (This approach also
1197 * ensures that the collector ends up with the right numbers if we abort
1198 * instead of committing.)
1200 if (rel->pgstat_info != NULL)
1202 PgStat_TableXactStatus *trans;
1204 for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
1206 livetuples -= trans->tuples_inserted - trans->tuples_deleted;
1207 deadtuples -= trans->tuples_deleted;
1209 /* count stuff inserted by already-aborted subxacts, too */
1210 deadtuples -= rel->pgstat_info->t_counts.t_new_dead_tuples;
1211 /* Since ANALYZE's counts are estimates, we could have underflowed */
1212 livetuples = Max(livetuples, 0);
1213 deadtuples = Max(deadtuples, 0);
1216 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ANALYZE);
1217 msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
1218 msg.m_tableoid = RelationGetRelid(rel);
1219 msg.m_autovacuum = IsAutoVacuumWorkerProcess(); /* is this autovacuum? */
1220 msg.m_analyzetime = GetCurrentTimestamp();
1221 msg.m_live_tuples = livetuples;
1222 msg.m_dead_tuples = deadtuples;
1223 pgstat_send(&msg, sizeof(msg));
1230 * Send some junk data to the collector to increase traffic.
1236 PgStat_MsgDummy msg;
1241 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DUMMY);
1242 pgstat_send(&msg, sizeof(msg));
1246 * Initialize function call usage data.
1247 * Called by the executor before invoking a function.
1250 pgstat_init_function_usage(FunctionCallInfoData *fcinfo,
1251 PgStat_FunctionCallUsage *fcu)
1253 PgStat_BackendFunctionEntry *htabent;
1256 if (pgstat_track_functions <= fcinfo->flinfo->fn_stats)
1258 /* stats not wanted */
1263 if (!pgStatFunctions)
1265 /* First time through - initialize function stat table */
1268 memset(&hash_ctl, 0, sizeof(hash_ctl));
1269 hash_ctl.keysize = sizeof(Oid);
1270 hash_ctl.entrysize = sizeof(PgStat_BackendFunctionEntry);
1271 hash_ctl.hash = oid_hash;
1272 pgStatFunctions = hash_create("Function stat entries",
1273 PGSTAT_FUNCTION_HASH_SIZE,
1275 HASH_ELEM | HASH_FUNCTION);
1278 /* Get the stats entry for this function, create if necessary */
1279 htabent = hash_search(pgStatFunctions, &fcinfo->flinfo->fn_oid,
1280 HASH_ENTER, &found);
1282 MemSet(&htabent->f_counts, 0, sizeof(PgStat_FunctionCounts));
1284 fcu->fs = &htabent->f_counts;
1286 /* save stats for this function, later used to compensate for recursion */
1287 fcu->save_f_time = htabent->f_counts.f_time;
1289 /* save current backend-wide total time */
1290 fcu->save_total = total_func_time;
1292 /* get clock time as of function start */
1293 INSTR_TIME_SET_CURRENT(fcu->f_start);
1297 * Calculate function call usage and update stat counters.
1298 * Called by the executor after invoking a function.
1300 * In the case of a set-returning function that runs in value-per-call mode,
1301 * we will see multiple pgstat_init_function_usage/pgstat_end_function_usage
1302 * calls for what the user considers a single call of the function. The
1303 * finalize flag should be TRUE on the last call.
1306 pgstat_end_function_usage(PgStat_FunctionCallUsage *fcu, bool finalize)
1308 PgStat_FunctionCounts *fs = fcu->fs;
1310 instr_time f_others;
1313 /* stats not wanted? */
1317 /* total elapsed time in this function call */
1318 INSTR_TIME_SET_CURRENT(f_total);
1319 INSTR_TIME_SUBTRACT(f_total, fcu->f_start);
1321 /* self usage: elapsed minus anything already charged to other calls */
1322 f_others = total_func_time;
1323 INSTR_TIME_SUBTRACT(f_others, fcu->save_total);
1325 INSTR_TIME_SUBTRACT(f_self, f_others);
1327 /* update backend-wide total time */
1328 INSTR_TIME_ADD(total_func_time, f_self);
1331 * Compute the new total f_time as the total elapsed time added to the
1332 * pre-call value of f_time. This is necessary to avoid double-counting
1333 * any time taken by recursive calls of myself. (We do not need any
1334 * similar kluge for self time, since that already excludes any
1337 INSTR_TIME_ADD(f_total, fcu->save_f_time);
1339 /* update counters in function stats table */
1342 fs->f_time = f_total;
1343 INSTR_TIME_ADD(fs->f_time_self, f_self);
1348 * pgstat_initstats() -
1350 * Initialize a relcache entry to count access statistics.
1351 * Called whenever a relation is opened.
1353 * We assume that a relcache entry's pgstat_info field is zeroed by
1354 * relcache.c when the relcache entry is made; thereafter it is long-lived
1355 * data. We can avoid repeated searches of the TabStatus arrays when the
1356 * same relation is touched repeatedly within a transaction.
1360 pgstat_initstats(Relation rel)
1362 Oid rel_id = rel->rd_id;
1363 char relkind = rel->rd_rel->relkind;
1365 /* We only count stats for things that have storage */
1366 if (!(relkind == RELKIND_RELATION ||
1367 relkind == RELKIND_INDEX ||
1368 relkind == RELKIND_TOASTVALUE))
1370 rel->pgstat_info = NULL;
1374 if (pgStatSock < 0 || !pgstat_track_counts)
1376 /* We're not counting at all */
1377 rel->pgstat_info = NULL;
1382 * If we already set up this relation in the current transaction, nothing
1385 if (rel->pgstat_info != NULL &&
1386 rel->pgstat_info->t_id == rel_id)
1389 /* Else find or make the PgStat_TableStatus entry, and update link */
1390 rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
1394 * get_tabstat_entry - find or create a PgStat_TableStatus entry for rel
1396 static PgStat_TableStatus *
1397 get_tabstat_entry(Oid rel_id, bool isshared)
1399 PgStat_TableStatus *entry;
1400 TabStatusArray *tsa;
1401 TabStatusArray *prev_tsa;
1405 * Search the already-used tabstat slots for this relation.
1408 for (tsa = pgStatTabList; tsa != NULL; prev_tsa = tsa, tsa = tsa->tsa_next)
1410 for (i = 0; i < tsa->tsa_used; i++)
1412 entry = &tsa->tsa_entries[i];
1413 if (entry->t_id == rel_id)
1417 if (tsa->tsa_used < TABSTAT_QUANTUM)
1420 * It must not be present, but we found a free slot instead. Fine,
1421 * let's use this one. We assume the entry was already zeroed,
1422 * either at creation or after last use.
1424 entry = &tsa->tsa_entries[tsa->tsa_used++];
1425 entry->t_id = rel_id;
1426 entry->t_shared = isshared;
1432 * We ran out of tabstat slots, so allocate more. Be sure they're zeroed.
1434 tsa = (TabStatusArray *) MemoryContextAllocZero(TopMemoryContext,
1435 sizeof(TabStatusArray));
1437 prev_tsa->tsa_next = tsa;
1439 pgStatTabList = tsa;
1442 * Use the first entry of the new TabStatusArray.
1444 entry = &tsa->tsa_entries[tsa->tsa_used++];
1445 entry->t_id = rel_id;
1446 entry->t_shared = isshared;
1451 * get_tabstat_stack_level - add a new (sub)transaction stack entry if needed
1453 static PgStat_SubXactStatus *
1454 get_tabstat_stack_level(int nest_level)
1456 PgStat_SubXactStatus *xact_state;
1458 xact_state = pgStatXactStack;
1459 if (xact_state == NULL || xact_state->nest_level != nest_level)
1461 xact_state = (PgStat_SubXactStatus *)
1462 MemoryContextAlloc(TopTransactionContext,
1463 sizeof(PgStat_SubXactStatus));
1464 xact_state->nest_level = nest_level;
1465 xact_state->prev = pgStatXactStack;
1466 xact_state->first = NULL;
1467 pgStatXactStack = xact_state;
1473 * add_tabstat_xact_level - add a new (sub)transaction state record
1476 add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
1478 PgStat_SubXactStatus *xact_state;
1479 PgStat_TableXactStatus *trans;
1482 * If this is the first rel to be modified at the current nest level, we
1483 * first have to push a transaction stack entry.
1485 xact_state = get_tabstat_stack_level(nest_level);
1487 /* Now make a per-table stack entry */
1488 trans = (PgStat_TableXactStatus *)
1489 MemoryContextAllocZero(TopTransactionContext,
1490 sizeof(PgStat_TableXactStatus));
1491 trans->nest_level = nest_level;
1492 trans->upper = pgstat_info->trans;
1493 trans->parent = pgstat_info;
1494 trans->next = xact_state->first;
1495 xact_state->first = trans;
1496 pgstat_info->trans = trans;
1500 * pgstat_count_heap_insert - count a tuple insertion
1503 pgstat_count_heap_insert(Relation rel)
1505 PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1507 if (pgstat_track_counts && pgstat_info != NULL)
1509 int nest_level = GetCurrentTransactionNestLevel();
1511 /* t_tuples_inserted is nontransactional, so just advance it */
1512 pgstat_info->t_counts.t_tuples_inserted++;
1514 /* We have to log the transactional effect at the proper level */
1515 if (pgstat_info->trans == NULL ||
1516 pgstat_info->trans->nest_level != nest_level)
1517 add_tabstat_xact_level(pgstat_info, nest_level);
1519 pgstat_info->trans->tuples_inserted++;
1524 * pgstat_count_heap_update - count a tuple update
1527 pgstat_count_heap_update(Relation rel, bool hot)
1529 PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1531 if (pgstat_track_counts && pgstat_info != NULL)
1533 int nest_level = GetCurrentTransactionNestLevel();
1535 /* t_tuples_updated is nontransactional, so just advance it */
1536 pgstat_info->t_counts.t_tuples_updated++;
1537 /* ditto for the hot_update counter */
1539 pgstat_info->t_counts.t_tuples_hot_updated++;
1541 /* We have to log the transactional effect at the proper level */
1542 if (pgstat_info->trans == NULL ||
1543 pgstat_info->trans->nest_level != nest_level)
1544 add_tabstat_xact_level(pgstat_info, nest_level);
1546 /* An UPDATE both inserts a new tuple and deletes the old */
1547 pgstat_info->trans->tuples_inserted++;
1548 pgstat_info->trans->tuples_deleted++;
1553 * pgstat_count_heap_delete - count a tuple deletion
1556 pgstat_count_heap_delete(Relation rel)
1558 PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1560 if (pgstat_track_counts && pgstat_info != NULL)
1562 int nest_level = GetCurrentTransactionNestLevel();
1564 /* t_tuples_deleted is nontransactional, so just advance it */
1565 pgstat_info->t_counts.t_tuples_deleted++;
1567 /* We have to log the transactional effect at the proper level */
1568 if (pgstat_info->trans == NULL ||
1569 pgstat_info->trans->nest_level != nest_level)
1570 add_tabstat_xact_level(pgstat_info, nest_level);
1572 pgstat_info->trans->tuples_deleted++;
1577 * pgstat_update_heap_dead_tuples - update dead-tuples count
1579 * The semantics of this are that we are reporting the nontransactional
1580 * recovery of "delta" dead tuples; so t_new_dead_tuples decreases
1581 * rather than increasing, and the change goes straight into the per-table
1582 * counter, not into transactional state.
1585 pgstat_update_heap_dead_tuples(Relation rel, int delta)
1587 PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1589 if (pgstat_track_counts && pgstat_info != NULL)
1590 pgstat_info->t_counts.t_new_dead_tuples -= delta;
1597 * Called from access/transam/xact.c at top-level transaction commit/abort.
1601 AtEOXact_PgStat(bool isCommit)
1603 PgStat_SubXactStatus *xact_state;
1606 * Count transaction commit or abort. (We use counters, not just bools,
1607 * in case the reporting message isn't sent right away.)
1612 pgStatXactRollback++;
1615 * Transfer transactional insert/update counts into the base tabstat
1616 * entries. We don't bother to free any of the transactional state, since
1617 * it's all in TopTransactionContext and will go away anyway.
1619 xact_state = pgStatXactStack;
1620 if (xact_state != NULL)
1622 PgStat_TableXactStatus *trans;
1624 Assert(xact_state->nest_level == 1);
1625 Assert(xact_state->prev == NULL);
1626 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1628 PgStat_TableStatus *tabstat;
1630 Assert(trans->nest_level == 1);
1631 Assert(trans->upper == NULL);
1632 tabstat = trans->parent;
1633 Assert(tabstat->trans == trans);
1636 tabstat->t_counts.t_new_live_tuples +=
1637 trans->tuples_inserted - trans->tuples_deleted;
1638 tabstat->t_counts.t_new_dead_tuples += trans->tuples_deleted;
1642 /* inserted tuples are dead, deleted tuples are unaffected */
1643 tabstat->t_counts.t_new_dead_tuples += trans->tuples_inserted;
1645 tabstat->trans = NULL;
1648 pgStatXactStack = NULL;
1650 /* Make sure any stats snapshot is thrown away */
1651 pgstat_clear_snapshot();
1655 * AtEOSubXact_PgStat
1657 * Called from access/transam/xact.c at subtransaction commit/abort.
1661 AtEOSubXact_PgStat(bool isCommit, int nestDepth)
1663 PgStat_SubXactStatus *xact_state;
1666 * Transfer transactional insert/update counts into the next higher
1667 * subtransaction state.
1669 xact_state = pgStatXactStack;
1670 if (xact_state != NULL &&
1671 xact_state->nest_level >= nestDepth)
1673 PgStat_TableXactStatus *trans;
1674 PgStat_TableXactStatus *next_trans;
1676 /* delink xact_state from stack immediately to simplify reuse case */
1677 pgStatXactStack = xact_state->prev;
1679 for (trans = xact_state->first; trans != NULL; trans = next_trans)
1681 PgStat_TableStatus *tabstat;
1683 next_trans = trans->next;
1684 Assert(trans->nest_level == nestDepth);
1685 tabstat = trans->parent;
1686 Assert(tabstat->trans == trans);
1689 if (trans->upper && trans->upper->nest_level == nestDepth - 1)
1691 trans->upper->tuples_inserted += trans->tuples_inserted;
1692 trans->upper->tuples_deleted += trans->tuples_deleted;
1693 tabstat->trans = trans->upper;
1699 * When there isn't an immediate parent state, we can just
1700 * reuse the record instead of going through a
1701 * palloc/pfree pushup (this works since it's all in
1702 * TopTransactionContext anyway). We have to re-link it
1703 * into the parent level, though, and that might mean
1704 * pushing a new entry into the pgStatXactStack.
1706 PgStat_SubXactStatus *upper_xact_state;
1708 upper_xact_state = get_tabstat_stack_level(nestDepth - 1);
1709 trans->next = upper_xact_state->first;
1710 upper_xact_state->first = trans;
1711 trans->nest_level = nestDepth - 1;
1717 * On abort, inserted tuples are dead (and can be bounced out
1718 * to the top-level tabstat), deleted tuples are unaffected
1720 tabstat->t_counts.t_new_dead_tuples += trans->tuples_inserted;
1721 tabstat->trans = trans->upper;
1732 * Save the transactional stats state at 2PC transaction prepare.
1734 * In this phase we just generate 2PC records for all the pending
1735 * transaction-dependent stats work.
1738 AtPrepare_PgStat(void)
1740 PgStat_SubXactStatus *xact_state;
1742 xact_state = pgStatXactStack;
1743 if (xact_state != NULL)
1745 PgStat_TableXactStatus *trans;
1747 Assert(xact_state->nest_level == 1);
1748 Assert(xact_state->prev == NULL);
1749 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1751 PgStat_TableStatus *tabstat;
1752 TwoPhasePgStatRecord record;
1754 Assert(trans->nest_level == 1);
1755 Assert(trans->upper == NULL);
1756 tabstat = trans->parent;
1757 Assert(tabstat->trans == trans);
1759 record.tuples_inserted = trans->tuples_inserted;
1760 record.tuples_deleted = trans->tuples_deleted;
1761 record.t_id = tabstat->t_id;
1762 record.t_shared = tabstat->t_shared;
1764 RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
1765 &record, sizeof(TwoPhasePgStatRecord));
1771 * PostPrepare_PgStat
1772 * Clean up after successful PREPARE.
1774 * All we need do here is unlink the transaction stats state from the
1775 * nontransactional state. The nontransactional action counts will be
1776 * reported to the stats collector immediately, while the effects on live
1777 * and dead tuple counts are preserved in the 2PC state file.
1779 * Note: AtEOXact_PgStat is not called during PREPARE.
1782 PostPrepare_PgStat(void)
1784 PgStat_SubXactStatus *xact_state;
1787 * We don't bother to free any of the transactional state, since it's all
1788 * in TopTransactionContext and will go away anyway.
1790 xact_state = pgStatXactStack;
1791 if (xact_state != NULL)
1793 PgStat_TableXactStatus *trans;
1795 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1797 PgStat_TableStatus *tabstat;
1799 tabstat = trans->parent;
1800 tabstat->trans = NULL;
1803 pgStatXactStack = NULL;
1805 /* Make sure any stats snapshot is thrown away */
1806 pgstat_clear_snapshot();
1810 * 2PC processing routine for COMMIT PREPARED case.
1812 * Load the saved counts into our local pgstats state.
1815 pgstat_twophase_postcommit(TransactionId xid, uint16 info,
1816 void *recdata, uint32 len)
1818 TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
1819 PgStat_TableStatus *pgstat_info;
1821 /* Find or create a tabstat entry for the rel */
1822 pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
1824 pgstat_info->t_counts.t_new_live_tuples +=
1825 rec->tuples_inserted - rec->tuples_deleted;
1826 pgstat_info->t_counts.t_new_dead_tuples += rec->tuples_deleted;
1830 * 2PC processing routine for ROLLBACK PREPARED case.
1832 * Load the saved counts into our local pgstats state, but treat them
1836 pgstat_twophase_postabort(TransactionId xid, uint16 info,
1837 void *recdata, uint32 len)
1839 TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
1840 PgStat_TableStatus *pgstat_info;
1842 /* Find or create a tabstat entry for the rel */
1843 pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
1845 /* inserted tuples are dead, deleted tuples are no-ops */
1846 pgstat_info->t_counts.t_new_dead_tuples += rec->tuples_inserted;
1851 * pgstat_fetch_stat_dbentry() -
1853 * Support function for the SQL-callable pgstat* functions. Returns
1854 * the collected statistics for one database or NULL. NULL doesn't mean
1855 * that the database doesn't exist, it is just not yet known by the
1856 * collector, so the caller is better off to report ZERO instead.
1859 PgStat_StatDBEntry *
1860 pgstat_fetch_stat_dbentry(Oid dbid)
1863 * If not done for this transaction, read the statistics collector stats
1864 * file into some hash tables.
1866 backend_read_statsfile();
1869 * Lookup the requested database; return NULL if not found
1871 return (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1878 * pgstat_fetch_stat_tabentry() -
1880 * Support function for the SQL-callable pgstat* functions. Returns
1881 * the collected statistics for one table or NULL. NULL doesn't mean
1882 * that the table doesn't exist, it is just not yet known by the
1883 * collector, so the caller is better off to report ZERO instead.
1886 PgStat_StatTabEntry *
1887 pgstat_fetch_stat_tabentry(Oid relid)
1890 PgStat_StatDBEntry *dbentry;
1891 PgStat_StatTabEntry *tabentry;
1894 * If not done for this transaction, read the statistics collector stats
1895 * file into some hash tables.
1897 backend_read_statsfile();
1900 * Lookup our database, then look in its table hash table.
1902 dbid = MyDatabaseId;
1903 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1906 if (dbentry != NULL && dbentry->tables != NULL)
1908 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1916 * If we didn't find it, maybe it's a shared table.
1919 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1922 if (dbentry != NULL && dbentry->tables != NULL)
1924 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1936 * pgstat_fetch_stat_funcentry() -
1938 * Support function for the SQL-callable pgstat* functions. Returns
1939 * the collected statistics for one function or NULL.
1942 PgStat_StatFuncEntry *
1943 pgstat_fetch_stat_funcentry(Oid func_id)
1945 PgStat_StatDBEntry *dbentry;
1946 PgStat_StatFuncEntry *funcentry = NULL;
1948 /* load the stats file if needed */
1949 backend_read_statsfile();
1951 /* Lookup our database, then find the requested function. */
1952 dbentry = pgstat_fetch_stat_dbentry(MyDatabaseId);
1953 if (dbentry != NULL && dbentry->functions != NULL)
1955 funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
1965 * pgstat_fetch_stat_beentry() -
1967 * Support function for the SQL-callable pgstat* functions. Returns
1968 * our local copy of the current-activity entry for one backend.
1970 * NB: caller is responsible for a check if the user is permitted to see
1971 * this info (especially the querystring).
1975 pgstat_fetch_stat_beentry(int beid)
1977 pgstat_read_current_status();
1979 if (beid < 1 || beid > localNumBackends)
1982 return &localBackendStatusTable[beid - 1];
1987 * pgstat_fetch_stat_numbackends() -
1989 * Support function for the SQL-callable pgstat* functions. Returns
1990 * the maximum current backend id.
1994 pgstat_fetch_stat_numbackends(void)
1996 pgstat_read_current_status();
1998 return localNumBackends;
2003 * pgstat_fetch_global() -
2005 * Support function for the SQL-callable pgstat* functions. Returns
2006 * a pointer to the global statistics struct.
2009 PgStat_GlobalStats *
2010 pgstat_fetch_global(void)
2012 backend_read_statsfile();
2014 return &globalStats;
2018 /* ------------------------------------------------------------
2019 * Functions for management of the shared-memory PgBackendStatus array
2020 * ------------------------------------------------------------
2023 static PgBackendStatus *BackendStatusArray = NULL;
2024 static PgBackendStatus *MyBEEntry = NULL;
2025 static char *BackendActivityBuffer = NULL;
2029 * Report shared-memory space needed by CreateSharedBackendStatus.
2032 BackendStatusShmemSize(void)
2036 size = add_size(mul_size(sizeof(PgBackendStatus), MaxBackends),
2037 mul_size(pgstat_track_activity_query_size, MaxBackends));
2042 * Initialize the shared status array and activity string buffer during
2043 * postmaster startup.
2046 CreateSharedBackendStatus(void)
2053 /* Create or attach to the shared array */
2054 size = mul_size(sizeof(PgBackendStatus), MaxBackends);
2055 BackendStatusArray = (PgBackendStatus *)
2056 ShmemInitStruct("Backend Status Array", size, &found);
2061 * We're the first - initialize.
2063 MemSet(BackendStatusArray, 0, size);
2066 /* Create or attach to the shared activity buffer */
2067 size = mul_size(pgstat_track_activity_query_size, MaxBackends);
2068 BackendActivityBuffer = (char*)
2069 ShmemInitStruct("Backend Activity Buffer", size, &found);
2073 MemSet(BackendActivityBuffer, 0, size);
2075 /* Initialize st_activity pointers. */
2076 buffer = BackendActivityBuffer;
2077 for (i = 0; i < MaxBackends; i++) {
2078 BackendStatusArray[i].st_activity = buffer;
2079 buffer += pgstat_track_activity_query_size;
2086 * pgstat_initialize() -
2088 * Initialize pgstats state, and set up our on-proc-exit hook.
2089 * Called from InitPostgres. MyBackendId must be set,
2090 * but we must not have started any transaction yet (since the
2091 * exit hook must run after the last transaction exit).
2095 pgstat_initialize(void)
2097 /* Initialize MyBEEntry */
2098 Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
2099 MyBEEntry = &BackendStatusArray[MyBackendId - 1];
2101 /* Set up a process-exit hook to clean up */
2102 on_shmem_exit(pgstat_beshutdown_hook, 0);
2106 * pgstat_bestart() -
2108 * Initialize this backend's entry in the PgBackendStatus array.
2109 * Called from InitPostgres. MyDatabaseId and session userid must be set
2110 * (hence, this cannot be combined with pgstat_initialize).
2114 pgstat_bestart(void)
2116 TimestampTz proc_start_timestamp;
2118 SockAddr clientaddr;
2119 volatile PgBackendStatus *beentry;
2122 * To minimize the time spent modifying the PgBackendStatus entry, fetch
2123 * all the needed data first.
2125 * If we have a MyProcPort, use its session start time (for consistency,
2126 * and to save a kernel call).
2129 proc_start_timestamp = MyProcPort->SessionStartTime;
2131 proc_start_timestamp = GetCurrentTimestamp();
2132 userid = GetSessionUserId();
2135 * We may not have a MyProcPort (eg, if this is the autovacuum process).
2136 * If so, use all-zeroes client address, which is dealt with specially in
2137 * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
2140 memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
2142 MemSet(&clientaddr, 0, sizeof(clientaddr));
2145 * Initialize my status entry, following the protocol of bumping
2146 * st_changecount before and after; and make sure it's even afterwards. We
2147 * use a volatile pointer here to ensure the compiler doesn't try to get
2150 beentry = MyBEEntry;
2153 beentry->st_changecount++;
2154 } while ((beentry->st_changecount & 1) == 0);
2156 beentry->st_procpid = MyProcPid;
2157 beentry->st_proc_start_timestamp = proc_start_timestamp;
2158 beentry->st_activity_start_timestamp = 0;
2159 beentry->st_xact_start_timestamp = 0;
2160 beentry->st_databaseid = MyDatabaseId;
2161 beentry->st_userid = userid;
2162 beentry->st_clientaddr = clientaddr;
2163 beentry->st_waiting = false;
2164 beentry->st_activity[0] = '\0';
2165 /* Also make sure the last byte in the string area is always 0 */
2166 beentry->st_activity[pgstat_track_activity_query_size - 1] = '\0';
2168 beentry->st_changecount++;
2169 Assert((beentry->st_changecount & 1) == 0);
2173 * Shut down a single backend's statistics reporting at process exit.
2175 * Flush any remaining statistics counts out to the collector.
2176 * Without this, operations triggered during backend exit (such as
2177 * temp table deletions) won't be counted.
2179 * Lastly, clear out our entry in the PgBackendStatus array.
2182 pgstat_beshutdown_hook(int code, Datum arg)
2184 volatile PgBackendStatus *beentry = MyBEEntry;
2186 pgstat_report_stat(true);
2189 * Clear my status entry, following the protocol of bumping st_changecount
2190 * before and after. We use a volatile pointer here to ensure the
2191 * compiler doesn't try to get cute.
2193 beentry->st_changecount++;
2195 beentry->st_procpid = 0; /* mark invalid */
2197 beentry->st_changecount++;
2198 Assert((beentry->st_changecount & 1) == 0);
2203 * pgstat_report_activity() -
2205 * Called from tcop/postgres.c to report what the backend is actually doing
2206 * (usually "<IDLE>" or the start of the query to be executed).
2210 pgstat_report_activity(const char *cmd_str)
2212 volatile PgBackendStatus *beentry = MyBEEntry;
2213 TimestampTz start_timestamp;
2216 TRACE_POSTGRESQL_STATEMENT_STATUS(cmd_str);
2218 if (!pgstat_track_activities || !beentry)
2222 * To minimize the time spent modifying the entry, fetch all the needed
2225 start_timestamp = GetCurrentStatementStartTimestamp();
2227 len = strlen(cmd_str);
2228 len = pg_mbcliplen(cmd_str, len, pgstat_track_activity_query_size - 1);
2231 * Update my status entry, following the protocol of bumping
2232 * st_changecount before and after. We use a volatile pointer here to
2233 * ensure the compiler doesn't try to get cute.
2235 beentry->st_changecount++;
2237 beentry->st_activity_start_timestamp = start_timestamp;
2238 memcpy((char *) beentry->st_activity, cmd_str, len);
2239 beentry->st_activity[len] = '\0';
2241 beentry->st_changecount++;
2242 Assert((beentry->st_changecount & 1) == 0);
2246 * Report current transaction start timestamp as the specified value.
2247 * Zero means there is no active transaction.
2250 pgstat_report_xact_timestamp(TimestampTz tstamp)
2252 volatile PgBackendStatus *beentry = MyBEEntry;
2254 if (!pgstat_track_activities || !beentry)
2258 * Update my status entry, following the protocol of bumping
2259 * st_changecount before and after. We use a volatile pointer here to
2260 * ensure the compiler doesn't try to get cute.
2262 beentry->st_changecount++;
2263 beentry->st_xact_start_timestamp = tstamp;
2264 beentry->st_changecount++;
2265 Assert((beentry->st_changecount & 1) == 0);
2269 * pgstat_report_waiting() -
2271 * Called from lock manager to report beginning or end of a lock wait.
2273 * NB: this *must* be able to survive being called before MyBEEntry has been
2278 pgstat_report_waiting(bool waiting)
2280 volatile PgBackendStatus *beentry = MyBEEntry;
2282 if (!pgstat_track_activities || !beentry)
2286 * Since this is a single-byte field in a struct that only this process
2287 * may modify, there seems no need to bother with the st_changecount
2288 * protocol. The update must appear atomic in any case.
2290 beentry->st_waiting = waiting;
2295 * pgstat_read_current_status() -
2297 * Copy the current contents of the PgBackendStatus array to local memory,
2298 * if not already done in this transaction.
2302 pgstat_read_current_status(void)
2304 volatile PgBackendStatus *beentry;
2305 PgBackendStatus *localtable;
2306 PgBackendStatus *localentry;
2307 char *localactivity;
2310 Assert(!pgStatRunningInCollector);
2311 if (localBackendStatusTable)
2312 return; /* already done */
2314 pgstat_setup_memcxt();
2316 localtable = (PgBackendStatus *)
2317 MemoryContextAlloc(pgStatLocalContext,
2318 sizeof(PgBackendStatus) * MaxBackends);
2319 localactivity = (char *)
2320 MemoryContextAlloc(pgStatLocalContext,
2321 pgstat_track_activity_query_size * MaxBackends);
2322 localNumBackends = 0;
2324 beentry = BackendStatusArray;
2325 localentry = localtable;
2326 for (i = 1; i <= MaxBackends; i++)
2329 * Follow the protocol of retrying if st_changecount changes while we
2330 * copy the entry, or if it's odd. (The check for odd is needed to
2331 * cover the case where we are able to completely copy the entry while
2332 * the source backend is between increment steps.) We use a volatile
2333 * pointer here to ensure the compiler doesn't try to get cute.
2337 int save_changecount = beentry->st_changecount;
2339 localentry->st_procpid = beentry->st_procpid;
2340 if (localentry->st_procpid > 0)
2342 memcpy(localentry, (char *) beentry, sizeof(PgBackendStatus));
2344 * strcpy is safe even if the string is modified concurrently,
2345 * because there's always a \0 at the end of the buffer.
2347 strcpy(localactivity, (char *) beentry->st_activity);
2348 localentry->st_activity = localactivity;
2351 if (save_changecount == beentry->st_changecount &&
2352 (save_changecount & 1) == 0)
2355 /* Make sure we can break out of loop if stuck... */
2356 CHECK_FOR_INTERRUPTS();
2360 /* Only valid entries get included into the local array */
2361 if (localentry->st_procpid > 0)
2364 localactivity += pgstat_track_activity_query_size;
2369 /* Set the pointer only after completion of a valid table */
2370 localBackendStatusTable = localtable;
2375 * pgstat_get_backend_current_activity() -
2377 * Return a string representing the current activity of the backend with
2378 * the specified PID. This looks directly at the BackendStatusArray,
2379 * and so will provide current information regardless of the age of our
2380 * transaction's snapshot of the status array.
2382 * It is the caller's responsibility to invoke this only for backends whose
2383 * state is expected to remain stable while the result is in use. The
2384 * only current use is in deadlock reporting, where we can expect that
2385 * the target backend is blocked on a lock. (There are corner cases
2386 * where the target's wait could get aborted while we are looking at it,
2387 * but the very worst consequence is to return a pointer to a string
2388 * that's been changed, so we won't worry too much.)
2390 * Note: return strings for special cases match pg_stat_get_backend_activity.
2394 pgstat_get_backend_current_activity(int pid, bool checkUser)
2396 PgBackendStatus *beentry;
2399 beentry = BackendStatusArray;
2400 for (i = 1; i <= MaxBackends; i++)
2403 * Although we expect the target backend's entry to be stable, that
2404 * doesn't imply that anyone else's is. To avoid identifying the
2405 * wrong backend, while we check for a match to the desired PID we
2406 * must follow the protocol of retrying if st_changecount changes
2407 * while we examine the entry, or if it's odd. (This might be
2408 * unnecessary, since fetching or storing an int is almost certainly
2409 * atomic, but let's play it safe.) We use a volatile pointer here
2410 * to ensure the compiler doesn't try to get cute.
2412 volatile PgBackendStatus *vbeentry = beentry;
2417 int save_changecount = vbeentry->st_changecount;
2419 found = (vbeentry->st_procpid == pid);
2421 if (save_changecount == vbeentry->st_changecount &&
2422 (save_changecount & 1) == 0)
2425 /* Make sure we can break out of loop if stuck... */
2426 CHECK_FOR_INTERRUPTS();
2431 /* Now it is safe to use the non-volatile pointer */
2432 if (checkUser && !superuser() && beentry->st_userid != GetUserId())
2433 return "<insufficient privilege>";
2434 else if (*(beentry->st_activity) == '\0')
2435 return "<command string not enabled>";
2437 return beentry->st_activity;
2443 /* If we get here, caller is in error ... */
2444 return "<backend information not available>";
2448 /* ------------------------------------------------------------
2449 * Local support functions follow
2450 * ------------------------------------------------------------
2455 * pgstat_setheader() -
2457 * Set common header fields in a statistics message
2461 pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
2463 hdr->m_type = mtype;
2470 * Send out one statistics message to the collector
2474 pgstat_send(void *msg, int len)
2481 ((PgStat_MsgHdr *) msg)->m_size = len;
2483 /* We'll retry after EINTR, but ignore all other failures */
2486 rc = send(pgStatSock, msg, len, 0);
2487 } while (rc < 0 && errno == EINTR);
2489 #ifdef USE_ASSERT_CHECKING
2490 /* In debug builds, log send failures ... */
2492 elog(LOG, "could not send to statistics collector: %m");
2497 * pgstat_send_bgwriter() -
2499 * Send bgwriter statistics to the collector
2503 pgstat_send_bgwriter(void)
2505 /* We assume this initializes to zeroes */
2506 static const PgStat_MsgBgWriter all_zeroes;
2509 * This function can be called even if nothing at all has happened. In
2510 * this case, avoid sending a completely empty message to the stats
2513 if (memcmp(&BgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
2517 * Prepare and send the message
2519 pgstat_setheader(&BgWriterStats.m_hdr, PGSTAT_MTYPE_BGWRITER);
2520 pgstat_send(&BgWriterStats, sizeof(BgWriterStats));
2523 * Clear out the statistics buffer, so it can be re-used.
2525 MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
2530 * PgstatCollectorMain() -
2532 * Start up the statistics collector process. This is the body of the
2533 * postmaster child process.
2535 * The argc/argv parameters are valid only in EXEC_BACKEND case.
2538 NON_EXEC_STATIC void
2539 PgstatCollectorMain(int argc, char *argv[])
2541 struct itimerval write_timeout;
2542 bool need_timer = false;
2548 struct pollfd input_fd;
2550 struct timeval sel_timeout;
2555 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
2557 MyProcPid = getpid(); /* reset MyProcPid */
2559 MyStartTime = time(NULL); /* record Start Time for logging */
2562 * If possible, make this process a group leader, so that the postmaster
2563 * can signal any child processes too. (pgstat probably never has any
2564 * child processes, but for consistency we make all postmaster child
2565 * processes do this.)
2569 elog(FATAL, "setsid() failed: %m");
2573 * Ignore all signals usually bound to some action in the postmaster,
2574 * except SIGQUIT and SIGALRM.
2576 pqsignal(SIGHUP, pgstat_sighup_handler);
2577 pqsignal(SIGINT, SIG_IGN);
2578 pqsignal(SIGTERM, SIG_IGN);
2579 pqsignal(SIGQUIT, pgstat_exit);
2580 pqsignal(SIGALRM, force_statwrite);
2581 pqsignal(SIGPIPE, SIG_IGN);
2582 pqsignal(SIGUSR1, SIG_IGN);
2583 pqsignal(SIGUSR2, SIG_IGN);
2584 pqsignal(SIGCHLD, SIG_DFL);
2585 pqsignal(SIGTTIN, SIG_DFL);
2586 pqsignal(SIGTTOU, SIG_DFL);
2587 pqsignal(SIGCONT, SIG_DFL);
2588 pqsignal(SIGWINCH, SIG_DFL);
2589 PG_SETMASK(&UnBlockSig);
2592 * Identify myself via ps
2594 init_ps_display("stats collector process", "", "", "");
2597 * Arrange to write the initial status file right away
2599 need_statwrite = true;
2601 /* Preset the delay between status file writes */
2602 MemSet(&write_timeout, 0, sizeof(struct itimerval));
2603 write_timeout.it_value.tv_sec = PGSTAT_STAT_INTERVAL / 1000;
2604 write_timeout.it_value.tv_usec = (PGSTAT_STAT_INTERVAL % 1000) * 1000;
2607 * Read in an existing statistics stats file or initialize the stats to
2610 pgStatRunningInCollector = true;
2611 pgStatDBHash = pgstat_read_statsfile(InvalidOid, true);
2614 * Setup the descriptor set for select(2). Since only one bit in the set
2615 * ever changes, we need not repeat FD_ZERO each time.
2617 #if !defined(HAVE_POLL) && !defined(WIN32)
2622 * Loop to process messages until we get SIGQUIT or detect ungraceful
2623 * death of our parent postmaster.
2625 * For performance reasons, we don't want to do a PostmasterIsAlive() test
2626 * after every message; instead, do it at statwrite time and if
2627 * select()/poll() is interrupted by timeout.
2634 * Quit if we get SIGQUIT from the postmaster.
2640 * Reload configuration if we got SIGHUP from the postmaster.
2641 * Also, signal a new write of the file, so we drop a new file as
2642 * soon as possible of the directory for it changes.
2646 ProcessConfigFile(PGC_SIGHUP);
2648 need_statwrite = true;
2652 * If time to write the stats file, do so. Note that the alarm
2653 * interrupt isn't re-enabled immediately, but only after we next
2654 * receive a stats message; so no cycles are wasted when there is
2659 /* Check for postmaster death; if so we'll write file below */
2660 if (!PostmasterIsAlive(true))
2663 pgstat_write_statsfile(false);
2664 need_statwrite = false;
2669 * Wait for a message to arrive; but not for more than
2670 * PGSTAT_SELECT_TIMEOUT seconds. (This determines how quickly we will
2671 * shut down after an ungraceful postmaster termination; so it needn't
2672 * be very fast. However, on some systems SIGQUIT won't interrupt the
2673 * poll/select call, so this also limits speed of response to SIGQUIT,
2674 * which is more important.)
2676 * We use poll(2) if available, otherwise select(2). Win32 has its own
2681 input_fd.fd = pgStatSock;
2682 input_fd.events = POLLIN | POLLERR;
2683 input_fd.revents = 0;
2685 if (poll(&input_fd, 1, PGSTAT_SELECT_TIMEOUT * 1000) < 0)
2690 (errcode_for_socket_access(),
2691 errmsg("poll() failed in statistics collector: %m")));
2694 got_data = (input_fd.revents != 0);
2695 #else /* !HAVE_POLL */
2697 FD_SET(pgStatSock, &rfds);
2700 * timeout struct is modified by select() on some operating systems,
2701 * so re-fill it each time.
2703 sel_timeout.tv_sec = PGSTAT_SELECT_TIMEOUT;
2704 sel_timeout.tv_usec = 0;
2706 if (select(pgStatSock + 1, &rfds, NULL, NULL, &sel_timeout) < 0)
2711 (errcode_for_socket_access(),
2712 errmsg("select() failed in statistics collector: %m")));
2715 got_data = FD_ISSET(pgStatSock, &rfds);
2716 #endif /* HAVE_POLL */
2718 got_data = pgwin32_waitforsinglesocket(pgStatSock, FD_READ,
2719 PGSTAT_SELECT_TIMEOUT * 1000);
2723 * If there is a message on the socket, read it and check for
2728 len = recv(pgStatSock, (char *) &msg,
2729 sizeof(PgStat_Msg), 0);
2735 (errcode_for_socket_access(),
2736 errmsg("could not read statistics message: %m")));
2740 * We ignore messages that are smaller than our common header
2742 if (len < sizeof(PgStat_MsgHdr))
2746 * The received length must match the length in the header
2748 if (msg.msg_hdr.m_size != len)
2752 * O.K. - we accept this message. Process it.
2754 switch (msg.msg_hdr.m_type)
2756 case PGSTAT_MTYPE_DUMMY:
2759 case PGSTAT_MTYPE_TABSTAT:
2760 pgstat_recv_tabstat((PgStat_MsgTabstat *) &msg, len);
2763 case PGSTAT_MTYPE_TABPURGE:
2764 pgstat_recv_tabpurge((PgStat_MsgTabpurge *) &msg, len);
2767 case PGSTAT_MTYPE_DROPDB:
2768 pgstat_recv_dropdb((PgStat_MsgDropdb *) &msg, len);
2771 case PGSTAT_MTYPE_RESETCOUNTER:
2772 pgstat_recv_resetcounter((PgStat_MsgResetcounter *) &msg,
2776 case PGSTAT_MTYPE_AUTOVAC_START:
2777 pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
2780 case PGSTAT_MTYPE_VACUUM:
2781 pgstat_recv_vacuum((PgStat_MsgVacuum *) &msg, len);
2784 case PGSTAT_MTYPE_ANALYZE:
2785 pgstat_recv_analyze((PgStat_MsgAnalyze *) &msg, len);
2788 case PGSTAT_MTYPE_BGWRITER:
2789 pgstat_recv_bgwriter((PgStat_MsgBgWriter *) &msg, len);
2792 case PGSTAT_MTYPE_FUNCSTAT:
2793 pgstat_recv_funcstat((PgStat_MsgFuncstat *) &msg, len);
2796 case PGSTAT_MTYPE_FUNCPURGE:
2797 pgstat_recv_funcpurge((PgStat_MsgFuncpurge *) &msg, len);
2805 * If this is the first message after we wrote the stats file the
2806 * last time, enable the alarm interrupt to make it be written
2811 if (setitimer(ITIMER_REAL, &write_timeout, NULL))
2813 (errmsg("could not set statistics collector timer: %m")));
2820 * We can only get here if the select/poll timeout elapsed. Check
2821 * for postmaster death.
2823 if (!PostmasterIsAlive(true))
2826 } /* end of message-processing loop */
2829 * Save the final stats to reuse at next startup.
2831 pgstat_write_statsfile(true);
2837 /* SIGQUIT signal handler for collector process */
2839 pgstat_exit(SIGNAL_ARGS)
2844 /* SIGALRM signal handler for collector process */
2846 force_statwrite(SIGNAL_ARGS)
2848 need_statwrite = true;
2851 /* SIGHUP handler for collector process */
2853 pgstat_sighup_handler(SIGNAL_ARGS)
2860 * Lookup the hash table entry for the specified database. If no hash
2861 * table entry exists, initialize it, if the create parameter is true.
2862 * Else, return NULL.
2864 static PgStat_StatDBEntry *
2865 pgstat_get_db_entry(Oid databaseid, bool create)
2867 PgStat_StatDBEntry *result;
2869 HASHACTION action = (create ? HASH_ENTER : HASH_FIND);
2871 /* Lookup or create the hash table entry for this database */
2872 result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2876 if (!create && !found)
2879 /* If not found, initialize the new one. */
2884 result->tables = NULL;
2885 result->functions = NULL;
2886 result->n_xact_commit = 0;
2887 result->n_xact_rollback = 0;
2888 result->n_blocks_fetched = 0;
2889 result->n_blocks_hit = 0;
2890 result->n_tuples_returned = 0;
2891 result->n_tuples_fetched = 0;
2892 result->n_tuples_inserted = 0;
2893 result->n_tuples_updated = 0;
2894 result->n_tuples_deleted = 0;
2895 result->last_autovac_time = 0;
2897 memset(&hash_ctl, 0, sizeof(hash_ctl));
2898 hash_ctl.keysize = sizeof(Oid);
2899 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2900 hash_ctl.hash = oid_hash;
2901 result->tables = hash_create("Per-database table",
2902 PGSTAT_TAB_HASH_SIZE,
2904 HASH_ELEM | HASH_FUNCTION);
2906 hash_ctl.keysize = sizeof(Oid);
2907 hash_ctl.entrysize = sizeof(PgStat_StatFuncEntry);
2908 hash_ctl.hash = oid_hash;
2909 result->functions = hash_create("Per-database function",
2910 PGSTAT_FUNCTION_HASH_SIZE,
2912 HASH_ELEM | HASH_FUNCTION);
2920 * pgstat_write_statsfile() -
2923 * If writing to the permanent file (happens when the collector is
2924 * shutting down only), remove the temporary file so that backends
2925 * starting up under a new postmaster can't read the old data before
2926 * the new collector is ready.
2930 pgstat_write_statsfile(bool permanent)
2932 HASH_SEQ_STATUS hstat;
2933 HASH_SEQ_STATUS tstat;
2934 HASH_SEQ_STATUS fstat;
2935 PgStat_StatDBEntry *dbentry;
2936 PgStat_StatTabEntry *tabentry;
2937 PgStat_StatFuncEntry *funcentry;
2940 const char *tmpfile = permanent?PGSTAT_STAT_PERMANENT_TMPFILE:pgstat_stat_tmpname;
2941 const char *statfile = permanent?PGSTAT_STAT_PERMANENT_FILENAME:pgstat_stat_filename;
2944 * Open the statistics temp file to write out the current values.
2946 fpout = fopen(tmpfile, PG_BINARY_W);
2950 (errcode_for_file_access(),
2951 errmsg("could not open temporary statistics file \"%s\": %m",
2957 * Write the file header --- currently just a format ID.
2959 format_id = PGSTAT_FILE_FORMAT_ID;
2960 fwrite(&format_id, sizeof(format_id), 1, fpout);
2963 * Write global stats struct
2965 fwrite(&globalStats, sizeof(globalStats), 1, fpout);
2968 * Walk through the database table.
2970 hash_seq_init(&hstat, pgStatDBHash);
2971 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
2974 * Write out the DB entry including the number of live backends. We
2975 * don't write the tables or functions pointers, since they're of
2976 * no use to any other process.
2979 fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
2982 * Walk through the database's access stats per table.
2984 hash_seq_init(&tstat, dbentry->tables);
2985 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
2988 fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
2992 * Walk through the database's function stats table.
2994 hash_seq_init(&fstat, dbentry->functions);
2995 while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&fstat)) != NULL)
2998 fwrite(funcentry, sizeof(PgStat_StatFuncEntry), 1, fpout);
3002 * Mark the end of this DB
3008 * No more output to be done. Close the temp file and replace the old
3009 * pgstat.stat with it. The ferror() check replaces testing for error
3010 * after each individual fputc or fwrite above.
3017 (errcode_for_file_access(),
3018 errmsg("could not write temporary statistics file \"%s\": %m",
3023 else if (fclose(fpout) < 0)
3026 (errcode_for_file_access(),
3027 errmsg("could not close temporary statistics file \"%s\": %m",
3031 else if (rename(tmpfile, statfile) < 0)
3034 (errcode_for_file_access(),
3035 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
3036 tmpfile, statfile)));
3041 unlink(pgstat_stat_filename);
3046 * pgstat_read_statsfile() -
3048 * Reads in an existing statistics collector file and initializes the
3049 * databases' hash table (whose entries point to the tables' hash tables).
3053 pgstat_read_statsfile(Oid onlydb, bool permanent)
3055 PgStat_StatDBEntry *dbentry;
3056 PgStat_StatDBEntry dbbuf;
3057 PgStat_StatTabEntry *tabentry;
3058 PgStat_StatTabEntry tabbuf;
3059 PgStat_StatFuncEntry funcbuf;
3060 PgStat_StatFuncEntry *funcentry;
3063 HTAB *tabhash = NULL;
3064 HTAB *funchash = NULL;
3068 const char *statfile = permanent?PGSTAT_STAT_PERMANENT_FILENAME:pgstat_stat_filename;
3071 * The tables will live in pgStatLocalContext.
3073 pgstat_setup_memcxt();
3076 * Create the DB hashtable
3078 memset(&hash_ctl, 0, sizeof(hash_ctl));
3079 hash_ctl.keysize = sizeof(Oid);
3080 hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
3081 hash_ctl.hash = oid_hash;
3082 hash_ctl.hcxt = pgStatLocalContext;
3083 dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
3084 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
3087 * Clear out global statistics so they start from zero in case we can't
3088 * load an existing statsfile.
3090 memset(&globalStats, 0, sizeof(globalStats));
3093 * Try to open the status file. If it doesn't exist, the backends simply
3094 * return zero for anything and the collector simply starts from scratch
3095 * with empty counters.
3097 if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
3101 * Verify it's of the expected format.
3103 if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id)
3104 || format_id != PGSTAT_FILE_FORMAT_ID)
3106 ereport(pgStatRunningInCollector ? LOG : WARNING,
3107 (errmsg("corrupted pgstat.stat file")));
3112 * Read global stats struct
3114 if (fread(&globalStats, 1, sizeof(globalStats), fpin) != sizeof(globalStats))
3116 ereport(pgStatRunningInCollector ? LOG : WARNING,
3117 (errmsg("corrupted pgstat.stat file")));
3122 * We found an existing collector stats file. Read it and put all the
3123 * hashtable entries into place.
3127 switch (fgetc(fpin))
3130 * 'D' A PgStat_StatDBEntry struct describing a database
3131 * follows. Subsequently, zero to many 'T' and 'F' entries
3132 * will follow until a 'd' is encountered.
3135 if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
3136 fpin) != offsetof(PgStat_StatDBEntry, tables))
3138 ereport(pgStatRunningInCollector ? LOG : WARNING,
3139 (errmsg("corrupted pgstat.stat file")));
3144 * Add to the DB hash
3146 dbentry = (PgStat_StatDBEntry *) hash_search(dbhash,
3147 (void *) &dbbuf.databaseid,
3152 ereport(pgStatRunningInCollector ? LOG : WARNING,
3153 (errmsg("corrupted pgstat.stat file")));
3157 memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
3158 dbentry->tables = NULL;
3159 dbentry->functions = NULL;
3162 * Don't collect tables if not the requested DB (or the
3163 * shared-table info)
3165 if (onlydb != InvalidOid)
3167 if (dbbuf.databaseid != onlydb &&
3168 dbbuf.databaseid != InvalidOid)
3172 memset(&hash_ctl, 0, sizeof(hash_ctl));
3173 hash_ctl.keysize = sizeof(Oid);
3174 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
3175 hash_ctl.hash = oid_hash;
3176 hash_ctl.hcxt = pgStatLocalContext;
3177 dbentry->tables = hash_create("Per-database table",
3178 PGSTAT_TAB_HASH_SIZE,
3180 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
3182 hash_ctl.keysize = sizeof(Oid);
3183 hash_ctl.entrysize = sizeof(PgStat_StatFuncEntry);
3184 hash_ctl.hash = oid_hash;
3185 hash_ctl.hcxt = pgStatLocalContext;
3186 dbentry->functions = hash_create("Per-database function",
3187 PGSTAT_FUNCTION_HASH_SIZE,
3189 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
3191 * Arrange that following records add entries to this
3192 * database's hash tables.
3194 tabhash = dbentry->tables;
3195 funchash = dbentry->functions;
3199 * 'd' End of this database.
3207 * 'T' A PgStat_StatTabEntry follows.
3210 if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
3211 fpin) != sizeof(PgStat_StatTabEntry))
3213 ereport(pgStatRunningInCollector ? LOG : WARNING,
3214 (errmsg("corrupted pgstat.stat file")));
3219 * Skip if table belongs to a not requested database.
3221 if (tabhash == NULL)
3224 tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
3225 (void *) &tabbuf.tableid,
3226 HASH_ENTER, &found);
3230 ereport(pgStatRunningInCollector ? LOG : WARNING,
3231 (errmsg("corrupted pgstat.stat file")));
3235 memcpy(tabentry, &tabbuf, sizeof(tabbuf));
3239 * 'F' A PgStat_StatFuncEntry follows.
3242 if (fread(&funcbuf, 1, sizeof(PgStat_StatFuncEntry),
3243 fpin) != sizeof(PgStat_StatFuncEntry))
3245 ereport(pgStatRunningInCollector ? LOG : WARNING,
3246 (errmsg("corrupted pgstat.stat file")));
3251 * Skip if function belongs to a not requested database.
3253 if (funchash == NULL)
3256 funcentry = (PgStat_StatFuncEntry *) hash_search(funchash,
3257 (void *) &funcbuf.functionid,
3258 HASH_ENTER, &found);
3262 ereport(pgStatRunningInCollector ? LOG : WARNING,
3263 (errmsg("corrupted pgstat.stat file")));
3267 memcpy(funcentry, &funcbuf, sizeof(funcbuf));
3271 * 'E' The EOF marker of a complete stats file.
3277 ereport(pgStatRunningInCollector ? LOG : WARNING,
3278 (errmsg("corrupted pgstat.stat file")));
3287 unlink(PGSTAT_STAT_PERMANENT_FILENAME);
3293 * If not already done, read the statistics collector stats file into
3294 * some hash tables. The results will be kept until pgstat_clear_snapshot()
3295 * is called (typically, at end of transaction).
3298 backend_read_statsfile(void)
3300 /* already read it? */
3303 Assert(!pgStatRunningInCollector);
3305 /* Autovacuum launcher wants stats about all databases */
3306 if (IsAutoVacuumLauncherProcess())
3307 pgStatDBHash = pgstat_read_statsfile(InvalidOid, false);
3309 pgStatDBHash = pgstat_read_statsfile(MyDatabaseId, false);
3314 * pgstat_setup_memcxt() -
3316 * Create pgStatLocalContext, if not already done.
3320 pgstat_setup_memcxt(void)
3322 if (!pgStatLocalContext)
3323 pgStatLocalContext = AllocSetContextCreate(TopMemoryContext,
3324 "Statistics snapshot",
3325 ALLOCSET_SMALL_MINSIZE,
3326 ALLOCSET_SMALL_INITSIZE,
3327 ALLOCSET_SMALL_MAXSIZE);
3332 * pgstat_clear_snapshot() -
3334 * Discard any data collected in the current transaction. Any subsequent
3335 * request will cause new snapshots to be read.
3337 * This is also invoked during transaction commit or abort to discard
3338 * the no-longer-wanted snapshot.
3342 pgstat_clear_snapshot(void)
3344 /* Release memory, if any was allocated */
3345 if (pgStatLocalContext)
3346 MemoryContextDelete(pgStatLocalContext);
3348 /* Reset variables */
3349 pgStatLocalContext = NULL;
3350 pgStatDBHash = NULL;
3351 localBackendStatusTable = NULL;
3352 localNumBackends = 0;
3357 * pgstat_recv_tabstat() -
3359 * Count what the backend has done.
3363 pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
3365 PgStat_TableEntry *tabmsg = &(msg->m_entry[0]);
3366 PgStat_StatDBEntry *dbentry;
3367 PgStat_StatTabEntry *tabentry;
3371 dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
3374 * Update database-wide stats.
3376 dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
3377 dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
3380 * Process all table entries in the message.
3382 for (i = 0; i < msg->m_nentries; i++)
3384 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
3385 (void *) &(tabmsg[i].t_id),
3386 HASH_ENTER, &found);
3391 * If it's a new table entry, initialize counters to the values we
3394 tabentry->numscans = tabmsg[i].t_counts.t_numscans;
3395 tabentry->tuples_returned = tabmsg[i].t_counts.t_tuples_returned;
3396 tabentry->tuples_fetched = tabmsg[i].t_counts.t_tuples_fetched;
3397 tabentry->tuples_inserted = tabmsg[i].t_counts.t_tuples_inserted;
3398 tabentry->tuples_updated = tabmsg[i].t_counts.t_tuples_updated;
3399 tabentry->tuples_deleted = tabmsg[i].t_counts.t_tuples_deleted;
3400 tabentry->tuples_hot_updated = tabmsg[i].t_counts.t_tuples_hot_updated;
3401 tabentry->n_live_tuples = tabmsg[i].t_counts.t_new_live_tuples;
3402 tabentry->n_dead_tuples = tabmsg[i].t_counts.t_new_dead_tuples;
3403 tabentry->blocks_fetched = tabmsg[i].t_counts.t_blocks_fetched;
3404 tabentry->blocks_hit = tabmsg[i].t_counts.t_blocks_hit;
3406 tabentry->last_anl_tuples = 0;
3407 tabentry->vacuum_timestamp = 0;
3408 tabentry->autovac_vacuum_timestamp = 0;
3409 tabentry->analyze_timestamp = 0;
3410 tabentry->autovac_analyze_timestamp = 0;
3415 * Otherwise add the values to the existing entry.
3417 tabentry->numscans += tabmsg[i].t_counts.t_numscans;
3418 tabentry->tuples_returned += tabmsg[i].t_counts.t_tuples_returned;
3419 tabentry->tuples_fetched += tabmsg[i].t_counts.t_tuples_fetched;
3420 tabentry->tuples_inserted += tabmsg[i].t_counts.t_tuples_inserted;
3421 tabentry->tuples_updated += tabmsg[i].t_counts.t_tuples_updated;
3422 tabentry->tuples_deleted += tabmsg[i].t_counts.t_tuples_deleted;
3423 tabentry->tuples_hot_updated += tabmsg[i].t_counts.t_tuples_hot_updated;
3424 tabentry->n_live_tuples += tabmsg[i].t_counts.t_new_live_tuples;
3425 tabentry->n_dead_tuples += tabmsg[i].t_counts.t_new_dead_tuples;
3426 tabentry->blocks_fetched += tabmsg[i].t_counts.t_blocks_fetched;
3427 tabentry->blocks_hit += tabmsg[i].t_counts.t_blocks_hit;
3430 /* Clamp n_live_tuples in case of negative new_live_tuples */
3431 tabentry->n_live_tuples = Max(tabentry->n_live_tuples, 0);
3432 /* Likewise for n_dead_tuples */
3433 tabentry->n_dead_tuples = Max(tabentry->n_dead_tuples, 0);
3436 * Add per-table stats to the per-database entry, too.
3438 dbentry->n_tuples_returned += tabmsg[i].t_counts.t_tuples_returned;
3439 dbentry->n_tuples_fetched += tabmsg[i].t_counts.t_tuples_fetched;
3440 dbentry->n_tuples_inserted += tabmsg[i].t_counts.t_tuples_inserted;
3441 dbentry->n_tuples_updated += tabmsg[i].t_counts.t_tuples_updated;
3442 dbentry->n_tuples_deleted += tabmsg[i].t_counts.t_tuples_deleted;
3443 dbentry->n_blocks_fetched += tabmsg[i].t_counts.t_blocks_fetched;
3444 dbentry->n_blocks_hit += tabmsg[i].t_counts.t_blocks_hit;
3450 * pgstat_recv_tabpurge() -
3452 * Arrange for dead table removal.
3456 pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
3458 PgStat_StatDBEntry *dbentry;
3461 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3464 * No need to purge if we don't even know the database.
3466 if (!dbentry || !dbentry->tables)
3470 * Process all table entries in the message.
3472 for (i = 0; i < msg->m_nentries; i++)
3474 /* Remove from hashtable if present; we don't care if it's not. */
3475 (void) hash_search(dbentry->tables,
3476 (void *) &(msg->m_tableid[i]),
3483 * pgstat_recv_dropdb() -
3485 * Arrange for dead database removal
3489 pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len)
3491 PgStat_StatDBEntry *dbentry;
3494 * Lookup the database in the hashtable.
3496 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3499 * If found, remove it.
3503 if (dbentry->tables != NULL)
3504 hash_destroy(dbentry->tables);
3505 if (dbentry->functions != NULL)
3506 hash_destroy(dbentry->functions);
3508 if (hash_search(pgStatDBHash,
3509 (void *) &(dbentry->databaseid),
3510 HASH_REMOVE, NULL) == NULL)
3512 (errmsg("database hash table corrupted "
3513 "during cleanup --- abort")));
3519 * pgstat_recv_resetcounter() -
3521 * Reset the statistics for the specified database.
3525 pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
3528 PgStat_StatDBEntry *dbentry;
3531 * Lookup the database in the hashtable. Nothing to do if not there.
3533 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3539 * We simply throw away all the database's table entries by recreating a
3540 * new hash table for them.
3542 if (dbentry->tables != NULL)
3543 hash_destroy(dbentry->tables);
3544 if (dbentry->functions != NULL)
3545 hash_destroy(dbentry->functions);
3547 dbentry->tables = NULL;
3548 dbentry->functions = NULL;
3549 dbentry->n_xact_commit = 0;
3550 dbentry->n_xact_rollback = 0;
3551 dbentry->n_blocks_fetched = 0;
3552 dbentry->n_blocks_hit = 0;
3554 memset(&hash_ctl, 0, sizeof(hash_ctl));
3555 hash_ctl.keysize = sizeof(Oid);
3556 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
3557 hash_ctl.hash = oid_hash;
3558 dbentry->tables = hash_create("Per-database table",
3559 PGSTAT_TAB_HASH_SIZE,
3561 HASH_ELEM | HASH_FUNCTION);
3563 hash_ctl.keysize = sizeof(Oid);
3564 hash_ctl.entrysize = sizeof(PgStat_StatFuncEntry);
3565 hash_ctl.hash = oid_hash;
3566 dbentry->functions = hash_create("Per-database function",
3567 PGSTAT_FUNCTION_HASH_SIZE,
3569 HASH_ELEM | HASH_FUNCTION);
3573 * pgstat_recv_autovac() -
3575 * Process an autovacuum signalling message.
3579 pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len)
3581 PgStat_StatDBEntry *dbentry;
3584 * Lookup the database in the hashtable. Don't create the entry if it
3585 * doesn't exist, because autovacuum may be processing a template
3586 * database. If this isn't the case, the database is most likely to have
3587 * an entry already. (If it doesn't, not much harm is done anyway --
3588 * it'll get created as soon as somebody actually uses the database.)
3590 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3591 if (dbentry == NULL)
3595 * Store the last autovacuum time in the database entry.
3597 dbentry->last_autovac_time = msg->m_start_time;
3601 * pgstat_recv_vacuum() -
3603 * Process a VACUUM message.
3607 pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len)
3609 PgStat_StatDBEntry *dbentry;
3610 PgStat_StatTabEntry *tabentry;
3613 * Don't create either the database or table entry if it doesn't already
3614 * exist. This avoids bloating the stats with entries for stuff that is
3615 * only touched by vacuum and not by live operations.
3617 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3618 if (dbentry == NULL)
3621 tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
3623 if (tabentry == NULL)
3626 if (msg->m_autovacuum)
3627 tabentry->autovac_vacuum_timestamp = msg->m_vacuumtime;
3629 tabentry->vacuum_timestamp = msg->m_vacuumtime;
3630 tabentry->n_live_tuples = msg->m_tuples;
3631 /* Resetting dead_tuples to 0 is an approximation ... */
3632 tabentry->n_dead_tuples = 0;
3635 tabentry->last_anl_tuples = msg->m_tuples;
3636 if (msg->m_autovacuum)
3637 tabentry->autovac_analyze_timestamp = msg->m_vacuumtime;
3639 tabentry->analyze_timestamp = msg->m_vacuumtime;
3643 /* last_anl_tuples must never exceed n_live_tuples+n_dead_tuples */
3644 tabentry->last_anl_tuples = Min(tabentry->last_anl_tuples,
3650 * pgstat_recv_analyze() -
3652 * Process an ANALYZE message.
3656 pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
3658 PgStat_StatDBEntry *dbentry;
3659 PgStat_StatTabEntry *tabentry;
3662 * Don't create either the database or table entry if it doesn't already
3663 * exist. This avoids bloating the stats with entries for stuff that is
3664 * only touched by analyze and not by live operations.
3666 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3667 if (dbentry == NULL)
3670 tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
3672 if (tabentry == NULL)
3675 if (msg->m_autovacuum)
3676 tabentry->autovac_analyze_timestamp = msg->m_analyzetime;
3678 tabentry->analyze_timestamp = msg->m_analyzetime;
3679 tabentry->n_live_tuples = msg->m_live_tuples;
3680 tabentry->n_dead_tuples = msg->m_dead_tuples;
3681 tabentry->last_anl_tuples = msg->m_live_tuples + msg->m_dead_tuples;
3686 * pgstat_recv_bgwriter() -
3688 * Process a BGWRITER message.
3692 pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len)
3694 globalStats.timed_checkpoints += msg->m_timed_checkpoints;
3695 globalStats.requested_checkpoints += msg->m_requested_checkpoints;
3696 globalStats.buf_written_checkpoints += msg->m_buf_written_checkpoints;
3697 globalStats.buf_written_clean += msg->m_buf_written_clean;
3698 globalStats.maxwritten_clean += msg->m_maxwritten_clean;
3699 globalStats.buf_written_backend += msg->m_buf_written_backend;
3700 globalStats.buf_alloc += msg->m_buf_alloc;
3704 * pgstat_recv_funcstat() -
3706 * Count what the backend has done.
3710 pgstat_recv_funcstat(PgStat_MsgFuncstat *msg, int len)
3712 PgStat_FunctionEntry *funcmsg = &(msg->m_entry[0]);
3713 PgStat_StatDBEntry *dbentry;
3714 PgStat_StatFuncEntry *funcentry;
3718 dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
3721 * Process all function entries in the message.
3723 for (i = 0; i < msg->m_nentries; i++, funcmsg++)
3725 funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
3726 (void *) &(funcmsg->f_id),
3727 HASH_ENTER, &found);
3732 * If it's a new function entry, initialize counters to the values
3735 funcentry->f_numcalls = funcmsg->f_numcalls;
3736 funcentry->f_time = funcmsg->f_time;
3737 funcentry->f_time_self = funcmsg->f_time_self;
3742 * Otherwise add the values to the existing entry.
3744 funcentry->f_numcalls += funcmsg->f_numcalls;
3745 funcentry->f_time += funcmsg->f_time;
3746 funcentry->f_time_self += funcmsg->f_time_self;
3752 * pgstat_recv_funcpurge() -
3754 * Arrange for dead function removal.
3758 pgstat_recv_funcpurge(PgStat_MsgFuncpurge *msg, int len)
3760 PgStat_StatDBEntry *dbentry;
3763 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3766 * No need to purge if we don't even know the database.
3768 if (!dbentry || !dbentry->functions)
3772 * Process all function entries in the message.
3774 for (i = 0; i < msg->m_nentries; i++)
3776 /* Remove from hashtable if present; we don't care if it's not. */
3777 (void) hash_search(dbentry->functions,
3778 (void *) &(msg->m_functionid[i]),