Minor improvements in backup and recovery:
[PostgreSQL.git] / src / backend / postmaster / pgstat.c
blob75f7400502fac611dfd66fc6e7ea539e3a9d67c0
1 /* ----------
2 * pgstat.c
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$
17 * ----------
19 #include "postgres.h"
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <sys/param.h>
24 #include <sys/time.h>
25 #include <sys/socket.h>
26 #include <netdb.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <signal.h>
30 #include <time.h>
31 #ifdef HAVE_POLL_H
32 #include <poll.h>
33 #endif
34 #ifdef HAVE_SYS_POLL_H
35 #include <sys/poll.h>
36 #endif
38 #include "pgstat.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 "libpq/ip.h"
46 #include "libpq/libpq.h"
47 #include "libpq/pqsignal.h"
48 #include "mb/pg_wchar.h"
49 #include "miscadmin.h"
50 #include "postmaster/autovacuum.h"
51 #include "postmaster/fork_process.h"
52 #include "postmaster/postmaster.h"
53 #include "storage/backendid.h"
54 #include "storage/fd.h"
55 #include "storage/ipc.h"
56 #include "storage/pg_shmem.h"
57 #include "storage/pmsignal.h"
58 #include "utils/guc.h"
59 #include "utils/memutils.h"
60 #include "utils/ps_status.h"
63 /* ----------
64 * Paths for the statistics files (relative to installation's $PGDATA).
65 * ----------
67 #define PGSTAT_STAT_FILENAME "global/pgstat.stat"
68 #define PGSTAT_STAT_TMPFILE "global/pgstat.tmp"
70 /* ----------
71 * Timer definitions.
72 * ----------
74 #define PGSTAT_STAT_INTERVAL 500 /* How often to write the status file;
75 * in milliseconds. */
77 #define PGSTAT_RESTART_INTERVAL 60 /* How often to attempt to restart a
78 * failed statistics collector; in
79 * seconds. */
81 #define PGSTAT_SELECT_TIMEOUT 2 /* How often to check for postmaster
82 * death; in seconds. */
85 /* ----------
86 * The initial size hints for the hash tables used in the collector.
87 * ----------
89 #define PGSTAT_DB_HASH_SIZE 16
90 #define PGSTAT_TAB_HASH_SIZE 512
93 /* ----------
94 * GUC parameters
95 * ----------
97 bool pgstat_track_activities = false;
98 bool pgstat_track_counts = false;
101 * BgWriter global statistics counters (unused in other processes).
102 * Stored directly in a stats message structure so it can be sent
103 * without needing to copy things around. We assume this inits to zeroes.
105 PgStat_MsgBgWriter BgWriterStats;
107 /* ----------
108 * Local data
109 * ----------
111 NON_EXEC_STATIC int pgStatSock = -1;
113 static struct sockaddr_storage pgStatAddr;
115 static time_t last_pgstat_start_time;
117 static bool pgStatRunningInCollector = false;
120 * Structures in which backends store per-table info that's waiting to be
121 * sent to the collector.
123 * NOTE: once allocated, TabStatusArray structures are never moved or deleted
124 * for the life of the backend. Also, we zero out the t_id fields of the
125 * contained PgStat_TableStatus structs whenever they are not actively in use.
126 * This allows relcache pgstat_info pointers to be treated as long-lived data,
127 * avoiding repeated searches in pgstat_initstats() when a relation is
128 * repeatedly opened during a transaction.
130 #define TABSTAT_QUANTUM 100 /* we alloc this many at a time */
132 typedef struct TabStatusArray
134 struct TabStatusArray *tsa_next; /* link to next array, if any */
135 int tsa_used; /* # entries currently used */
136 PgStat_TableStatus tsa_entries[TABSTAT_QUANTUM]; /* per-table data */
137 } TabStatusArray;
139 static TabStatusArray *pgStatTabList = NULL;
142 * Tuple insertion/deletion counts for an open transaction can't be propagated
143 * into PgStat_TableStatus counters until we know if it is going to commit
144 * or abort. Hence, we keep these counts in per-subxact structs that live
145 * in TopTransactionContext. This data structure is designed on the assumption
146 * that subxacts won't usually modify very many tables.
148 typedef struct PgStat_SubXactStatus
150 int nest_level; /* subtransaction nest level */
151 struct PgStat_SubXactStatus *prev; /* higher-level subxact if any */
152 PgStat_TableXactStatus *first; /* head of list for this subxact */
153 } PgStat_SubXactStatus;
155 static PgStat_SubXactStatus *pgStatXactStack = NULL;
157 static int pgStatXactCommit = 0;
158 static int pgStatXactRollback = 0;
160 /* Record that's written to 2PC state file when pgstat state is persisted */
161 typedef struct TwoPhasePgStatRecord
163 PgStat_Counter tuples_inserted; /* tuples inserted in xact */
164 PgStat_Counter tuples_deleted; /* tuples deleted in xact */
165 Oid t_id; /* table's OID */
166 bool t_shared; /* is it a shared catalog? */
167 } TwoPhasePgStatRecord;
170 * Info about current "snapshot" of stats file
172 static MemoryContext pgStatLocalContext = NULL;
173 static HTAB *pgStatDBHash = NULL;
174 static PgBackendStatus *localBackendStatusTable = NULL;
175 static int localNumBackends = 0;
178 * Cluster wide statistics, kept in the stats collector.
179 * Contains statistics that are not collected per database
180 * or per table.
182 static PgStat_GlobalStats globalStats;
184 static volatile bool need_exit = false;
185 static volatile bool need_statwrite = false;
188 /* ----------
189 * Local function forward declarations
190 * ----------
192 #ifdef EXEC_BACKEND
193 static pid_t pgstat_forkexec(void);
194 #endif
196 NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]);
197 static void pgstat_exit(SIGNAL_ARGS);
198 static void force_statwrite(SIGNAL_ARGS);
199 static void pgstat_beshutdown_hook(int code, Datum arg);
201 static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
202 static void pgstat_write_statsfile(void);
203 static HTAB *pgstat_read_statsfile(Oid onlydb);
204 static void backend_read_statsfile(void);
205 static void pgstat_read_current_status(void);
207 static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg);
208 static HTAB *pgstat_collect_oids(Oid catalogid);
210 static PgStat_TableStatus *get_tabstat_entry(Oid rel_id, bool isshared);
212 static void pgstat_setup_memcxt(void);
214 static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype);
215 static void pgstat_send(void *msg, int len);
217 static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
218 static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
219 static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
220 static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
221 static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
222 static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
223 static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
224 static void pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len);
227 /* ------------------------------------------------------------
228 * Public functions called from postmaster follow
229 * ------------------------------------------------------------
232 /* ----------
233 * pgstat_init() -
235 * Called from postmaster at startup. Create the resources required
236 * by the statistics collector process. If unable to do so, do not
237 * fail --- better to let the postmaster start with stats collection
238 * disabled.
239 * ----------
241 void
242 pgstat_init(void)
244 ACCEPT_TYPE_ARG3 alen;
245 struct addrinfo *addrs = NULL,
246 *addr,
247 hints;
248 int ret;
249 fd_set rset;
250 struct timeval tv;
251 char test_byte;
252 int sel_res;
253 int tries = 0;
255 #define TESTBYTEVAL ((char) 199)
258 * Create the UDP socket for sending and receiving statistic messages
260 hints.ai_flags = AI_PASSIVE;
261 hints.ai_family = PF_UNSPEC;
262 hints.ai_socktype = SOCK_DGRAM;
263 hints.ai_protocol = 0;
264 hints.ai_addrlen = 0;
265 hints.ai_addr = NULL;
266 hints.ai_canonname = NULL;
267 hints.ai_next = NULL;
268 ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
269 if (ret || !addrs)
271 ereport(LOG,
272 (errmsg("could not resolve \"localhost\": %s",
273 gai_strerror(ret))));
274 goto startup_failed;
278 * On some platforms, pg_getaddrinfo_all() may return multiple addresses
279 * only one of which will actually work (eg, both IPv6 and IPv4 addresses
280 * when kernel will reject IPv6). Worse, the failure may occur at the
281 * bind() or perhaps even connect() stage. So we must loop through the
282 * results till we find a working combination. We will generate LOG
283 * messages, but no error, for bogus combinations.
285 for (addr = addrs; addr; addr = addr->ai_next)
287 #ifdef HAVE_UNIX_SOCKETS
288 /* Ignore AF_UNIX sockets, if any are returned. */
289 if (addr->ai_family == AF_UNIX)
290 continue;
291 #endif
293 if (++tries > 1)
294 ereport(LOG,
295 (errmsg("trying another address for the statistics collector")));
298 * Create the socket.
300 if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) < 0)
302 ereport(LOG,
303 (errcode_for_socket_access(),
304 errmsg("could not create socket for statistics collector: %m")));
305 continue;
309 * Bind it to a kernel assigned port on localhost and get the assigned
310 * port via getsockname().
312 if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
314 ereport(LOG,
315 (errcode_for_socket_access(),
316 errmsg("could not bind socket for statistics collector: %m")));
317 closesocket(pgStatSock);
318 pgStatSock = -1;
319 continue;
322 alen = sizeof(pgStatAddr);
323 if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
325 ereport(LOG,
326 (errcode_for_socket_access(),
327 errmsg("could not get address of socket for statistics collector: %m")));
328 closesocket(pgStatSock);
329 pgStatSock = -1;
330 continue;
334 * Connect the socket to its own address. This saves a few cycles by
335 * not having to respecify the target address on every send. This also
336 * provides a kernel-level check that only packets from this same
337 * address will be received.
339 if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
341 ereport(LOG,
342 (errcode_for_socket_access(),
343 errmsg("could not connect socket for statistics collector: %m")));
344 closesocket(pgStatSock);
345 pgStatSock = -1;
346 continue;
350 * Try to send and receive a one-byte test message on the socket. This
351 * is to catch situations where the socket can be created but will not
352 * actually pass data (for instance, because kernel packet filtering
353 * rules prevent it).
355 test_byte = TESTBYTEVAL;
357 retry1:
358 if (send(pgStatSock, &test_byte, 1, 0) != 1)
360 if (errno == EINTR)
361 goto retry1; /* if interrupted, just retry */
362 ereport(LOG,
363 (errcode_for_socket_access(),
364 errmsg("could not send test message on socket for statistics collector: %m")));
365 closesocket(pgStatSock);
366 pgStatSock = -1;
367 continue;
371 * There could possibly be a little delay before the message can be
372 * received. We arbitrarily allow up to half a second before deciding
373 * it's broken.
375 for (;;) /* need a loop to handle EINTR */
377 FD_ZERO(&rset);
378 FD_SET(pgStatSock, &rset);
379 tv.tv_sec = 0;
380 tv.tv_usec = 500000;
381 sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
382 if (sel_res >= 0 || errno != EINTR)
383 break;
385 if (sel_res < 0)
387 ereport(LOG,
388 (errcode_for_socket_access(),
389 errmsg("select() failed in statistics collector: %m")));
390 closesocket(pgStatSock);
391 pgStatSock = -1;
392 continue;
394 if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
397 * This is the case we actually think is likely, so take pains to
398 * give a specific message for it.
400 * errno will not be set meaningfully here, so don't use it.
402 ereport(LOG,
403 (errcode(ERRCODE_CONNECTION_FAILURE),
404 errmsg("test message did not get through on socket for statistics collector")));
405 closesocket(pgStatSock);
406 pgStatSock = -1;
407 continue;
410 test_byte++; /* just make sure variable is changed */
412 retry2:
413 if (recv(pgStatSock, &test_byte, 1, 0) != 1)
415 if (errno == EINTR)
416 goto retry2; /* if interrupted, just retry */
417 ereport(LOG,
418 (errcode_for_socket_access(),
419 errmsg("could not receive test message on socket for statistics collector: %m")));
420 closesocket(pgStatSock);
421 pgStatSock = -1;
422 continue;
425 if (test_byte != TESTBYTEVAL) /* strictly paranoia ... */
427 ereport(LOG,
428 (errcode(ERRCODE_INTERNAL_ERROR),
429 errmsg("incorrect test message transmission on socket for statistics collector")));
430 closesocket(pgStatSock);
431 pgStatSock = -1;
432 continue;
435 /* If we get here, we have a working socket */
436 break;
439 /* Did we find a working address? */
440 if (!addr || pgStatSock < 0)
441 goto startup_failed;
444 * Set the socket to non-blocking IO. This ensures that if the collector
445 * falls behind, statistics messages will be discarded; backends won't
446 * block waiting to send messages to the collector.
448 if (!pg_set_noblock(pgStatSock))
450 ereport(LOG,
451 (errcode_for_socket_access(),
452 errmsg("could not set statistics collector socket to nonblocking mode: %m")));
453 goto startup_failed;
456 pg_freeaddrinfo_all(hints.ai_family, addrs);
458 return;
460 startup_failed:
461 ereport(LOG,
462 (errmsg("disabling statistics collector for lack of working socket")));
464 if (addrs)
465 pg_freeaddrinfo_all(hints.ai_family, addrs);
467 if (pgStatSock >= 0)
468 closesocket(pgStatSock);
469 pgStatSock = -1;
472 * Adjust GUC variables to suppress useless activity, and for debugging
473 * purposes (seeing track_counts off is a clue that we failed here).
474 * We use PGC_S_OVERRIDE because there is no point in trying to turn it
475 * back on from postgresql.conf without a restart.
477 SetConfigOption("track_counts", "off", PGC_INTERNAL, PGC_S_OVERRIDE);
481 * pgstat_reset_all() -
483 * Remove the stats file. This is currently used only if WAL
484 * recovery is needed after a crash.
486 void
487 pgstat_reset_all(void)
489 unlink(PGSTAT_STAT_FILENAME);
492 #ifdef EXEC_BACKEND
495 * pgstat_forkexec() -
497 * Format up the arglist for, then fork and exec, statistics collector process
499 static pid_t
500 pgstat_forkexec(void)
502 char *av[10];
503 int ac = 0;
505 av[ac++] = "postgres";
506 av[ac++] = "--forkcol";
507 av[ac++] = NULL; /* filled in by postmaster_forkexec */
509 av[ac] = NULL;
510 Assert(ac < lengthof(av));
512 return postmaster_forkexec(ac, av);
514 #endif /* EXEC_BACKEND */
518 * pgstat_start() -
520 * Called from postmaster at startup or after an existing collector
521 * died. Attempt to fire up a fresh statistics collector.
523 * Returns PID of child process, or 0 if fail.
525 * Note: if fail, we will be called again from the postmaster main loop.
528 pgstat_start(void)
530 time_t curtime;
531 pid_t pgStatPid;
534 * Check that the socket is there, else pgstat_init failed and we can
535 * do nothing useful.
537 if (pgStatSock < 0)
538 return 0;
541 * Do nothing if too soon since last collector start. This is a safety
542 * valve to protect against continuous respawn attempts if the collector
543 * is dying immediately at launch. Note that since we will be re-called
544 * from the postmaster main loop, we will get another chance later.
546 curtime = time(NULL);
547 if ((unsigned int) (curtime - last_pgstat_start_time) <
548 (unsigned int) PGSTAT_RESTART_INTERVAL)
549 return 0;
550 last_pgstat_start_time = curtime;
553 * Okay, fork off the collector.
555 #ifdef EXEC_BACKEND
556 switch ((pgStatPid = pgstat_forkexec()))
557 #else
558 switch ((pgStatPid = fork_process()))
559 #endif
561 case -1:
562 ereport(LOG,
563 (errmsg("could not fork statistics collector: %m")));
564 return 0;
566 #ifndef EXEC_BACKEND
567 case 0:
568 /* in postmaster child ... */
569 /* Close the postmaster's sockets */
570 ClosePostmasterPorts(false);
572 /* Lose the postmaster's on-exit routines */
573 on_exit_reset();
575 /* Drop our connection to postmaster's shared memory, as well */
576 PGSharedMemoryDetach();
578 PgstatCollectorMain(0, NULL);
579 break;
580 #endif
582 default:
583 return (int) pgStatPid;
586 /* shouldn't get here */
587 return 0;
590 void allow_immediate_pgstat_restart(void)
592 last_pgstat_start_time = 0;
595 /* ------------------------------------------------------------
596 * Public functions used by backends follow
597 *------------------------------------------------------------
601 /* ----------
602 * pgstat_report_tabstat() -
604 * Called from tcop/postgres.c to send the so far collected per-table
605 * access statistics to the collector. Note that this is called only
606 * when not within a transaction, so it is fair to use transaction stop
607 * time as an approximation of current time.
608 * ----------
610 void
611 pgstat_report_tabstat(bool force)
613 /* we assume this inits to all zeroes: */
614 static const PgStat_TableCounts all_zeroes;
615 static TimestampTz last_report = 0;
617 TimestampTz now;
618 PgStat_MsgTabstat regular_msg;
619 PgStat_MsgTabstat shared_msg;
620 TabStatusArray *tsa;
621 int i;
623 /* Don't expend a clock check if nothing to do */
624 if (pgStatTabList == NULL ||
625 pgStatTabList->tsa_used == 0)
626 return;
629 * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
630 * msec since we last sent one, or the caller wants to force stats out.
632 now = GetCurrentTransactionStopTimestamp();
633 if (!force &&
634 !TimestampDifferenceExceeds(last_report, now, PGSTAT_STAT_INTERVAL))
635 return;
636 last_report = now;
639 * Scan through the TabStatusArray struct(s) to find tables that actually
640 * have counts, and build messages to send. We have to separate shared
641 * relations from regular ones because the databaseid field in the
642 * message header has to depend on that.
644 regular_msg.m_databaseid = MyDatabaseId;
645 shared_msg.m_databaseid = InvalidOid;
646 regular_msg.m_nentries = 0;
647 shared_msg.m_nentries = 0;
649 for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
651 for (i = 0; i < tsa->tsa_used; i++)
653 PgStat_TableStatus *entry = &tsa->tsa_entries[i];
654 PgStat_MsgTabstat *this_msg;
655 PgStat_TableEntry *this_ent;
657 /* Shouldn't have any pending transaction-dependent counts */
658 Assert(entry->trans == NULL);
661 * Ignore entries that didn't accumulate any actual counts,
662 * such as indexes that were opened by the planner but not used.
664 if (memcmp(&entry->t_counts, &all_zeroes,
665 sizeof(PgStat_TableCounts)) == 0)
666 continue;
668 * OK, insert data into the appropriate message, and send if full.
670 this_msg = entry->t_shared ? &shared_msg : &regular_msg;
671 this_ent = &this_msg->m_entry[this_msg->m_nentries];
672 this_ent->t_id = entry->t_id;
673 memcpy(&this_ent->t_counts, &entry->t_counts,
674 sizeof(PgStat_TableCounts));
675 if (++this_msg->m_nentries >= PGSTAT_NUM_TABENTRIES)
677 pgstat_send_tabstat(this_msg);
678 this_msg->m_nentries = 0;
681 /* zero out TableStatus structs after use */
682 MemSet(tsa->tsa_entries, 0,
683 tsa->tsa_used * sizeof(PgStat_TableStatus));
684 tsa->tsa_used = 0;
688 * Send partial messages. If force is true, make sure that any pending
689 * xact commit/abort gets counted, even if no table stats to send.
691 if (regular_msg.m_nentries > 0 ||
692 (force && (pgStatXactCommit > 0 || pgStatXactRollback > 0)))
693 pgstat_send_tabstat(&regular_msg);
694 if (shared_msg.m_nentries > 0)
695 pgstat_send_tabstat(&shared_msg);
699 * Subroutine for pgstat_report_tabstat: finish and send a tabstat message
701 static void
702 pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg)
704 int n;
705 int len;
707 /* It's unlikely we'd get here with no socket, but maybe not impossible */
708 if (pgStatSock < 0)
709 return;
712 * Report accumulated xact commit/rollback whenever we send a normal
713 * tabstat message
715 if (OidIsValid(tsmsg->m_databaseid))
717 tsmsg->m_xact_commit = pgStatXactCommit;
718 tsmsg->m_xact_rollback = pgStatXactRollback;
719 pgStatXactCommit = 0;
720 pgStatXactRollback = 0;
722 else
724 tsmsg->m_xact_commit = 0;
725 tsmsg->m_xact_rollback = 0;
728 n = tsmsg->m_nentries;
729 len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
730 n * sizeof(PgStat_TableEntry);
732 pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
733 pgstat_send(tsmsg, len);
737 /* ----------
738 * pgstat_vacuum_tabstat() -
740 * Will tell the collector about objects he can get rid of.
741 * ----------
743 void
744 pgstat_vacuum_tabstat(void)
746 HTAB *htab;
747 PgStat_MsgTabpurge msg;
748 HASH_SEQ_STATUS hstat;
749 PgStat_StatDBEntry *dbentry;
750 PgStat_StatTabEntry *tabentry;
751 int len;
753 if (pgStatSock < 0)
754 return;
757 * If not done for this transaction, read the statistics collector stats
758 * file into some hash tables.
760 backend_read_statsfile();
763 * Read pg_database and make a list of OIDs of all existing databases
765 htab = pgstat_collect_oids(DatabaseRelationId);
768 * Search the database hash table for dead databases and tell the
769 * collector to drop them.
771 hash_seq_init(&hstat, pgStatDBHash);
772 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
774 Oid dbid = dbentry->databaseid;
776 CHECK_FOR_INTERRUPTS();
778 /* the DB entry for shared tables (with InvalidOid) is never dropped */
779 if (OidIsValid(dbid) &&
780 hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
781 pgstat_drop_database(dbid);
784 /* Clean up */
785 hash_destroy(htab);
788 * Lookup our own database entry; if not found, nothing more to do.
790 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
791 (void *) &MyDatabaseId,
792 HASH_FIND, NULL);
793 if (dbentry == NULL || dbentry->tables == NULL)
794 return;
797 * Similarly to above, make a list of all known relations in this DB.
799 htab = pgstat_collect_oids(RelationRelationId);
802 * Initialize our messages table counter to zero
804 msg.m_nentries = 0;
807 * Check for all tables listed in stats hashtable if they still exist.
809 hash_seq_init(&hstat, dbentry->tables);
810 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
812 Oid tabid = tabentry->tableid;
814 CHECK_FOR_INTERRUPTS();
816 if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
817 continue;
820 * Not there, so add this table's Oid to the message
822 msg.m_tableid[msg.m_nentries++] = tabid;
825 * If the message is full, send it out and reinitialize to empty
827 if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
829 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
830 +msg.m_nentries * sizeof(Oid);
832 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
833 msg.m_databaseid = MyDatabaseId;
834 pgstat_send(&msg, len);
836 msg.m_nentries = 0;
841 * Send the rest
843 if (msg.m_nentries > 0)
845 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
846 +msg.m_nentries * sizeof(Oid);
848 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
849 msg.m_databaseid = MyDatabaseId;
850 pgstat_send(&msg, len);
853 /* Clean up */
854 hash_destroy(htab);
858 /* ----------
859 * pgstat_collect_oids() -
861 * Collect the OIDs of either all databases or all tables, according to
862 * the parameter, into a temporary hash table. Caller should hash_destroy
863 * the result when done with it.
864 * ----------
866 static HTAB *
867 pgstat_collect_oids(Oid catalogid)
869 HTAB *htab;
870 HASHCTL hash_ctl;
871 Relation rel;
872 HeapScanDesc scan;
873 HeapTuple tup;
875 memset(&hash_ctl, 0, sizeof(hash_ctl));
876 hash_ctl.keysize = sizeof(Oid);
877 hash_ctl.entrysize = sizeof(Oid);
878 hash_ctl.hash = oid_hash;
879 htab = hash_create("Temporary table of OIDs",
880 PGSTAT_TAB_HASH_SIZE,
881 &hash_ctl,
882 HASH_ELEM | HASH_FUNCTION);
884 rel = heap_open(catalogid, AccessShareLock);
885 scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
886 while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
888 Oid thisoid = HeapTupleGetOid(tup);
890 CHECK_FOR_INTERRUPTS();
892 (void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
894 heap_endscan(scan);
895 heap_close(rel, AccessShareLock);
897 return htab;
901 /* ----------
902 * pgstat_drop_database() -
904 * Tell the collector that we just dropped a database.
905 * (If the message gets lost, we will still clean the dead DB eventually
906 * via future invocations of pgstat_vacuum_tabstat().)
907 * ----------
909 void
910 pgstat_drop_database(Oid databaseid)
912 PgStat_MsgDropdb msg;
914 if (pgStatSock < 0)
915 return;
917 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
918 msg.m_databaseid = databaseid;
919 pgstat_send(&msg, sizeof(msg));
923 /* ----------
924 * pgstat_drop_relation() -
926 * Tell the collector that we just dropped a relation.
927 * (If the message gets lost, we will still clean the dead entry eventually
928 * via future invocations of pgstat_vacuum_tabstat().)
930 * Currently not used for lack of any good place to call it; we rely
931 * entirely on pgstat_vacuum_tabstat() to clean out stats for dead rels.
932 * ----------
934 #ifdef NOT_USED
935 void
936 pgstat_drop_relation(Oid relid)
938 PgStat_MsgTabpurge msg;
939 int len;
941 if (pgStatSock < 0)
942 return;
944 msg.m_tableid[0] = relid;
945 msg.m_nentries = 1;
947 len = offsetof(PgStat_MsgTabpurge, m_tableid[0]) +sizeof(Oid);
949 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
950 msg.m_databaseid = MyDatabaseId;
951 pgstat_send(&msg, len);
953 #endif /* NOT_USED */
956 /* ----------
957 * pgstat_reset_counters() -
959 * Tell the statistics collector to reset counters for our database.
960 * ----------
962 void
963 pgstat_reset_counters(void)
965 PgStat_MsgResetcounter msg;
967 if (pgStatSock < 0)
968 return;
970 if (!superuser())
971 ereport(ERROR,
972 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
973 errmsg("must be superuser to reset statistics counters")));
975 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
976 msg.m_databaseid = MyDatabaseId;
977 pgstat_send(&msg, sizeof(msg));
981 /* ----------
982 * pgstat_report_autovac() -
984 * Called from autovacuum.c to report startup of an autovacuum process.
985 * We are called before InitPostgres is done, so can't rely on MyDatabaseId;
986 * the db OID must be passed in, instead.
987 * ----------
989 void
990 pgstat_report_autovac(Oid dboid)
992 PgStat_MsgAutovacStart msg;
994 if (pgStatSock < 0)
995 return;
997 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_AUTOVAC_START);
998 msg.m_databaseid = dboid;
999 msg.m_start_time = GetCurrentTimestamp();
1001 pgstat_send(&msg, sizeof(msg));
1005 /* ---------
1006 * pgstat_report_vacuum() -
1008 * Tell the collector about the table we just vacuumed.
1009 * ---------
1011 void
1012 pgstat_report_vacuum(Oid tableoid, bool shared,
1013 bool analyze, PgStat_Counter tuples)
1015 PgStat_MsgVacuum msg;
1017 if (pgStatSock < 0 || !pgstat_track_counts)
1018 return;
1020 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_VACUUM);
1021 msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1022 msg.m_tableoid = tableoid;
1023 msg.m_analyze = analyze;
1024 msg.m_autovacuum = IsAutoVacuumWorkerProcess(); /* is this autovacuum? */
1025 msg.m_vacuumtime = GetCurrentTimestamp();
1026 msg.m_tuples = tuples;
1027 pgstat_send(&msg, sizeof(msg));
1030 /* --------
1031 * pgstat_report_analyze() -
1033 * Tell the collector about the table we just analyzed.
1034 * --------
1036 void
1037 pgstat_report_analyze(Oid tableoid, bool shared, PgStat_Counter livetuples,
1038 PgStat_Counter deadtuples)
1040 PgStat_MsgAnalyze msg;
1042 if (pgStatSock < 0 || !pgstat_track_counts)
1043 return;
1045 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ANALYZE);
1046 msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1047 msg.m_tableoid = tableoid;
1048 msg.m_autovacuum = IsAutoVacuumWorkerProcess(); /* is this autovacuum? */
1049 msg.m_analyzetime = GetCurrentTimestamp();
1050 msg.m_live_tuples = livetuples;
1051 msg.m_dead_tuples = deadtuples;
1052 pgstat_send(&msg, sizeof(msg));
1056 /* ----------
1057 * pgstat_ping() -
1059 * Send some junk data to the collector to increase traffic.
1060 * ----------
1062 void
1063 pgstat_ping(void)
1065 PgStat_MsgDummy msg;
1067 if (pgStatSock < 0)
1068 return;
1070 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DUMMY);
1071 pgstat_send(&msg, sizeof(msg));
1075 /* ----------
1076 * pgstat_initstats() -
1078 * Initialize a relcache entry to count access statistics.
1079 * Called whenever a relation is opened.
1081 * We assume that a relcache entry's pgstat_info field is zeroed by
1082 * relcache.c when the relcache entry is made; thereafter it is long-lived
1083 * data. We can avoid repeated searches of the TabStatus arrays when the
1084 * same relation is touched repeatedly within a transaction.
1085 * ----------
1087 void
1088 pgstat_initstats(Relation rel)
1090 Oid rel_id = rel->rd_id;
1091 char relkind = rel->rd_rel->relkind;
1093 /* We only count stats for things that have storage */
1094 if (!(relkind == RELKIND_RELATION ||
1095 relkind == RELKIND_INDEX ||
1096 relkind == RELKIND_TOASTVALUE))
1098 rel->pgstat_info = NULL;
1099 return;
1102 if (pgStatSock < 0 || !pgstat_track_counts)
1104 /* We're not counting at all */
1105 rel->pgstat_info = NULL;
1106 return;
1110 * If we already set up this relation in the current transaction,
1111 * nothing to do.
1113 if (rel->pgstat_info != NULL &&
1114 rel->pgstat_info->t_id == rel_id)
1115 return;
1117 /* Else find or make the PgStat_TableStatus entry, and update link */
1118 rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
1122 * get_tabstat_entry - find or create a PgStat_TableStatus entry for rel
1124 static PgStat_TableStatus *
1125 get_tabstat_entry(Oid rel_id, bool isshared)
1127 PgStat_TableStatus *entry;
1128 TabStatusArray *tsa;
1129 TabStatusArray *prev_tsa;
1130 int i;
1133 * Search the already-used tabstat slots for this relation.
1135 prev_tsa = NULL;
1136 for (tsa = pgStatTabList; tsa != NULL; prev_tsa = tsa, tsa = tsa->tsa_next)
1138 for (i = 0; i < tsa->tsa_used; i++)
1140 entry = &tsa->tsa_entries[i];
1141 if (entry->t_id == rel_id)
1142 return entry;
1145 if (tsa->tsa_used < TABSTAT_QUANTUM)
1148 * It must not be present, but we found a free slot instead.
1149 * Fine, let's use this one. We assume the entry was already
1150 * zeroed, either at creation or after last use.
1152 entry = &tsa->tsa_entries[tsa->tsa_used++];
1153 entry->t_id = rel_id;
1154 entry->t_shared = isshared;
1155 return entry;
1160 * We ran out of tabstat slots, so allocate more. Be sure they're zeroed.
1162 tsa = (TabStatusArray *) MemoryContextAllocZero(TopMemoryContext,
1163 sizeof(TabStatusArray));
1164 if (prev_tsa)
1165 prev_tsa->tsa_next = tsa;
1166 else
1167 pgStatTabList = tsa;
1170 * Use the first entry of the new TabStatusArray.
1172 entry = &tsa->tsa_entries[tsa->tsa_used++];
1173 entry->t_id = rel_id;
1174 entry->t_shared = isshared;
1175 return entry;
1179 * get_tabstat_stack_level - add a new (sub)transaction stack entry if needed
1181 static PgStat_SubXactStatus *
1182 get_tabstat_stack_level(int nest_level)
1184 PgStat_SubXactStatus *xact_state;
1186 xact_state = pgStatXactStack;
1187 if (xact_state == NULL || xact_state->nest_level != nest_level)
1189 xact_state = (PgStat_SubXactStatus *)
1190 MemoryContextAlloc(TopTransactionContext,
1191 sizeof(PgStat_SubXactStatus));
1192 xact_state->nest_level = nest_level;
1193 xact_state->prev = pgStatXactStack;
1194 xact_state->first = NULL;
1195 pgStatXactStack = xact_state;
1197 return xact_state;
1201 * add_tabstat_xact_level - add a new (sub)transaction state record
1203 static void
1204 add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
1206 PgStat_SubXactStatus *xact_state;
1207 PgStat_TableXactStatus *trans;
1210 * If this is the first rel to be modified at the current nest level,
1211 * we first have to push a transaction stack entry.
1213 xact_state = get_tabstat_stack_level(nest_level);
1215 /* Now make a per-table stack entry */
1216 trans = (PgStat_TableXactStatus *)
1217 MemoryContextAllocZero(TopTransactionContext,
1218 sizeof(PgStat_TableXactStatus));
1219 trans->nest_level = nest_level;
1220 trans->upper = pgstat_info->trans;
1221 trans->parent = pgstat_info;
1222 trans->next = xact_state->first;
1223 xact_state->first = trans;
1224 pgstat_info->trans = trans;
1228 * pgstat_count_heap_insert - count a tuple insertion
1230 void
1231 pgstat_count_heap_insert(Relation rel)
1233 PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1235 if (pgstat_track_counts && pgstat_info != NULL)
1237 int nest_level = GetCurrentTransactionNestLevel();
1239 /* t_tuples_inserted is nontransactional, so just advance it */
1240 pgstat_info->t_counts.t_tuples_inserted++;
1242 /* We have to log the transactional effect at the proper level */
1243 if (pgstat_info->trans == NULL ||
1244 pgstat_info->trans->nest_level != nest_level)
1245 add_tabstat_xact_level(pgstat_info, nest_level);
1247 pgstat_info->trans->tuples_inserted++;
1252 * pgstat_count_heap_update - count a tuple update
1254 void
1255 pgstat_count_heap_update(Relation rel, bool hot)
1257 PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1259 if (pgstat_track_counts && pgstat_info != NULL)
1261 int nest_level = GetCurrentTransactionNestLevel();
1263 /* t_tuples_updated is nontransactional, so just advance it */
1264 pgstat_info->t_counts.t_tuples_updated++;
1265 /* ditto for the hot_update counter */
1266 if (hot)
1267 pgstat_info->t_counts.t_tuples_hot_updated++;
1269 /* We have to log the transactional effect at the proper level */
1270 if (pgstat_info->trans == NULL ||
1271 pgstat_info->trans->nest_level != nest_level)
1272 add_tabstat_xact_level(pgstat_info, nest_level);
1274 /* An UPDATE both inserts a new tuple and deletes the old */
1275 pgstat_info->trans->tuples_inserted++;
1276 pgstat_info->trans->tuples_deleted++;
1281 * pgstat_count_heap_delete - count a tuple deletion
1283 void
1284 pgstat_count_heap_delete(Relation rel)
1286 PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1288 if (pgstat_track_counts && pgstat_info != NULL)
1290 int nest_level = GetCurrentTransactionNestLevel();
1292 /* t_tuples_deleted is nontransactional, so just advance it */
1293 pgstat_info->t_counts.t_tuples_deleted++;
1295 /* We have to log the transactional effect at the proper level */
1296 if (pgstat_info->trans == NULL ||
1297 pgstat_info->trans->nest_level != nest_level)
1298 add_tabstat_xact_level(pgstat_info, nest_level);
1300 pgstat_info->trans->tuples_deleted++;
1305 * pgstat_update_heap_dead_tuples - update dead-tuples count
1307 * The semantics of this are that we are reporting the nontransactional
1308 * recovery of "delta" dead tuples; so t_new_dead_tuples decreases
1309 * rather than increasing, and the change goes straight into the per-table
1310 * counter, not into transactional state.
1312 void
1313 pgstat_update_heap_dead_tuples(Relation rel, int delta)
1315 PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1317 if (pgstat_track_counts && pgstat_info != NULL)
1318 pgstat_info->t_counts.t_new_dead_tuples -= delta;
1322 /* ----------
1323 * AtEOXact_PgStat
1325 * Called from access/transam/xact.c at top-level transaction commit/abort.
1326 * ----------
1328 void
1329 AtEOXact_PgStat(bool isCommit)
1331 PgStat_SubXactStatus *xact_state;
1334 * Count transaction commit or abort. (We use counters, not just bools,
1335 * in case the reporting message isn't sent right away.)
1337 if (isCommit)
1338 pgStatXactCommit++;
1339 else
1340 pgStatXactRollback++;
1343 * Transfer transactional insert/update counts into the base tabstat
1344 * entries. We don't bother to free any of the transactional state,
1345 * since it's all in TopTransactionContext and will go away anyway.
1347 xact_state = pgStatXactStack;
1348 if (xact_state != NULL)
1350 PgStat_TableXactStatus *trans;
1352 Assert(xact_state->nest_level == 1);
1353 Assert(xact_state->prev == NULL);
1354 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1356 PgStat_TableStatus *tabstat;
1358 Assert(trans->nest_level == 1);
1359 Assert(trans->upper == NULL);
1360 tabstat = trans->parent;
1361 Assert(tabstat->trans == trans);
1362 if (isCommit)
1364 tabstat->t_counts.t_new_live_tuples +=
1365 trans->tuples_inserted - trans->tuples_deleted;
1366 tabstat->t_counts.t_new_dead_tuples += trans->tuples_deleted;
1368 else
1370 /* inserted tuples are dead, deleted tuples are unaffected */
1371 tabstat->t_counts.t_new_dead_tuples += trans->tuples_inserted;
1373 tabstat->trans = NULL;
1376 pgStatXactStack = NULL;
1378 /* Make sure any stats snapshot is thrown away */
1379 pgstat_clear_snapshot();
1382 /* ----------
1383 * AtEOSubXact_PgStat
1385 * Called from access/transam/xact.c at subtransaction commit/abort.
1386 * ----------
1388 void
1389 AtEOSubXact_PgStat(bool isCommit, int nestDepth)
1391 PgStat_SubXactStatus *xact_state;
1394 * Transfer transactional insert/update counts into the next higher
1395 * subtransaction state.
1397 xact_state = pgStatXactStack;
1398 if (xact_state != NULL &&
1399 xact_state->nest_level >= nestDepth)
1401 PgStat_TableXactStatus *trans;
1402 PgStat_TableXactStatus *next_trans;
1404 /* delink xact_state from stack immediately to simplify reuse case */
1405 pgStatXactStack = xact_state->prev;
1407 for (trans = xact_state->first; trans != NULL; trans = next_trans)
1409 PgStat_TableStatus *tabstat;
1411 next_trans = trans->next;
1412 Assert(trans->nest_level == nestDepth);
1413 tabstat = trans->parent;
1414 Assert(tabstat->trans == trans);
1415 if (isCommit)
1417 if (trans->upper && trans->upper->nest_level == nestDepth - 1)
1419 trans->upper->tuples_inserted += trans->tuples_inserted;
1420 trans->upper->tuples_deleted += trans->tuples_deleted;
1421 tabstat->trans = trans->upper;
1422 pfree(trans);
1424 else
1427 * When there isn't an immediate parent state, we can
1428 * just reuse the record instead of going through a
1429 * palloc/pfree pushup (this works since it's all in
1430 * TopTransactionContext anyway). We have to re-link
1431 * it into the parent level, though, and that might mean
1432 * pushing a new entry into the pgStatXactStack.
1434 PgStat_SubXactStatus *upper_xact_state;
1436 upper_xact_state = get_tabstat_stack_level(nestDepth - 1);
1437 trans->next = upper_xact_state->first;
1438 upper_xact_state->first = trans;
1439 trans->nest_level = nestDepth - 1;
1442 else
1445 * On abort, inserted tuples are dead (and can be bounced out
1446 * to the top-level tabstat), deleted tuples are unaffected
1448 tabstat->t_counts.t_new_dead_tuples += trans->tuples_inserted;
1449 tabstat->trans = trans->upper;
1450 pfree(trans);
1453 pfree(xact_state);
1459 * AtPrepare_PgStat
1460 * Save the transactional stats state at 2PC transaction prepare.
1462 * In this phase we just generate 2PC records for all the pending
1463 * transaction-dependent stats work.
1465 void
1466 AtPrepare_PgStat(void)
1468 PgStat_SubXactStatus *xact_state;
1470 xact_state = pgStatXactStack;
1471 if (xact_state != NULL)
1473 PgStat_TableXactStatus *trans;
1475 Assert(xact_state->nest_level == 1);
1476 Assert(xact_state->prev == NULL);
1477 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1479 PgStat_TableStatus *tabstat;
1480 TwoPhasePgStatRecord record;
1482 Assert(trans->nest_level == 1);
1483 Assert(trans->upper == NULL);
1484 tabstat = trans->parent;
1485 Assert(tabstat->trans == trans);
1487 record.tuples_inserted = trans->tuples_inserted;
1488 record.tuples_deleted = trans->tuples_deleted;
1489 record.t_id = tabstat->t_id;
1490 record.t_shared = tabstat->t_shared;
1492 RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
1493 &record, sizeof(TwoPhasePgStatRecord));
1499 * PostPrepare_PgStat
1500 * Clean up after successful PREPARE.
1502 * All we need do here is unlink the transaction stats state from the
1503 * nontransactional state. The nontransactional action counts will be
1504 * reported to the stats collector immediately, while the effects on live
1505 * and dead tuple counts are preserved in the 2PC state file.
1507 * Note: AtEOXact_PgStat is not called during PREPARE.
1509 void
1510 PostPrepare_PgStat(void)
1512 PgStat_SubXactStatus *xact_state;
1515 * We don't bother to free any of the transactional state,
1516 * since it's all in TopTransactionContext and will go away anyway.
1518 xact_state = pgStatXactStack;
1519 if (xact_state != NULL)
1521 PgStat_TableXactStatus *trans;
1523 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1525 PgStat_TableStatus *tabstat;
1527 tabstat = trans->parent;
1528 tabstat->trans = NULL;
1531 pgStatXactStack = NULL;
1533 /* Make sure any stats snapshot is thrown away */
1534 pgstat_clear_snapshot();
1538 * 2PC processing routine for COMMIT PREPARED case.
1540 * Load the saved counts into our local pgstats state.
1542 void
1543 pgstat_twophase_postcommit(TransactionId xid, uint16 info,
1544 void *recdata, uint32 len)
1546 TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
1547 PgStat_TableStatus *pgstat_info;
1549 /* Find or create a tabstat entry for the rel */
1550 pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
1552 pgstat_info->t_counts.t_new_live_tuples +=
1553 rec->tuples_inserted - rec->tuples_deleted;
1554 pgstat_info->t_counts.t_new_dead_tuples += rec->tuples_deleted;
1558 * 2PC processing routine for ROLLBACK PREPARED case.
1560 * Load the saved counts into our local pgstats state, but treat them
1561 * as aborted.
1563 void
1564 pgstat_twophase_postabort(TransactionId xid, uint16 info,
1565 void *recdata, uint32 len)
1567 TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
1568 PgStat_TableStatus *pgstat_info;
1570 /* Find or create a tabstat entry for the rel */
1571 pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
1573 /* inserted tuples are dead, deleted tuples are no-ops */
1574 pgstat_info->t_counts.t_new_dead_tuples += rec->tuples_inserted;
1578 /* ----------
1579 * pgstat_fetch_stat_dbentry() -
1581 * Support function for the SQL-callable pgstat* functions. Returns
1582 * the collected statistics for one database or NULL. NULL doesn't mean
1583 * that the database doesn't exist, it is just not yet known by the
1584 * collector, so the caller is better off to report ZERO instead.
1585 * ----------
1587 PgStat_StatDBEntry *
1588 pgstat_fetch_stat_dbentry(Oid dbid)
1591 * If not done for this transaction, read the statistics collector stats
1592 * file into some hash tables.
1594 backend_read_statsfile();
1597 * Lookup the requested database; return NULL if not found
1599 return (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1600 (void *) &dbid,
1601 HASH_FIND, NULL);
1605 /* ----------
1606 * pgstat_fetch_stat_tabentry() -
1608 * Support function for the SQL-callable pgstat* functions. Returns
1609 * the collected statistics for one table or NULL. NULL doesn't mean
1610 * that the table doesn't exist, it is just not yet known by the
1611 * collector, so the caller is better off to report ZERO instead.
1612 * ----------
1614 PgStat_StatTabEntry *
1615 pgstat_fetch_stat_tabentry(Oid relid)
1617 Oid dbid;
1618 PgStat_StatDBEntry *dbentry;
1619 PgStat_StatTabEntry *tabentry;
1622 * If not done for this transaction, read the statistics collector stats
1623 * file into some hash tables.
1625 backend_read_statsfile();
1628 * Lookup our database, then look in its table hash table.
1630 dbid = MyDatabaseId;
1631 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1632 (void *) &dbid,
1633 HASH_FIND, NULL);
1634 if (dbentry != NULL && dbentry->tables != NULL)
1636 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1637 (void *) &relid,
1638 HASH_FIND, NULL);
1639 if (tabentry)
1640 return tabentry;
1644 * If we didn't find it, maybe it's a shared table.
1646 dbid = InvalidOid;
1647 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1648 (void *) &dbid,
1649 HASH_FIND, NULL);
1650 if (dbentry != NULL && dbentry->tables != NULL)
1652 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1653 (void *) &relid,
1654 HASH_FIND, NULL);
1655 if (tabentry)
1656 return tabentry;
1659 return NULL;
1663 /* ----------
1664 * pgstat_fetch_stat_beentry() -
1666 * Support function for the SQL-callable pgstat* functions. Returns
1667 * our local copy of the current-activity entry for one backend.
1669 * NB: caller is responsible for a check if the user is permitted to see
1670 * this info (especially the querystring).
1671 * ----------
1673 PgBackendStatus *
1674 pgstat_fetch_stat_beentry(int beid)
1676 pgstat_read_current_status();
1678 if (beid < 1 || beid > localNumBackends)
1679 return NULL;
1681 return &localBackendStatusTable[beid - 1];
1685 /* ----------
1686 * pgstat_fetch_stat_numbackends() -
1688 * Support function for the SQL-callable pgstat* functions. Returns
1689 * the maximum current backend id.
1690 * ----------
1693 pgstat_fetch_stat_numbackends(void)
1695 pgstat_read_current_status();
1697 return localNumBackends;
1701 * ---------
1702 * pgstat_fetch_global() -
1704 * Support function for the SQL-callable pgstat* functions. Returns
1705 * a pointer to the global statistics struct.
1706 * ---------
1708 PgStat_GlobalStats *
1709 pgstat_fetch_global(void)
1711 backend_read_statsfile();
1713 return &globalStats;
1717 /* ------------------------------------------------------------
1718 * Functions for management of the shared-memory PgBackendStatus array
1719 * ------------------------------------------------------------
1722 static PgBackendStatus *BackendStatusArray = NULL;
1723 static PgBackendStatus *MyBEEntry = NULL;
1727 * Report shared-memory space needed by CreateSharedBackendStatus.
1729 Size
1730 BackendStatusShmemSize(void)
1732 Size size;
1734 size = mul_size(sizeof(PgBackendStatus), MaxBackends);
1735 return size;
1739 * Initialize the shared status array during postmaster startup.
1741 void
1742 CreateSharedBackendStatus(void)
1744 Size size = BackendStatusShmemSize();
1745 bool found;
1747 /* Create or attach to the shared array */
1748 BackendStatusArray = (PgBackendStatus *)
1749 ShmemInitStruct("Backend Status Array", size, &found);
1751 if (!found)
1754 * We're the first - initialize.
1756 MemSet(BackendStatusArray, 0, size);
1761 /* ----------
1762 * pgstat_initialize() -
1764 * Initialize pgstats state, and set up our on-proc-exit hook.
1765 * Called from InitPostgres. MyBackendId must be set,
1766 * but we must not have started any transaction yet (since the
1767 * exit hook must run after the last transaction exit).
1768 * ----------
1770 void
1771 pgstat_initialize(void)
1773 /* Initialize MyBEEntry */
1774 Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
1775 MyBEEntry = &BackendStatusArray[MyBackendId - 1];
1777 /* Set up a process-exit hook to clean up */
1778 on_shmem_exit(pgstat_beshutdown_hook, 0);
1781 /* ----------
1782 * pgstat_bestart() -
1784 * Initialize this backend's entry in the PgBackendStatus array.
1785 * Called from InitPostgres. MyDatabaseId and session userid must be set
1786 * (hence, this cannot be combined with pgstat_initialize).
1787 * ----------
1789 void
1790 pgstat_bestart(void)
1792 TimestampTz proc_start_timestamp;
1793 Oid userid;
1794 SockAddr clientaddr;
1795 volatile PgBackendStatus *beentry;
1798 * To minimize the time spent modifying the PgBackendStatus entry,
1799 * fetch all the needed data first.
1801 * If we have a MyProcPort, use its session start time (for consistency,
1802 * and to save a kernel call).
1804 if (MyProcPort)
1805 proc_start_timestamp = MyProcPort->SessionStartTime;
1806 else
1807 proc_start_timestamp = GetCurrentTimestamp();
1808 userid = GetSessionUserId();
1811 * We may not have a MyProcPort (eg, if this is the autovacuum process).
1812 * If so, use all-zeroes client address, which is dealt with specially in
1813 * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
1815 if (MyProcPort)
1816 memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
1817 else
1818 MemSet(&clientaddr, 0, sizeof(clientaddr));
1821 * Initialize my status entry, following the protocol of bumping
1822 * st_changecount before and after; and make sure it's even afterwards. We
1823 * use a volatile pointer here to ensure the compiler doesn't try to get
1824 * cute.
1826 beentry = MyBEEntry;
1829 beentry->st_changecount++;
1830 } while ((beentry->st_changecount & 1) == 0);
1832 beentry->st_procpid = MyProcPid;
1833 beentry->st_proc_start_timestamp = proc_start_timestamp;
1834 beentry->st_activity_start_timestamp = 0;
1835 beentry->st_xact_start_timestamp = 0;
1836 beentry->st_databaseid = MyDatabaseId;
1837 beentry->st_userid = userid;
1838 beentry->st_clientaddr = clientaddr;
1839 beentry->st_waiting = false;
1840 beentry->st_activity[0] = '\0';
1841 /* Also make sure the last byte in the string area is always 0 */
1842 beentry->st_activity[PGBE_ACTIVITY_SIZE - 1] = '\0';
1844 beentry->st_changecount++;
1845 Assert((beentry->st_changecount & 1) == 0);
1849 * Shut down a single backend's statistics reporting at process exit.
1851 * Flush any remaining statistics counts out to the collector.
1852 * Without this, operations triggered during backend exit (such as
1853 * temp table deletions) won't be counted.
1855 * Lastly, clear out our entry in the PgBackendStatus array.
1857 static void
1858 pgstat_beshutdown_hook(int code, Datum arg)
1860 volatile PgBackendStatus *beentry = MyBEEntry;
1862 pgstat_report_tabstat(true);
1865 * Clear my status entry, following the protocol of bumping st_changecount
1866 * before and after. We use a volatile pointer here to ensure the
1867 * compiler doesn't try to get cute.
1869 beentry->st_changecount++;
1871 beentry->st_procpid = 0; /* mark invalid */
1873 beentry->st_changecount++;
1874 Assert((beentry->st_changecount & 1) == 0);
1878 /* ----------
1879 * pgstat_report_activity() -
1881 * Called from tcop/postgres.c to report what the backend is actually doing
1882 * (usually "<IDLE>" or the start of the query to be executed).
1883 * ----------
1885 void
1886 pgstat_report_activity(const char *cmd_str)
1888 volatile PgBackendStatus *beentry = MyBEEntry;
1889 TimestampTz start_timestamp;
1890 int len;
1892 if (!pgstat_track_activities || !beentry)
1893 return;
1896 * To minimize the time spent modifying the entry, fetch all the needed
1897 * data first.
1899 start_timestamp = GetCurrentStatementStartTimestamp();
1901 len = strlen(cmd_str);
1902 len = pg_mbcliplen(cmd_str, len, PGBE_ACTIVITY_SIZE - 1);
1905 * Update my status entry, following the protocol of bumping
1906 * st_changecount before and after. We use a volatile pointer here to
1907 * ensure the compiler doesn't try to get cute.
1909 beentry->st_changecount++;
1911 beentry->st_activity_start_timestamp = start_timestamp;
1912 memcpy((char *) beentry->st_activity, cmd_str, len);
1913 beentry->st_activity[len] = '\0';
1915 beentry->st_changecount++;
1916 Assert((beentry->st_changecount & 1) == 0);
1920 * Report current transaction start timestamp as the specified value.
1921 * Zero means there is no active transaction.
1923 void
1924 pgstat_report_xact_timestamp(TimestampTz tstamp)
1926 volatile PgBackendStatus *beentry = MyBEEntry;
1928 if (!pgstat_track_activities || !beentry)
1929 return;
1932 * Update my status entry, following the protocol of bumping
1933 * st_changecount before and after. We use a volatile pointer
1934 * here to ensure the compiler doesn't try to get cute.
1936 beentry->st_changecount++;
1937 beentry->st_xact_start_timestamp = tstamp;
1938 beentry->st_changecount++;
1939 Assert((beentry->st_changecount & 1) == 0);
1942 /* ----------
1943 * pgstat_report_waiting() -
1945 * Called from lock manager to report beginning or end of a lock wait.
1947 * NB: this *must* be able to survive being called before MyBEEntry has been
1948 * initialized.
1949 * ----------
1951 void
1952 pgstat_report_waiting(bool waiting)
1954 volatile PgBackendStatus *beentry = MyBEEntry;
1956 if (!pgstat_track_activities || !beentry)
1957 return;
1960 * Since this is a single-byte field in a struct that only this process
1961 * may modify, there seems no need to bother with the st_changecount
1962 * protocol. The update must appear atomic in any case.
1964 beentry->st_waiting = waiting;
1968 /* ----------
1969 * pgstat_read_current_status() -
1971 * Copy the current contents of the PgBackendStatus array to local memory,
1972 * if not already done in this transaction.
1973 * ----------
1975 static void
1976 pgstat_read_current_status(void)
1978 volatile PgBackendStatus *beentry;
1979 PgBackendStatus *localtable;
1980 PgBackendStatus *localentry;
1981 int i;
1983 Assert(!pgStatRunningInCollector);
1984 if (localBackendStatusTable)
1985 return; /* already done */
1987 pgstat_setup_memcxt();
1989 localtable = (PgBackendStatus *)
1990 MemoryContextAlloc(pgStatLocalContext,
1991 sizeof(PgBackendStatus) * MaxBackends);
1992 localNumBackends = 0;
1994 beentry = BackendStatusArray;
1995 localentry = localtable;
1996 for (i = 1; i <= MaxBackends; i++)
1999 * Follow the protocol of retrying if st_changecount changes while we
2000 * copy the entry, or if it's odd. (The check for odd is needed to
2001 * cover the case where we are able to completely copy the entry while
2002 * the source backend is between increment steps.) We use a volatile
2003 * pointer here to ensure the compiler doesn't try to get cute.
2005 for (;;)
2007 int save_changecount = beentry->st_changecount;
2010 * XXX if PGBE_ACTIVITY_SIZE is really large, it might be best to
2011 * use strcpy not memcpy for copying the activity string?
2013 memcpy(localentry, (char *) beentry, sizeof(PgBackendStatus));
2015 if (save_changecount == beentry->st_changecount &&
2016 (save_changecount & 1) == 0)
2017 break;
2019 /* Make sure we can break out of loop if stuck... */
2020 CHECK_FOR_INTERRUPTS();
2023 beentry++;
2024 /* Only valid entries get included into the local array */
2025 if (localentry->st_procpid > 0)
2027 localentry++;
2028 localNumBackends++;
2032 /* Set the pointer only after completion of a valid table */
2033 localBackendStatusTable = localtable;
2037 /* ------------------------------------------------------------
2038 * Local support functions follow
2039 * ------------------------------------------------------------
2043 /* ----------
2044 * pgstat_setheader() -
2046 * Set common header fields in a statistics message
2047 * ----------
2049 static void
2050 pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
2052 hdr->m_type = mtype;
2056 /* ----------
2057 * pgstat_send() -
2059 * Send out one statistics message to the collector
2060 * ----------
2062 static void
2063 pgstat_send(void *msg, int len)
2065 int rc;
2067 if (pgStatSock < 0)
2068 return;
2070 ((PgStat_MsgHdr *) msg)->m_size = len;
2072 /* We'll retry after EINTR, but ignore all other failures */
2075 rc = send(pgStatSock, msg, len, 0);
2076 } while (rc < 0 && errno == EINTR);
2078 #ifdef USE_ASSERT_CHECKING
2079 /* In debug builds, log send failures ... */
2080 if (rc < 0)
2081 elog(LOG, "could not send to statistics collector: %m");
2082 #endif
2085 /* ----------
2086 * pgstat_send_bgwriter() -
2088 * Send bgwriter statistics to the collector
2089 * ----------
2091 void
2092 pgstat_send_bgwriter(void)
2094 /* We assume this initializes to zeroes */
2095 static const PgStat_MsgBgWriter all_zeroes;
2098 * This function can be called even if nothing at all has happened.
2099 * In this case, avoid sending a completely empty message to
2100 * the stats collector.
2102 if (memcmp(&BgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
2103 return;
2106 * Prepare and send the message
2108 pgstat_setheader(&BgWriterStats.m_hdr, PGSTAT_MTYPE_BGWRITER);
2109 pgstat_send(&BgWriterStats, sizeof(BgWriterStats));
2112 * Clear out the statistics buffer, so it can be re-used.
2114 MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
2118 /* ----------
2119 * PgstatCollectorMain() -
2121 * Start up the statistics collector process. This is the body of the
2122 * postmaster child process.
2124 * The argc/argv parameters are valid only in EXEC_BACKEND case.
2125 * ----------
2127 NON_EXEC_STATIC void
2128 PgstatCollectorMain(int argc, char *argv[])
2130 struct itimerval write_timeout;
2131 bool need_timer = false;
2132 int len;
2133 PgStat_Msg msg;
2135 #ifndef WIN32
2136 #ifdef HAVE_POLL
2137 struct pollfd input_fd;
2138 #else
2139 struct timeval sel_timeout;
2140 fd_set rfds;
2141 #endif
2142 #endif
2144 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
2146 MyProcPid = getpid(); /* reset MyProcPid */
2148 MyStartTime = time(NULL); /* record Start Time for logging */
2151 * If possible, make this process a group leader, so that the postmaster
2152 * can signal any child processes too. (pgstat probably never has
2153 * any child processes, but for consistency we make all postmaster
2154 * child processes do this.)
2156 #ifdef HAVE_SETSID
2157 if (setsid() < 0)
2158 elog(FATAL, "setsid() failed: %m");
2159 #endif
2162 * Ignore all signals usually bound to some action in the postmaster,
2163 * except SIGQUIT and SIGALRM.
2165 pqsignal(SIGHUP, SIG_IGN);
2166 pqsignal(SIGINT, SIG_IGN);
2167 pqsignal(SIGTERM, SIG_IGN);
2168 pqsignal(SIGQUIT, pgstat_exit);
2169 pqsignal(SIGALRM, force_statwrite);
2170 pqsignal(SIGPIPE, SIG_IGN);
2171 pqsignal(SIGUSR1, SIG_IGN);
2172 pqsignal(SIGUSR2, SIG_IGN);
2173 pqsignal(SIGCHLD, SIG_DFL);
2174 pqsignal(SIGTTIN, SIG_DFL);
2175 pqsignal(SIGTTOU, SIG_DFL);
2176 pqsignal(SIGCONT, SIG_DFL);
2177 pqsignal(SIGWINCH, SIG_DFL);
2178 PG_SETMASK(&UnBlockSig);
2181 * Identify myself via ps
2183 init_ps_display("stats collector process", "", "", "");
2186 * Arrange to write the initial status file right away
2188 need_statwrite = true;
2190 /* Preset the delay between status file writes */
2191 MemSet(&write_timeout, 0, sizeof(struct itimerval));
2192 write_timeout.it_value.tv_sec = PGSTAT_STAT_INTERVAL / 1000;
2193 write_timeout.it_value.tv_usec = (PGSTAT_STAT_INTERVAL % 1000) * 1000;
2196 * Read in an existing statistics stats file or initialize the stats to
2197 * zero.
2199 pgStatRunningInCollector = true;
2200 pgStatDBHash = pgstat_read_statsfile(InvalidOid);
2203 * Setup the descriptor set for select(2). Since only one bit in the set
2204 * ever changes, we need not repeat FD_ZERO each time.
2206 #if !defined(HAVE_POLL) && !defined(WIN32)
2207 FD_ZERO(&rfds);
2208 #endif
2211 * Loop to process messages until we get SIGQUIT or detect ungraceful
2212 * death of our parent postmaster.
2214 * For performance reasons, we don't want to do a PostmasterIsAlive() test
2215 * after every message; instead, do it at statwrite time and if
2216 * select()/poll() is interrupted by timeout.
2218 for (;;)
2220 int got_data;
2223 * Quit if we get SIGQUIT from the postmaster.
2225 if (need_exit)
2226 break;
2229 * If time to write the stats file, do so. Note that the alarm
2230 * interrupt isn't re-enabled immediately, but only after we next
2231 * receive a stats message; so no cycles are wasted when there is
2232 * nothing going on.
2234 if (need_statwrite)
2236 /* Check for postmaster death; if so we'll write file below */
2237 if (!PostmasterIsAlive(true))
2238 break;
2240 pgstat_write_statsfile();
2241 need_statwrite = false;
2242 need_timer = true;
2246 * Wait for a message to arrive; but not for more than
2247 * PGSTAT_SELECT_TIMEOUT seconds. (This determines how quickly we will
2248 * shut down after an ungraceful postmaster termination; so it needn't
2249 * be very fast. However, on some systems SIGQUIT won't interrupt the
2250 * poll/select call, so this also limits speed of response to SIGQUIT,
2251 * which is more important.)
2253 * We use poll(2) if available, otherwise select(2).
2254 * Win32 has its own implementation.
2256 #ifndef WIN32
2257 #ifdef HAVE_POLL
2258 input_fd.fd = pgStatSock;
2259 input_fd.events = POLLIN | POLLERR;
2260 input_fd.revents = 0;
2262 if (poll(&input_fd, 1, PGSTAT_SELECT_TIMEOUT * 1000) < 0)
2264 if (errno == EINTR)
2265 continue;
2266 ereport(ERROR,
2267 (errcode_for_socket_access(),
2268 errmsg("poll() failed in statistics collector: %m")));
2271 got_data = (input_fd.revents != 0);
2272 #else /* !HAVE_POLL */
2274 FD_SET(pgStatSock, &rfds);
2277 * timeout struct is modified by select() on some operating systems,
2278 * so re-fill it each time.
2280 sel_timeout.tv_sec = PGSTAT_SELECT_TIMEOUT;
2281 sel_timeout.tv_usec = 0;
2283 if (select(pgStatSock + 1, &rfds, NULL, NULL, &sel_timeout) < 0)
2285 if (errno == EINTR)
2286 continue;
2287 ereport(ERROR,
2288 (errcode_for_socket_access(),
2289 errmsg("select() failed in statistics collector: %m")));
2292 got_data = FD_ISSET(pgStatSock, &rfds);
2293 #endif /* HAVE_POLL */
2294 #else /* WIN32 */
2295 got_data = pgwin32_waitforsinglesocket(pgStatSock, FD_READ,
2296 PGSTAT_SELECT_TIMEOUT*1000);
2297 #endif
2300 * If there is a message on the socket, read it and check for
2301 * validity.
2303 if (got_data)
2305 len = recv(pgStatSock, (char *) &msg,
2306 sizeof(PgStat_Msg), 0);
2307 if (len < 0)
2309 if (errno == EINTR)
2310 continue;
2311 ereport(ERROR,
2312 (errcode_for_socket_access(),
2313 errmsg("could not read statistics message: %m")));
2317 * We ignore messages that are smaller than our common header
2319 if (len < sizeof(PgStat_MsgHdr))
2320 continue;
2323 * The received length must match the length in the header
2325 if (msg.msg_hdr.m_size != len)
2326 continue;
2329 * O.K. - we accept this message. Process it.
2331 switch (msg.msg_hdr.m_type)
2333 case PGSTAT_MTYPE_DUMMY:
2334 break;
2336 case PGSTAT_MTYPE_TABSTAT:
2337 pgstat_recv_tabstat((PgStat_MsgTabstat *) &msg, len);
2338 break;
2340 case PGSTAT_MTYPE_TABPURGE:
2341 pgstat_recv_tabpurge((PgStat_MsgTabpurge *) &msg, len);
2342 break;
2344 case PGSTAT_MTYPE_DROPDB:
2345 pgstat_recv_dropdb((PgStat_MsgDropdb *) &msg, len);
2346 break;
2348 case PGSTAT_MTYPE_RESETCOUNTER:
2349 pgstat_recv_resetcounter((PgStat_MsgResetcounter *) &msg,
2350 len);
2351 break;
2353 case PGSTAT_MTYPE_AUTOVAC_START:
2354 pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
2355 break;
2357 case PGSTAT_MTYPE_VACUUM:
2358 pgstat_recv_vacuum((PgStat_MsgVacuum *) &msg, len);
2359 break;
2361 case PGSTAT_MTYPE_ANALYZE:
2362 pgstat_recv_analyze((PgStat_MsgAnalyze *) &msg, len);
2363 break;
2365 case PGSTAT_MTYPE_BGWRITER:
2366 pgstat_recv_bgwriter((PgStat_MsgBgWriter *) &msg, len);
2367 break;
2369 default:
2370 break;
2374 * If this is the first message after we wrote the stats file the
2375 * last time, enable the alarm interrupt to make it be written
2376 * again later.
2378 if (need_timer)
2380 if (setitimer(ITIMER_REAL, &write_timeout, NULL))
2381 ereport(ERROR,
2382 (errmsg("could not set statistics collector timer: %m")));
2383 need_timer = false;
2386 else
2389 * We can only get here if the select/poll timeout elapsed. Check
2390 * for postmaster death.
2392 if (!PostmasterIsAlive(true))
2393 break;
2395 } /* end of message-processing loop */
2398 * Save the final stats to reuse at next startup.
2400 pgstat_write_statsfile();
2402 exit(0);
2406 /* SIGQUIT signal handler for collector process */
2407 static void
2408 pgstat_exit(SIGNAL_ARGS)
2410 need_exit = true;
2413 /* SIGALRM signal handler for collector process */
2414 static void
2415 force_statwrite(SIGNAL_ARGS)
2417 need_statwrite = true;
2422 * Lookup the hash table entry for the specified database. If no hash
2423 * table entry exists, initialize it, if the create parameter is true.
2424 * Else, return NULL.
2426 static PgStat_StatDBEntry *
2427 pgstat_get_db_entry(Oid databaseid, bool create)
2429 PgStat_StatDBEntry *result;
2430 bool found;
2431 HASHACTION action = (create ? HASH_ENTER : HASH_FIND);
2433 /* Lookup or create the hash table entry for this database */
2434 result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2435 &databaseid,
2436 action, &found);
2438 if (!create && !found)
2439 return NULL;
2441 /* If not found, initialize the new one. */
2442 if (!found)
2444 HASHCTL hash_ctl;
2446 result->tables = NULL;
2447 result->n_xact_commit = 0;
2448 result->n_xact_rollback = 0;
2449 result->n_blocks_fetched = 0;
2450 result->n_blocks_hit = 0;
2451 result->n_tuples_returned = 0;
2452 result->n_tuples_fetched = 0;
2453 result->n_tuples_inserted = 0;
2454 result->n_tuples_updated = 0;
2455 result->n_tuples_deleted = 0;
2456 result->last_autovac_time = 0;
2458 memset(&hash_ctl, 0, sizeof(hash_ctl));
2459 hash_ctl.keysize = sizeof(Oid);
2460 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2461 hash_ctl.hash = oid_hash;
2462 result->tables = hash_create("Per-database table",
2463 PGSTAT_TAB_HASH_SIZE,
2464 &hash_ctl,
2465 HASH_ELEM | HASH_FUNCTION);
2468 return result;
2472 /* ----------
2473 * pgstat_write_statsfile() -
2475 * Tell the news.
2476 * ----------
2478 static void
2479 pgstat_write_statsfile(void)
2481 HASH_SEQ_STATUS hstat;
2482 HASH_SEQ_STATUS tstat;
2483 PgStat_StatDBEntry *dbentry;
2484 PgStat_StatTabEntry *tabentry;
2485 FILE *fpout;
2486 int32 format_id;
2489 * Open the statistics temp file to write out the current values.
2491 fpout = fopen(PGSTAT_STAT_TMPFILE, PG_BINARY_W);
2492 if (fpout == NULL)
2494 ereport(LOG,
2495 (errcode_for_file_access(),
2496 errmsg("could not open temporary statistics file \"%s\": %m",
2497 PGSTAT_STAT_TMPFILE)));
2498 return;
2502 * Write the file header --- currently just a format ID.
2504 format_id = PGSTAT_FILE_FORMAT_ID;
2505 fwrite(&format_id, sizeof(format_id), 1, fpout);
2508 * Write global stats struct
2510 fwrite(&globalStats, sizeof(globalStats), 1, fpout);
2513 * Walk through the database table.
2515 hash_seq_init(&hstat, pgStatDBHash);
2516 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
2519 * Write out the DB entry including the number of live backends. We
2520 * don't write the tables pointer since it's of no use to any other
2521 * process.
2523 fputc('D', fpout);
2524 fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
2527 * Walk through the database's access stats per table.
2529 hash_seq_init(&tstat, dbentry->tables);
2530 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
2532 fputc('T', fpout);
2533 fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
2537 * Mark the end of this DB
2539 fputc('d', fpout);
2543 * No more output to be done. Close the temp file and replace the old
2544 * pgstat.stat with it. The ferror() check replaces testing for error
2545 * after each individual fputc or fwrite above.
2547 fputc('E', fpout);
2549 if (ferror(fpout))
2551 ereport(LOG,
2552 (errcode_for_file_access(),
2553 errmsg("could not write temporary statistics file \"%s\": %m",
2554 PGSTAT_STAT_TMPFILE)));
2555 fclose(fpout);
2556 unlink(PGSTAT_STAT_TMPFILE);
2558 else if (fclose(fpout) < 0)
2560 ereport(LOG,
2561 (errcode_for_file_access(),
2562 errmsg("could not close temporary statistics file \"%s\": %m",
2563 PGSTAT_STAT_TMPFILE)));
2564 unlink(PGSTAT_STAT_TMPFILE);
2566 else if (rename(PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME) < 0)
2568 ereport(LOG,
2569 (errcode_for_file_access(),
2570 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
2571 PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME)));
2572 unlink(PGSTAT_STAT_TMPFILE);
2577 /* ----------
2578 * pgstat_read_statsfile() -
2580 * Reads in an existing statistics collector file and initializes the
2581 * databases' hash table (whose entries point to the tables' hash tables).
2582 * ----------
2584 static HTAB *
2585 pgstat_read_statsfile(Oid onlydb)
2587 PgStat_StatDBEntry *dbentry;
2588 PgStat_StatDBEntry dbbuf;
2589 PgStat_StatTabEntry *tabentry;
2590 PgStat_StatTabEntry tabbuf;
2591 HASHCTL hash_ctl;
2592 HTAB *dbhash;
2593 HTAB *tabhash = NULL;
2594 FILE *fpin;
2595 int32 format_id;
2596 bool found;
2599 * The tables will live in pgStatLocalContext.
2601 pgstat_setup_memcxt();
2604 * Create the DB hashtable
2606 memset(&hash_ctl, 0, sizeof(hash_ctl));
2607 hash_ctl.keysize = sizeof(Oid);
2608 hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
2609 hash_ctl.hash = oid_hash;
2610 hash_ctl.hcxt = pgStatLocalContext;
2611 dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
2612 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
2615 * Clear out global statistics so they start from zero in case we can't
2616 * load an existing statsfile.
2618 memset(&globalStats, 0, sizeof(globalStats));
2621 * Try to open the status file. If it doesn't exist, the backends simply
2622 * return zero for anything and the collector simply starts from scratch
2623 * with empty counters.
2625 if ((fpin = AllocateFile(PGSTAT_STAT_FILENAME, PG_BINARY_R)) == NULL)
2626 return dbhash;
2629 * Verify it's of the expected format.
2631 if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id)
2632 || format_id != PGSTAT_FILE_FORMAT_ID)
2634 ereport(pgStatRunningInCollector ? LOG : WARNING,
2635 (errmsg("corrupted pgstat.stat file")));
2636 goto done;
2640 * Read global stats struct
2642 if (fread(&globalStats, 1, sizeof(globalStats), fpin) != sizeof(globalStats))
2644 ereport(pgStatRunningInCollector ? LOG : WARNING,
2645 (errmsg("corrupted pgstat.stat file")));
2646 goto done;
2650 * We found an existing collector stats file. Read it and put all the
2651 * hashtable entries into place.
2653 for (;;)
2655 switch (fgetc(fpin))
2658 * 'D' A PgStat_StatDBEntry struct describing a database
2659 * follows. Subsequently, zero to many 'T' entries will follow
2660 * until a 'd' is encountered.
2662 case 'D':
2663 if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
2664 fpin) != offsetof(PgStat_StatDBEntry, tables))
2666 ereport(pgStatRunningInCollector ? LOG : WARNING,
2667 (errmsg("corrupted pgstat.stat file")));
2668 goto done;
2672 * Add to the DB hash
2674 dbentry = (PgStat_StatDBEntry *) hash_search(dbhash,
2675 (void *) &dbbuf.databaseid,
2676 HASH_ENTER,
2677 &found);
2678 if (found)
2680 ereport(pgStatRunningInCollector ? LOG : WARNING,
2681 (errmsg("corrupted pgstat.stat file")));
2682 goto done;
2685 memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
2686 dbentry->tables = NULL;
2689 * Don't collect tables if not the requested DB (or the
2690 * shared-table info)
2692 if (onlydb != InvalidOid)
2694 if (dbbuf.databaseid != onlydb &&
2695 dbbuf.databaseid != InvalidOid)
2696 break;
2699 memset(&hash_ctl, 0, sizeof(hash_ctl));
2700 hash_ctl.keysize = sizeof(Oid);
2701 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2702 hash_ctl.hash = oid_hash;
2703 hash_ctl.hcxt = pgStatLocalContext;
2704 dbentry->tables = hash_create("Per-database table",
2705 PGSTAT_TAB_HASH_SIZE,
2706 &hash_ctl,
2707 HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
2710 * Arrange that following 'T's add entries to this database's
2711 * tables hash table.
2713 tabhash = dbentry->tables;
2714 break;
2717 * 'd' End of this database.
2719 case 'd':
2720 tabhash = NULL;
2721 break;
2724 * 'T' A PgStat_StatTabEntry follows.
2726 case 'T':
2727 if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
2728 fpin) != sizeof(PgStat_StatTabEntry))
2730 ereport(pgStatRunningInCollector ? LOG : WARNING,
2731 (errmsg("corrupted pgstat.stat file")));
2732 goto done;
2736 * Skip if table belongs to a not requested database.
2738 if (tabhash == NULL)
2739 break;
2741 tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
2742 (void *) &tabbuf.tableid,
2743 HASH_ENTER, &found);
2745 if (found)
2747 ereport(pgStatRunningInCollector ? LOG : WARNING,
2748 (errmsg("corrupted pgstat.stat file")));
2749 goto done;
2752 memcpy(tabentry, &tabbuf, sizeof(tabbuf));
2753 break;
2756 * 'E' The EOF marker of a complete stats file.
2758 case 'E':
2759 goto done;
2761 default:
2762 ereport(pgStatRunningInCollector ? LOG : WARNING,
2763 (errmsg("corrupted pgstat.stat file")));
2764 goto done;
2768 done:
2769 FreeFile(fpin);
2771 return dbhash;
2775 * If not already done, read the statistics collector stats file into
2776 * some hash tables. The results will be kept until pgstat_clear_snapshot()
2777 * is called (typically, at end of transaction).
2779 static void
2780 backend_read_statsfile(void)
2782 /* already read it? */
2783 if (pgStatDBHash)
2784 return;
2785 Assert(!pgStatRunningInCollector);
2787 /* Autovacuum launcher wants stats about all databases */
2788 if (IsAutoVacuumLauncherProcess())
2789 pgStatDBHash = pgstat_read_statsfile(InvalidOid);
2790 else
2791 pgStatDBHash = pgstat_read_statsfile(MyDatabaseId);
2795 /* ----------
2796 * pgstat_setup_memcxt() -
2798 * Create pgStatLocalContext, if not already done.
2799 * ----------
2801 static void
2802 pgstat_setup_memcxt(void)
2804 if (!pgStatLocalContext)
2805 pgStatLocalContext = AllocSetContextCreate(TopMemoryContext,
2806 "Statistics snapshot",
2807 ALLOCSET_SMALL_MINSIZE,
2808 ALLOCSET_SMALL_INITSIZE,
2809 ALLOCSET_SMALL_MAXSIZE);
2813 /* ----------
2814 * pgstat_clear_snapshot() -
2816 * Discard any data collected in the current transaction. Any subsequent
2817 * request will cause new snapshots to be read.
2819 * This is also invoked during transaction commit or abort to discard
2820 * the no-longer-wanted snapshot.
2821 * ----------
2823 void
2824 pgstat_clear_snapshot(void)
2826 /* Release memory, if any was allocated */
2827 if (pgStatLocalContext)
2828 MemoryContextDelete(pgStatLocalContext);
2830 /* Reset variables */
2831 pgStatLocalContext = NULL;
2832 pgStatDBHash = NULL;
2833 localBackendStatusTable = NULL;
2834 localNumBackends = 0;
2838 /* ----------
2839 * pgstat_recv_tabstat() -
2841 * Count what the backend has done.
2842 * ----------
2844 static void
2845 pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
2847 PgStat_TableEntry *tabmsg = &(msg->m_entry[0]);
2848 PgStat_StatDBEntry *dbentry;
2849 PgStat_StatTabEntry *tabentry;
2850 int i;
2851 bool found;
2853 dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
2856 * Update database-wide stats.
2858 dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
2859 dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
2862 * Process all table entries in the message.
2864 for (i = 0; i < msg->m_nentries; i++)
2866 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2867 (void *) &(tabmsg[i].t_id),
2868 HASH_ENTER, &found);
2870 if (!found)
2873 * If it's a new table entry, initialize counters to the values we
2874 * just got.
2876 tabentry->numscans = tabmsg[i].t_counts.t_numscans;
2877 tabentry->tuples_returned = tabmsg[i].t_counts.t_tuples_returned;
2878 tabentry->tuples_fetched = tabmsg[i].t_counts.t_tuples_fetched;
2879 tabentry->tuples_inserted = tabmsg[i].t_counts.t_tuples_inserted;
2880 tabentry->tuples_updated = tabmsg[i].t_counts.t_tuples_updated;
2881 tabentry->tuples_deleted = tabmsg[i].t_counts.t_tuples_deleted;
2882 tabentry->tuples_hot_updated = tabmsg[i].t_counts.t_tuples_hot_updated;
2883 tabentry->n_live_tuples = tabmsg[i].t_counts.t_new_live_tuples;
2884 tabentry->n_dead_tuples = tabmsg[i].t_counts.t_new_dead_tuples;
2885 tabentry->blocks_fetched = tabmsg[i].t_counts.t_blocks_fetched;
2886 tabentry->blocks_hit = tabmsg[i].t_counts.t_blocks_hit;
2888 tabentry->last_anl_tuples = 0;
2889 tabentry->vacuum_timestamp = 0;
2890 tabentry->autovac_vacuum_timestamp = 0;
2891 tabentry->analyze_timestamp = 0;
2892 tabentry->autovac_analyze_timestamp = 0;
2894 else
2897 * Otherwise add the values to the existing entry.
2899 tabentry->numscans += tabmsg[i].t_counts.t_numscans;
2900 tabentry->tuples_returned += tabmsg[i].t_counts.t_tuples_returned;
2901 tabentry->tuples_fetched += tabmsg[i].t_counts.t_tuples_fetched;
2902 tabentry->tuples_inserted += tabmsg[i].t_counts.t_tuples_inserted;
2903 tabentry->tuples_updated += tabmsg[i].t_counts.t_tuples_updated;
2904 tabentry->tuples_deleted += tabmsg[i].t_counts.t_tuples_deleted;
2905 tabentry->tuples_hot_updated += tabmsg[i].t_counts.t_tuples_hot_updated;
2906 tabentry->n_live_tuples += tabmsg[i].t_counts.t_new_live_tuples;
2907 tabentry->n_dead_tuples += tabmsg[i].t_counts.t_new_dead_tuples;
2908 tabentry->blocks_fetched += tabmsg[i].t_counts.t_blocks_fetched;
2909 tabentry->blocks_hit += tabmsg[i].t_counts.t_blocks_hit;
2912 /* Clamp n_live_tuples in case of negative new_live_tuples */
2913 tabentry->n_live_tuples = Max(tabentry->n_live_tuples, 0);
2914 /* Likewise for n_dead_tuples */
2915 tabentry->n_dead_tuples = Max(tabentry->n_dead_tuples, 0);
2918 * Add per-table stats to the per-database entry, too.
2920 dbentry->n_tuples_returned += tabmsg[i].t_counts.t_tuples_returned;
2921 dbentry->n_tuples_fetched += tabmsg[i].t_counts.t_tuples_fetched;
2922 dbentry->n_tuples_inserted += tabmsg[i].t_counts.t_tuples_inserted;
2923 dbentry->n_tuples_updated += tabmsg[i].t_counts.t_tuples_updated;
2924 dbentry->n_tuples_deleted += tabmsg[i].t_counts.t_tuples_deleted;
2925 dbentry->n_blocks_fetched += tabmsg[i].t_counts.t_blocks_fetched;
2926 dbentry->n_blocks_hit += tabmsg[i].t_counts.t_blocks_hit;
2931 /* ----------
2932 * pgstat_recv_tabpurge() -
2934 * Arrange for dead table removal.
2935 * ----------
2937 static void
2938 pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
2940 PgStat_StatDBEntry *dbentry;
2941 int i;
2943 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2946 * No need to purge if we don't even know the database.
2948 if (!dbentry || !dbentry->tables)
2949 return;
2952 * Process all table entries in the message.
2954 for (i = 0; i < msg->m_nentries; i++)
2956 /* Remove from hashtable if present; we don't care if it's not. */
2957 (void) hash_search(dbentry->tables,
2958 (void *) &(msg->m_tableid[i]),
2959 HASH_REMOVE, NULL);
2964 /* ----------
2965 * pgstat_recv_dropdb() -
2967 * Arrange for dead database removal
2968 * ----------
2970 static void
2971 pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len)
2973 PgStat_StatDBEntry *dbentry;
2976 * Lookup the database in the hashtable.
2978 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2981 * If found, remove it.
2983 if (dbentry)
2985 if (dbentry->tables != NULL)
2986 hash_destroy(dbentry->tables);
2988 if (hash_search(pgStatDBHash,
2989 (void *) &(dbentry->databaseid),
2990 HASH_REMOVE, NULL) == NULL)
2991 ereport(ERROR,
2992 (errmsg("database hash table corrupted "
2993 "during cleanup --- abort")));
2998 /* ----------
2999 * pgstat_recv_resetcounter() -
3001 * Reset the statistics for the specified database.
3002 * ----------
3004 static void
3005 pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
3007 HASHCTL hash_ctl;
3008 PgStat_StatDBEntry *dbentry;
3011 * Lookup the database in the hashtable. Nothing to do if not there.
3013 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3015 if (!dbentry)
3016 return;
3019 * We simply throw away all the database's table entries by recreating a
3020 * new hash table for them.
3022 if (dbentry->tables != NULL)
3023 hash_destroy(dbentry->tables);
3025 dbentry->tables = NULL;
3026 dbentry->n_xact_commit = 0;
3027 dbentry->n_xact_rollback = 0;
3028 dbentry->n_blocks_fetched = 0;
3029 dbentry->n_blocks_hit = 0;
3031 memset(&hash_ctl, 0, sizeof(hash_ctl));
3032 hash_ctl.keysize = sizeof(Oid);
3033 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
3034 hash_ctl.hash = oid_hash;
3035 dbentry->tables = hash_create("Per-database table",
3036 PGSTAT_TAB_HASH_SIZE,
3037 &hash_ctl,
3038 HASH_ELEM | HASH_FUNCTION);
3041 /* ----------
3042 * pgstat_recv_autovac() -
3044 * Process an autovacuum signalling message.
3045 * ----------
3047 static void
3048 pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len)
3050 PgStat_StatDBEntry *dbentry;
3053 * Lookup the database in the hashtable. Don't create the entry if it
3054 * doesn't exist, because autovacuum may be processing a template
3055 * database. If this isn't the case, the database is most likely to have
3056 * an entry already. (If it doesn't, not much harm is done anyway --
3057 * it'll get created as soon as somebody actually uses the database.)
3059 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3060 if (dbentry == NULL)
3061 return;
3064 * Store the last autovacuum time in the database entry.
3066 dbentry->last_autovac_time = msg->m_start_time;
3069 /* ----------
3070 * pgstat_recv_vacuum() -
3072 * Process a VACUUM message.
3073 * ----------
3075 static void
3076 pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len)
3078 PgStat_StatDBEntry *dbentry;
3079 PgStat_StatTabEntry *tabentry;
3082 * Don't create either the database or table entry if it doesn't already
3083 * exist. This avoids bloating the stats with entries for stuff that is
3084 * only touched by vacuum and not by live operations.
3086 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3087 if (dbentry == NULL)
3088 return;
3090 tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
3091 HASH_FIND, NULL);
3092 if (tabentry == NULL)
3093 return;
3095 if (msg->m_autovacuum)
3096 tabentry->autovac_vacuum_timestamp = msg->m_vacuumtime;
3097 else
3098 tabentry->vacuum_timestamp = msg->m_vacuumtime;
3099 tabentry->n_live_tuples = msg->m_tuples;
3100 /* Resetting dead_tuples to 0 is an approximation ... */
3101 tabentry->n_dead_tuples = 0;
3102 if (msg->m_analyze)
3104 tabentry->last_anl_tuples = msg->m_tuples;
3105 if (msg->m_autovacuum)
3106 tabentry->autovac_analyze_timestamp = msg->m_vacuumtime;
3107 else
3108 tabentry->analyze_timestamp = msg->m_vacuumtime;
3110 else
3112 /* last_anl_tuples must never exceed n_live_tuples+n_dead_tuples */
3113 tabentry->last_anl_tuples = Min(tabentry->last_anl_tuples,
3114 msg->m_tuples);
3118 /* ----------
3119 * pgstat_recv_analyze() -
3121 * Process an ANALYZE message.
3122 * ----------
3124 static void
3125 pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
3127 PgStat_StatDBEntry *dbentry;
3128 PgStat_StatTabEntry *tabentry;
3131 * Don't create either the database or table entry if it doesn't already
3132 * exist. This avoids bloating the stats with entries for stuff that is
3133 * only touched by analyze and not by live operations.
3135 dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3136 if (dbentry == NULL)
3137 return;
3139 tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
3140 HASH_FIND, NULL);
3141 if (tabentry == NULL)
3142 return;
3144 if (msg->m_autovacuum)
3145 tabentry->autovac_analyze_timestamp = msg->m_analyzetime;
3146 else
3147 tabentry->analyze_timestamp = msg->m_analyzetime;
3148 tabentry->n_live_tuples = msg->m_live_tuples;
3149 tabentry->n_dead_tuples = msg->m_dead_tuples;
3150 tabentry->last_anl_tuples = msg->m_live_tuples + msg->m_dead_tuples;
3154 /* ----------
3155 * pgstat_recv_bgwriter() -
3157 * Process a BGWRITER message.
3158 * ----------
3160 static void
3161 pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len)
3163 globalStats.timed_checkpoints += msg->m_timed_checkpoints;
3164 globalStats.requested_checkpoints += msg->m_requested_checkpoints;
3165 globalStats.buf_written_checkpoints += msg->m_buf_written_checkpoints;
3166 globalStats.buf_written_clean += msg->m_buf_written_clean;
3167 globalStats.maxwritten_clean += msg->m_maxwritten_clean;
3168 globalStats.buf_written_backend += msg->m_buf_written_backend;
3169 globalStats.buf_alloc += msg->m_buf_alloc;