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