PostgreSQL Source Code  git master
pgstat.c
Go to the documentation of this file.
1 /* ----------
2  * pgstat.c
3  *
4  * All the statistics collector stuff hacked up in one big, ugly file.
5  *
6  * TODO: - Separate collector, postmaster and backend stuff
7  * into different files.
8  *
9  * - Add some automatic call for pgstat vacuuming.
10  *
11  * - Add a pgstat config column to pg_database, so this
12  * entire thing can be enabled/disabled on a per db basis.
13  *
14  * Copyright (c) 2001-2021, PostgreSQL Global Development Group
15  *
16  * src/backend/postmaster/pgstat.c
17  * ----------
18  */
19 #include "postgres.h"
20 
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_SYS_SELECT_H
32 #include <sys/select.h>
33 #endif
34 
35 #include "access/heapam.h"
36 #include "access/htup_details.h"
37 #include "access/tableam.h"
38 #include "access/transam.h"
39 #include "access/twophase_rmgr.h"
40 #include "access/xact.h"
41 #include "catalog/pg_database.h"
42 #include "catalog/pg_proc.h"
43 #include "common/ip.h"
44 #include "executor/instrument.h"
45 #include "libpq/libpq.h"
46 #include "libpq/pqsignal.h"
47 #include "mb/pg_wchar.h"
48 #include "miscadmin.h"
49 #include "pg_trace.h"
50 #include "pgstat.h"
51 #include "postmaster/autovacuum.h"
53 #include "postmaster/interrupt.h"
54 #include "postmaster/postmaster.h"
55 #include "replication/slot.h"
56 #include "replication/walsender.h"
57 #include "storage/backendid.h"
58 #include "storage/dsm.h"
59 #include "storage/fd.h"
60 #include "storage/ipc.h"
61 #include "storage/latch.h"
62 #include "storage/lmgr.h"
63 #include "storage/pg_shmem.h"
64 #include "storage/procsignal.h"
65 #include "storage/sinvaladt.h"
66 #include "utils/ascii.h"
67 #include "utils/guc.h"
68 #include "utils/memutils.h"
69 #include "utils/ps_status.h"
70 #include "utils/rel.h"
71 #include "utils/snapmgr.h"
72 #include "utils/timestamp.h"
73 
74 /* ----------
75  * Timer definitions.
76  * ----------
77  */
78 #define PGSTAT_STAT_INTERVAL 500 /* Minimum time between stats file
79  * updates; in milliseconds. */
80 
81 #define PGSTAT_RETRY_DELAY 10 /* How long to wait between checks for a
82  * new file; in milliseconds. */
83 
84 #define PGSTAT_MAX_WAIT_TIME 10000 /* Maximum time to wait for a stats
85  * file update; in milliseconds. */
86 
87 #define PGSTAT_INQ_INTERVAL 640 /* How often to ping the collector for a
88  * new file; in milliseconds. */
89 
90 #define PGSTAT_RESTART_INTERVAL 60 /* How often to attempt to restart a
91  * failed statistics collector; in
92  * seconds. */
93 
94 #define PGSTAT_POLL_LOOP_COUNT (PGSTAT_MAX_WAIT_TIME / PGSTAT_RETRY_DELAY)
95 #define PGSTAT_INQ_LOOP_COUNT (PGSTAT_INQ_INTERVAL / PGSTAT_RETRY_DELAY)
96 
97 /* Minimum receive buffer size for the collector's socket. */
98 #define PGSTAT_MIN_RCVBUF (100 * 1024)
99 
100 
101 /* ----------
102  * The initial size hints for the hash tables used in the collector.
103  * ----------
104  */
105 #define PGSTAT_DB_HASH_SIZE 16
106 #define PGSTAT_TAB_HASH_SIZE 512
107 #define PGSTAT_FUNCTION_HASH_SIZE 512
108 
109 
110 /* ----------
111  * Total number of backends including auxiliary
112  *
113  * We reserve a slot for each possible BackendId, plus one for each
114  * possible auxiliary process type. (This scheme assumes there is not
115  * more than one of any auxiliary process type at a time.) MaxBackends
116  * includes autovacuum workers and background workers as well.
117  * ----------
118  */
119 #define NumBackendStatSlots (MaxBackends + NUM_AUXPROCTYPES)
120 
121 
122 /* ----------
123  * GUC parameters
124  * ----------
125  */
127 bool pgstat_track_counts = false;
130 
131 /* ----------
132  * Built from GUC parameter
133  * ----------
134  */
136 char *pgstat_stat_filename = NULL;
137 char *pgstat_stat_tmpname = NULL;
138 
139 /*
140  * BgWriter and WAL global statistics counters.
141  * Stored directly in a stats message structure so they can be sent
142  * without needing to copy things around. We assume these init to zeroes.
143  */
146 
147 /*
148  * WAL usage counters saved from pgWALUsage at the previous call to
149  * pgstat_send_wal(). This is used to calculate how much WAL usage
150  * happens between pgstat_send_wal() calls, by substracting
151  * the previous counters from the current ones.
152  */
154 
155 /*
156  * List of SLRU names that we keep stats for. There is no central registry of
157  * SLRUs, so we use this fixed list instead. The "other" entry is used for
158  * all SLRUs without an explicit entry (e.g. SLRUs in extensions).
159  */
160 static const char *const slru_names[] = {
161  "CommitTs",
162  "MultiXactMember",
163  "MultiXactOffset",
164  "Notify",
165  "Serial",
166  "Subtrans",
167  "Xact",
168  "other" /* has to be last */
169 };
170 
171 #define SLRU_NUM_ELEMENTS lengthof(slru_names)
172 
173 /*
174  * SLRU statistics counts waiting to be sent to the collector. These are
175  * stored directly in stats message format so they can be sent without needing
176  * to copy things around. We assume this variable inits to zeroes. Entries
177  * are one-to-one with slru_names[].
178  */
180 
181 /* ----------
182  * Local data
183  * ----------
184  */
186 
188 
190 
191 static bool pgStatRunningInCollector = false;
192 
193 /*
194  * Structures in which backends store per-table info that's waiting to be
195  * sent to the collector.
196  *
197  * NOTE: once allocated, TabStatusArray structures are never moved or deleted
198  * for the life of the backend. Also, we zero out the t_id fields of the
199  * contained PgStat_TableStatus structs whenever they are not actively in use.
200  * This allows relcache pgstat_info pointers to be treated as long-lived data,
201  * avoiding repeated searches in pgstat_initstats() when a relation is
202  * repeatedly opened during a transaction.
203  */
204 #define TABSTAT_QUANTUM 100 /* we alloc this many at a time */
205 
206 typedef struct TabStatusArray
207 {
208  struct TabStatusArray *tsa_next; /* link to next array, if any */
209  int tsa_used; /* # entries currently used */
212 
214 
215 /*
216  * pgStatTabHash entry: map from relation OID to PgStat_TableStatus pointer
217  */
218 typedef struct TabStatHashEntry
219 {
223 
224 /*
225  * Hash table for O(1) t_id -> tsa_entry lookup
226  */
227 static HTAB *pgStatTabHash = NULL;
228 
229 /*
230  * Backends store per-function info that's waiting to be sent to the collector
231  * in this hash table (indexed by function OID).
232  */
233 static HTAB *pgStatFunctions = NULL;
234 
235 /*
236  * Indicates if backend has some function stats that it hasn't yet
237  * sent to the collector.
238  */
239 static bool have_function_stats = false;
240 
241 /*
242  * Tuple insertion/deletion counts for an open transaction can't be propagated
243  * into PgStat_TableStatus counters until we know if it is going to commit
244  * or abort. Hence, we keep these counts in per-subxact structs that live
245  * in TopTransactionContext. This data structure is designed on the assumption
246  * that subxacts won't usually modify very many tables.
247  */
248 typedef struct PgStat_SubXactStatus
249 {
250  int nest_level; /* subtransaction nest level */
251  struct PgStat_SubXactStatus *prev; /* higher-level subxact if any */
252  PgStat_TableXactStatus *first; /* head of list for this subxact */
254 
256 
257 static int pgStatXactCommit = 0;
258 static int pgStatXactRollback = 0;
264 
265 /* Record that's written to 2PC state file when pgstat state is persisted */
266 typedef struct TwoPhasePgStatRecord
267 {
268  PgStat_Counter tuples_inserted; /* tuples inserted in xact */
269  PgStat_Counter tuples_updated; /* tuples updated in xact */
270  PgStat_Counter tuples_deleted; /* tuples deleted in xact */
271  PgStat_Counter inserted_pre_trunc; /* tuples inserted prior to truncate */
272  PgStat_Counter updated_pre_trunc; /* tuples updated prior to truncate */
273  PgStat_Counter deleted_pre_trunc; /* tuples deleted prior to truncate */
274  Oid t_id; /* table's OID */
275  bool t_shared; /* is it a shared catalog? */
276  bool t_truncated; /* was the relation truncated? */
278 
279 /*
280  * Info about current "snapshot" of stats file
281  */
283 static HTAB *pgStatDBHash = NULL;
284 
285 /* Status for backends including auxiliary */
287 
288 /* Total number of backends including auxiliary */
289 static int localNumBackends = 0;
290 
291 /*
292  * Cluster wide statistics, kept in the stats collector.
293  * Contains statistics that are not collected per database
294  * or per table.
295  */
301 static int nReplSlotStats;
302 
303 /*
304  * List of OIDs of databases we need to write out. If an entry is InvalidOid,
305  * it means to write only the shared-catalog stats ("DB 0"); otherwise, we
306  * will write both that DB's data and the shared stats.
307  */
309 
310 /*
311  * Total time charged to functions so far in the current backend.
312  * We use this to help separate "self" and "other" time charges.
313  * (We assume this initializes to zero.)
314  */
316 
317 
318 /* ----------
319  * Local function forward declarations
320  * ----------
321  */
322 #ifdef EXEC_BACKEND
323 static pid_t pgstat_forkexec(void);
324 #endif
325 
326 NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]) pg_attribute_noreturn();
327 static void pgstat_beshutdown_hook(int code, Datum arg);
328 
329 static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
331  Oid tableoid, bool create);
332 static void pgstat_write_statsfiles(bool permanent, bool allDbs);
333 static void pgstat_write_db_statsfile(PgStat_StatDBEntry *dbentry, bool permanent);
334 static HTAB *pgstat_read_statsfiles(Oid onlydb, bool permanent, bool deep);
335 static void pgstat_read_db_statsfile(Oid databaseid, HTAB *tabhash, HTAB *funchash, bool permanent);
336 static void backend_read_statsfile(void);
337 static void pgstat_read_current_status(void);
338 
339 static bool pgstat_write_statsfile_needed(void);
340 static bool pgstat_db_requested(Oid databaseid);
341 
342 static int pgstat_replslot_index(const char *name, bool create_it);
343 static void pgstat_reset_replslot(int i, TimestampTz ts);
344 
345 static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg);
346 static void pgstat_send_funcstats(void);
347 static void pgstat_send_slru(void);
348 static HTAB *pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid);
349 static void pgstat_send_connstats(bool disconnect, TimestampTz last_report);
350 
351 static PgStat_TableStatus *get_tabstat_entry(Oid rel_id, bool isshared);
352 
353 static void pgstat_setup_memcxt(void);
354 
355 static const char *pgstat_get_wait_activity(WaitEventActivity w);
356 static const char *pgstat_get_wait_client(WaitEventClient w);
357 static const char *pgstat_get_wait_ipc(WaitEventIPC w);
358 static const char *pgstat_get_wait_timeout(WaitEventTimeout w);
359 static const char *pgstat_get_wait_io(WaitEventIO w);
360 
361 static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype);
362 static void pgstat_send(void *msg, int len);
363 
364 static void pgstat_recv_inquiry(PgStat_MsgInquiry *msg, int len);
365 static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
366 static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
367 static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
368 static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
373 static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
374 static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
375 static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
376 static void pgstat_recv_archiver(PgStat_MsgArchiver *msg, int len);
377 static void pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len);
378 static void pgstat_recv_wal(PgStat_MsgWal *msg, int len);
379 static void pgstat_recv_slru(PgStat_MsgSLRU *msg, int len);
380 static void pgstat_recv_funcstat(PgStat_MsgFuncstat *msg, int len);
381 static void pgstat_recv_funcpurge(PgStat_MsgFuncpurge *msg, int len);
383 static void pgstat_recv_deadlock(PgStat_MsgDeadlock *msg, int len);
385 static void pgstat_recv_connstat(PgStat_MsgConn *msg, int len);
386 static void pgstat_recv_replslot(PgStat_MsgReplSlot *msg, int len);
387 static void pgstat_recv_tempfile(PgStat_MsgTempFile *msg, int len);
388 
389 /* ------------------------------------------------------------
390  * Public functions called from postmaster follow
391  * ------------------------------------------------------------
392  */
393 
394 /* ----------
395  * pgstat_init() -
396  *
397  * Called from postmaster at startup. Create the resources required
398  * by the statistics collector process. If unable to do so, do not
399  * fail --- better to let the postmaster start with stats collection
400  * disabled.
401  * ----------
402  */
403 void
405 {
406  ACCEPT_TYPE_ARG3 alen;
407  struct addrinfo *addrs = NULL,
408  *addr,
409  hints;
410  int ret;
411  fd_set rset;
412  struct timeval tv;
413  char test_byte;
414  int sel_res;
415  int tries = 0;
416 
417 #define TESTBYTEVAL ((char) 199)
418 
419  /*
420  * This static assertion verifies that we didn't mess up the calculations
421  * involved in selecting maximum payload sizes for our UDP messages.
422  * Because the only consequence of overrunning PGSTAT_MAX_MSG_SIZE would
423  * be silent performance loss from fragmentation, it seems worth having a
424  * compile-time cross-check that we didn't.
425  */
427  "maximum stats message size exceeds PGSTAT_MAX_MSG_SIZE");
428 
429  /*
430  * Create the UDP socket for sending and receiving statistic messages
431  */
432  hints.ai_flags = AI_PASSIVE;
433  hints.ai_family = AF_UNSPEC;
434  hints.ai_socktype = SOCK_DGRAM;
435  hints.ai_protocol = 0;
436  hints.ai_addrlen = 0;
437  hints.ai_addr = NULL;
438  hints.ai_canonname = NULL;
439  hints.ai_next = NULL;
440  ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
441  if (ret || !addrs)
442  {
443  ereport(LOG,
444  (errmsg("could not resolve \"localhost\": %s",
445  gai_strerror(ret))));
446  goto startup_failed;
447  }
448 
449  /*
450  * On some platforms, pg_getaddrinfo_all() may return multiple addresses
451  * only one of which will actually work (eg, both IPv6 and IPv4 addresses
452  * when kernel will reject IPv6). Worse, the failure may occur at the
453  * bind() or perhaps even connect() stage. So we must loop through the
454  * results till we find a working combination. We will generate LOG
455  * messages, but no error, for bogus combinations.
456  */
457  for (addr = addrs; addr; addr = addr->ai_next)
458  {
459 #ifdef HAVE_UNIX_SOCKETS
460  /* Ignore AF_UNIX sockets, if any are returned. */
461  if (addr->ai_family == AF_UNIX)
462  continue;
463 #endif
464 
465  if (++tries > 1)
466  ereport(LOG,
467  (errmsg("trying another address for the statistics collector")));
468 
469  /*
470  * Create the socket.
471  */
472  if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) == PGINVALID_SOCKET)
473  {
474  ereport(LOG,
476  errmsg("could not create socket for statistics collector: %m")));
477  continue;
478  }
479 
480  /*
481  * Bind it to a kernel assigned port on localhost and get the assigned
482  * port via getsockname().
483  */
484  if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
485  {
486  ereport(LOG,
488  errmsg("could not bind socket for statistics collector: %m")));
491  continue;
492  }
493 
494  alen = sizeof(pgStatAddr);
495  if (getsockname(pgStatSock, (struct sockaddr *) &pgStatAddr, &alen) < 0)
496  {
497  ereport(LOG,
499  errmsg("could not get address of socket for statistics collector: %m")));
502  continue;
503  }
504 
505  /*
506  * Connect the socket to its own address. This saves a few cycles by
507  * not having to respecify the target address on every send. This also
508  * provides a kernel-level check that only packets from this same
509  * address will be received.
510  */
511  if (connect(pgStatSock, (struct sockaddr *) &pgStatAddr, alen) < 0)
512  {
513  ereport(LOG,
515  errmsg("could not connect socket for statistics collector: %m")));
518  continue;
519  }
520 
521  /*
522  * Try to send and receive a one-byte test message on the socket. This
523  * is to catch situations where the socket can be created but will not
524  * actually pass data (for instance, because kernel packet filtering
525  * rules prevent it).
526  */
527  test_byte = TESTBYTEVAL;
528 
529 retry1:
530  if (send(pgStatSock, &test_byte, 1, 0) != 1)
531  {
532  if (errno == EINTR)
533  goto retry1; /* if interrupted, just retry */
534  ereport(LOG,
536  errmsg("could not send test message on socket for statistics collector: %m")));
539  continue;
540  }
541 
542  /*
543  * There could possibly be a little delay before the message can be
544  * received. We arbitrarily allow up to half a second before deciding
545  * it's broken.
546  */
547  for (;;) /* need a loop to handle EINTR */
548  {
549  FD_ZERO(&rset);
550  FD_SET(pgStatSock, &rset);
551 
552  tv.tv_sec = 0;
553  tv.tv_usec = 500000;
554  sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
555  if (sel_res >= 0 || errno != EINTR)
556  break;
557  }
558  if (sel_res < 0)
559  {
560  ereport(LOG,
562  errmsg("select() failed in statistics collector: %m")));
565  continue;
566  }
567  if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
568  {
569  /*
570  * This is the case we actually think is likely, so take pains to
571  * give a specific message for it.
572  *
573  * errno will not be set meaningfully here, so don't use it.
574  */
575  ereport(LOG,
576  (errcode(ERRCODE_CONNECTION_FAILURE),
577  errmsg("test message did not get through on socket for statistics collector")));
580  continue;
581  }
582 
583  test_byte++; /* just make sure variable is changed */
584 
585 retry2:
586  if (recv(pgStatSock, &test_byte, 1, 0) != 1)
587  {
588  if (errno == EINTR)
589  goto retry2; /* if interrupted, just retry */
590  ereport(LOG,
592  errmsg("could not receive test message on socket for statistics collector: %m")));
595  continue;
596  }
597 
598  if (test_byte != TESTBYTEVAL) /* strictly paranoia ... */
599  {
600  ereport(LOG,
601  (errcode(ERRCODE_INTERNAL_ERROR),
602  errmsg("incorrect test message transmission on socket for statistics collector")));
605  continue;
606  }
607 
608  /* If we get here, we have a working socket */
609  break;
610  }
611 
612  /* Did we find a working address? */
613  if (!addr || pgStatSock == PGINVALID_SOCKET)
614  goto startup_failed;
615 
616  /*
617  * Set the socket to non-blocking IO. This ensures that if the collector
618  * falls behind, statistics messages will be discarded; backends won't
619  * block waiting to send messages to the collector.
620  */
622  {
623  ereport(LOG,
625  errmsg("could not set statistics collector socket to nonblocking mode: %m")));
626  goto startup_failed;
627  }
628 
629  /*
630  * Try to ensure that the socket's receive buffer is at least
631  * PGSTAT_MIN_RCVBUF bytes, so that it won't easily overflow and lose
632  * data. Use of UDP protocol means that we are willing to lose data under
633  * heavy load, but we don't want it to happen just because of ridiculously
634  * small default buffer sizes (such as 8KB on older Windows versions).
635  */
636  {
637  int old_rcvbuf;
638  int new_rcvbuf;
639  ACCEPT_TYPE_ARG3 rcvbufsize = sizeof(old_rcvbuf);
640 
641  if (getsockopt(pgStatSock, SOL_SOCKET, SO_RCVBUF,
642  (char *) &old_rcvbuf, &rcvbufsize) < 0)
643  {
644  ereport(LOG,
645  (errmsg("getsockopt(%s) failed: %m", "SO_RCVBUF")));
646  /* if we can't get existing size, always try to set it */
647  old_rcvbuf = 0;
648  }
649 
650  new_rcvbuf = PGSTAT_MIN_RCVBUF;
651  if (old_rcvbuf < new_rcvbuf)
652  {
653  if (setsockopt(pgStatSock, SOL_SOCKET, SO_RCVBUF,
654  (char *) &new_rcvbuf, sizeof(new_rcvbuf)) < 0)
655  ereport(LOG,
656  (errmsg("setsockopt(%s) failed: %m", "SO_RCVBUF")));
657  }
658  }
659 
660  pg_freeaddrinfo_all(hints.ai_family, addrs);
661 
662  /* Now that we have a long-lived socket, tell fd.c about it. */
664 
665  return;
666 
667 startup_failed:
668  ereport(LOG,
669  (errmsg("disabling statistics collector for lack of working socket")));
670 
671  if (addrs)
672  pg_freeaddrinfo_all(hints.ai_family, addrs);
673 
677 
678  /*
679  * Adjust GUC variables to suppress useless activity, and for debugging
680  * purposes (seeing track_counts off is a clue that we failed here). We
681  * use PGC_S_OVERRIDE because there is no point in trying to turn it back
682  * on from postgresql.conf without a restart.
683  */
684  SetConfigOption("track_counts", "off", PGC_INTERNAL, PGC_S_OVERRIDE);
685 }
686 
687 /*
688  * subroutine for pgstat_reset_all
689  */
690 static void
692 {
693  DIR *dir;
694  struct dirent *entry;
695  char fname[MAXPGPATH * 2];
696 
697  dir = AllocateDir(directory);
698  while ((entry = ReadDir(dir, directory)) != NULL)
699  {
700  int nchars;
701  Oid tmp_oid;
702 
703  /*
704  * Skip directory entries that don't match the file names we write.
705  * See get_dbstat_filename for the database-specific pattern.
706  */
707  if (strncmp(entry->d_name, "global.", 7) == 0)
708  nchars = 7;
709  else
710  {
711  nchars = 0;
712  (void) sscanf(entry->d_name, "db_%u.%n",
713  &tmp_oid, &nchars);
714  if (nchars <= 0)
715  continue;
716  /* %u allows leading whitespace, so reject that */
717  if (strchr("0123456789", entry->d_name[3]) == NULL)
718  continue;
719  }
720 
721  if (strcmp(entry->d_name + nchars, "tmp") != 0 &&
722  strcmp(entry->d_name + nchars, "stat") != 0)
723  continue;
724 
725  snprintf(fname, sizeof(fname), "%s/%s", directory,
726  entry->d_name);
727  unlink(fname);
728  }
729  FreeDir(dir);
730 }
731 
732 /*
733  * pgstat_reset_all() -
734  *
735  * Remove the stats files. This is currently used only if WAL
736  * recovery is needed after a crash.
737  */
738 void
740 {
743 }
744 
745 #ifdef EXEC_BACKEND
746 
747 /*
748  * pgstat_forkexec() -
749  *
750  * Format up the arglist for, then fork and exec, statistics collector process
751  */
752 static pid_t
753 pgstat_forkexec(void)
754 {
755  char *av[10];
756  int ac = 0;
757 
758  av[ac++] = "postgres";
759  av[ac++] = "--forkcol";
760  av[ac++] = NULL; /* filled in by postmaster_forkexec */
761 
762  av[ac] = NULL;
763  Assert(ac < lengthof(av));
764 
765  return postmaster_forkexec(ac, av);
766 }
767 #endif /* EXEC_BACKEND */
768 
769 
770 /*
771  * pgstat_start() -
772  *
773  * Called from postmaster at startup or after an existing collector
774  * died. Attempt to fire up a fresh statistics collector.
775  *
776  * Returns PID of child process, or 0 if fail.
777  *
778  * Note: if fail, we will be called again from the postmaster main loop.
779  */
780 int
782 {
783  time_t curtime;
784  pid_t pgStatPid;
785 
786  /*
787  * Check that the socket is there, else pgstat_init failed and we can do
788  * nothing useful.
789  */
791  return 0;
792 
793  /*
794  * Do nothing if too soon since last collector start. This is a safety
795  * valve to protect against continuous respawn attempts if the collector
796  * is dying immediately at launch. Note that since we will be re-called
797  * from the postmaster main loop, we will get another chance later.
798  */
799  curtime = time(NULL);
800  if ((unsigned int) (curtime - last_pgstat_start_time) <
801  (unsigned int) PGSTAT_RESTART_INTERVAL)
802  return 0;
803  last_pgstat_start_time = curtime;
804 
805  /*
806  * Okay, fork off the collector.
807  */
808 #ifdef EXEC_BACKEND
809  switch ((pgStatPid = pgstat_forkexec()))
810 #else
811  switch ((pgStatPid = fork_process()))
812 #endif
813  {
814  case -1:
815  ereport(LOG,
816  (errmsg("could not fork statistics collector: %m")));
817  return 0;
818 
819 #ifndef EXEC_BACKEND
820  case 0:
821  /* in postmaster child ... */
823 
824  /* Close the postmaster's sockets */
825  ClosePostmasterPorts(false);
826 
827  /* Drop our connection to postmaster's shared memory, as well */
828  dsm_detach_all();
830 
831  PgstatCollectorMain(0, NULL);
832  break;
833 #endif
834 
835  default:
836  return (int) pgStatPid;
837  }
838 
839  /* shouldn't get here */
840  return 0;
841 }
842 
843 void
845 {
847 }
848 
849 /* ------------------------------------------------------------
850  * Public functions used by backends follow
851  *------------------------------------------------------------
852  */
853 
854 
855 /* ----------
856  * pgstat_report_stat() -
857  *
858  * Must be called by processes that performs DML: tcop/postgres.c, logical
859  * receiver processes, SPI worker, etc. to send the so far collected
860  * per-table and function usage statistics to the collector. Note that this
861  * is called only when not within a transaction, so it is fair to use
862  * transaction stop time as an approximation of current time.
863  *
864  * "disconnect" is "true" only for the last call before the backend
865  * exits. This makes sure that no data is lost and that interrupted
866  * sessions are reported correctly.
867  * ----------
868  */
869 void
870 pgstat_report_stat(bool disconnect)
871 {
872  /* we assume this inits to all zeroes: */
873  static const PgStat_TableCounts all_zeroes;
874  static TimestampTz last_report = 0;
875 
877  PgStat_MsgTabstat regular_msg;
878  PgStat_MsgTabstat shared_msg;
879  TabStatusArray *tsa;
880  int i;
881 
882  /* Don't expend a clock check if nothing to do */
883  if ((pgStatTabList == NULL || pgStatTabList->tsa_used == 0) &&
884  pgStatXactCommit == 0 && pgStatXactRollback == 0 &&
885  !have_function_stats && !disconnect)
886  return;
887 
888  /*
889  * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
890  * msec since we last sent one, or the backend is about to exit.
891  */
893  if (!disconnect &&
895  return;
896 
897  /* for backends, send connection statistics */
898  if (MyBackendType == B_BACKEND)
899  pgstat_send_connstats(disconnect, last_report);
900 
901  last_report = now;
902 
903  /*
904  * Destroy pgStatTabHash before we start invalidating PgStat_TableEntry
905  * entries it points to. (Should we fail partway through the loop below,
906  * it's okay to have removed the hashtable already --- the only
907  * consequence is we'd get multiple entries for the same table in the
908  * pgStatTabList, and that's safe.)
909  */
910  if (pgStatTabHash)
911  hash_destroy(pgStatTabHash);
912  pgStatTabHash = NULL;
913 
914  /*
915  * Scan through the TabStatusArray struct(s) to find tables that actually
916  * have counts, and build messages to send. We have to separate shared
917  * relations from regular ones because the databaseid field in the message
918  * header has to depend on that.
919  */
920  regular_msg.m_databaseid = MyDatabaseId;
921  shared_msg.m_databaseid = InvalidOid;
922  regular_msg.m_nentries = 0;
923  shared_msg.m_nentries = 0;
924 
925  for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
926  {
927  for (i = 0; i < tsa->tsa_used; i++)
928  {
929  PgStat_TableStatus *entry = &tsa->tsa_entries[i];
930  PgStat_MsgTabstat *this_msg;
931  PgStat_TableEntry *this_ent;
932 
933  /* Shouldn't have any pending transaction-dependent counts */
934  Assert(entry->trans == NULL);
935 
936  /*
937  * Ignore entries that didn't accumulate any actual counts, such
938  * as indexes that were opened by the planner but not used.
939  */
940  if (memcmp(&entry->t_counts, &all_zeroes,
941  sizeof(PgStat_TableCounts)) == 0)
942  continue;
943 
944  /*
945  * OK, insert data into the appropriate message, and send if full.
946  */
947  this_msg = entry->t_shared ? &shared_msg : &regular_msg;
948  this_ent = &this_msg->m_entry[this_msg->m_nentries];
949  this_ent->t_id = entry->t_id;
950  memcpy(&this_ent->t_counts, &entry->t_counts,
951  sizeof(PgStat_TableCounts));
952  if (++this_msg->m_nentries >= PGSTAT_NUM_TABENTRIES)
953  {
954  pgstat_send_tabstat(this_msg);
955  this_msg->m_nentries = 0;
956  }
957  }
958  /* zero out PgStat_TableStatus structs after use */
959  MemSet(tsa->tsa_entries, 0,
960  tsa->tsa_used * sizeof(PgStat_TableStatus));
961  tsa->tsa_used = 0;
962  }
963 
964  /*
965  * Send partial messages. Make sure that any pending xact commit/abort
966  * gets counted, even if there are no table stats to send.
967  */
968  if (regular_msg.m_nentries > 0 ||
970  pgstat_send_tabstat(&regular_msg);
971  if (shared_msg.m_nentries > 0)
972  pgstat_send_tabstat(&shared_msg);
973 
974  /* Now, send function statistics */
976 
977  /* Send WAL statistics */
978  pgstat_send_wal();
979 
980  /* Finally send SLRU statistics */
982 }
983 
984 /*
985  * Subroutine for pgstat_report_stat: finish and send a tabstat message
986  */
987 static void
989 {
990  int n;
991  int len;
992 
993  /* It's unlikely we'd get here with no socket, but maybe not impossible */
995  return;
996 
997  /*
998  * Report and reset accumulated xact commit/rollback and I/O timings
999  * whenever we send a normal tabstat message
1000  */
1001  if (OidIsValid(tsmsg->m_databaseid))
1002  {
1007  pgStatXactCommit = 0;
1008  pgStatXactRollback = 0;
1009  pgStatBlockReadTime = 0;
1011  }
1012  else
1013  {
1014  tsmsg->m_xact_commit = 0;
1015  tsmsg->m_xact_rollback = 0;
1016  tsmsg->m_block_read_time = 0;
1017  tsmsg->m_block_write_time = 0;
1018  }
1019 
1020  n = tsmsg->m_nentries;
1021  len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
1022  n * sizeof(PgStat_TableEntry);
1023 
1025  pgstat_send(tsmsg, len);
1026 }
1027 
1028 /*
1029  * Subroutine for pgstat_report_stat: populate and send a function stat message
1030  */
1031 static void
1033 {
1034  /* we assume this inits to all zeroes: */
1035  static const PgStat_FunctionCounts all_zeroes;
1036 
1037  PgStat_MsgFuncstat msg;
1040 
1041  if (pgStatFunctions == NULL)
1042  return;
1043 
1045  msg.m_databaseid = MyDatabaseId;
1046  msg.m_nentries = 0;
1047 
1048  hash_seq_init(&fstat, pgStatFunctions);
1049  while ((entry = (PgStat_BackendFunctionEntry *) hash_seq_search(&fstat)) != NULL)
1050  {
1051  PgStat_FunctionEntry *m_ent;
1052 
1053  /* Skip it if no counts accumulated since last time */
1054  if (memcmp(&entry->f_counts, &all_zeroes,
1055  sizeof(PgStat_FunctionCounts)) == 0)
1056  continue;
1057 
1058  /* need to convert format of time accumulators */
1059  m_ent = &msg.m_entry[msg.m_nentries];
1060  m_ent->f_id = entry->f_id;
1061  m_ent->f_numcalls = entry->f_counts.f_numcalls;
1064 
1065  if (++msg.m_nentries >= PGSTAT_NUM_FUNCENTRIES)
1066  {
1067  pgstat_send(&msg, offsetof(PgStat_MsgFuncstat, m_entry[0]) +
1068  msg.m_nentries * sizeof(PgStat_FunctionEntry));
1069  msg.m_nentries = 0;
1070  }
1071 
1072  /* reset the entry's counts */
1073  MemSet(&entry->f_counts, 0, sizeof(PgStat_FunctionCounts));
1074  }
1075 
1076  if (msg.m_nentries > 0)
1077  pgstat_send(&msg, offsetof(PgStat_MsgFuncstat, m_entry[0]) +
1078  msg.m_nentries * sizeof(PgStat_FunctionEntry));
1079 
1080  have_function_stats = false;
1081 }
1082 
1083 
1084 /* ----------
1085  * pgstat_vacuum_stat() -
1086  *
1087  * Will tell the collector about objects he can get rid of.
1088  * ----------
1089  */
1090 void
1092 {
1093  HTAB *htab;
1094  PgStat_MsgTabpurge msg;
1095  PgStat_MsgFuncpurge f_msg;
1096  HASH_SEQ_STATUS hstat;
1097  PgStat_StatDBEntry *dbentry;
1098  PgStat_StatTabEntry *tabentry;
1099  PgStat_StatFuncEntry *funcentry;
1100  int len;
1101 
1103  return;
1104 
1105  /*
1106  * If not done for this transaction, read the statistics collector stats
1107  * file into some hash tables.
1108  */
1110 
1111  /*
1112  * Read pg_database and make a list of OIDs of all existing databases
1113  */
1114  htab = pgstat_collect_oids(DatabaseRelationId, Anum_pg_database_oid);
1115 
1116  /*
1117  * Search the database hash table for dead databases and tell the
1118  * collector to drop them.
1119  */
1120  hash_seq_init(&hstat, pgStatDBHash);
1121  while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
1122  {
1123  Oid dbid = dbentry->databaseid;
1124 
1126 
1127  /* the DB entry for shared tables (with InvalidOid) is never dropped */
1128  if (OidIsValid(dbid) &&
1129  hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
1130  pgstat_drop_database(dbid);
1131  }
1132 
1133  /* Clean up */
1134  hash_destroy(htab);
1135 
1136  /*
1137  * Lookup our own database entry; if not found, nothing more to do.
1138  */
1139  dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1140  (void *) &MyDatabaseId,
1141  HASH_FIND, NULL);
1142  if (dbentry == NULL || dbentry->tables == NULL)
1143  return;
1144 
1145  /*
1146  * Similarly to above, make a list of all known relations in this DB.
1147  */
1148  htab = pgstat_collect_oids(RelationRelationId, Anum_pg_class_oid);
1149 
1150  /*
1151  * Initialize our messages table counter to zero
1152  */
1153  msg.m_nentries = 0;
1154 
1155  /*
1156  * Check for all tables listed in stats hashtable if they still exist.
1157  */
1158  hash_seq_init(&hstat, dbentry->tables);
1159  while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
1160  {
1161  Oid tabid = tabentry->tableid;
1162 
1164 
1165  if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
1166  continue;
1167 
1168  /*
1169  * Not there, so add this table's Oid to the message
1170  */
1171  msg.m_tableid[msg.m_nentries++] = tabid;
1172 
1173  /*
1174  * If the message is full, send it out and reinitialize to empty
1175  */
1176  if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
1177  {
1178  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1179  + msg.m_nentries * sizeof(Oid);
1180 
1182  msg.m_databaseid = MyDatabaseId;
1183  pgstat_send(&msg, len);
1184 
1185  msg.m_nentries = 0;
1186  }
1187  }
1188 
1189  /*
1190  * Send the rest
1191  */
1192  if (msg.m_nentries > 0)
1193  {
1194  len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
1195  + msg.m_nentries * sizeof(Oid);
1196 
1198  msg.m_databaseid = MyDatabaseId;
1199  pgstat_send(&msg, len);
1200  }
1201 
1202  /* Clean up */
1203  hash_destroy(htab);
1204 
1205  /*
1206  * Now repeat the above steps for functions. However, we needn't bother
1207  * in the common case where no function stats are being collected.
1208  */
1209  if (dbentry->functions != NULL &&
1210  hash_get_num_entries(dbentry->functions) > 0)
1211  {
1212  htab = pgstat_collect_oids(ProcedureRelationId, Anum_pg_proc_oid);
1213 
1215  f_msg.m_databaseid = MyDatabaseId;
1216  f_msg.m_nentries = 0;
1217 
1218  hash_seq_init(&hstat, dbentry->functions);
1219  while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&hstat)) != NULL)
1220  {
1221  Oid funcid = funcentry->functionid;
1222 
1224 
1225  if (hash_search(htab, (void *) &funcid, HASH_FIND, NULL) != NULL)
1226  continue;
1227 
1228  /*
1229  * Not there, so add this function's Oid to the message
1230  */
1231  f_msg.m_functionid[f_msg.m_nentries++] = funcid;
1232 
1233  /*
1234  * If the message is full, send it out and reinitialize to empty
1235  */
1236  if (f_msg.m_nentries >= PGSTAT_NUM_FUNCPURGE)
1237  {
1238  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1239  + f_msg.m_nentries * sizeof(Oid);
1240 
1241  pgstat_send(&f_msg, len);
1242 
1243  f_msg.m_nentries = 0;
1244  }
1245  }
1246 
1247  /*
1248  * Send the rest
1249  */
1250  if (f_msg.m_nentries > 0)
1251  {
1252  len = offsetof(PgStat_MsgFuncpurge, m_functionid[0])
1253  + f_msg.m_nentries * sizeof(Oid);
1254 
1255  pgstat_send(&f_msg, len);
1256  }
1257 
1258  hash_destroy(htab);
1259  }
1260 }
1261 
1262 
1263 /* ----------
1264  * pgstat_collect_oids() -
1265  *
1266  * Collect the OIDs of all objects listed in the specified system catalog
1267  * into a temporary hash table. Caller should hash_destroy the result
1268  * when done with it. (However, we make the table in CurrentMemoryContext
1269  * so that it will be freed properly in event of an error.)
1270  * ----------
1271  */
1272 static HTAB *
1273 pgstat_collect_oids(Oid catalogid, AttrNumber anum_oid)
1274 {
1275  HTAB *htab;
1276  HASHCTL hash_ctl;
1277  Relation rel;
1278  TableScanDesc scan;
1279  HeapTuple tup;
1280  Snapshot snapshot;
1281 
1282  hash_ctl.keysize = sizeof(Oid);
1283  hash_ctl.entrysize = sizeof(Oid);
1284  hash_ctl.hcxt = CurrentMemoryContext;
1285  htab = hash_create("Temporary table of OIDs",
1287  &hash_ctl,
1289 
1290  rel = table_open(catalogid, AccessShareLock);
1291  snapshot = RegisterSnapshot(GetLatestSnapshot());
1292  scan = table_beginscan(rel, snapshot, 0, NULL);
1293  while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
1294  {
1295  Oid thisoid;
1296  bool isnull;
1297 
1298  thisoid = heap_getattr(tup, anum_oid, RelationGetDescr(rel), &isnull);
1299  Assert(!isnull);
1300 
1302 
1303  (void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
1304  }
1305  table_endscan(scan);
1306  UnregisterSnapshot(snapshot);
1308 
1309  return htab;
1310 }
1311 
1312 
1313 /* ----------
1314  * pgstat_drop_database() -
1315  *
1316  * Tell the collector that we just dropped a database.
1317  * (If the message gets lost, we will still clean the dead DB eventually
1318  * via future invocations of pgstat_vacuum_stat().)
1319  * ----------
1320  */
1321 void
1323 {
1324  PgStat_MsgDropdb msg;
1325 
1327  return;
1328 
1330  msg.m_databaseid = databaseid;
1331  pgstat_send(&msg, sizeof(msg));
1332 }
1333 
1334 
1335 /* ----------
1336  * pgstat_drop_relation() -
1337  *
1338  * Tell the collector that we just dropped a relation.
1339  * (If the message gets lost, we will still clean the dead entry eventually
1340  * via future invocations of pgstat_vacuum_stat().)
1341  *
1342  * Currently not used for lack of any good place to call it; we rely
1343  * entirely on pgstat_vacuum_stat() to clean out stats for dead rels.
1344  * ----------
1345  */
1346 #ifdef NOT_USED
1347 void
1348 pgstat_drop_relation(Oid relid)
1349 {
1350  PgStat_MsgTabpurge msg;
1351  int len;
1352 
1354  return;
1355 
1356  msg.m_tableid[0] = relid;
1357  msg.m_nentries = 1;
1358 
1359  len = offsetof(PgStat_MsgTabpurge, m_tableid[0]) + sizeof(Oid);
1360 
1362  msg.m_databaseid = MyDatabaseId;
1363  pgstat_send(&msg, len);
1364 }
1365 #endif /* NOT_USED */
1366 
1367 
1368 /* ----------
1369  * pgstat_send_connstats() -
1370  *
1371  * Tell the collector about session statistics.
1372  * The parameter "disconnect" will be true when the backend exits.
1373  * "last_report" is the last time we were called (0 if never).
1374  * ----------
1375  */
1376 static void
1377 pgstat_send_connstats(bool disconnect, TimestampTz last_report)
1378 {
1379  PgStat_MsgConn msg;
1380  long secs;
1381  int usecs;
1382 
1384  return;
1385 
1387  msg.m_databaseid = MyDatabaseId;
1388 
1389  /* session time since the last report */
1390  TimestampDifference(((last_report == 0) ? MyStartTimestamp : last_report),
1392  &secs, &usecs);
1393  msg.m_session_time = secs * 1000000 + usecs;
1394 
1396 
1398  pgStatActiveTime = 0;
1399 
1402 
1403  /* report a new session only the first time */
1404  msg.m_count = (last_report == 0) ? 1 : 0;
1405 
1406  pgstat_send(&msg, sizeof(PgStat_MsgConn));
1407 }
1408 
1409 
1410 /* ----------
1411  * pgstat_reset_counters() -
1412  *
1413  * Tell the statistics collector to reset counters for our database.
1414  *
1415  * Permission checking for this function is managed through the normal
1416  * GRANT system.
1417  * ----------
1418  */
1419 void
1421 {
1423 
1425  return;
1426 
1428  msg.m_databaseid = MyDatabaseId;
1429  pgstat_send(&msg, sizeof(msg));
1430 }
1431 
1432 /* ----------
1433  * pgstat_reset_shared_counters() -
1434  *
1435  * Tell the statistics collector to reset cluster-wide shared counters.
1436  *
1437  * Permission checking for this function is managed through the normal
1438  * GRANT system.
1439  * ----------
1440  */
1441 void
1442 pgstat_reset_shared_counters(const char *target)
1443 {
1445 
1447  return;
1448 
1449  if (strcmp(target, "archiver") == 0)
1451  else if (strcmp(target, "bgwriter") == 0)
1453  else if (strcmp(target, "wal") == 0)
1454  msg.m_resettarget = RESET_WAL;
1455  else
1456  ereport(ERROR,
1457  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1458  errmsg("unrecognized reset target: \"%s\"", target),
1459  errhint("Target must be \"archiver\", \"bgwriter\" or \"wal\".")));
1460 
1462  pgstat_send(&msg, sizeof(msg));
1463 }
1464 
1465 /* ----------
1466  * pgstat_reset_single_counter() -
1467  *
1468  * Tell the statistics collector to reset a single counter.
1469  *
1470  * Permission checking for this function is managed through the normal
1471  * GRANT system.
1472  * ----------
1473  */
1474 void
1476 {
1478 
1480  return;
1481 
1483  msg.m_databaseid = MyDatabaseId;
1484  msg.m_resettype = type;
1485  msg.m_objectid = objoid;
1486 
1487  pgstat_send(&msg, sizeof(msg));
1488 }
1489 
1490 /* ----------
1491  * pgstat_reset_slru_counter() -
1492  *
1493  * Tell the statistics collector to reset a single SLRU counter, or all
1494  * SLRU counters (when name is null).
1495  *
1496  * Permission checking for this function is managed through the normal
1497  * GRANT system.
1498  * ----------
1499  */
1500 void
1502 {
1504 
1506  return;
1507 
1509  msg.m_index = (name) ? pgstat_slru_index(name) : -1;
1510 
1511  pgstat_send(&msg, sizeof(msg));
1512 }
1513 
1514 /* ----------
1515  * pgstat_reset_replslot_counter() -
1516  *
1517  * Tell the statistics collector to reset a single replication slot
1518  * counter, or all replication slots counters (when name is null).
1519  *
1520  * Permission checking for this function is managed through the normal
1521  * GRANT system.
1522  * ----------
1523  */
1524 void
1526 {
1528 
1530  return;
1531 
1532  if (name)
1533  {
1534  ReplicationSlot *slot;
1535 
1536  /*
1537  * Check if the slot exits with the given name. It is possible that by
1538  * the time this message is executed the slot is dropped but at least
1539  * this check will ensure that the given name is for a valid slot.
1540  */
1541  LWLockAcquire(ReplicationSlotControlLock, LW_SHARED);
1542  slot = SearchNamedReplicationSlot(name);
1543  LWLockRelease(ReplicationSlotControlLock);
1544 
1545  if (!slot)
1546  ereport(ERROR,
1547  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1548  errmsg("replication slot \"%s\" does not exist",
1549  name)));
1550 
1551  /*
1552  * Nothing to do for physical slots as we collect stats only for
1553  * logical slots.
1554  */
1555  if (SlotIsPhysical(slot))
1556  return;
1557 
1558  strlcpy(msg.m_slotname, name, NAMEDATALEN);
1559  msg.clearall = false;
1560  }
1561  else
1562  msg.clearall = true;
1563 
1565 
1566  pgstat_send(&msg, sizeof(msg));
1567 }
1568 
1569 /* ----------
1570  * pgstat_report_autovac() -
1571  *
1572  * Called from autovacuum.c to report startup of an autovacuum process.
1573  * We are called before InitPostgres is done, so can't rely on MyDatabaseId;
1574  * the db OID must be passed in, instead.
1575  * ----------
1576  */
1577 void
1579 {
1581 
1583  return;
1584 
1586  msg.m_databaseid = dboid;
1588 
1589  pgstat_send(&msg, sizeof(msg));
1590 }
1591 
1592 
1593 /* ---------
1594  * pgstat_report_vacuum() -
1595  *
1596  * Tell the collector about the table we just vacuumed.
1597  * ---------
1598  */
1599 void
1600 pgstat_report_vacuum(Oid tableoid, bool shared,
1601  PgStat_Counter livetuples, PgStat_Counter deadtuples)
1602 {
1603  PgStat_MsgVacuum msg;
1604 
1606  return;
1607 
1609  msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1610  msg.m_tableoid = tableoid;
1613  msg.m_live_tuples = livetuples;
1614  msg.m_dead_tuples = deadtuples;
1615  pgstat_send(&msg, sizeof(msg));
1616 }
1617 
1618 /* --------
1619  * pgstat_report_analyze() -
1620  *
1621  * Tell the collector about the table we just analyzed.
1622  *
1623  * Caller must provide new live- and dead-tuples estimates, as well as a
1624  * flag indicating whether to reset the changes_since_analyze counter.
1625  * --------
1626  */
1627 void
1629  PgStat_Counter livetuples, PgStat_Counter deadtuples,
1630  bool resetcounter)
1631 {
1632  PgStat_MsgAnalyze msg;
1633 
1635  return;
1636 
1637  /*
1638  * Unlike VACUUM, ANALYZE might be running inside a transaction that has
1639  * already inserted and/or deleted rows in the target table. ANALYZE will
1640  * have counted such rows as live or dead respectively. Because we will
1641  * report our counts of such rows at transaction end, we should subtract
1642  * off these counts from what we send to the collector now, else they'll
1643  * be double-counted after commit. (This approach also ensures that the
1644  * collector ends up with the right numbers if we abort instead of
1645  * committing.)
1646  */
1647  if (rel->pgstat_info != NULL)
1648  {
1650 
1651  for (trans = rel->pgstat_info->trans; trans; trans = trans->upper)
1652  {
1653  livetuples -= trans->tuples_inserted - trans->tuples_deleted;
1654  deadtuples -= trans->tuples_updated + trans->tuples_deleted;
1655  }
1656  /* count stuff inserted by already-aborted subxacts, too */
1657  deadtuples -= rel->pgstat_info->t_counts.t_delta_dead_tuples;
1658  /* Since ANALYZE's counts are estimates, we could have underflowed */
1659  livetuples = Max(livetuples, 0);
1660  deadtuples = Max(deadtuples, 0);
1661  }
1662 
1664  msg.m_databaseid = rel->rd_rel->relisshared ? InvalidOid : MyDatabaseId;
1665  msg.m_tableoid = RelationGetRelid(rel);
1667  msg.m_resetcounter = resetcounter;
1669  msg.m_live_tuples = livetuples;
1670  msg.m_dead_tuples = deadtuples;
1671  pgstat_send(&msg, sizeof(msg));
1672 }
1673 
1674 /* --------
1675  * pgstat_report_recovery_conflict() -
1676  *
1677  * Tell the collector about a Hot Standby recovery conflict.
1678  * --------
1679  */
1680 void
1682 {
1684 
1686  return;
1687 
1689  msg.m_databaseid = MyDatabaseId;
1690  msg.m_reason = reason;
1691  pgstat_send(&msg, sizeof(msg));
1692 }
1693 
1694 /* --------
1695  * pgstat_report_deadlock() -
1696  *
1697  * Tell the collector about a deadlock detected.
1698  * --------
1699  */
1700 void
1702 {
1703  PgStat_MsgDeadlock msg;
1704 
1706  return;
1707 
1709  msg.m_databaseid = MyDatabaseId;
1710  pgstat_send(&msg, sizeof(msg));
1711 }
1712 
1713 
1714 
1715 /* --------
1716  * pgstat_report_checksum_failures_in_db() -
1717  *
1718  * Tell the collector about one or more checksum failures.
1719  * --------
1720  */
1721 void
1723 {
1725 
1727  return;
1728 
1730  msg.m_databaseid = dboid;
1731  msg.m_failurecount = failurecount;
1733 
1734  pgstat_send(&msg, sizeof(msg));
1735 }
1736 
1737 /* --------
1738  * pgstat_report_checksum_failure() -
1739  *
1740  * Tell the collector about a checksum failure.
1741  * --------
1742  */
1743 void
1745 {
1747 }
1748 
1749 /* --------
1750  * pgstat_report_tempfile() -
1751  *
1752  * Tell the collector about a temporary file.
1753  * --------
1754  */
1755 void
1756 pgstat_report_tempfile(size_t filesize)
1757 {
1758  PgStat_MsgTempFile msg;
1759 
1761  return;
1762 
1764  msg.m_databaseid = MyDatabaseId;
1765  msg.m_filesize = filesize;
1766  pgstat_send(&msg, sizeof(msg));
1767 }
1768 
1769 /* ----------
1770  * pgstat_report_replslot() -
1771  *
1772  * Tell the collector about replication slot statistics.
1773  * ----------
1774  */
1775 void
1776 pgstat_report_replslot(const char *slotname, int spilltxns, int spillcount,
1777  int spillbytes, int streamtxns, int streamcount, int streambytes)
1778 {
1779  PgStat_MsgReplSlot msg;
1780 
1781  /*
1782  * Prepare and send the message
1783  */
1785  strlcpy(msg.m_slotname, slotname, NAMEDATALEN);
1786  msg.m_drop = false;
1787  msg.m_spill_txns = spilltxns;
1788  msg.m_spill_count = spillcount;
1789  msg.m_spill_bytes = spillbytes;
1790  msg.m_stream_txns = streamtxns;
1791  msg.m_stream_count = streamcount;
1792  msg.m_stream_bytes = streambytes;
1793  pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
1794 }
1795 
1796 /* ----------
1797  * pgstat_report_replslot_drop() -
1798  *
1799  * Tell the collector about dropping the replication slot.
1800  * ----------
1801  */
1802 void
1803 pgstat_report_replslot_drop(const char *slotname)
1804 {
1805  PgStat_MsgReplSlot msg;
1806 
1808  strlcpy(msg.m_slotname, slotname, NAMEDATALEN);
1809  msg.m_drop = true;
1810  pgstat_send(&msg, sizeof(PgStat_MsgReplSlot));
1811 }
1812 
1813 /* ----------
1814  * pgstat_ping() -
1815  *
1816  * Send some junk data to the collector to increase traffic.
1817  * ----------
1818  */
1819 void
1821 {
1822  PgStat_MsgDummy msg;
1823 
1825  return;
1826 
1828  pgstat_send(&msg, sizeof(msg));
1829 }
1830 
1831 /* ----------
1832  * pgstat_send_inquiry() -
1833  *
1834  * Notify collector that we need fresh data.
1835  * ----------
1836  */
1837 static void
1838 pgstat_send_inquiry(TimestampTz clock_time, TimestampTz cutoff_time, Oid databaseid)
1839 {
1840  PgStat_MsgInquiry msg;
1841 
1843  msg.clock_time = clock_time;
1844  msg.cutoff_time = cutoff_time;
1845  msg.databaseid = databaseid;
1846  pgstat_send(&msg, sizeof(msg));
1847 }
1848 
1849 
1850 /*
1851  * Initialize function call usage data.
1852  * Called by the executor before invoking a function.
1853  */
1854 void
1857 {
1858  PgStat_BackendFunctionEntry *htabent;
1859  bool found;
1860 
1861  if (pgstat_track_functions <= fcinfo->flinfo->fn_stats)
1862  {
1863  /* stats not wanted */
1864  fcu->fs = NULL;
1865  return;
1866  }
1867 
1868  if (!pgStatFunctions)
1869  {
1870  /* First time through - initialize function stat table */
1871  HASHCTL hash_ctl;
1872 
1873  hash_ctl.keysize = sizeof(Oid);
1874  hash_ctl.entrysize = sizeof(PgStat_BackendFunctionEntry);
1875  pgStatFunctions = hash_create("Function stat entries",
1877  &hash_ctl,
1878  HASH_ELEM | HASH_BLOBS);
1879  }
1880 
1881  /* Get the stats entry for this function, create if necessary */
1882  htabent = hash_search(pgStatFunctions, &fcinfo->flinfo->fn_oid,
1883  HASH_ENTER, &found);
1884  if (!found)
1885  MemSet(&htabent->f_counts, 0, sizeof(PgStat_FunctionCounts));
1886 
1887  fcu->fs = &htabent->f_counts;
1888 
1889  /* save stats for this function, later used to compensate for recursion */
1890  fcu->save_f_total_time = htabent->f_counts.f_total_time;
1891 
1892  /* save current backend-wide total time */
1893  fcu->save_total = total_func_time;
1894 
1895  /* get clock time as of function start */
1897 }
1898 
1899 /*
1900  * find_funcstat_entry - find any existing PgStat_BackendFunctionEntry entry
1901  * for specified function
1902  *
1903  * If no entry, return NULL, don't create a new one
1904  */
1907 {
1908  if (pgStatFunctions == NULL)
1909  return NULL;
1910 
1911  return (PgStat_BackendFunctionEntry *) hash_search(pgStatFunctions,
1912  (void *) &func_id,
1913  HASH_FIND, NULL);
1914 }
1915 
1916 /*
1917  * Calculate function call usage and update stat counters.
1918  * Called by the executor after invoking a function.
1919  *
1920  * In the case of a set-returning function that runs in value-per-call mode,
1921  * we will see multiple pgstat_init_function_usage/pgstat_end_function_usage
1922  * calls for what the user considers a single call of the function. The
1923  * finalize flag should be TRUE on the last call.
1924  */
1925 void
1927 {
1928  PgStat_FunctionCounts *fs = fcu->fs;
1929  instr_time f_total;
1930  instr_time f_others;
1931  instr_time f_self;
1932 
1933  /* stats not wanted? */
1934  if (fs == NULL)
1935  return;
1936 
1937  /* total elapsed time in this function call */
1938  INSTR_TIME_SET_CURRENT(f_total);
1939  INSTR_TIME_SUBTRACT(f_total, fcu->f_start);
1940 
1941  /* self usage: elapsed minus anything already charged to other calls */
1942  f_others = total_func_time;
1943  INSTR_TIME_SUBTRACT(f_others, fcu->save_total);
1944  f_self = f_total;
1945  INSTR_TIME_SUBTRACT(f_self, f_others);
1946 
1947  /* update backend-wide total time */
1949 
1950  /*
1951  * Compute the new f_total_time as the total elapsed time added to the
1952  * pre-call value of f_total_time. This is necessary to avoid
1953  * double-counting any time taken by recursive calls of myself. (We do
1954  * not need any similar kluge for self time, since that already excludes
1955  * any recursive calls.)
1956  */
1957  INSTR_TIME_ADD(f_total, fcu->save_f_total_time);
1958 
1959  /* update counters in function stats table */
1960  if (finalize)
1961  fs->f_numcalls++;
1962  fs->f_total_time = f_total;
1963  INSTR_TIME_ADD(fs->f_self_time, f_self);
1964 
1965  /* indicate that we have something to send */
1966  have_function_stats = true;
1967 }
1968 
1969 
1970 /* ----------
1971  * pgstat_initstats() -
1972  *
1973  * Initialize a relcache entry to count access statistics.
1974  * Called whenever a relation is opened.
1975  *
1976  * We assume that a relcache entry's pgstat_info field is zeroed by
1977  * relcache.c when the relcache entry is made; thereafter it is long-lived
1978  * data. We can avoid repeated searches of the TabStatus arrays when the
1979  * same relation is touched repeatedly within a transaction.
1980  * ----------
1981  */
1982 void
1984 {
1985  Oid rel_id = rel->rd_id;
1986  char relkind = rel->rd_rel->relkind;
1987 
1988  /* We only count stats for things that have storage */
1989  if (!RELKIND_HAS_STORAGE(relkind))
1990  {
1991  rel->pgstat_info = NULL;
1992  return;
1993  }
1994 
1996  {
1997  /* We're not counting at all */
1998  rel->pgstat_info = NULL;
1999  return;
2000  }
2001 
2002  /*
2003  * If we already set up this relation in the current transaction, nothing
2004  * to do.
2005  */
2006  if (rel->pgstat_info != NULL &&
2007  rel->pgstat_info->t_id == rel_id)
2008  return;
2009 
2010  /* Else find or make the PgStat_TableStatus entry, and update link */
2011  rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
2012 }
2013 
2014 /*
2015  * get_tabstat_entry - find or create a PgStat_TableStatus entry for rel
2016  */
2017 static PgStat_TableStatus *
2018 get_tabstat_entry(Oid rel_id, bool isshared)
2019 {
2020  TabStatHashEntry *hash_entry;
2021  PgStat_TableStatus *entry;
2022  TabStatusArray *tsa;
2023  bool found;
2024 
2025  /*
2026  * Create hash table if we don't have it already.
2027  */
2028  if (pgStatTabHash == NULL)
2029  {
2030  HASHCTL ctl;
2031 
2032  ctl.keysize = sizeof(Oid);
2033  ctl.entrysize = sizeof(TabStatHashEntry);
2034 
2035  pgStatTabHash = hash_create("pgstat TabStatusArray lookup hash table",
2037  &ctl,
2038  HASH_ELEM | HASH_BLOBS);
2039  }
2040 
2041  /*
2042  * Find an entry or create a new one.
2043  */
2044  hash_entry = hash_search(pgStatTabHash, &rel_id, HASH_ENTER, &found);
2045  if (!found)
2046  {
2047  /* initialize new entry with null pointer */
2048  hash_entry->tsa_entry = NULL;
2049  }
2050 
2051  /*
2052  * If entry is already valid, we're done.
2053  */
2054  if (hash_entry->tsa_entry)
2055  return hash_entry->tsa_entry;
2056 
2057  /*
2058  * Locate the first pgStatTabList entry with free space, making a new list
2059  * entry if needed. Note that we could get an OOM failure here, but if so
2060  * we have left the hashtable and the list in a consistent state.
2061  */
2062  if (pgStatTabList == NULL)
2063  {
2064  /* Set up first pgStatTabList entry */
2065  pgStatTabList = (TabStatusArray *)
2067  sizeof(TabStatusArray));
2068  }
2069 
2070  tsa = pgStatTabList;
2071  while (tsa->tsa_used >= TABSTAT_QUANTUM)
2072  {
2073  if (tsa->tsa_next == NULL)
2074  tsa->tsa_next = (TabStatusArray *)
2076  sizeof(TabStatusArray));
2077  tsa = tsa->tsa_next;
2078  }
2079 
2080  /*
2081  * Allocate a PgStat_TableStatus entry within this list entry. We assume
2082  * the entry was already zeroed, either at creation or after last use.
2083  */
2084  entry = &tsa->tsa_entries[tsa->tsa_used++];
2085  entry->t_id = rel_id;
2086  entry->t_shared = isshared;
2087 
2088  /*
2089  * Now we can fill the entry in pgStatTabHash.
2090  */
2091  hash_entry->tsa_entry = entry;
2092 
2093  return entry;
2094 }
2095 
2096 /*
2097  * find_tabstat_entry - find any existing PgStat_TableStatus entry for rel
2098  *
2099  * If no entry, return NULL, don't create a new one
2100  *
2101  * Note: if we got an error in the most recent execution of pgstat_report_stat,
2102  * it's possible that an entry exists but there's no hashtable entry for it.
2103  * That's okay, we'll treat this case as "doesn't exist".
2104  */
2107 {
2108  TabStatHashEntry *hash_entry;
2109 
2110  /* If hashtable doesn't exist, there are no entries at all */
2111  if (!pgStatTabHash)
2112  return NULL;
2113 
2114  hash_entry = hash_search(pgStatTabHash, &rel_id, HASH_FIND, NULL);
2115  if (!hash_entry)
2116  return NULL;
2117 
2118  /* Note that this step could also return NULL, but that's correct */
2119  return hash_entry->tsa_entry;
2120 }
2121 
2122 /*
2123  * get_tabstat_stack_level - add a new (sub)transaction stack entry if needed
2124  */
2125 static PgStat_SubXactStatus *
2127 {
2128  PgStat_SubXactStatus *xact_state;
2129 
2130  xact_state = pgStatXactStack;
2131  if (xact_state == NULL || xact_state->nest_level != nest_level)
2132  {
2133  xact_state = (PgStat_SubXactStatus *)
2135  sizeof(PgStat_SubXactStatus));
2136  xact_state->nest_level = nest_level;
2137  xact_state->prev = pgStatXactStack;
2138  xact_state->first = NULL;
2139  pgStatXactStack = xact_state;
2140  }
2141  return xact_state;
2142 }
2143 
2144 /*
2145  * add_tabstat_xact_level - add a new (sub)transaction state record
2146  */
2147 static void
2148 add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
2149 {
2150  PgStat_SubXactStatus *xact_state;
2152 
2153  /*
2154  * If this is the first rel to be modified at the current nest level, we
2155  * first have to push a transaction stack entry.
2156  */
2157  xact_state = get_tabstat_stack_level(nest_level);
2158 
2159  /* Now make a per-table stack entry */
2160  trans = (PgStat_TableXactStatus *)
2162  sizeof(PgStat_TableXactStatus));
2163  trans->nest_level = nest_level;
2164  trans->upper = pgstat_info->trans;
2165  trans->parent = pgstat_info;
2166  trans->next = xact_state->first;
2167  xact_state->first = trans;
2168  pgstat_info->trans = trans;
2169 }
2170 
2171 /*
2172  * pgstat_count_heap_insert - count a tuple insertion of n tuples
2173  */
2174 void
2176 {
2177  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2178 
2179  if (pgstat_info != NULL)
2180  {
2181  /* We have to log the effect at the proper transactional level */
2182  int nest_level = GetCurrentTransactionNestLevel();
2183 
2184  if (pgstat_info->trans == NULL ||
2185  pgstat_info->trans->nest_level != nest_level)
2186  add_tabstat_xact_level(pgstat_info, nest_level);
2187 
2188  pgstat_info->trans->tuples_inserted += n;
2189  }
2190 }
2191 
2192 /*
2193  * pgstat_count_heap_update - count a tuple update
2194  */
2195 void
2197 {
2198  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2199 
2200  if (pgstat_info != NULL)
2201  {
2202  /* We have to log the effect at the proper transactional level */
2203  int nest_level = GetCurrentTransactionNestLevel();
2204 
2205  if (pgstat_info->trans == NULL ||
2206  pgstat_info->trans->nest_level != nest_level)
2207  add_tabstat_xact_level(pgstat_info, nest_level);
2208 
2209  pgstat_info->trans->tuples_updated++;
2210 
2211  /* t_tuples_hot_updated is nontransactional, so just advance it */
2212  if (hot)
2213  pgstat_info->t_counts.t_tuples_hot_updated++;
2214  }
2215 }
2216 
2217 /*
2218  * pgstat_count_heap_delete - count a tuple deletion
2219  */
2220 void
2222 {
2223  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2224 
2225  if (pgstat_info != NULL)
2226  {
2227  /* We have to log the effect at the proper transactional level */
2228  int nest_level = GetCurrentTransactionNestLevel();
2229 
2230  if (pgstat_info->trans == NULL ||
2231  pgstat_info->trans->nest_level != nest_level)
2232  add_tabstat_xact_level(pgstat_info, nest_level);
2233 
2234  pgstat_info->trans->tuples_deleted++;
2235  }
2236 }
2237 
2238 /*
2239  * pgstat_truncate_save_counters
2240  *
2241  * Whenever a table is truncated, we save its i/u/d counters so that they can
2242  * be cleared, and if the (sub)xact that executed the truncate later aborts,
2243  * the counters can be restored to the saved (pre-truncate) values. Note we do
2244  * this on the first truncate in any particular subxact level only.
2245  */
2246 static void
2248 {
2249  if (!trans->truncated)
2250  {
2251  trans->inserted_pre_trunc = trans->tuples_inserted;
2252  trans->updated_pre_trunc = trans->tuples_updated;
2253  trans->deleted_pre_trunc = trans->tuples_deleted;
2254  trans->truncated = true;
2255  }
2256 }
2257 
2258 /*
2259  * pgstat_truncate_restore_counters - restore counters when a truncate aborts
2260  */
2261 static void
2263 {
2264  if (trans->truncated)
2265  {
2266  trans->tuples_inserted = trans->inserted_pre_trunc;
2267  trans->tuples_updated = trans->updated_pre_trunc;
2268  trans->tuples_deleted = trans->deleted_pre_trunc;
2269  }
2270 }
2271 
2272 /*
2273  * pgstat_count_truncate - update tuple counters due to truncate
2274  */
2275 void
2277 {
2278  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2279 
2280  if (pgstat_info != NULL)
2281  {
2282  /* We have to log the effect at the proper transactional level */
2283  int nest_level = GetCurrentTransactionNestLevel();
2284 
2285  if (pgstat_info->trans == NULL ||
2286  pgstat_info->trans->nest_level != nest_level)
2287  add_tabstat_xact_level(pgstat_info, nest_level);
2288 
2289  pgstat_truncate_save_counters(pgstat_info->trans);
2290  pgstat_info->trans->tuples_inserted = 0;
2291  pgstat_info->trans->tuples_updated = 0;
2292  pgstat_info->trans->tuples_deleted = 0;
2293  }
2294 }
2295 
2296 /*
2297  * pgstat_update_heap_dead_tuples - update dead-tuples count
2298  *
2299  * The semantics of this are that we are reporting the nontransactional
2300  * recovery of "delta" dead tuples; so t_delta_dead_tuples decreases
2301  * rather than increasing, and the change goes straight into the per-table
2302  * counter, not into transactional state.
2303  */
2304 void
2306 {
2307  PgStat_TableStatus *pgstat_info = rel->pgstat_info;
2308 
2309  if (pgstat_info != NULL)
2310  pgstat_info->t_counts.t_delta_dead_tuples -= delta;
2311 }
2312 
2313 
2314 /* ----------
2315  * AtEOXact_PgStat
2316  *
2317  * Called from access/transam/xact.c at top-level transaction commit/abort.
2318  * ----------
2319  */
2320 void
2321 AtEOXact_PgStat(bool isCommit, bool parallel)
2322 {
2323  PgStat_SubXactStatus *xact_state;
2324 
2325  /* Don't count parallel worker transaction stats */
2326  if (!parallel)
2327  {
2328  /*
2329  * Count transaction commit or abort. (We use counters, not just
2330  * bools, in case the reporting message isn't sent right away.)
2331  */
2332  if (isCommit)
2333  pgStatXactCommit++;
2334  else
2336  }
2337 
2338  /*
2339  * Transfer transactional insert/update counts into the base tabstat
2340  * entries. We don't bother to free any of the transactional state, since
2341  * it's all in TopTransactionContext and will go away anyway.
2342  */
2343  xact_state = pgStatXactStack;
2344  if (xact_state != NULL)
2345  {
2347 
2348  Assert(xact_state->nest_level == 1);
2349  Assert(xact_state->prev == NULL);
2350  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2351  {
2352  PgStat_TableStatus *tabstat;
2353 
2354  Assert(trans->nest_level == 1);
2355  Assert(trans->upper == NULL);
2356  tabstat = trans->parent;
2357  Assert(tabstat->trans == trans);
2358  /* restore pre-truncate stats (if any) in case of aborted xact */
2359  if (!isCommit)
2361  /* count attempted actions regardless of commit/abort */
2362  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
2363  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
2364  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
2365  if (isCommit)
2366  {
2367  tabstat->t_counts.t_truncated = trans->truncated;
2368  if (trans->truncated)
2369  {
2370  /* forget live/dead stats seen by backend thus far */
2371  tabstat->t_counts.t_delta_live_tuples = 0;
2372  tabstat->t_counts.t_delta_dead_tuples = 0;
2373  }
2374  /* insert adds a live tuple, delete removes one */
2375  tabstat->t_counts.t_delta_live_tuples +=
2376  trans->tuples_inserted - trans->tuples_deleted;
2377  /* update and delete each create a dead tuple */
2378  tabstat->t_counts.t_delta_dead_tuples +=
2379  trans->tuples_updated + trans->tuples_deleted;
2380  /* insert, update, delete each count as one change event */
2381  tabstat->t_counts.t_changed_tuples +=
2382  trans->tuples_inserted + trans->tuples_updated +
2383  trans->tuples_deleted;
2384  }
2385  else
2386  {
2387  /* inserted tuples are dead, deleted tuples are unaffected */
2388  tabstat->t_counts.t_delta_dead_tuples +=
2389  trans->tuples_inserted + trans->tuples_updated;
2390  /* an aborted xact generates no changed_tuple events */
2391  }
2392  tabstat->trans = NULL;
2393  }
2394  }
2395  pgStatXactStack = NULL;
2396 
2397  /* Make sure any stats snapshot is thrown away */
2399 }
2400 
2401 /* ----------
2402  * AtEOSubXact_PgStat
2403  *
2404  * Called from access/transam/xact.c at subtransaction commit/abort.
2405  * ----------
2406  */
2407 void
2408 AtEOSubXact_PgStat(bool isCommit, int nestDepth)
2409 {
2410  PgStat_SubXactStatus *xact_state;
2411 
2412  /*
2413  * Transfer transactional insert/update counts into the next higher
2414  * subtransaction state.
2415  */
2416  xact_state = pgStatXactStack;
2417  if (xact_state != NULL &&
2418  xact_state->nest_level >= nestDepth)
2419  {
2421  PgStat_TableXactStatus *next_trans;
2422 
2423  /* delink xact_state from stack immediately to simplify reuse case */
2424  pgStatXactStack = xact_state->prev;
2425 
2426  for (trans = xact_state->first; trans != NULL; trans = next_trans)
2427  {
2428  PgStat_TableStatus *tabstat;
2429 
2430  next_trans = trans->next;
2431  Assert(trans->nest_level == nestDepth);
2432  tabstat = trans->parent;
2433  Assert(tabstat->trans == trans);
2434  if (isCommit)
2435  {
2436  if (trans->upper && trans->upper->nest_level == nestDepth - 1)
2437  {
2438  if (trans->truncated)
2439  {
2440  /* propagate the truncate status one level up */
2442  /* replace upper xact stats with ours */
2443  trans->upper->tuples_inserted = trans->tuples_inserted;
2444  trans->upper->tuples_updated = trans->tuples_updated;
2445  trans->upper->tuples_deleted = trans->tuples_deleted;
2446  }
2447  else
2448  {
2449  trans->upper->tuples_inserted += trans->tuples_inserted;
2450  trans->upper->tuples_updated += trans->tuples_updated;
2451  trans->upper->tuples_deleted += trans->tuples_deleted;
2452  }
2453  tabstat->trans = trans->upper;
2454  pfree(trans);
2455  }
2456  else
2457  {
2458  /*
2459  * When there isn't an immediate parent state, we can just
2460  * reuse the record instead of going through a
2461  * palloc/pfree pushup (this works since it's all in
2462  * TopTransactionContext anyway). We have to re-link it
2463  * into the parent level, though, and that might mean
2464  * pushing a new entry into the pgStatXactStack.
2465  */
2466  PgStat_SubXactStatus *upper_xact_state;
2467 
2468  upper_xact_state = get_tabstat_stack_level(nestDepth - 1);
2469  trans->next = upper_xact_state->first;
2470  upper_xact_state->first = trans;
2471  trans->nest_level = nestDepth - 1;
2472  }
2473  }
2474  else
2475  {
2476  /*
2477  * On abort, update top-level tabstat counts, then forget the
2478  * subtransaction
2479  */
2480 
2481  /* first restore values obliterated by truncate */
2483  /* count attempted actions regardless of commit/abort */
2484  tabstat->t_counts.t_tuples_inserted += trans->tuples_inserted;
2485  tabstat->t_counts.t_tuples_updated += trans->tuples_updated;
2486  tabstat->t_counts.t_tuples_deleted += trans->tuples_deleted;
2487  /* inserted tuples are dead, deleted tuples are unaffected */
2488  tabstat->t_counts.t_delta_dead_tuples +=
2489  trans->tuples_inserted + trans->tuples_updated;
2490  tabstat->trans = trans->upper;
2491  pfree(trans);
2492  }
2493  }
2494  pfree(xact_state);
2495  }
2496 }
2497 
2498 
2499 /*
2500  * AtPrepare_PgStat
2501  * Save the transactional stats state at 2PC transaction prepare.
2502  *
2503  * In this phase we just generate 2PC records for all the pending
2504  * transaction-dependent stats work.
2505  */
2506 void
2508 {
2509  PgStat_SubXactStatus *xact_state;
2510 
2511  xact_state = pgStatXactStack;
2512  if (xact_state != NULL)
2513  {
2515 
2516  Assert(xact_state->nest_level == 1);
2517  Assert(xact_state->prev == NULL);
2518  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2519  {
2520  PgStat_TableStatus *tabstat;
2521  TwoPhasePgStatRecord record;
2522 
2523  Assert(trans->nest_level == 1);
2524  Assert(trans->upper == NULL);
2525  tabstat = trans->parent;
2526  Assert(tabstat->trans == trans);
2527 
2528  record.tuples_inserted = trans->tuples_inserted;
2529  record.tuples_updated = trans->tuples_updated;
2530  record.tuples_deleted = trans->tuples_deleted;
2531  record.inserted_pre_trunc = trans->inserted_pre_trunc;
2532  record.updated_pre_trunc = trans->updated_pre_trunc;
2533  record.deleted_pre_trunc = trans->deleted_pre_trunc;
2534  record.t_id = tabstat->t_id;
2535  record.t_shared = tabstat->t_shared;
2536  record.t_truncated = trans->truncated;
2537 
2539  &record, sizeof(TwoPhasePgStatRecord));
2540  }
2541  }
2542 }
2543 
2544 /*
2545  * PostPrepare_PgStat
2546  * Clean up after successful PREPARE.
2547  *
2548  * All we need do here is unlink the transaction stats state from the
2549  * nontransactional state. The nontransactional action counts will be
2550  * reported to the stats collector immediately, while the effects on live
2551  * and dead tuple counts are preserved in the 2PC state file.
2552  *
2553  * Note: AtEOXact_PgStat is not called during PREPARE.
2554  */
2555 void
2557 {
2558  PgStat_SubXactStatus *xact_state;
2559 
2560  /*
2561  * We don't bother to free any of the transactional state, since it's all
2562  * in TopTransactionContext and will go away anyway.
2563  */
2564  xact_state = pgStatXactStack;
2565  if (xact_state != NULL)
2566  {
2568 
2569  for (trans = xact_state->first; trans != NULL; trans = trans->next)
2570  {
2571  PgStat_TableStatus *tabstat;
2572 
2573  tabstat = trans->parent;
2574  tabstat->trans = NULL;
2575  }
2576  }
2577  pgStatXactStack = NULL;
2578 
2579  /* Make sure any stats snapshot is thrown away */
2581 }
2582 
2583 /*
2584  * 2PC processing routine for COMMIT PREPARED case.
2585  *
2586  * Load the saved counts into our local pgstats state.
2587  */
2588 void
2590  void *recdata, uint32 len)
2591 {
2592  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2593  PgStat_TableStatus *pgstat_info;
2594 
2595  /* Find or create a tabstat entry for the rel */
2596  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2597 
2598  /* Same math as in AtEOXact_PgStat, commit case */
2599  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2600  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2601  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2602  pgstat_info->t_counts.t_truncated = rec->t_truncated;
2603  if (rec->t_truncated)
2604  {
2605  /* forget live/dead stats seen by backend thus far */
2606  pgstat_info->t_counts.t_delta_live_tuples = 0;
2607  pgstat_info->t_counts.t_delta_dead_tuples = 0;
2608  }
2609  pgstat_info->t_counts.t_delta_live_tuples +=
2610  rec->tuples_inserted - rec->tuples_deleted;
2611  pgstat_info->t_counts.t_delta_dead_tuples +=
2612  rec->tuples_updated + rec->tuples_deleted;
2613  pgstat_info->t_counts.t_changed_tuples +=
2614  rec->tuples_inserted + rec->tuples_updated +
2615  rec->tuples_deleted;
2616 }
2617 
2618 /*
2619  * 2PC processing routine for ROLLBACK PREPARED case.
2620  *
2621  * Load the saved counts into our local pgstats state, but treat them
2622  * as aborted.
2623  */
2624 void
2626  void *recdata, uint32 len)
2627 {
2628  TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
2629  PgStat_TableStatus *pgstat_info;
2630 
2631  /* Find or create a tabstat entry for the rel */
2632  pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
2633 
2634  /* Same math as in AtEOXact_PgStat, abort case */
2635  if (rec->t_truncated)
2636  {
2637  rec->tuples_inserted = rec->inserted_pre_trunc;
2638  rec->tuples_updated = rec->updated_pre_trunc;
2639  rec->tuples_deleted = rec->deleted_pre_trunc;
2640  }
2641  pgstat_info->t_counts.t_tuples_inserted += rec->tuples_inserted;
2642  pgstat_info->t_counts.t_tuples_updated += rec->tuples_updated;
2643  pgstat_info->t_counts.t_tuples_deleted += rec->tuples_deleted;
2644  pgstat_info->t_counts.t_delta_dead_tuples +=
2645  rec->tuples_inserted + rec->tuples_updated;
2646 }
2647 
2648 
2649 /* ----------
2650  * pgstat_fetch_stat_dbentry() -
2651  *
2652  * Support function for the SQL-callable pgstat* functions. Returns
2653  * the collected statistics for one database or NULL. NULL doesn't mean
2654  * that the database doesn't exist, it is just not yet known by the
2655  * collector, so the caller is better off to report ZERO instead.
2656  * ----------
2657  */
2660 {
2661  /*
2662  * If not done for this transaction, read the statistics collector stats
2663  * file into some hash tables.
2664  */
2666 
2667  /*
2668  * Lookup the requested database; return NULL if not found
2669  */
2670  return (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2671  (void *) &dbid,
2672  HASH_FIND, NULL);
2673 }
2674 
2675 
2676 /* ----------
2677  * pgstat_fetch_stat_tabentry() -
2678  *
2679  * Support function for the SQL-callable pgstat* functions. Returns
2680  * the collected statistics for one table or NULL. NULL doesn't mean
2681  * that the table doesn't exist, it is just not yet known by the
2682  * collector, so the caller is better off to report ZERO instead.
2683  * ----------
2684  */
2687 {
2688  Oid dbid;
2689  PgStat_StatDBEntry *dbentry;
2690  PgStat_StatTabEntry *tabentry;
2691 
2692  /*
2693  * If not done for this transaction, read the statistics collector stats
2694  * file into some hash tables.
2695  */
2697 
2698  /*
2699  * Lookup our database, then look in its table hash table.
2700  */
2701  dbid = MyDatabaseId;
2702  dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2703  (void *) &dbid,
2704  HASH_FIND, NULL);
2705  if (dbentry != NULL && dbentry->tables != NULL)
2706  {
2707  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2708  (void *) &relid,
2709  HASH_FIND, NULL);
2710  if (tabentry)
2711  return tabentry;
2712  }
2713 
2714  /*
2715  * If we didn't find it, maybe it's a shared table.
2716  */
2717  dbid = InvalidOid;
2718  dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2719  (void *) &dbid,
2720  HASH_FIND, NULL);
2721  if (dbentry != NULL && dbentry->tables != NULL)
2722  {
2723  tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2724  (void *) &relid,
2725  HASH_FIND, NULL);
2726  if (tabentry)
2727  return tabentry;
2728  }
2729 
2730  return NULL;
2731 }
2732 
2733 
2734 /* ----------
2735  * pgstat_fetch_stat_funcentry() -
2736  *
2737  * Support function for the SQL-callable pgstat* functions. Returns
2738  * the collected statistics for one function or NULL.
2739  * ----------
2740  */
2743 {
2744  PgStat_StatDBEntry *dbentry;
2745  PgStat_StatFuncEntry *funcentry = NULL;
2746 
2747  /* load the stats file if needed */
2749 
2750  /* Lookup our database, then find the requested function. */
2752  if (dbentry != NULL && dbentry->functions != NULL)
2753  {
2754  funcentry = (PgStat_StatFuncEntry *) hash_search(dbentry->functions,
2755  (void *) &func_id,
2756  HASH_FIND, NULL);
2757  }
2758 
2759  return funcentry;
2760 }
2761 
2762 
2763 /* ----------
2764  * pgstat_fetch_stat_beentry() -
2765  *
2766  * Support function for the SQL-callable pgstat* functions. Returns
2767  * our local copy of the current-activity entry for one backend.
2768  *
2769  * NB: caller is responsible for a check if the user is permitted to see
2770  * this info (especially the querystring).
2771  * ----------
2772  */
2775 {
2777 
2778  if (beid < 1 || beid > localNumBackends)
2779  return NULL;
2780 
2781  return &localBackendStatusTable[beid - 1].backendStatus;
2782 }
2783 
2784 
2785 /* ----------
2786  * pgstat_fetch_stat_local_beentry() -
2787  *
2788  * Like pgstat_fetch_stat_beentry() but with locally computed additions (like
2789  * xid and xmin values of the backend)
2790  *
2791  * NB: caller is responsible for a check if the user is permitted to see
2792  * this info (especially the querystring).
2793  * ----------
2794  */
2797 {
2799 
2800  if (beid < 1 || beid > localNumBackends)
2801  return NULL;
2802 
2803  return &localBackendStatusTable[beid - 1];
2804 }
2805 
2806 
2807 /* ----------
2808  * pgstat_fetch_stat_numbackends() -
2809  *
2810  * Support function for the SQL-callable pgstat* functions. Returns
2811  * the maximum current backend id.
2812  * ----------
2813  */
2814 int
2816 {
2818 
2819  return localNumBackends;
2820 }
2821 
2822 /*
2823  * ---------
2824  * pgstat_fetch_stat_archiver() -
2825  *
2826  * Support function for the SQL-callable pgstat* functions. Returns
2827  * a pointer to the archiver statistics struct.
2828  * ---------
2829  */
2832 {
2834 
2835  return &archiverStats;
2836 }
2837 
2838 
2839 /*
2840  * ---------
2841  * pgstat_fetch_global() -
2842  *
2843  * Support function for the SQL-callable pgstat* functions. Returns
2844  * a pointer to the global statistics struct.
2845  * ---------
2846  */
2849 {
2851 
2852  return &globalStats;
2853 }
2854 
2855 /*
2856  * ---------
2857  * pgstat_fetch_stat_wal() -
2858  *
2859  * Support function for the SQL-callable pgstat* functions. Returns
2860  * a pointer to the WAL statistics struct.
2861  * ---------
2862  */
2865 {
2867 
2868  return &walStats;
2869 }
2870 
2871 /*
2872  * ---------
2873  * pgstat_fetch_slru() -
2874  *
2875  * Support function for the SQL-callable pgstat* functions. Returns
2876  * a pointer to the slru statistics struct.
2877  * ---------
2878  */
2881 {
2883 
2884  return slruStats;
2885 }
2886 
2887 /*
2888  * ---------
2889  * pgstat_fetch_replslot() -
2890  *
2891  * Support function for the SQL-callable pgstat* functions. Returns
2892  * a pointer to the replication slot statistics struct and sets the
2893  * number of entries in nslots_p.
2894  * ---------
2895  */
2898 {
2900 
2901  *nslots_p = nReplSlotStats;
2902  return replSlotStats;
2903 }
2904 
2905 /* ------------------------------------------------------------
2906  * Functions for management of the shared-memory PgBackendStatus array
2907  * ------------------------------------------------------------
2908  */
2909 
2912 static char *BackendAppnameBuffer = NULL;
2913 static char *BackendClientHostnameBuffer = NULL;
2914 static char *BackendActivityBuffer = NULL;
2916 #ifdef USE_SSL
2917 static PgBackendSSLStatus *BackendSslStatusBuffer = NULL;
2918 #endif
2919 #ifdef ENABLE_GSS
2920 static PgBackendGSSStatus *BackendGssStatusBuffer = NULL;
2921 #endif
2922 
2923 
2924 /*
2925  * Report shared-memory space needed by CreateSharedBackendStatus.
2926  */
2927 Size
2929 {
2930  Size size;
2931 
2932  /* BackendStatusArray: */
2933  size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
2934  /* BackendAppnameBuffer: */
2935  size = add_size(size,
2937  /* BackendClientHostnameBuffer: */
2938  size = add_size(size,
2940  /* BackendActivityBuffer: */
2941  size = add_size(size,
2943 #ifdef USE_SSL
2944  /* BackendSslStatusBuffer: */
2945  size = add_size(size,
2947 #endif
2948 #ifdef ENABLE_GSS
2949  /* BackendGssStatusBuffer: */
2950  size = add_size(size,
2952 #endif
2953  return size;
2954 }
2955 
2956 /*
2957  * Initialize the shared status array and several string buffers
2958  * during postmaster startup.
2959  */
2960 void
2962 {
2963  Size size;
2964  bool found;
2965  int i;
2966  char *buffer;
2967 
2968  /* Create or attach to the shared array */
2969  size = mul_size(sizeof(PgBackendStatus), NumBackendStatSlots);
2970  BackendStatusArray = (PgBackendStatus *)
2971  ShmemInitStruct("Backend Status Array", size, &found);
2972 
2973  if (!found)
2974  {
2975  /*
2976  * We're the first - initialize.
2977  */
2978  MemSet(BackendStatusArray, 0, size);
2979  }
2980 
2981  /* Create or attach to the shared appname buffer */
2983  BackendAppnameBuffer = (char *)
2984  ShmemInitStruct("Backend Application Name Buffer", size, &found);
2985 
2986  if (!found)
2987  {
2988  MemSet(BackendAppnameBuffer, 0, size);
2989 
2990  /* Initialize st_appname pointers. */
2991  buffer = BackendAppnameBuffer;
2992  for (i = 0; i < NumBackendStatSlots; i++)
2993  {
2994  BackendStatusArray[i].st_appname = buffer;
2995  buffer += NAMEDATALEN;
2996  }
2997  }
2998 
2999  /* Create or attach to the shared client hostname buffer */
3001  BackendClientHostnameBuffer = (char *)
3002  ShmemInitStruct("Backend Client Host Name Buffer", size, &found);
3003 
3004  if (!found)
3005  {
3007 
3008  /* Initialize st_clienthostname pointers. */
3009  buffer = BackendClientHostnameBuffer;
3010  for (i = 0; i < NumBackendStatSlots; i++)
3011  {
3012  BackendStatusArray[i].st_clienthostname = buffer;
3013  buffer += NAMEDATALEN;
3014  }
3015  }
3016 
3017  /* Create or attach to the shared activity buffer */
3020  BackendActivityBuffer = (char *)
3021  ShmemInitStruct("Backend Activity Buffer",
3023  &found);
3024 
3025  if (!found)
3026  {
3028 
3029  /* Initialize st_activity pointers. */
3030  buffer = BackendActivityBuffer;
3031  for (i = 0; i < NumBackendStatSlots; i++)
3032  {
3033  BackendStatusArray[i].st_activity_raw = buffer;
3035  }
3036  }
3037 
3038 #ifdef USE_SSL
3039  /* Create or attach to the shared SSL status buffer */
3041  BackendSslStatusBuffer = (PgBackendSSLStatus *)
3042  ShmemInitStruct("Backend SSL Status Buffer", size, &found);
3043 
3044  if (!found)
3045  {
3046  PgBackendSSLStatus *ptr;
3047 
3048  MemSet(BackendSslStatusBuffer, 0, size);
3049 
3050  /* Initialize st_sslstatus pointers. */
3051  ptr = BackendSslStatusBuffer;
3052  for (i = 0; i < NumBackendStatSlots; i++)
3053  {
3054  BackendStatusArray[i].st_sslstatus = ptr;
3055  ptr++;
3056  }
3057  }
3058 #endif
3059 
3060 #ifdef ENABLE_GSS
3061  /* Create or attach to the shared GSSAPI status buffer */
3063  BackendGssStatusBuffer = (PgBackendGSSStatus *)
3064  ShmemInitStruct("Backend GSS Status Buffer", size, &found);
3065 
3066  if (!found)
3067  {
3068  PgBackendGSSStatus *ptr;
3069 
3070  MemSet(BackendGssStatusBuffer, 0, size);
3071 
3072  /* Initialize st_gssstatus pointers. */
3073  ptr = BackendGssStatusBuffer;
3074  for (i = 0; i < NumBackendStatSlots; i++)
3075  {
3076  BackendStatusArray[i].st_gssstatus = ptr;
3077  ptr++;
3078  }
3079  }
3080 #endif
3081 }
3082 
3083 
3084 /* ----------
3085  * pgstat_initialize() -
3086  *
3087  * Initialize pgstats state, and set up our on-proc-exit hook.
3088  * Called from InitPostgres and AuxiliaryProcessMain. For auxiliary process,
3089  * MyBackendId is invalid. Otherwise, MyBackendId must be set,
3090  * but we must not have started any transaction yet (since the
3091  * exit hook must run after the last transaction exit).
3092  * NOTE: MyDatabaseId isn't set yet; so the shutdown hook has to be careful.
3093  * ----------
3094  */
3095 void
3097 {
3098  /* Initialize MyBEEntry */
3100  {
3102  MyBEEntry = &BackendStatusArray[MyBackendId - 1];
3103  }
3104  else
3105  {
3106  /* Must be an auxiliary process */
3108 
3109  /*
3110  * Assign the MyBEEntry for an auxiliary process. Since it doesn't
3111  * have a BackendId, the slot is statically allocated based on the
3112  * auxiliary process type (MyAuxProcType). Backends use slots indexed
3113  * in the range from 1 to MaxBackends (inclusive), so we use
3114  * MaxBackends + AuxBackendType + 1 as the index of the slot for an
3115  * auxiliary process.
3116  */
3117  MyBEEntry = &BackendStatusArray[MaxBackends + MyAuxProcType];
3118  }
3119 
3120  /*
3121  * Initialize prevWalUsage with pgWalUsage so that pgstat_send_wal() can
3122  * calculate how much pgWalUsage counters are increased by substracting
3123  * prevWalUsage from pgWalUsage.
3124  */
3125  prevWalUsage = pgWalUsage;
3126 
3127  /* Set up a process-exit hook to clean up */
3129 }
3130 
3131 /* ----------
3132  * pgstat_bestart() -
3133  *
3134  * Initialize this backend's entry in the PgBackendStatus array.
3135  * Called from InitPostgres.
3136  *
3137  * Apart from auxiliary processes, MyBackendId, MyDatabaseId,
3138  * session userid, and application_name must be set for a
3139  * backend (hence, this cannot be combined with pgstat_initialize).
3140  * Note also that we must be inside a transaction if this isn't an aux
3141  * process, as we may need to do encoding conversion on some strings.
3142  * ----------
3143  */
3144 void
3146 {
3147  volatile PgBackendStatus *vbeentry = MyBEEntry;
3148  PgBackendStatus lbeentry;
3149 #ifdef USE_SSL
3150  PgBackendSSLStatus lsslstatus;
3151 #endif
3152 #ifdef ENABLE_GSS
3153  PgBackendGSSStatus lgssstatus;
3154 #endif
3155 
3156  /* pgstats state must be initialized from pgstat_initialize() */
3157  Assert(vbeentry != NULL);
3158 
3159  /*
3160  * To minimize the time spent modifying the PgBackendStatus entry, and
3161  * avoid risk of errors inside the critical section, we first copy the
3162  * shared-memory struct to a local variable, then modify the data in the
3163  * local variable, then copy the local variable back to shared memory.
3164  * Only the last step has to be inside the critical section.
3165  *
3166  * Most of the data we copy from shared memory is just going to be
3167  * overwritten, but the struct's not so large that it's worth the
3168  * maintenance hassle to copy only the needful fields.
3169  */
3170  memcpy(&lbeentry,
3171  unvolatize(PgBackendStatus *, vbeentry),
3172  sizeof(PgBackendStatus));
3173 
3174  /* These structs can just start from zeroes each time, though */
3175 #ifdef USE_SSL
3176  memset(&lsslstatus, 0, sizeof(lsslstatus));
3177 #endif
3178 #ifdef ENABLE_GSS
3179  memset(&lgssstatus, 0, sizeof(lgssstatus));
3180 #endif
3181 
3182  /*
3183  * Now fill in all the fields of lbeentry, except for strings that are
3184  * out-of-line data. Those have to be handled separately, below.
3185  */
3186  lbeentry.st_procpid = MyProcPid;
3187  lbeentry.st_backendType = MyBackendType;
3189  lbeentry.st_activity_start_timestamp = 0;
3190  lbeentry.st_state_start_timestamp = 0;
3191  lbeentry.st_xact_start_timestamp = 0;
3192  lbeentry.st_databaseid = MyDatabaseId;
3193 
3194  /* We have userid for client-backends, wal-sender and bgworker processes */
3195  if (lbeentry.st_backendType == B_BACKEND
3196  || lbeentry.st_backendType == B_WAL_SENDER
3197  || lbeentry.st_backendType == B_BG_WORKER)
3198  lbeentry.st_userid = GetSessionUserId();
3199  else
3200  lbeentry.st_userid = InvalidOid;
3201 
3202  /*
3203  * We may not have a MyProcPort (eg, if this is the autovacuum process).
3204  * If so, use all-zeroes client address, which is dealt with specially in
3205  * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
3206  */
3207  if (MyProcPort)
3208  memcpy(&lbeentry.st_clientaddr, &MyProcPort->raddr,
3209  sizeof(lbeentry.st_clientaddr));
3210  else
3211  MemSet(&lbeentry.st_clientaddr, 0, sizeof(lbeentry.st_clientaddr));
3212 
3213 #ifdef USE_SSL
3215  {
3216  lbeentry.st_ssl = true;
3224  }
3225  else
3226  {
3227  lbeentry.st_ssl = false;
3228  }
3229 #else
3230  lbeentry.st_ssl = false;
3231 #endif
3232 
3233 #ifdef ENABLE_GSS
3234  if (MyProcPort && MyProcPort->gss != NULL)
3235  {
3236  const char *princ = be_gssapi_get_princ(MyProcPort);
3237 
3238  lbeentry.st_gss = true;
3239  lgssstatus.gss_auth = be_gssapi_get_auth(MyProcPort);
3240  lgssstatus.gss_enc = be_gssapi_get_enc(MyProcPort);
3241  if (princ)
3242  strlcpy(lgssstatus.gss_princ, princ, NAMEDATALEN);
3243  }
3244  else
3245  {
3246  lbeentry.st_gss = false;
3247  }
3248 #else
3249  lbeentry.st_gss = false;
3250 #endif
3251 
3252  lbeentry.st_state = STATE_UNDEFINED;
3255 
3256  /*
3257  * we don't zero st_progress_param here to save cycles; nobody should
3258  * examine it until st_progress_command has been set to something other
3259  * than PROGRESS_COMMAND_INVALID
3260  */
3261 
3262  /*
3263  * We're ready to enter the critical section that fills the shared-memory
3264  * status entry. We follow the protocol of bumping st_changecount before
3265  * and after; and make sure it's even afterwards. We use a volatile
3266  * pointer here to ensure the compiler doesn't try to get cute.
3267  */
3268  PGSTAT_BEGIN_WRITE_ACTIVITY(vbeentry);
3269 
3270  /* make sure we'll memcpy the same st_changecount back */
3271  lbeentry.st_changecount = vbeentry->st_changecount;
3272 
3273  memcpy(unvolatize(PgBackendStatus *, vbeentry),
3274  &lbeentry,
3275  sizeof(PgBackendStatus));
3276 
3277  /*
3278  * We can write the out-of-line strings and structs using the pointers
3279  * that are in lbeentry; this saves some de-volatilizing messiness.
3280  */
3281  lbeentry.st_appname[0] = '\0';
3284  NAMEDATALEN);
3285  else
3286  lbeentry.st_clienthostname[0] = '\0';
3287  lbeentry.st_activity_raw[0] = '\0';
3288  /* Also make sure the last byte in each string area is always 0 */
3289  lbeentry.st_appname[NAMEDATALEN - 1] = '\0';
3290  lbeentry.st_clienthostname[NAMEDATALEN - 1] = '\0';
3292 
3293 #ifdef USE_SSL
3294  memcpy(lbeentry.st_sslstatus, &lsslstatus, sizeof(PgBackendSSLStatus));
3295 #endif
3296 #ifdef ENABLE_GSS
3297  memcpy(lbeentry.st_gssstatus, &lgssstatus, sizeof(PgBackendGSSStatus));
3298 #endif
3299 
3300  PGSTAT_END_WRITE_ACTIVITY(vbeentry);
3301 
3302  /* Update app name to current GUC setting */
3303  if (application_name)
3305 }
3306 
3307 /*
3308  * Shut down a single backend's statistics reporting at process exit.
3309  *
3310  * Flush any remaining statistics counts out to the collector.
3311  * Without this, operations triggered during backend exit (such as
3312  * temp table deletions) won't be counted.
3313  *
3314  * Lastly, clear out our entry in the PgBackendStatus array.
3315  */
3316 static void
3318 {
3319  volatile PgBackendStatus *beentry = MyBEEntry;
3320 
3321  /*
3322  * If we got as far as discovering our own database ID, we can report what
3323  * we did to the collector. Otherwise, we'd be sending an invalid
3324  * database ID, so forget it. (This means that accesses to pg_database
3325  * during failed backend starts might never get counted.)
3326  */
3327  if (OidIsValid(MyDatabaseId))
3328  pgstat_report_stat(true);
3329 
3330  /*
3331  * Clear my status entry, following the protocol of bumping st_changecount
3332  * before and after. We use a volatile pointer here to ensure the
3333  * compiler doesn't try to get cute.
3334  */
3335  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3336 
3337  beentry->st_procpid = 0; /* mark invalid */
3338 
3339  PGSTAT_END_WRITE_ACTIVITY(beentry);
3340 }
3341 
3342 
3343 /* ----------
3344  * pgstat_report_activity() -
3345  *
3346  * Called from tcop/postgres.c to report what the backend is actually doing
3347  * (but note cmd_str can be NULL for certain cases).
3348  *
3349  * All updates of the status entry follow the protocol of bumping
3350  * st_changecount before and after. We use a volatile pointer here to
3351  * ensure the compiler doesn't try to get cute.
3352  * ----------
3353  */
3354 void
3356 {
3357  volatile PgBackendStatus *beentry = MyBEEntry;
3358  TimestampTz start_timestamp;
3359  TimestampTz current_timestamp;
3360  int len = 0;
3361 
3362  TRACE_POSTGRESQL_STATEMENT_STATUS(cmd_str);
3363 
3364  if (!beentry)
3365  return;
3366 
3368  {
3369  if (beentry->st_state != STATE_DISABLED)
3370  {
3371  volatile PGPROC *proc = MyProc;
3372 
3373  /*
3374  * track_activities is disabled, but we last reported a
3375  * non-disabled state. As our final update, change the state and
3376  * clear fields we will not be updating anymore.
3377  */
3378  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3379  beentry->st_state = STATE_DISABLED;
3380  beentry->st_state_start_timestamp = 0;
3381  beentry->st_activity_raw[0] = '\0';
3382  beentry->st_activity_start_timestamp = 0;
3383  /* st_xact_start_timestamp and wait_event_info are also disabled */
3384  beentry->st_xact_start_timestamp = 0;
3385  proc->wait_event_info = 0;
3386  PGSTAT_END_WRITE_ACTIVITY(beentry);
3387  }
3388  return;
3389  }
3390 
3391  /*
3392  * To minimize the time spent modifying the entry, and avoid risk of
3393  * errors inside the critical section, fetch all the needed data first.
3394  */
3395  start_timestamp = GetCurrentStatementStartTimestamp();
3396  if (cmd_str != NULL)
3397  {
3398  /*
3399  * Compute length of to-be-stored string unaware of multi-byte
3400  * characters. For speed reasons that'll get corrected on read, rather
3401  * than computed every write.
3402  */
3403  len = Min(strlen(cmd_str), pgstat_track_activity_query_size - 1);
3404  }
3405  current_timestamp = GetCurrentTimestamp();
3406 
3407  /*
3408  * If the state has changed from "active" or "idle in transaction",
3409  * calculate the duration.
3410  */
3411  if ((beentry->st_state == STATE_RUNNING ||
3412  beentry->st_state == STATE_FASTPATH ||
3413  beentry->st_state == STATE_IDLEINTRANSACTION ||
3415  state != beentry->st_state)
3416  {
3417  long secs;
3418  int usecs;
3419 
3421  current_timestamp,
3422  &secs, &usecs);
3423 
3424  if (beentry->st_state == STATE_RUNNING ||
3425  beentry->st_state == STATE_FASTPATH)
3426  pgStatActiveTime += secs * 1000000 + usecs;
3427  else
3428  pgStatTransactionIdleTime += secs * 1000000 + usecs;
3429  }
3430 
3431  /*
3432  * Now update the status entry
3433  */
3434  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3435 
3436  beentry->st_state = state;
3437  beentry->st_state_start_timestamp = current_timestamp;
3438 
3439  if (cmd_str != NULL)
3440  {
3441  memcpy((char *) beentry->st_activity_raw, cmd_str, len);
3442  beentry->st_activity_raw[len] = '\0';
3443  beentry->st_activity_start_timestamp = start_timestamp;
3444  }
3445 
3446  PGSTAT_END_WRITE_ACTIVITY(beentry);
3447 }
3448 
3449 /*-----------
3450  * pgstat_progress_start_command() -
3451  *
3452  * Set st_progress_command (and st_progress_command_target) in own backend
3453  * entry. Also, zero-initialize st_progress_param array.
3454  *-----------
3455  */
3456 void
3458 {
3459  volatile PgBackendStatus *beentry = MyBEEntry;
3460 
3461  if (!beentry || !pgstat_track_activities)
3462  return;
3463 
3464  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3465  beentry->st_progress_command = cmdtype;
3466  beentry->st_progress_command_target = relid;
3467  MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
3468  PGSTAT_END_WRITE_ACTIVITY(beentry);
3469 }
3470 
3471 /*-----------
3472  * pgstat_progress_update_param() -
3473  *
3474  * Update index'th member in st_progress_param[] of own backend entry.
3475  *-----------
3476  */
3477 void
3479 {
3480  volatile PgBackendStatus *beentry = MyBEEntry;
3481 
3482  Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
3483 
3484  if (!beentry || !pgstat_track_activities)
3485  return;
3486 
3487  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3488  beentry->st_progress_param[index] = val;
3489  PGSTAT_END_WRITE_ACTIVITY(beentry);
3490 }
3491 
3492 /*-----------
3493  * pgstat_progress_update_multi_param() -
3494  *
3495  * Update multiple members in st_progress_param[] of own backend entry.
3496  * This is atomic; readers won't see intermediate states.
3497  *-----------
3498  */
3499 void
3501  const int64 *val)
3502 {
3503  volatile PgBackendStatus *beentry = MyBEEntry;
3504  int i;
3505 
3506  if (!beentry || !pgstat_track_activities || nparam == 0)
3507  return;
3508 
3509  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3510 
3511  for (i = 0; i < nparam; ++i)
3512  {
3513  Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM);
3514 
3515  beentry->st_progress_param[index[i]] = val[i];
3516  }
3517 
3518  PGSTAT_END_WRITE_ACTIVITY(beentry);
3519 }
3520 
3521 /*-----------
3522  * pgstat_progress_end_command() -
3523  *
3524  * Reset st_progress_command (and st_progress_command_target) in own backend
3525  * entry. This signals the end of the command.
3526  *-----------
3527  */
3528 void
3530 {
3531  volatile PgBackendStatus *beentry = MyBEEntry;
3532 
3533  if (!beentry || !pgstat_track_activities)
3534  return;
3535 
3537  return;
3538 
3539  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3542  PGSTAT_END_WRITE_ACTIVITY(beentry);
3543 }
3544 
3545 /* ----------
3546  * pgstat_report_appname() -
3547  *
3548  * Called to update our application name.
3549  * ----------
3550  */
3551 void
3552 pgstat_report_appname(const char *appname)
3553 {
3554  volatile PgBackendStatus *beentry = MyBEEntry;
3555  int len;
3556 
3557  if (!beentry)
3558  return;
3559 
3560  /* This should be unnecessary if GUC did its job, but be safe */
3561  len = pg_mbcliplen(appname, strlen(appname), NAMEDATALEN - 1);
3562 
3563  /*
3564  * Update my status entry, following the protocol of bumping
3565  * st_changecount before and after. We use a volatile pointer here to
3566  * ensure the compiler doesn't try to get cute.
3567  */
3568  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3569 
3570  memcpy((char *) beentry->st_appname, appname, len);
3571  beentry->st_appname[len] = '\0';
3572 
3573  PGSTAT_END_WRITE_ACTIVITY(beentry);
3574 }
3575 
3576 /*
3577  * Report current transaction start timestamp as the specified value.
3578  * Zero means there is no active transaction.
3579  */
3580 void
3582 {
3583  volatile PgBackendStatus *beentry = MyBEEntry;
3584 
3585  if (!pgstat_track_activities || !beentry)
3586  return;
3587 
3588  /*
3589  * Update my status entry, following the protocol of bumping
3590  * st_changecount before and after. We use a volatile pointer here to
3591  * ensure the compiler doesn't try to get cute.
3592  */
3593  PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
3594 
3595  beentry->st_xact_start_timestamp = tstamp;
3596 
3597  PGSTAT_END_WRITE_ACTIVITY(beentry);
3598 }
3599 
3600 /* ----------
3601  * pgstat_read_current_status() -
3602  *
3603  * Copy the current contents of the PgBackendStatus array to local memory,
3604  * if not already done in this transaction.
3605  * ----------
3606  */
3607 static void
3609 {
3610  volatile PgBackendStatus *beentry;
3611  LocalPgBackendStatus *localtable;
3612  LocalPgBackendStatus *localentry;
3613  char *localappname,
3614  *localclienthostname,
3615  *localactivity;
3616 #ifdef USE_SSL
3617  PgBackendSSLStatus *localsslstatus;
3618 #endif
3619 #ifdef ENABLE_GSS
3620  PgBackendGSSStatus *localgssstatus;
3621 #endif
3622  int i;
3623 
3625  if (localBackendStatusTable)
3626  return; /* already done */
3627 
3629 
3630  /*
3631  * Allocate storage for local copy of state data. We can presume that
3632  * none of these requests overflow size_t, because we already calculated
3633  * the same values using mul_size during shmem setup. However, with
3634  * probably-silly values of pgstat_track_activity_query_size and
3635  * max_connections, the localactivity buffer could exceed 1GB, so use
3636  * "huge" allocation for that one.
3637  */
3638  localtable = (LocalPgBackendStatus *)
3639  MemoryContextAlloc(pgStatLocalContext,
3641  localappname = (char *)
3642  MemoryContextAlloc(pgStatLocalContext,
3644  localclienthostname = (char *)
3645  MemoryContextAlloc(pgStatLocalContext,
3646  NAMEDATALEN * NumBackendStatSlots);
3647  localactivity = (char *)
3648  MemoryContextAllocHuge(pgStatLocalContext,
3649  pgstat_track_activity_query_size * NumBackendStatSlots);
3650 #ifdef USE_SSL
3651  localsslstatus = (PgBackendSSLStatus *)
3652  MemoryContextAlloc(pgStatLocalContext,
3654 #endif
3655 #ifdef ENABLE_GSS
3656  localgssstatus = (PgBackendGSSStatus *)
3657  MemoryContextAlloc(pgStatLocalContext,
3659 #endif
3660 
3661  localNumBackends = 0;
3662 
3663  beentry = BackendStatusArray;
3664  localentry = localtable;
3665  for (i = 1; i <= NumBackendStatSlots; i++)
3666  {
3667  /*
3668  * Follow the protocol of retrying if st_changecount changes while we
3669  * copy the entry, or if it's odd. (The check for odd is needed to
3670  * cover the case where we are able to completely copy the entry while
3671  * the source backend is between increment steps.) We use a volatile
3672  * pointer here to ensure the compiler doesn't try to get cute.
3673  */
3674  for (;;)
3675  {
3676  int before_changecount;
3677  int after_changecount;
3678 
3679  pgstat_begin_read_activity(beentry, before_changecount);
3680 
3681  localentry->backendStatus.st_procpid = beentry->st_procpid;
3682  /* Skip all the data-copying work if entry is not in use */
3683  if (localentry->backendStatus.st_procpid > 0)
3684  {
3685  memcpy(&localentry->backendStatus, unvolatize(PgBackendStatus *, beentry), sizeof(PgBackendStatus));
3686 
3687  /*
3688  * For each PgBackendStatus field that is a pointer, copy the
3689  * pointed-to data, then adjust the local copy of the pointer
3690  * field to point at the local copy of the data.
3691  *
3692  * strcpy is safe even if the string is modified concurrently,
3693  * because there's always a \0 at the end of the buffer.
3694  */
3695  strcpy(localappname, (char *) beentry->st_appname);
3696  localentry->backendStatus.st_appname = localappname;
3697  strcpy(localclienthostname, (char *) beentry->st_clienthostname);
3698  localentry->backendStatus.st_clienthostname = localclienthostname;
3699  strcpy(localactivity, (char *) beentry->st_activity_raw);
3700  localentry->backendStatus.st_activity_raw = localactivity;
3701 #ifdef USE_SSL
3702  if (beentry->st_ssl)
3703  {
3704  memcpy(localsslstatus, beentry->st_sslstatus, sizeof(PgBackendSSLStatus));
3705  localentry->backendStatus.st_sslstatus = localsslstatus;
3706  }
3707 #endif
3708 #ifdef ENABLE_GSS
3709  if (beentry->st_gss)
3710  {
3711  memcpy(localgssstatus, beentry->st_gssstatus, sizeof(PgBackendGSSStatus));
3712  localentry->backendStatus.st_gssstatus = localgssstatus;
3713  }
3714 #endif
3715  }
3716 
3717  pgstat_end_read_activity(beentry, after_changecount);
3718 
3719  if (pgstat_read_activity_complete(before_changecount,
3720  after_changecount))
3721  break;
3722 
3723  /* Make sure we can break out of loop if stuck... */
3725  }
3726 
3727  beentry++;
3728  /* Only valid entries get included into the local array */
3729  if (localentry->backendStatus.st_procpid > 0)
3730  {
3732  &localentry->backend_xid,
3733  &localentry->backend_xmin);
3734 
3735  localentry++;
3736  localappname += NAMEDATALEN;
3737  localclienthostname += NAMEDATALEN;
3738  localactivity += pgstat_track_activity_query_size;
3739 #ifdef USE_SSL
3740  localsslstatus++;
3741 #endif
3742 #ifdef ENABLE_GSS
3743  localgssstatus++;
3744 #endif
3745  localNumBackends++;
3746  }
3747  }
3748 
3749  /* Set the pointer only after completion of a valid table */
3750  localBackendStatusTable = localtable;
3751 }
3752 
3753 /* ----------
3754  * pgstat_get_wait_event_type() -
3755  *
3756  * Return a string representing the current wait event type, backend is
3757  * waiting on.
3758  */
3759 const char *
3761 {
3762  uint32 classId;
3763  const char *event_type;
3764 
3765  /* report process as not waiting. */
3766  if (wait_event_info == 0)
3767  return NULL;
3768 
3769  classId = wait_event_info & 0xFF000000;
3770 
3771  switch (classId)
3772  {
3773  case PG_WAIT_LWLOCK:
3774  event_type = "LWLock";
3775  break;
3776  case PG_WAIT_LOCK:
3777  event_type = "Lock";
3778  break;
3779  case PG_WAIT_BUFFER_PIN:
3780  event_type = "BufferPin";
3781  break;
3782  case PG_WAIT_ACTIVITY:
3783  event_type = "Activity";
3784  break;
3785  case PG_WAIT_CLIENT:
3786  event_type = "Client";
3787  break;
3788  case PG_WAIT_EXTENSION:
3789  event_type = "Extension";
3790  break;
3791  case PG_WAIT_IPC:
3792  event_type = "IPC";
3793  break;
3794  case PG_WAIT_TIMEOUT:
3795  event_type = "Timeout";
3796  break;
3797  case PG_WAIT_IO:
3798  event_type = "IO";
3799  break;
3800  default:
3801  event_type = "???";
3802  break;
3803  }
3804 
3805  return event_type;
3806 }
3807 
3808 /* ----------
3809  * pgstat_get_wait_event() -
3810  *
3811  * Return a string representing the current wait event, backend is
3812  * waiting on.
3813  */
3814 const char *
3816 {
3817  uint32 classId;
3818  uint16 eventId;
3819  const char *event_name;
3820 
3821  /* report process as not waiting. */
3822  if (wait_event_info == 0)
3823  return NULL;
3824 
3825  classId = wait_event_info & 0xFF000000;
3826  eventId = wait_event_info & 0x0000FFFF;
3827 
3828  switch (classId)
3829  {
3830  case PG_WAIT_LWLOCK:
3831  event_name = GetLWLockIdentifier(classId, eventId);
3832  break;
3833  case PG_WAIT_LOCK:
3834  event_name = GetLockNameFromTagType(eventId);
3835  break;
3836  case PG_WAIT_BUFFER_PIN:
3837  event_name = "BufferPin";
3838  break;
3839  case PG_WAIT_ACTIVITY:
3840  {
3841  WaitEventActivity w = (WaitEventActivity) wait_event_info;
3842 
3843  event_name = pgstat_get_wait_activity(w);
3844  break;
3845  }
3846  case PG_WAIT_CLIENT:
3847  {
3848  WaitEventClient w = (WaitEventClient) wait_event_info;
3849 
3850  event_name = pgstat_get_wait_client(w);
3851  break;
3852  }
3853  case PG_WAIT_EXTENSION:
3854  event_name = "Extension";
3855  break;
3856  case PG_WAIT_IPC:
3857  {
3858  WaitEventIPC w = (WaitEventIPC) wait_event_info;
3859 
3860  event_name = pgstat_get_wait_ipc(w);
3861  break;
3862  }
3863  case PG_WAIT_TIMEOUT:
3864  {
3865  WaitEventTimeout w = (WaitEventTimeout) wait_event_info;
3866 
3867  event_name = pgstat_get_wait_timeout(w);
3868  break;
3869  }
3870  case PG_WAIT_IO:
3871  {
3872  WaitEventIO w = (WaitEventIO) wait_event_info;
3873 
3874  event_name = pgstat_get_wait_io(w);
3875  break;
3876  }
3877  default:
3878  event_name = "unknown wait event";
3879  break;
3880  }
3881 
3882  return event_name;
3883 }
3884 
3885 /* ----------
3886  * pgstat_get_wait_activity() -
3887  *
3888  * Convert WaitEventActivity to string.
3889  * ----------
3890  */
3891 static const char *
3893 {
3894  const char *event_name = "unknown wait event";
3895 
3896  switch (w)
3897  {
3899  event_name = "ArchiverMain";
3900  break;
3902  event_name = "AutoVacuumMain";
3903  break;
3905  event_name = "BgWriterHibernate";
3906  break;
3908  event_name = "BgWriterMain";
3909  break;
3911  event_name = "CheckpointerMain";
3912  break;
3914  event_name = "LogicalApplyMain";
3915  break;
3917  event_name = "LogicalLauncherMain";
3918  break;
3920  event_name = "PgStatMain";
3921  break;
3923  event_name = "RecoveryWalStream";
3924  break;
3926  event_name = "SysLoggerMain";
3927  break;
3929  event_name = "WalReceiverMain";
3930  break;
3932  event_name = "WalSenderMain";
3933  break;
3935  event_name = "WalWriterMain";
3936  break;
3937  /* no default case, so that compiler will warn */
3938  }
3939 
3940  return event_name;
3941 }
3942 
3943 /* ----------
3944  * pgstat_get_wait_client() -
3945  *
3946  * Convert WaitEventClient to string.
3947  * ----------
3948  */
3949 static const char *
3951 {
3952  const char *event_name = "unknown wait event";
3953 
3954  switch (w)
3955  {
3957  event_name = "ClientRead";
3958  break;
3960  event_name = "ClientWrite";
3961  break;
3963  event_name = "GSSOpenServer";
3964  break;
3966  event_name = "LibPQWalReceiverConnect";
3967  break;
3969  event_name = "LibPQWalReceiverReceive";
3970  break;
3972  event_name = "SSLOpenServer";
3973  break;
3975  event_name = "WalReceiverWaitStart";
3976  break;
3978  event_name = "WalSenderWaitForWAL";
3979  break;
3981  event_name = "WalSenderWriteData";
3982  break;
3983  /* no default case, so that compiler will warn */
3984  }
3985 
3986  return event_name;
3987 }
3988 
3989 /* ----------
3990  * pgstat_get_wait_ipc() -
3991  *
3992  * Convert WaitEventIPC to string.
3993  * ----------
3994  */
3995 static const char *
3997 {
3998  const char *event_name = "unknown wait event";
3999 
4000  switch (w)
4001  {
4003  event_name = "BackupWaitWalArchive";
4004  break;
4006  event_name = "BgWorkerShutdown";
4007  break;
4009  event_name = "BgWorkerStartup";
4010  break;
4011  case WAIT_EVENT_BTREE_PAGE:
4012  event_name = "BtreePage";
4013  break;
4015  event_name = "CheckpointDone";
4016  break;
4018  event_name = "CheckpointStart";
4019  break;
4021  event_name = "ExecuteGather";
4022  break;
4024  event_name = "HashBatchAllocate";
4025  break;
4027  event_name = "HashBatchElect";
4028  break;
4030  event_name = "HashBatchLoad";
4031  break;
4033  event_name = "HashBuildAllocate";
4034  break;
4036  event_name = "HashBuildElect";
4037  break;
4039  event_name = "HashBuildHashInner";
4040  break;
4042  event_name = "HashBuildHashOuter";
4043  break;
4045  event_name = "HashGrowBatchesAllocate";
4046  break;
4048  event_name = "HashGrowBatchesDecide";
4049  break;
4051  event_name = "HashGrowBatchesElect";
4052  break;
4054  event_name = "HashGrowBatchesFinish";
4055  break;
4057  event_name = "HashGrowBatchesRepartition";
4058  break;
4060  event_name = "HashGrowBucketsAllocate";
4061  break;
4063  event_name = "HashGrowBucketsElect";
4064  break;
4066  event_name = "HashGrowBucketsReinsert";
4067  break;
4069  event_name = "LogicalSyncData";
4070  break;
4072  event_name = "LogicalSyncStateChange";
4073  break;
4075  event_name = "MessageQueueInternal";
4076  break;
4078  event_name = "MessageQueuePutMessage";
4079  break;
4080  case WAIT_EVENT_MQ_RECEIVE:
4081  event_name = "MessageQueueReceive";
4082  break;
4083  case WAIT_EVENT_MQ_SEND:
4084  event_name = "MessageQueueSend";
4085  break;
4087  event_name = "ParallelBitmapScan";
4088  break;
4090  event_name = "ParallelCreateIndexScan";
4091  break;
4093  event_name = "ParallelFinish";
4094  break;
4096  event_name = "ProcArrayGroupUpdate";
4097  break;
4099  event_name = "ProcSignalBarrier";
4100  break;
4101  case WAIT_EVENT_PROMOTE:
4102  event_name = "Promote";
4103  break;
4105  event_name = "RecoveryConflictSnapshot";
4106  break;
4108  event_name = "RecoveryConflictTablespace";
4109  break;
4111  event_name = "RecoveryPause";
4112  break;
4114  event_name = "ReplicationOriginDrop";
4115  break;
4117  event_name = "ReplicationSlotDrop";
4118  break;
4120  event_name = "SafeSnapshot";
4121  break;
4122  case WAIT_EVENT_SYNC_REP:
4123  event_name = "SyncRep";
4124  break;
4126  event_name = "XactGroupUpdate";
4127  break;
4128  /* no default case, so that compiler will warn */
4129  }
4130 
4131  return event_name;
4132 }
4133 
4134 /* ----------
4135  * pgstat_get_wait_timeout() -
4136  *
4137  * Convert WaitEventTimeout to string.
4138  * ----------
4139  */
4140 static const char *
4142 {
4143  const char *event_name = "unknown wait event";
4144 
4145  switch (w)
4146  {
4148  event_name = "BaseBackupThrottle";
4149  break;
4150  case WAIT_EVENT_PG_SLEEP:
4151  event_name = "PgSleep";
4152  break;
4154  event_name = "RecoveryApplyDelay";
4155  break;
4157  event_name = "RecoveryRetrieveRetryInterval";
4158  break;
4160  event_name = "VacuumDelay";
4161  break;
4162  /* no default case, so that compiler will warn */
4163  }
4164 
4165  return event_name;
4166 }
4167 
4168 /* ----------
4169  * pgstat_get_wait_io() -
4170  *
4171  * Convert WaitEventIO to string.
4172  * ----------
4173  */
4174 static const char *
4176 {
4177  const char *event_name = "unknown wait event";
4178 
4179  switch (w)
4180  {
4182  event_name = "BaseBackupRead";
4183  break;
4185  event_name = "BufFileRead";
4186  break;
4188  event_name = "BufFileWrite";
4189  break;
4191  event_name = "BufFileTruncate";
4192  break;
4194  event_name = "ControlFileRead";
4195  break;
4197  event_name = "ControlFileSync";
4198  break;
4200  event_name = "ControlFileSyncUpdate";
4201  break;
4203  event_name = "ControlFileWrite";
4204  break;
4206  event_name = "ControlFileWriteUpdate";
4207  break;
4209  event_name = "CopyFileRead";
4210  break;
4212  event_name = "CopyFileWrite";
4213  break;
4215  event_name = "DataFileExtend";
4216  break;
4218  event_name = "DataFileFlush";
4219  break;
4221  event_name = "DataFileImmediateSync";
4222  break;
4224  event_name = "DataFilePrefetch";
4225  break;
4227  event_name = "DataFileRead";
4228  break;
4230  event_name = "DataFileSync";
4231  break;
4233  event_name = "DataFileTruncate";
4234  break;
4236  event_name = "DataFileWrite";
4237  break;
4239  event_name = "DSMFillZeroWrite";
4240  break;
4242  event_name = "LockFileAddToDataDirRead";
4243  break;
4245  event_name = "LockFileAddToDataDirSync";
4246  break;
4248  event_name = "LockFileAddToDataDirWrite";
4249  break;
4251  event_name = "LockFileCreateRead";
4252  break;
4254  event_name = "LockFileCreateSync";
4255  break;
4257  event_name = "LockFileCreateWrite";
4258  break;
4260  event_name = "LockFileReCheckDataDirRead";
4261  break;
4263  event_name = "LogicalRewriteCheckpointSync";
4264  break;
4266  event_name = "LogicalRewriteMappingSync";
4267  break;
4269  event_name = "LogicalRewriteMappingWrite";
4270  break;
4272  event_name = "LogicalRewriteSync";
4273  break;
4275  event_name = "LogicalRewriteTruncate";
4276  break;
4278  event_name = "LogicalRewriteWrite";
4279  break;
4281  event_name = "RelationMapRead";
4282  break;
4284  event_name = "RelationMapSync";
4285  break;
4287  event_name = "RelationMapWrite";
4288  break;
4290  event_name = "ReorderBufferRead";
4291  break;
4293  event_name = "ReorderBufferWrite";
4294  break;
4296  event_name = "ReorderLogicalMappingRead";
4297  break;
4299  event_name = "ReplicationSlotRead";
4300  break;
4302  event_name = "ReplicationSlotRestoreSync";
4303  break;
4305  event_name = "ReplicationSlotSync";
4306  break;
4308  event_name = "ReplicationSlotWrite";
4309  break;
4311  event_name = "SLRUFlushSync";
4312  break;
4313  case WAIT_EVENT_SLRU_READ:
4314  event_name = "SLRURead";
4315  break;
4316  case WAIT_EVENT_SLRU_SYNC:
4317  event_name = "SLRUSync";
4318  break;
4319  case WAIT_EVENT_SLRU_WRITE:
4320  event_name = "SLRUWrite";
4321  break;
4323  event_name = "SnapbuildRead";
4324  break;
4326  event_name = "SnapbuildSync";
4327  break;
4329  event_name = "SnapbuildWrite";
4330  break;
4332  event_name = "TimelineHistoryFileSync";
4333  break;
4335  event_name = "TimelineHistoryFileWrite";
4336  break;
4338  event_name = "TimelineHistoryRead";
4339  break;
4341  event_name = "TimelineHistorySync";
4342  break;
4344  event_name = "TimelineHistoryWrite";
4345  break;
4347  event_name = "TwophaseFileRead";
4348  break;
4350  event_name = "TwophaseFileSync";
4351  break;
4353  event_name = "TwophaseFileWrite";
4354  break;
4356  event_name = "WALSenderTimelineHistoryRead";
4357  break;
4359  event_name = "WALBootstrapSync";
4360  break;
4362  event_name = "WALBootstrapWrite";
4363  break;
4365  event_name = "WALCopyRead";
4366  break;
4368  event_name = "WALCopySync";
4369  break;
4371  event_name = "WALCopyWrite";
4372  break;
4374  event_name = "WALInitSync";
4375  break;
4377  event_name = "WALInitWrite";
4378  break;
4379  case WAIT_EVENT_WAL_READ:
4380  event_name = "WALRead";
4381  break;
4382  case WAIT_EVENT_WAL_SYNC:
4383  event_name = "WALSync";
4384  break;
4386  event_name = "WALSyncMethodAssign";
4387  break;
4388  case WAIT_EVENT_WAL_WRITE:
4389  event_name = "WALWrite";
4390  break;
4392  event_name = "LogicalChangesRead";
4393  break;
4395  event_name = "LogicalChangesWrite";
4396  break;
4398  event_name = "LogicalSubxactRead";
4399  break;
4401  event_name = "LogicalSubxactWrite";
4402  break;
4403 
4404  /* no default case, so that compiler will warn */
4405  }
4406 
4407  return event_name;
4408 }
4409 
4410 
4411 /* ----------
4412  * pgstat_get_backend_current_activity() -
4413  *
4414  * Return a string representing the current activity of the backend with
4415  * the specified PID. This looks directly at the BackendStatusArray,
4416  * and so will provide current information regardless of the age of our
4417  * transaction's snapshot of the status array.
4418  *
4419  * It is the caller's responsibility to invoke this only for backends whose
4420  * state is expected to remain stable while the result is in use. The
4421  * only current use is in deadlock reporting, where we can expect that
4422  * the target backend is blocked on a lock. (There are corner cases
4423  * where the target's wait could get aborted while we are looking at it,
4424  * but the very worst consequence is to return a pointer to a string
4425  * that's been changed, so we won't worry too much.)
4426  *
4427  * Note: return strings for special cases match pg_stat_get_backend_activity.
4428  * ----------
4429  */
4430 const char *
4431 pgstat_get_backend_current_activity(int pid, bool checkUser)
4432 {
4433  PgBackendStatus *beentry;
4434  int i;
4435 
4436  beentry = BackendStatusArray;
4437  for (i = 1; i <= MaxBackends; i++)
4438  {
4439  /*
4440  * Although we expect the target backend's entry to be stable, that
4441  * doesn't imply that anyone else's is. To avoid identifying the
4442  * wrong backend, while we check for a match to the desired PID we
4443  * must follow the protocol of retrying if st_changecount changes
4444  * while we examine the entry, or if it's odd. (This might be
4445  * unnecessary, since fetching or storing an int is almost certainly
4446  * atomic, but let's play it safe.) We use a volatile pointer here to
4447  * ensure the compiler doesn't try to get cute.
4448  */
4449  volatile PgBackendStatus *vbeentry = beentry;
4450  bool found;
4451 
4452  for (;;)
4453  {
4454  int before_changecount;
4455  int after_changecount;
4456 
4457  pgstat_begin_read_activity(vbeentry, before_changecount);
4458 
4459  found = (vbeentry->st_procpid == pid);
4460 
4461  pgstat_end_read_activity(vbeentry, after_changecount);
4462 
4463  if (pgstat_read_activity_complete(before_changecount,
4464  after_changecount))
4465  break;
4466 
4467  /* Make sure we can break out of loop if stuck... */
4469  }
4470 
4471  if (found)
4472  {
4473  /* Now it is safe to use the non-volatile pointer */
4474  if (checkUser && !superuser() && beentry->st_userid != GetUserId())
4475  return "<insufficient privilege>";
4476  else if (*(beentry->st_activity_raw) == '\0')
4477  return "<command string not enabled>";
4478  else
4479  {
4480  /* this'll leak a bit of memory, but that seems acceptable */
4481  return pgstat_clip_activity(beentry->st_activity_raw);
4482  }
4483  }
4484 
4485  beentry++;
4486  }
4487 
4488  /* If we get here, caller is in error ... */
4489  return "<backend information not available>";
4490 }
4491 
4492 /* ----------
4493  * pgstat_get_crashed_backend_activity() -
4494  *
4495  * Return a string representing the current activity of the backend with
4496  * the specified PID. Like the function above, but reads shared memory with
4497  * the expectation that it may be corrupt. On success, copy the string
4498  * into the "buffer" argument and return that pointer. On failure,
4499  * return NULL.
4500  *
4501  * This function is only intended to be used by the postmaster to report the
4502  * query that crashed a backend. In particular, no attempt is made to
4503  * follow the correct concurrency protocol when accessing the
4504  * BackendStatusArray. But that's OK, in the worst case we'll return a
4505  * corrupted message. We also must take care not to trip on ereport(ERROR).
4506  * ----------
4507  */
4508 const char *
4509 pgstat_get_crashed_backend_activity(int pid, char *buffer, int buflen)
4510 {
4511  volatile PgBackendStatus *beentry;
4512  int i;
4513 
4514  beentry = BackendStatusArray;
4515 
4516  /*
4517  * We probably shouldn't get here before shared memory has been set up,
4518  * but be safe.
4519  */
4520  if (beentry == NULL || BackendActivityBuffer == NULL)
4521  return NULL;
4522 
4523  for (i = 1; i <= MaxBackends; i++)
4524  {
4525  if (beentry->st_procpid == pid)
4526  {
4527  /* Read pointer just once, so it can't change after validation */
4528  const char *activity = beentry->st_activity_raw;
4529  const char *activity_last;
4530 
4531  /*
4532  * We mustn't access activity string before we verify that it
4533  * falls within the BackendActivityBuffer. To make sure that the
4534  * entire string including its ending is contained within the
4535  * buffer, subtract one activity length from the buffer size.
4536  */
4539 
4540  if (activity < BackendActivityBuffer ||
4541  activity > activity_last)
4542  return NULL;
4543 
4544  /* If no string available, no point in a report */
4545  if (activity[0] == '\0')
4546  return NULL;
4547 
4548  /*
4549  * Copy only ASCII-safe characters so we don't run into encoding
4550  * problems when reporting the message; and be sure not to run off
4551  * the end of memory. As only ASCII characters are reported, it
4552  * doesn't seem necessary to perform multibyte aware clipping.
4553  */
4554  ascii_safe_strlcpy(buffer, activity,
4555  Min(buflen, pgstat_track_activity_query_size));
4556 
4557  return buffer;
4558  }
4559 
4560  beentry++;
4561  }
4562 
4563  /* PID not found */
4564  return NULL;
4565 }
4566 
4567 /* ------------------------------------------------------------
4568  * Local support functions follow
4569  * ------------------------------------------------------------
4570  */
4571 
4572 
4573 /* ----------
4574  * pgstat_setheader() -
4575  *
4576  * Set common header fields in a statistics message
4577  * ----------
4578  */
4579 static void
4581 {
4582  hdr->m_type = mtype;
4583 }
4584 
4585 
4586 /* ----------
4587  * pgstat_send() -
4588  *
4589  * Send out one statistics message to the collector
4590  * ----------
4591  */
4592 static void
4593 pgstat_send(void *msg, int len)
4594 {
4595  int rc;
4596 
4598  return;
4599 
4600  ((PgStat_MsgHdr *) msg)->m_size = len;
4601 
4602  /* We'll retry after EINTR, but ignore all other failures */
4603  do
4604  {
4605  rc = send(pgStatSock, msg, len, 0);
4606  } while (rc < 0 && errno == EINTR);
4607 
4608 #ifdef USE_ASSERT_CHECKING
4609  /* In debug builds, log send failures ... */
4610  if (rc < 0)
4611  elog(LOG, "could not send to statistics collector: %m");
4612 #endif
4613 }
4614 
4615 /* ----------
4616  * pgstat_send_archiver() -
4617  *
4618  * Tell the collector about the WAL file that we successfully
4619  * archived or failed to archive.
4620  * ----------
4621  */
4622 void
4623 pgstat_send_archiver(const char *xlog, bool failed)
4624 {
4625  PgStat_MsgArchiver msg;
4626 
4627  /*
4628  * Prepare and send the message
4629  */
4631  msg.m_failed = failed;
4632  strlcpy(msg.m_xlog, xlog, sizeof(msg.m_xlog));
4634  pgstat_send(&msg, sizeof(msg));
4635 }
4636 
4637 /* ----------
4638  * pgstat_send_bgwriter() -
4639  *
4640  * Send bgwriter statistics to the collector
4641  * ----------
4642  */
4643 void
4645 {
4646  /* We assume this initializes to zeroes */
4647  static const PgStat_MsgBgWriter all_zeroes;
4648 
4649  /*
4650  * This function can be called even if nothing at all has happened. In
4651  * this case, avoid sending a completely empty message to the stats
4652  * collector.
4653  */
4654  if (memcmp(&BgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
4655  return;
4656 
4657  /*
4658  * Prepare and send the message
4659  */
4660  pgstat_setheader(&BgWriterStats.m_hdr, PGSTAT_MTYPE_BGWRITER);
4661  pgstat_send(&BgWriterStats, sizeof(BgWriterStats));
4662 
4663  /*
4664  * Clear out the statistics buffer, so it can be re-used.
4665  */
4666  MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
4667 }
4668 
4669 /* ----------
4670  * pgstat_send_wal() -
4671  *
4672  * Send WAL statistics to the collector
4673  * ----------
4674  */
4675 void
4677 {
4678  /* We assume this initializes to zeroes */
4679  static const PgStat_MsgWal all_zeroes;
4680 
4681  WalUsage walusage;
4682 
4683  /*
4684  * Calculate how much WAL usage counters are increased by substracting the
4685  * previous counters from the current ones. Fill the results in WAL stats
4686  * message.
4687  */
4688  MemSet(&walusage, 0, sizeof(WalUsage));
4689  WalUsageAccumDiff(&walusage, &pgWalUsage, &prevWalUsage);
4690 
4691  WalStats.m_wal_records = walusage.wal_records;
4692  WalStats.m_wal_fpi = walusage.wal_fpi;
4693  WalStats.m_wal_bytes = walusage.wal_bytes;
4694 
4695  /*
4696  * This function can be called even if nothing at all has happened. In
4697  * this case, avoid sending a completely empty message to the stats
4698  * collector.
4699  */
4700  if (memcmp(&WalStats, &all_zeroes, sizeof(PgStat_MsgWal)) == 0)
4701  return;
4702 
4703  /*
4704  * Prepare and send the message
4705  */
4707  pgstat_send(&WalStats, sizeof(WalStats));
4708 
4709  /*
4710  * Save the current counters for the subsequent calculation of WAL usage.
4711  */
4712  prevWalUsage = pgWalUsage;
4713 
4714  /*
4715  * Clear out the statistics buffer, so it can be re-used.
4716  */
4717  MemSet(&WalStats, 0, sizeof(WalStats));
4718 }
4719 
4720 /* ----------
4721  * pgstat_send_slru() -
4722  *
4723  * Send SLRU statistics to the collector
4724  * ----------
4725  */
4726 static void
4728 {
4729  /* We assume this initializes to zeroes */
4730  static const PgStat_MsgSLRU all_zeroes;
4731 
4732  for (int i = 0; i < SLRU_NUM_ELEMENTS; i++)
4733  {
4734  /*
4735  * This function can be called even if nothing at all has happened. In
4736  * this case, avoid sending a completely empty message to the stats
4737  * collector.
4738  */
4739  if (memcmp(&SLRUStats[i], &all_zeroes, sizeof(PgStat_MsgSLRU)) == 0)
4740  continue;
4741 
4742  /* set the SLRU type before each send */
4743  SLRUStats[i].m_index = i;
4744 
4745  /*
4746  * Prepare and send the message
4747  */
4748  pgstat_setheader(&SLRUStats[i].m_hdr, PGSTAT_MTYPE_SLRU);
4749  pgstat_send(&SLRUStats[i], sizeof(PgStat_MsgSLRU));
4750 
4751  /*
4752  * Clear out the statistics buffer, so it can be re-used.
4753  */
4754  MemSet(&SLRUStats[i], 0, sizeof(PgStat_MsgSLRU));
4755  }
4756 }
4757 
4758 
4759 /* ----------
4760  * PgstatCollectorMain() -
4761  *
4762  * Start up the statistics collector process. This is the body of the
4763  * postmaster child process.
4764  *
4765  * The argc/argv parameters are valid only in EXEC_BACKEND case.
4766  * ----------
4767  */
4768 NON_EXEC_STATIC void
4769 PgstatCollectorMain(int argc, char *argv[])
4770 {
4771  int len;
4772  PgStat_Msg msg;
4773  int wr;
4774  WaitEvent event;
4775  WaitEventSet *wes;
4776 
4777  /*
4778  * Ignore all signals usually bound to some action in the postmaster,
4779  * except SIGHUP and SIGQUIT. Note we don't need a SIGUSR1 handler to
4780  * support latch operations, because we only use a local latch.
4781  */
4783  pqsignal(SIGINT, SIG_IGN);
4784  pqsignal(SIGTERM, SIG_IGN);
4790  /* Reset some signals that are accepted by postmaster but not here */
4793 
4795  init_ps_display(NULL);
4796 
4797  /*
4798  * Read in existing stats files or initialize the stats to zero.
4799  */
4800  pgStatRunningInCollector = true;
4801  pgStatDBHash = pgstat_read_statsfiles(InvalidOid, true, true);
4802 
4803  /* Prepare to wait for our latch or data in our socket. */
4807  AddWaitEventToSet(wes, WL_SOCKET_READABLE, pgStatSock, NULL, NULL);
4808 
4809  /*
4810  * Loop to process messages until we get SIGQUIT or detect ungraceful
4811  * death of our parent postmaster.
4812  *
4813  * For performance reasons, we don't want to do ResetLatch/WaitLatch after
4814  * every message; instead, do that only after a recv() fails to obtain a
4815  * message. (This effectively means that if backends are sending us stuff
4816  * like mad, we won't notice postmaster death until things slack off a
4817  * bit; which seems fine.) To do that, we have an inner loop that
4818  * iterates as long as recv() succeeds. We do check ConfigReloadPending
4819  * inside the inner loop, which means that such interrupts will get
4820  * serviced but the latch won't get cleared until next time there is a
4821  * break in the action.
4822  */
4823  for (;;)
4824  {
4825  /* Clear any already-pending wakeups */
4827 
4828  /*
4829  * Quit if we get SIGQUIT from the postmaster.
4830  */
4832  break;
4833 
4834  /*
4835  * Inner loop iterates as long as we keep getting messages, or until
4836  * ShutdownRequestPending becomes set.
4837  */
4838  while (!ShutdownRequestPending)
4839  {
4840  /*
4841  * Reload configuration if we got SIGHUP from the postmaster.
4842  */
4843  if (ConfigReloadPending)
4844  {
4845  ConfigReloadPending = false;
4847  }
4848 
4849  /*
4850  * Write the stats file(s) if a new request has arrived that is
4851  * not satisfied by existing file(s).
4852  */
4854  pgstat_write_statsfiles(false, false);
4855 
4856  /*
4857  * Try to receive and process a message. This will not block,
4858  * since the socket is set to non-blocking mode.
4859  *
4860  * XXX On Windows, we have to force pgwin32_recv to cooperate,
4861  * despite the previous use of pg_set_noblock() on the socket.
4862  * This is extremely broken and should be fixed someday.
4863  */
4864 #ifdef WIN32
4865  pgwin32_noblock = 1;
4866 #endif
4867 
4868  len = recv(pgStatSock, (char *) &msg,
4869  sizeof(PgStat_Msg), 0);
4870 
4871 #ifdef WIN32
4872  pgwin32_noblock = 0;
4873 #endif
4874 
4875  if (len < 0)
4876  {
4877  if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
4878  break; /* out of inner loop */
4879  ereport(ERROR,
4881  errmsg("could not read statistics message: %m")));
4882  }
4883 
4884  /*
4885  * We ignore messages that are smaller than our common header
4886  */
4887  if (len < sizeof(PgStat_MsgHdr))
4888  continue;
4889 
4890  /*
4891  * The received length must match the length in the header
4892  */
4893  if (msg.msg_hdr.m_size != len)
4894  continue;
4895 
4896  /*
4897  * O.K. - we accept this message. Process it.
4898  */
4899  switch (msg.msg_hdr.m_type)
4900  {
4901  case PGSTAT_MTYPE_DUMMY:
4902  break;
4903 
4904  case PGSTAT_MTYPE_INQUIRY:
4905  pgstat_recv_inquiry(&msg.msg_inquiry, len);
4906  break;
4907 
4908  case PGSTAT_MTYPE_TABSTAT:
4909  pgstat_recv_tabstat(&msg.msg_tabstat, len);
4910  break;
4911 
4912  case PGSTAT_MTYPE_TABPURGE:
4913  pgstat_recv_tabpurge(&msg.msg_tabpurge, len);
4914  break;
4915 
4916  case PGSTAT_MTYPE_DROPDB:
4917  pgstat_recv_dropdb(&msg.msg_dropdb, len);
4918  break;
4919 
4921  pgstat_recv_resetcounter(&msg.msg_resetcounter, len);
4922  break;
4923 
4925  pgstat_recv_resetsharedcounter(&msg.msg_resetsharedcounter,
4926  len);
4927  break;
4928 
4930  pgstat_recv_resetsinglecounter(&msg.msg_resetsinglecounter,
4931  len);
4932  break;
4933 
4935  pgstat_recv_resetslrucounter(&msg.msg_resetslrucounter,
4936  len);
4937  break;
4938 
4940  pgstat_recv_resetreplslotcounter(&msg.msg_resetreplslotcounter,
4941  len);
4942  break;
4943 
4945  pgstat_recv_autovac(&msg.msg_autovacuum_start, len);
4946  break;
4947 
4948  case PGSTAT_MTYPE_VACUUM:
4949  pgstat_recv_vacuum(&msg.msg_vacuum, len);
4950  break;
4951 
4952  case PGSTAT_MTYPE_ANALYZE:
4953  pgstat_recv_analyze(&msg.msg_analyze, len);
4954  break;
4955 
4956  case PGSTAT_MTYPE_ARCHIVER:
4957  pgstat_recv_archiver(&msg.msg_archiver, len);
4958  break;
4959 
4960  case PGSTAT_MTYPE_BGWRITER:
4961  pgstat_recv_bgwriter(&msg.msg_bgwriter, len);
4962  break;
4963 
4964  case PGSTAT_MTYPE_WAL:
4965  pgstat_recv_wal(&msg.msg_wal, len);
4966  break;
4967 
4968  case PGSTAT_MTYPE_SLRU:
4969  pgstat_recv_slru(&msg.msg_slru, len);
4970  break;
4971 
4972  case PGSTAT_MTYPE_FUNCSTAT:
4973  pgstat_recv_funcstat(&msg.msg_funcstat, len);
4974  break;
4975 
4977  pgstat_recv_funcpurge(&msg.msg_funcpurge, len);
4978  break;
4979 
4981  pgstat_recv_recoveryconflict(&msg.msg_recoveryconflict,
4982  len);
4983  break;
4984 
4985  case PGSTAT_MTYPE_DEADLOCK:
4986  pgstat_recv_deadlock(&msg.msg_deadlock, len);
4987  break;
4988 
4989  case PGSTAT_MTYPE_TEMPFILE:
4990  pgstat_recv_tempfile(&msg.msg_tempfile, len);
4991  break;
4992 
4994  pgstat_recv_checksum_failure(&msg.msg_checksumfailure,
4995  len);
4996  break;
4997 
4998  case PGSTAT_MTYPE_REPLSLOT:
4999  pgstat_recv_replslot(&msg.msg_replslot, len);
5000  break;
5001 
5003  pgstat_recv_connstat(&msg.msg_conn, len);
5004  break;
5005 
5006  default:
5007  break;
5008  }
5009  } /* end of inner message-processing loop */
5010 
5011  /* Sleep until there's something to do */
5012 #ifndef WIN32
5013  wr = WaitEventSetWait(wes, -1L, &event, 1, WAIT_EVENT_PGSTAT_MAIN);
5014 #else
5015 
5016  /*
5017  * Windows, at least in its Windows Server 2003 R2 incarnation,
5018  * sometimes loses FD_READ events. Waking up and retrying the recv()
5019  * fixes that, so don't sleep indefinitely. This is a crock of the
5020  * first water, but until somebody wants to debug exactly what's
5021  * happening there, this is the best we can do. The two-second
5022  * timeout matches our pre-9.2 behavior, and needs to be short enough
5023  * to not provoke "using stale statistics" complaints from
5024  * backend_read_statsfile.
5025  */
5026  wr = WaitEventSetWait(wes, 2 * 1000L /* msec */ , &event, 1,
5028 #endif
5029 
5030  /*
5031  * Emergency bailout if postmaster has died. This is to avoid the
5032  * necessity for manual cleanup of all postmaster children.
5033  */
5034  if (wr == 1 && event.events == WL_POSTMASTER_DEATH)
5035  break;
5036  } /* end of outer loop */
5037 
5038  /*
5039  * Save the final stats to reuse at next startup.
5040  */
5041  pgstat_write_statsfiles(true, true);
5042 
5043  FreeWaitEventSet(wes);
5044 
5045  exit(0);
5046 }
5047 
5048 /*
5049  * Subroutine to clear stats in a database entry
5050  *
5051  * Tables and functions hashes are initialized to empty.
5052  */
5053 static void
5055 {
5056  HASHCTL hash_ctl;
5057 
5058  dbentry->n_xact_commit = 0;
5059  dbentry->n_xact_rollback = 0;
5060  dbentry->n_blocks_fetched = 0;
5061  dbentry->n_blocks_hit = 0;
5062  dbentry->n_tuples_returned = 0;
5063  dbentry->n_tuples_fetched = 0;
5064  dbentry->n_tuples_inserted = 0;
5065  dbentry->n_tuples_updated = 0;
5066  dbentry->n_tuples_deleted = 0;
5067  dbentry->last_autovac_time = 0;
5068  dbentry->n_conflict_tablespace = 0;
5069  dbentry->n_conflict_lock = 0;
5070  dbentry->n_conflict_snapshot = 0;
5071  dbentry->n_conflict_bufferpin = 0;
5072  dbentry->n_conflict_startup_deadlock = 0;
5073  dbentry->n_temp_files = 0;
5074  dbentry->n_temp_bytes = 0;
5075  dbentry->n_deadlocks = 0;
5076  dbentry->n_checksum_failures = 0;
5077  dbentry->last_checksum_failure = 0;
5078  dbentry->n_block_read_time = 0;
5079  dbentry->n_block_write_time = 0;
5080  dbentry->n_sessions = 0;
5081  dbentry->total_session_time = 0;
5082  dbentry->total_active_time = 0;
5083  dbentry->total_idle_in_xact_time = 0;
5084  dbentry->n_sessions_abandoned = 0;
5085  dbentry->n_sessions_fatal = 0;
5086  dbentry->n_sessions_killed = 0;
5087 
5089  dbentry->stats_timestamp = 0;
5090 
5091  hash_ctl.keysize = sizeof(Oid);
5092  hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
5093  dbentry->tables = hash_create("Per-database table",
5095  &hash_ctl,
5096  HASH_ELEM | HASH_BLOBS);
5097 
5098  hash_ctl.keysize = sizeof(Oid);
5099  hash_ctl.entrysize = sizeof(PgStat_StatFuncEntry);
5100  dbentry->functions = hash_create("Per-database function",
5102  &hash_ctl,
5103  HASH_ELEM | HASH_BLOBS);
5104 }
5105 
5106 /*
5107  * Lookup the hash table entry for the specified database. If no hash
5108  * table entry exists, initialize it, if the create parameter is true.
5109  * Else, return NULL.
5110  */
5111 static PgStat_StatDBEntry *
5112 pgstat_get_db_entry(Oid databaseid, bool create)
5113 {
5114  PgStat_StatDBEntry *result;
5115  bool found;
5116  HASHACTION action = (create ? HASH_ENTER : HASH_FIND);
5117 
5118  /* Lookup or create the hash table entry for this database */
5119  result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
5120  &databaseid,
5121  action, &found);
5122 
5123  if (!create && !found)
5124  return NULL;
5125 
5126  /*
5127  * If not found, initialize the new one. This creates empty hash tables
5128  * for tables and functions, too.
5129  */
5130  if (!found)
5131  reset_dbentry_counters(result);
5132 
5133  return result;
5134 }
5135 
5136 
5137 /*
5138  * Lookup the hash table entry for the specified table. If no hash
5139  * table entry exists, initialize it, if the create parameter is true.
5140  * Else, return NULL.
5141  */
5142 static PgStat_StatTabEntry *
5143 pgstat_get_tab_entry(PgStat_StatDBEntry *dbentry, Oid tableoid, bool create)
5144 {
5145  PgStat_StatTabEntry *result;
5146  bool found;
5147  HASHACTION action = (create ? HASH_ENTER : HASH_FIND);
5148 
5149  /* Lookup or create the hash table entry for this table */
5150  result = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
5151  &tableoid,
5152  action, &found);
5153 
5154  if (!create && !found)
5155  return NULL;
5156 
5157  /* If not found, initialize the new one. */
5158  if (!found)
5159  {
5160  result->numscans = 0;
5161  result->tuples_returned = 0;
5162  result->tuples_fetched = 0;
5163  result->tuples_inserted = 0;
5164  result->tuples_updated = 0;
5165  result->tuples_deleted = 0;
5166  result->tuples_hot_updated = 0;
5167  result->n_live_tuples = 0;
5168  result->n_dead_tuples = 0;
5169  result->changes_since_analyze = 0;
5170  result->inserts_since_vacuum = 0;
5171  result->blocks_fetched = 0;
5172  result->blocks_hit = 0;
5173  result->vacuum_timestamp = 0;
5174  result->vacuum_count = 0;
5175  result->autovac_vacuum_timestamp = 0;
5176  result->autovac_vacuum_count = 0;
5177  result->analyze_timestamp = 0;
5178  result->analyze_count = 0;
5179  result->autovac_analyze_timestamp = 0;
5180  result->autovac_analyze_count = 0;
5181  }
5182 
5183  return result;
5184 }
5185 
5186 
5187 /* ----------
5188  * pgstat_write_statsfiles() -
5189  * Write the global statistics file, as well as requested DB files.
5190  *
5191  * 'permanent' specifies writing to the permanent files not temporary ones.
5192  * When true (happens only when the collector is shutting down), also remove
5193  * the temporary files so that backends starting up under a new postmaster
5194  * can't read old data before the new collector is ready.
5195  *
5196  * When 'allDbs' is false, only the requested databases (listed in
5197  * pending_write_requests) will be written; otherwise, all databases
5198  * will be written.
5199  * ----------
5200  */
5201 static void
5202 pgstat_write_statsfiles(bool permanent, bool allDbs)
5203 {
5204  HASH_SEQ_STATUS hstat;
5205  PgStat_StatDBEntry *dbentry;
5206  FILE *fpout;
5207  int32 format_id;
5208  const char *tmpfile = permanent ? PGSTAT_STAT_PERMANENT_TMPFILE : pgstat_stat_tmpname;
5209  const char *statfile = permanent ? PGSTAT_STAT_PERMANENT_FILENAME : pgstat_stat_filename;
5210  int rc;
5211  int i;
5212 
5213  elog(DEBUG2, "writing stats file \"%s\"", statfile);
5214 
5215  /*
5216  * Open the statistics temp file to write out the current values.
5217  */
5218  fpout = AllocateFile(tmpfile, PG_BINARY_W);
5219  if (fpout == NULL)
5220  {
5221  ereport(LOG,
5223  errmsg("could not open temporary statistics file \"%s\": %m",
5224  tmpfile)));
5225  return;
5226  }
5227 
5228  /*
5229  * Set the timestamp of the stats file.
5230  */
5231  globalStats.stats_timestamp = GetCurrentTimestamp();
5232 
5233  /*
5234  * Write the file header --- currently just a format ID.
5235  */
5236  format_id = PGSTAT_FILE_FORMAT_ID;
5237  rc = fwrite(&format_id, sizeof(format_id), 1, fpout);
5238  (void) rc; /* we'll check for error with ferror */
5239 
5240  /*
5241  * Write global stats struct
5242  */
5243  rc = fwrite(&globalStats, sizeof(globalStats), 1, fpout);
5244  (void) rc; /* we'll check for error with ferror */
5245 
5246  /*
5247  * Write archiver stats struct
5248  */
5249  rc = fwrite(&archiverStats, sizeof(archiverStats), 1, fpout);
5250  (void) rc; /* we'll check for error with ferror */
5251 
5252  /*
5253  * Write WAL stats struct
5254  */
5255  rc = fwrite(&walStats, sizeof(walStats), 1, fpout);
5256  (void) rc; /* we'll check for error with ferror */
5257 
5258  /*
5259  * Write SLRU stats struct
5260  */
5261  rc = fwrite(slruStats, sizeof(slruStats), 1, fpout);
5262  (void) rc; /* we'll check for error with ferror */
5263 
5264  /*
5265  * Walk through the database table.
5266  */
5267  hash_seq_init(&hstat, pgStatDBHash);
5268  while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
5269  {
5270  /*
5271  * Write out the table and function stats for this DB into the
5272  * appropriate per-DB stat file, if required.
5273  */
5274  if (allDbs || pgstat_db_requested(dbentry->databaseid))
5275  {
5276  /* Make DB's timestamp consistent with the global stats */
5277  dbentry->stats_timestamp = globalStats.stats_timestamp;
5278 
5279  pgstat_write_db_statsfile(dbentry, permanent);
5280  }
5281 
5282  /*
5283  * Write out the DB entry. We don't write the tables or functions
5284  * pointers, since they're of no use to any other process.
5285  */
5286  fputc('D', fpout);
5287  rc = fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
5288  (void) rc; /* we'll check for error with ferror */
5289  }
5290 
5291  /*
5292  * Write replication slot stats struct
5293  */
5294  for (i = 0; i < nReplSlotStats; i++)
5295  {
5296  fputc('R', fpout);
5297  rc = fwrite(&replSlotStats[i], sizeof(PgStat_ReplSlotStats), 1, fpout);
5298  (void) rc; /* we'll check for error with ferror */
5299  }
5300 
5301  /*
5302  * No more output to be done. Close the temp file and replace the old
5303  * pgstat.stat with it. The ferror() check replaces testing for error
5304  * after each individual fputc or fwrite above.
5305  */
5306  fputc('E', fpout);
5307 
5308  if (ferror(fpout))
5309  {
5310  ereport(LOG,
5312  errmsg("could not write temporary statistics file \"%s\": %m",
5313  tmpfile)));
5314  FreeFile(fpout);
5315  unlink(tmpfile);
5316  }
5317  else if (FreeFile(fpout) < 0)
5318  {
5319  ereport(LOG,
5321  errmsg("could not close temporary statistics file \"%s\": %m",
5322  tmpfile)));
5323  unlink(tmpfile);
5324  }
5325  else if (rename(tmpfile, statfile) < 0)
5326  {
5327  ereport(LOG,
5329  errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
5330  tmpfile, statfile)));
5331  unlink(tmpfile);
5332  }
5333 
5334  if (permanent)
5335  unlink(pgstat_stat_filename);
5336 
5337  /*
5338  * Now throw away the list of requests. Note that requests sent after we
5339  * started the write are still waiting on the network socket.
5340  */
5341  list_free(pending_write_requests);
5342  pending_write_requests = NIL;
5343 }
5344 
5345 /*
5346  * return the filename for a DB stat file; filename is the output buffer,
5347  * of length len.
5348  */
5349 static void
5350 get_dbstat_filename(bool permanent, bool tempname, Oid databaseid,
5351  char *filename, int len)
5352 {
5353  int printed;
5354 
5355  /* NB -- pgstat_reset_remove_files knows about the pattern this uses */
5356  printed = snprintf(filename, len, "%s/db_%u.%s",
5357  permanent ? PGSTAT_STAT_PERMANENT_DIRECTORY :
5359  databaseid,
5360  tempname ? "tmp" : "stat");
5361  if (printed >= len)
5362  elog(ERROR, "overlength pgstat path");
5363 }
5364 
5365 /* ----------
5366  * pgstat_write_db_statsfile() -
5367  * Write the stat file for a single database.
5368  *
5369  * If writing to the permanent file (happens when the collector is
5370  * shutting down only), remove the temporary file so that backends
5371  * starting up under a new postmaster can't read the old data before
5372  * the new collector is ready.
5373  * ----------
5374  */
5375 static void
5377 {
5378  HASH_SEQ_STATUS tstat;
5380  PgStat_StatTabEntry *tabentry;
5381  PgStat_StatFuncEntry *funcentry;
5382  FILE *fpout;
5383  int32 format_id;
5384  Oid dbid = dbentry->databaseid;
5385  int rc;
5386  char tmpfile[MAXPGPATH];
5387  char statfile[MAXPGPATH];
5388 
5389  get_dbstat_filename(permanent, true, dbid, tmpfile, MAXPGPATH);
5390  get_dbstat_filename(permanent, false, dbid, statfile, MAXPGPATH);
5391 
5392  elog(DEBUG2, "writing stats file \"%s\"", statfile);
5393 
5394  /*
5395  * Open the statistics temp file to write out the current values.
5396  */
5397  fpout = AllocateFile(tmpfile, PG_BINARY_W);
5398  if (fpout == NULL)
5399  {
5400  ereport(LOG,
5402  errmsg("could not open temporary statistics file \"%s\": %m",
5403  tmpfile)));
5404  return;
5405  }
5406 
5407  /*
5408  * Write the file header --- currently just a format ID.
5409  */
5410  format_id = PGSTAT_FILE_FORMAT_ID;
5411  rc = fwrite(&format_id, sizeof(format_id), 1, fpout);
5412  (void) rc; /* we'll check for error with ferror */
5413 
5414  /*
5415  * Walk through the database's access stats per table.
5416  */
5417  hash_seq_init(&tstat, dbentry->tables);
5418  while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
5419  {
5420  fputc('T', fpout);
5421  rc = fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
5422  (void) rc; /* we'll check for error with ferror */
5423  }
5424 
5425  /*
5426  * Walk through the database's function stats table.
5427  */
5428  hash_seq_init(&fstat, dbentry->functions);
5429  while ((funcentry = (PgStat_StatFuncEntry *) hash_seq_search(&fstat)) != NULL)
5430  {
5431  fputc('F', fpout);
5432  rc = fwrite(funcentry, sizeof(PgStat_StatFuncEntry), 1, fpout);
5433  (void) rc; /* we'll check for error with ferror */
5434  }
5435 
5436  /*
5437  * No more output to be done. Close the temp file and replace the old
5438  * pgstat.stat with it. The ferror() check replaces testing for error
5439  * after each individual fputc or fwrite above.
5440  */
5441  fputc('E', fpout);
5442 
5443  if (ferror(fpout))
5444  {
5445  ereport(LOG,
5447  errmsg("could not write temporary statistics file \"%s\": %m",
5448  tmpfile)));
5449  FreeFile(fpout);
5450  unlink(tmpfile);
5451  }
5452  else if (FreeFile(fpout) < 0)
5453  {
5454  ereport(LOG,
5456  errmsg("could not close temporary statistics file \"%s\": %m",
5457  tmpfile)));
5458  unlink(tmpfile);
5459  }
5460  else if (rename(tmpfile, statfile) < 0)
5461  {
5462  ereport(LOG,
5464  errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
5465  tmpfile, statfile)));
5466  unlink(tmpfile);
5467  }
5468 
5469  if (permanent)
5470  {
5471  get_dbstat_filename(false, false, dbid, statfile, MAXPGPATH);
5472 
5473  elog(DEBUG2, "removing temporary stats file \"%s\"", statfile);
5474  unlink(statfile);
5475  }
5476 }
5477 
5478 /* ----------
5479  * pgstat_read_statsfiles() -
5480  *
5481  * Reads in some existing statistics collector files and returns the
5482  * databases hash table that is the top level of the data.
5483  *
5484  * If 'onlydb' is not InvalidOid, it means we only want data for that DB
5485  * plus the shared catalogs ("DB 0"). We'll still populate the DB hash
5486  * table for all databases, but we don't bother even creating table/function
5487  * hash tables for other databases.
5488  *
5489  * 'permanent' specifies reading from the permanent files not temporary ones.
5490  * When true (happens only when the collector is starting up), remove the
5491  * files after reading; the in-memory status is now authoritative, and the
5492  * files would be out of date in case somebody else reads them.
5493  *
5494  * If a 'deep' read is requested, table/function stats are read, otherwise
5495  * the table/function hash tables remain empty.
5496  * ----------
5497  */
5498 static HTAB *
5499 pgstat_read_statsfiles(Oid onlydb, bool permanent, bool deep)
5500 {
5501  PgStat_StatDBEntry *dbentry;
5502  PgStat_StatDBEntry dbbuf;
5503  HASHCTL hash_ctl;
5504  HTAB *dbhash;
5505  FILE *fpin;
5506  int32 format_id;
5507  bool found;
5508  const char *statfile = permanent ? PGSTAT_STAT_PERMANENT_FILENAME : pgstat_stat_filename;
5509  int i;
5510 
5511  /*
5512  * The tables will live in pgStatLocalContext.
5513  */
5515 
5516  /*
5517  * Create the DB hashtable
5518  */
5519  hash_ctl.keysize = sizeof(Oid);
5520  hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
5521  hash_ctl.hcxt = pgStatLocalContext;
5522  dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
5524 
5525  /* Allocate the space for replication slot statistics */
5526  replSlotStats = palloc0(max_replication_slots * sizeof(PgStat_ReplSlotStats));
5527  nReplSlotStats = 0;
5528 
5529  /*
5530  * Clear out global, archiver, WAL and SLRU statistics so they start from
5531  * zero in case we can't load an existing statsfile.
5532  */
5533  memset(&globalStats, 0, sizeof(globalStats));
5534  memset(&archiverStats, 0, sizeof(archiverStats));
5535  memset(&walStats, 0, sizeof(walStats));
5536  memset(&slruStats, 0, sizeof(slruStats));
5537 
5538  /*
5539  * Set the current timestamp (will be kept only in case we can't load an
5540  * existing statsfile).
5541  */
5542  globalStats.stat_reset_timestamp = GetCurrentTimestamp();
5543  archiverStats.stat_reset_timestamp = globalStats.stat_reset_timestamp;
5544  walStats.stat_reset_timestamp = globalStats.stat_reset_timestamp;
5545 
5546  /*
5547  * Set the same reset timestamp for all SLRU items too.
5548  */
5549  for (i = 0; i < SLRU_NUM_ELEMENTS; i++)
5550  slruStats[i].stat_reset_timestamp = globalStats.stat_reset_timestamp;
5551 
5552  /*
5553  * Set the same reset timestamp for all replication slots too.
5554  */
5555  for (i = 0; i < max_replication_slots; i++)
5556  replSlotStats[i].stat_reset_timestamp = globalStats.stat_reset_timestamp;
5557 
5558  /*
5559  * Try to open the stats file. If it doesn't exist, the backends simply
5560  * return zero for anything and the collector simply starts from scratch
5561  * with empty counters.
5562  *
5563  * ENOENT is a possibility if the stats collector is not running or has
5564  * not yet written the stats file the first time. Any other failure
5565  * condition is suspicious.
5566  */
5567  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
5568  {
5569  if (errno != ENOENT)
5572  errmsg("could not open statistics file \"%s\": %m",
5573  statfile)));
5574  return dbhash;
5575  }
5576 
5577  /*
5578  * Verify it's of the expected format.
5579  */
5580  if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id) ||
5581  format_id != PGSTAT_FILE_FORMAT_ID)
5582  {
5584  (errmsg("corrupted statistics file \"%s\"", statfile)));
5585  goto done;
5586  }
5587 
5588  /*
5589  * Read global stats struct
5590  */
5591  if (fread(&globalStats, 1, sizeof(globalStats), fpin) != sizeof(globalStats))
5592  {
5594  (errmsg("corrupted statistics file \"%s\"", statfile)));
5595  memset(&globalStats, 0, sizeof(globalStats));
5596  goto done;
5597  }
5598 
5599  /*
5600  * In the collector, disregard the timestamp we read from the permanent
5601  * stats file; we should be willing to write a temp stats file immediately
5602  * upon the first request from any backend. This only matters if the old
5603  * file's timestamp is less than PGSTAT_STAT_INTERVAL ago, but that's not
5604  * an unusual scenario.
5605  */
5607  globalStats.stats_timestamp = 0;
5608 
5609  /*
5610  * Read archiver stats struct
5611  */
5612  if (fread(&archiverStats, 1, sizeof(archiverStats), fpin) != sizeof(archiverStats))
5613  {
5615  (errmsg("corrupted statistics file \"%s\"", statfile)));
5616  memset(&archiverStats, 0, sizeof(archiverStats));
5617  goto done;
5618  }
5619 
5620  /*
5621  * Read WAL stats struct
5622  */
5623  if (fread(&walStats, 1, sizeof(walStats), fpin) != sizeof(walStats))
5624  {
5626  (errmsg("corrupted statistics file \"%s\"", statfile)));
5627  memset(&walStats, 0, sizeof(walStats));
5628  goto done;
5629  }
5630 
5631  /*
5632  * Read SLRU stats struct
5633  */
5634  if (fread(slruStats, 1, sizeof(slruStats), fpin) != sizeof(slruStats))
5635  {
5637  (errmsg("corrupted statistics file \"%s\"", statfile)));
5638  memset(&slruStats, 0, sizeof(slruStats));
5639  goto done;
5640  }
5641 
5642  /*
5643  * We found an existing collector stats file. Read it and put all the
5644  * hashtable entries into place.
5645  */
5646  for (;;)
5647  {
5648  switch (fgetc(fpin))
5649  {
5650  /*
5651  * 'D' A PgStat_StatDBEntry struct describing a database
5652  * follows.
5653  */
5654  case 'D':
5655  if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
5656  fpin) != offsetof(PgStat_StatDBEntry, tables))
5657  {
5659  (errmsg("corrupted statistics file \"%s\"",
5660  statfile)));
5661  goto done;
5662  }
5663 
5664  /*
5665  * Add to the DB hash
5666  */
5667  dbentry = (PgStat_StatDBEntry *) hash_search(dbhash,
5668  (void *) &dbbuf.databaseid,
5669  HASH_ENTER,
5670  &found);
5671  if (found)
5672  {
5674  (errmsg("corrupted statistics file \"%s\"",
5675  statfile)));
5676  goto done;
5677  }
5678 
5679  memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
5680  dbentry->tables = NULL;
5681  dbentry->functions = NULL;
5682 
5683  /*
5684  * In the collector, disregard the timestamp we read from the
5685  * permanent stats file; we should be willing to write a temp
5686  * stats file immediately upon the first request from any
5687  * backend.
5688  */
5690  dbentry->stats_timestamp = 0;
5691 
5692  /*
5693  * Don't create tables/functions hashtables for uninteresting
5694  * databases.
5695  */
5696  if (onlydb != InvalidOid)
5697  {
5698  if (dbbuf.databaseid != onlydb &&
5699  dbbuf.databaseid != InvalidOid)
5700  break;
5701  }
5702 
5703  hash_ctl.keysize = sizeof(Oid);
5704  hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
5705  hash_ctl.hcxt = pgStatLocalContext;
5706  dbentry->tables = hash_create("Per-database table",
5708  &hash_ctl,
5710 
5711  hash_ctl.keysize = sizeof(Oid);
5712  hash_ctl.entrysize = sizeof(PgStat_StatFuncEntry);
5713  hash_ctl.hcxt = pgStatLocalContext;
5714  dbentry->functions = hash_create("Per-database function",
5716  &hash_ctl,
5718 
5719  /*
5720  * If requested, read the data from the database-specific
5721  * file. Otherwise we just leave the hashtables empty.
5722  */
5723  if (deep)
5725  dbentry->tables,
5726  dbentry->functions,
5727  permanent);
5728 
5729  break;
5730 
5731  /*
5732  * 'R' A PgStat_ReplSlotStats struct describing a replication
5733  * slot follows.
5734  */
5735  case 'R':
5736  if (fread(&replSlotStats[nReplSlotStats], 1, sizeof(PgStat_ReplSlotStats), fpin)
5737  != sizeof(PgStat_ReplSlotStats))
5738  {
5740  (errmsg("corrupted statistics file \"%s\"",
5741  statfile)));
5742  memset(&replSlotStats[nReplSlotStats], 0, sizeof(PgStat_ReplSlotStats));
5743  goto done;
5744  }
5745  nReplSlotStats++;
5746  break;
5747 
5748  case 'E':
5749  goto done;
5750 
5751  default:
5753  (errmsg("corrupted statistics file \"%s\"",
5754  statfile)));
5755  goto done;
5756  }
5757  }
5758 
5759 done:
5760  FreeFile(fpin);
5761 
5762  /* If requested to read the permanent file, also get rid of it. */
5763  if (permanent)
5764  {
5765  elog(DEBUG2, "removing permanent stats file \"%s\"", statfile);
5766  unlink(statfile);
5767  }
5768 
5769  return dbhash;
5770 }
5771 
5772 
5773 /* ----------
5774  * pgstat_read_db_statsfile() -
5775  *
5776  * Reads in the existing statistics collector file for the given database,
5777  * filling the passed-in tables and functions hash tables.
5778  *
5779  * As in pgstat_read_statsfiles, if the permanent file is requested, it is
5780  * removed after reading.
5781  *
5782  * Note: this code has the ability to skip storing per-table or per-function
5783  * data, if NULL is passed for the corresponding hashtable. That's not used
5784  * at the moment though.
5785  * ----------
5786  */
5787 static void
5788 pgstat_read_db_statsfile(Oid databaseid, HTAB *tabhash, HTAB *funchash,
5789  bool permanent)
5790 {
5791  PgStat_StatTabEntry *tabentry;
5792  PgStat_StatTabEntry tabbuf;
5793  PgStat_StatFuncEntry funcbuf;
5794  PgStat_StatFuncEntry *funcentry;
5795  FILE *fpin;
5796  int32 format_id;
5797  bool found;
5798  char statfile[MAXPGPATH];
5799 
5800  get_dbstat_filename(permanent, false, databaseid, statfile, MAXPGPATH);
5801 
5802  /*
5803  * Try to open the stats file. If it doesn't exist, the backends simply
5804  * return zero for anything and the collector simply starts from scratch
5805  * with empty counters.
5806  *
5807  * ENOENT is a possibility if the stats collector is not running or has
5808  * not yet written the stats file the first time. Any other failure
5809  * condition is suspicious.
5810  */
5811  if ((fpin = AllocateFile(statfile, PG_BINARY_R)) == NULL)
5812  {
5813  if (errno != ENOENT)
5816  errmsg("could not open statistics file \"%s\": %m",
5817  statfile)));
5818  return;
5819  }
5820 
5821  /*
5822  * Verify it's of the expected format.
5823  */
5824  if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id) ||
5825  format_id != PGSTAT_FILE_FORMAT_ID)
5826  {
5828  (errmsg("corrupted statistics file \"%s\"", statfile)));
5829  goto done;
5830  }
5831 
5832  /*
5833  * We found an existing collector stats file. Read it and put all the
5834  * hashtable entries into place.
5835  */
5836  for (;;)
5837  {
5838  switch (fgetc(fpin))
5839  {
5840  /*
5841  * 'T' A PgStat_StatTabEntry follows.
5842  */
5843  case 'T':
5844  if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
5845  fpin) != sizeof(PgStat_StatTabEntry))
5846  {
5848  (errmsg("corrupted statistics file \"%s\"",
5849  statfile)));
5850  goto done;
5851  }
5852 
5853  /*
5854  * Skip if table data not wanted.
5855  */
5856  if (tabhash == NULL)
5857  break;
5858 
5859  tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
5860  (void *) &tabbuf.tableid,
5861  HASH_ENTER, &found);
5862 
5863  if (found)
5864  {
5866  (errmsg("corrupted statistics file \"%s\"",
5867  statfile)));
5868  goto done;
5869  }
5870 
5871  memcpy(tabentry, &tabbuf, sizeof(tabbuf));
5872  break;
5873 
5874  /*
5875  * 'F' A PgStat_StatFuncEntry follows.
5876  */
5877  case 'F':
5878  if (fread(&funcbuf, 1, sizeof(PgStat_StatFuncEntry),
5879  fpin) != sizeof(PgStat_StatFuncEntry))
5880  {
5882  (errmsg("corrupted statistics file \"%s\"",
5883  statfile)));
5884  goto done;
5885  }
5886 
5887  /*
5888  * Skip if function data not wanted.
5889  */
5890  if (funchash == NULL)
5891  break;
5892 
5893  funcentry = (PgStat_StatFuncEntry *) hash_search(funchash,
5894  (void *) &funcbuf.functionid,
5895  HASH_ENTER, &found);
5896 
5897  if (found)
5898  {
5899  e