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