]> granicus.if.org Git - postgresql/blob - src/backend/postmaster/pgstat.c
Just-in-time background writing strategy. This code avoids re-scanning
[postgresql] / src / backend / postmaster / pgstat.c
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-2007, PostgreSQL Global Development Group
15  *
16  *      $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.166 2007/09/25 20:03:37 tgl Exp $
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_POLL_H
32 #include <poll.h>
33 #endif
34 #ifdef HAVE_SYS_POLL_H
35 #include <sys/poll.h>
36 #endif
37
38 #include "pgstat.h"
39
40 #include "access/heapam.h"
41 #include "access/transam.h"
42 #include "access/twophase_rmgr.h"
43 #include "access/xact.h"
44 #include "catalog/pg_database.h"
45 #include "libpq/ip.h"
46 #include "libpq/libpq.h"
47 #include "libpq/pqsignal.h"
48 #include "mb/pg_wchar.h"
49 #include "miscadmin.h"
50 #include "postmaster/autovacuum.h"
51 #include "postmaster/fork_process.h"
52 #include "postmaster/postmaster.h"
53 #include "storage/backendid.h"
54 #include "storage/fd.h"
55 #include "storage/ipc.h"
56 #include "storage/pg_shmem.h"
57 #include "storage/pmsignal.h"
58 #include "utils/guc.h"
59 #include "utils/memutils.h"
60 #include "utils/ps_status.h"
61
62
63 /* ----------
64  * Paths for the statistics files (relative to installation's $PGDATA).
65  * ----------
66  */
67 #define PGSTAT_STAT_FILENAME    "global/pgstat.stat"
68 #define PGSTAT_STAT_TMPFILE             "global/pgstat.tmp"
69
70 /* ----------
71  * Timer definitions.
72  * ----------
73  */
74 #define PGSTAT_STAT_INTERVAL    500             /* How often to write the status file;
75                                                                                  * in milliseconds. */
76
77 #define PGSTAT_RESTART_INTERVAL 60              /* How often to attempt to restart a
78                                                                                  * failed statistics collector; in
79                                                                                  * seconds. */
80
81 #define PGSTAT_SELECT_TIMEOUT   2               /* How often to check for postmaster
82                                                                                  * death; in seconds. */
83
84
85 /* ----------
86  * The initial size hints for the hash tables used in the collector.
87  * ----------
88  */
89 #define PGSTAT_DB_HASH_SIZE             16
90 #define PGSTAT_TAB_HASH_SIZE    512
91
92
93 /* ----------
94  * GUC parameters
95  * ----------
96  */
97 bool            pgstat_track_activities = false;
98 bool            pgstat_track_counts = false;
99
100 /*
101  * BgWriter global statistics counters (unused in other processes).
102  * Stored directly in a stats message structure so it can be sent
103  * without needing to copy things around.  We assume this inits to zeroes.
104  */
105 PgStat_MsgBgWriter BgWriterStats;
106
107 /* ----------
108  * Local data
109  * ----------
110  */
111 NON_EXEC_STATIC int pgStatSock = -1;
112
113 static struct sockaddr_storage pgStatAddr;
114
115 static time_t last_pgstat_start_time;
116
117 static bool pgStatRunningInCollector = false;
118
119 /*
120  * Structures in which backends store per-table info that's waiting to be
121  * sent to the collector.
122  *
123  * NOTE: once allocated, TabStatusArray structures are never moved or deleted
124  * for the life of the backend.  Also, we zero out the t_id fields of the
125  * contained PgStat_TableStatus structs whenever they are not actively in use.
126  * This allows relcache pgstat_info pointers to be treated as long-lived data,
127  * avoiding repeated searches in pgstat_initstats() when a relation is
128  * repeatedly opened during a transaction.
129  */
130 #define TABSTAT_QUANTUM         100                     /* we alloc this many at a time */
131
132 typedef struct TabStatusArray
133 {
134         struct TabStatusArray *tsa_next;        /* link to next array, if any */
135         int                     tsa_used;                               /* # entries currently used */
136         PgStat_TableStatus tsa_entries[TABSTAT_QUANTUM];        /* per-table data */
137 } TabStatusArray;
138
139 static TabStatusArray *pgStatTabList = NULL;
140
141 /*
142  * Tuple insertion/deletion counts for an open transaction can't be propagated
143  * into PgStat_TableStatus counters until we know if it is going to commit
144  * or abort.  Hence, we keep these counts in per-subxact structs that live
145  * in TopTransactionContext.  This data structure is designed on the assumption
146  * that subxacts won't usually modify very many tables.
147  */
148 typedef struct PgStat_SubXactStatus
149 {
150         int                     nest_level;                             /* subtransaction nest level */
151         struct PgStat_SubXactStatus *prev;      /* higher-level subxact if any */
152         PgStat_TableXactStatus *first;          /* head of list for this subxact */
153 } PgStat_SubXactStatus;
154
155 static PgStat_SubXactStatus *pgStatXactStack = NULL;
156
157 static int      pgStatXactCommit = 0;
158 static int      pgStatXactRollback = 0;
159
160 /* Record that's written to 2PC state file when pgstat state is persisted */
161 typedef struct TwoPhasePgStatRecord
162 {
163         PgStat_Counter tuples_inserted; /* tuples inserted in xact */
164         PgStat_Counter tuples_deleted;  /* tuples deleted in xact */
165         Oid                     t_id;                           /* table's OID */
166         bool            t_shared;                       /* is it a shared catalog? */
167 } TwoPhasePgStatRecord;
168
169 /*
170  * Info about current "snapshot" of stats file
171  */
172 static MemoryContext pgStatLocalContext = NULL;
173 static HTAB *pgStatDBHash = NULL;
174 static PgBackendStatus *localBackendStatusTable = NULL;
175 static int      localNumBackends = 0;
176
177 /*
178  * Cluster wide statistics, kept in the stats collector.
179  * Contains statistics that are not collected per database
180  * or per table.
181  */
182 static PgStat_GlobalStats globalStats;
183
184 static volatile bool need_exit = false;
185 static volatile bool need_statwrite = false;
186
187
188 /* ----------
189  * Local function forward declarations
190  * ----------
191  */
192 #ifdef EXEC_BACKEND
193 static pid_t pgstat_forkexec(void);
194 #endif
195
196 NON_EXEC_STATIC void PgstatCollectorMain(int argc, char *argv[]);
197 static void pgstat_exit(SIGNAL_ARGS);
198 static void force_statwrite(SIGNAL_ARGS);
199 static void pgstat_beshutdown_hook(int code, Datum arg);
200
201 static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, bool create);
202 static void pgstat_write_statsfile(void);
203 static HTAB *pgstat_read_statsfile(Oid onlydb);
204 static void backend_read_statsfile(void);
205 static void pgstat_read_current_status(void);
206
207 static void pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg);
208 static HTAB *pgstat_collect_oids(Oid catalogid);
209
210 static PgStat_TableStatus *get_tabstat_entry(Oid rel_id, bool isshared);
211
212 static void pgstat_setup_memcxt(void);
213
214 static void pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype);
215 static void pgstat_send(void *msg, int len);
216
217 static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
218 static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
219 static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
220 static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
221 static void pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len);
222 static void pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len);
223 static void pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len);
224 static void pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len);
225
226
227 /* ------------------------------------------------------------
228  * Public functions called from postmaster follow
229  * ------------------------------------------------------------
230  */
231
232 /* ----------
233  * pgstat_init() -
234  *
235  *      Called from postmaster at startup. Create the resources required
236  *      by the statistics collector process.  If unable to do so, do not
237  *      fail --- better to let the postmaster start with stats collection
238  *      disabled.
239  * ----------
240  */
241 void
242 pgstat_init(void)
243 {
244         ACCEPT_TYPE_ARG3 alen;
245         struct addrinfo *addrs = NULL,
246                            *addr,
247                                 hints;
248         int                     ret;
249         fd_set          rset;
250         struct timeval tv;
251         char            test_byte;
252         int                     sel_res;
253         int                     tries = 0;
254
255 #define TESTBYTEVAL ((char) 199)
256
257         /*
258          * Create the UDP socket for sending and receiving statistic messages
259          */
260         hints.ai_flags = AI_PASSIVE;
261         hints.ai_family = PF_UNSPEC;
262         hints.ai_socktype = SOCK_DGRAM;
263         hints.ai_protocol = 0;
264         hints.ai_addrlen = 0;
265         hints.ai_addr = NULL;
266         hints.ai_canonname = NULL;
267         hints.ai_next = NULL;
268         ret = pg_getaddrinfo_all("localhost", NULL, &hints, &addrs);
269         if (ret || !addrs)
270         {
271                 ereport(LOG,
272                                 (errmsg("could not resolve \"localhost\": %s",
273                                                 gai_strerror(ret))));
274                 goto startup_failed;
275         }
276
277         /*
278          * On some platforms, pg_getaddrinfo_all() may return multiple addresses
279          * only one of which will actually work (eg, both IPv6 and IPv4 addresses
280          * when kernel will reject IPv6).  Worse, the failure may occur at the
281          * bind() or perhaps even connect() stage.      So we must loop through the
282          * results till we find a working combination. We will generate LOG
283          * messages, but no error, for bogus combinations.
284          */
285         for (addr = addrs; addr; addr = addr->ai_next)
286         {
287 #ifdef HAVE_UNIX_SOCKETS
288                 /* Ignore AF_UNIX sockets, if any are returned. */
289                 if (addr->ai_family == AF_UNIX)
290                         continue;
291 #endif
292
293                 if (++tries > 1)
294                         ereport(LOG,
295                         (errmsg("trying another address for the statistics collector")));
296
297                 /*
298                  * Create the socket.
299                  */
300                 if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) < 0)
301                 {
302                         ereport(LOG,
303                                         (errcode_for_socket_access(),
304                         errmsg("could not create socket for statistics collector: %m")));
305                         continue;
306                 }
307
308                 /*
309                  * Bind it to a kernel assigned port on localhost and get the assigned
310                  * port via getsockname().
311                  */
312                 if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
313                 {
314                         ereport(LOG,
315                                         (errcode_for_socket_access(),
316                           errmsg("could not bind socket for statistics collector: %m")));
317                         closesocket(pgStatSock);
318                         pgStatSock = -1;
319                         continue;
320                 }
321
322                 alen = sizeof(pgStatAddr);
323                 if (getsockname(pgStatSock, (struct sockaddr *) & pgStatAddr, &alen) < 0)
324                 {
325                         ereport(LOG,
326                                         (errcode_for_socket_access(),
327                                          errmsg("could not get address of socket for statistics collector: %m")));
328                         closesocket(pgStatSock);
329                         pgStatSock = -1;
330                         continue;
331                 }
332
333                 /*
334                  * Connect the socket to its own address.  This saves a few cycles by
335                  * not having to respecify the target address on every send. This also
336                  * provides a kernel-level check that only packets from this same
337                  * address will be received.
338                  */
339                 if (connect(pgStatSock, (struct sockaddr *) & pgStatAddr, alen) < 0)
340                 {
341                         ereport(LOG,
342                                         (errcode_for_socket_access(),
343                         errmsg("could not connect socket for statistics collector: %m")));
344                         closesocket(pgStatSock);
345                         pgStatSock = -1;
346                         continue;
347                 }
348
349                 /*
350                  * Try to send and receive a one-byte test message on the socket. This
351                  * is to catch situations where the socket can be created but will not
352                  * actually pass data (for instance, because kernel packet filtering
353                  * rules prevent it).
354                  */
355                 test_byte = TESTBYTEVAL;
356
357 retry1:
358                 if (send(pgStatSock, &test_byte, 1, 0) != 1)
359                 {
360                         if (errno == EINTR)
361                                 goto retry1;    /* if interrupted, just retry */
362                         ereport(LOG,
363                                         (errcode_for_socket_access(),
364                                          errmsg("could not send test message on socket for statistics collector: %m")));
365                         closesocket(pgStatSock);
366                         pgStatSock = -1;
367                         continue;
368                 }
369
370                 /*
371                  * There could possibly be a little delay before the message can be
372                  * received.  We arbitrarily allow up to half a second before deciding
373                  * it's broken.
374                  */
375                 for (;;)                                /* need a loop to handle EINTR */
376                 {
377                         FD_ZERO(&rset);
378                         FD_SET(pgStatSock, &rset);
379                         tv.tv_sec = 0;
380                         tv.tv_usec = 500000;
381                         sel_res = select(pgStatSock + 1, &rset, NULL, NULL, &tv);
382                         if (sel_res >= 0 || errno != EINTR)
383                                 break;
384                 }
385                 if (sel_res < 0)
386                 {
387                         ereport(LOG,
388                                         (errcode_for_socket_access(),
389                                          errmsg("select() failed in statistics collector: %m")));
390                         closesocket(pgStatSock);
391                         pgStatSock = -1;
392                         continue;
393                 }
394                 if (sel_res == 0 || !FD_ISSET(pgStatSock, &rset))
395                 {
396                         /*
397                          * This is the case we actually think is likely, so take pains to
398                          * give a specific message for it.
399                          *
400                          * errno will not be set meaningfully here, so don't use it.
401                          */
402                         ereport(LOG,
403                                         (errcode(ERRCODE_CONNECTION_FAILURE),
404                                          errmsg("test message did not get through on socket for statistics collector")));
405                         closesocket(pgStatSock);
406                         pgStatSock = -1;
407                         continue;
408                 }
409
410                 test_byte++;                    /* just make sure variable is changed */
411
412 retry2:
413                 if (recv(pgStatSock, &test_byte, 1, 0) != 1)
414                 {
415                         if (errno == EINTR)
416                                 goto retry2;    /* if interrupted, just retry */
417                         ereport(LOG,
418                                         (errcode_for_socket_access(),
419                                          errmsg("could not receive test message on socket for statistics collector: %m")));
420                         closesocket(pgStatSock);
421                         pgStatSock = -1;
422                         continue;
423                 }
424
425                 if (test_byte != TESTBYTEVAL)   /* strictly paranoia ... */
426                 {
427                         ereport(LOG,
428                                         (errcode(ERRCODE_INTERNAL_ERROR),
429                                          errmsg("incorrect test message transmission on socket for statistics collector")));
430                         closesocket(pgStatSock);
431                         pgStatSock = -1;
432                         continue;
433                 }
434
435                 /* If we get here, we have a working socket */
436                 break;
437         }
438
439         /* Did we find a working address? */
440         if (!addr || pgStatSock < 0)
441                 goto startup_failed;
442
443         /*
444          * Set the socket to non-blocking IO.  This ensures that if the collector
445          * falls behind, statistics messages will be discarded; backends won't
446          * block waiting to send messages to the collector.
447          */
448         if (!pg_set_noblock(pgStatSock))
449         {
450                 ereport(LOG,
451                                 (errcode_for_socket_access(),
452                                  errmsg("could not set statistics collector socket to nonblocking mode: %m")));
453                 goto startup_failed;
454         }
455
456         pg_freeaddrinfo_all(hints.ai_family, addrs);
457
458         return;
459
460 startup_failed:
461         ereport(LOG,
462           (errmsg("disabling statistics collector for lack of working socket")));
463
464         if (addrs)
465                 pg_freeaddrinfo_all(hints.ai_family, addrs);
466
467         if (pgStatSock >= 0)
468                 closesocket(pgStatSock);
469         pgStatSock = -1;
470
471         /*
472          * Adjust GUC variables to suppress useless activity, and for debugging
473          * purposes (seeing track_counts off is a clue that we failed here).
474          * We use PGC_S_OVERRIDE because there is no point in trying to turn it
475          * back on from postgresql.conf without a restart.
476          */
477         SetConfigOption("track_counts", "off", PGC_INTERNAL, PGC_S_OVERRIDE);
478 }
479
480 /*
481  * pgstat_reset_all() -
482  *
483  * Remove the stats file.  This is currently used only if WAL
484  * recovery is needed after a crash.
485  */
486 void
487 pgstat_reset_all(void)
488 {
489         unlink(PGSTAT_STAT_FILENAME);
490 }
491
492 #ifdef EXEC_BACKEND
493
494 /*
495  * pgstat_forkexec() -
496  *
497  * Format up the arglist for, then fork and exec, statistics collector process
498  */
499 static pid_t
500 pgstat_forkexec(void)
501 {
502         char       *av[10];
503         int                     ac = 0;
504
505         av[ac++] = "postgres";
506         av[ac++] = "--forkcol";
507         av[ac++] = NULL;                        /* filled in by postmaster_forkexec */
508
509         av[ac] = NULL;
510         Assert(ac < lengthof(av));
511
512         return postmaster_forkexec(ac, av);
513 }
514 #endif   /* EXEC_BACKEND */
515
516
517 /*
518  * pgstat_start() -
519  *
520  *      Called from postmaster at startup or after an existing collector
521  *      died.  Attempt to fire up a fresh statistics collector.
522  *
523  *      Returns PID of child process, or 0 if fail.
524  *
525  *      Note: if fail, we will be called again from the postmaster main loop.
526  */
527 int
528 pgstat_start(void)
529 {
530         time_t          curtime;
531         pid_t           pgStatPid;
532
533         /*
534          * Check that the socket is there, else pgstat_init failed and we can
535          * do nothing useful.
536          */
537         if (pgStatSock < 0)
538                 return 0;
539
540         /*
541          * Do nothing if too soon since last collector start.  This is a safety
542          * valve to protect against continuous respawn attempts if the collector
543          * is dying immediately at launch.      Note that since we will be re-called
544          * from the postmaster main loop, we will get another chance later.
545          */
546         curtime = time(NULL);
547         if ((unsigned int) (curtime - last_pgstat_start_time) <
548                 (unsigned int) PGSTAT_RESTART_INTERVAL)
549                 return 0;
550         last_pgstat_start_time = curtime;
551
552         /*
553          * Okay, fork off the collector.
554          */
555 #ifdef EXEC_BACKEND
556         switch ((pgStatPid = pgstat_forkexec()))
557 #else
558         switch ((pgStatPid = fork_process()))
559 #endif
560         {
561                 case -1:
562                         ereport(LOG,
563                                         (errmsg("could not fork statistics collector: %m")));
564                         return 0;
565
566 #ifndef EXEC_BACKEND
567                 case 0:
568                         /* in postmaster child ... */
569                         /* Close the postmaster's sockets */
570                         ClosePostmasterPorts(false);
571
572                         /* Lose the postmaster's on-exit routines */
573                         on_exit_reset();
574
575                         /* Drop our connection to postmaster's shared memory, as well */
576                         PGSharedMemoryDetach();
577
578                         PgstatCollectorMain(0, NULL);
579                         break;
580 #endif
581
582                 default:
583                         return (int) pgStatPid;
584         }
585
586         /* shouldn't get here */
587         return 0;
588 }
589
590 void allow_immediate_pgstat_restart(void)
591 {
592                 last_pgstat_start_time = 0;
593 }
594
595 /* ------------------------------------------------------------
596  * Public functions used by backends follow
597  *------------------------------------------------------------
598  */
599
600
601 /* ----------
602  * pgstat_report_tabstat() -
603  *
604  *      Called from tcop/postgres.c to send the so far collected per-table
605  *      access statistics to the collector.  Note that this is called only
606  *      when not within a transaction, so it is fair to use transaction stop
607  *      time as an approximation of current time.
608  * ----------
609  */
610 void
611 pgstat_report_tabstat(bool force)
612 {
613         /* we assume this inits to all zeroes: */
614         static const PgStat_TableCounts all_zeroes;
615         static TimestampTz last_report = 0;     
616
617         TimestampTz now;
618         PgStat_MsgTabstat regular_msg;
619         PgStat_MsgTabstat shared_msg;
620         TabStatusArray *tsa;
621         int                     i;
622
623         /* Don't expend a clock check if nothing to do */
624         if (pgStatTabList == NULL ||
625                 pgStatTabList->tsa_used == 0)
626                 return;
627
628         /*
629          * Don't send a message unless it's been at least PGSTAT_STAT_INTERVAL
630          * msec since we last sent one, or the caller wants to force stats out.
631          */
632         now = GetCurrentTransactionStopTimestamp();
633         if (!force &&
634                 !TimestampDifferenceExceeds(last_report, now, PGSTAT_STAT_INTERVAL))
635                 return;
636         last_report = now;
637
638         /*
639          * Scan through the TabStatusArray struct(s) to find tables that actually
640          * have counts, and build messages to send.  We have to separate shared
641          * relations from regular ones because the databaseid field in the
642          * message header has to depend on that.
643          */
644         regular_msg.m_databaseid = MyDatabaseId;
645         shared_msg.m_databaseid = InvalidOid;
646         regular_msg.m_nentries = 0;
647         shared_msg.m_nentries = 0;
648
649         for (tsa = pgStatTabList; tsa != NULL; tsa = tsa->tsa_next)
650         {
651                 for (i = 0; i < tsa->tsa_used; i++)
652                 {
653                         PgStat_TableStatus *entry = &tsa->tsa_entries[i];
654                         PgStat_MsgTabstat *this_msg;
655                         PgStat_TableEntry *this_ent;
656
657                         /* Shouldn't have any pending transaction-dependent counts */
658                         Assert(entry->trans == NULL);
659
660                         /*
661                          * Ignore entries that didn't accumulate any actual counts,
662                          * such as indexes that were opened by the planner but not used.
663                          */
664                         if (memcmp(&entry->t_counts, &all_zeroes,
665                                            sizeof(PgStat_TableCounts)) == 0)
666                                 continue;
667                         /*
668                          * OK, insert data into the appropriate message, and send if full.
669                          */
670                         this_msg = entry->t_shared ? &shared_msg : &regular_msg;
671                         this_ent = &this_msg->m_entry[this_msg->m_nentries];
672                         this_ent->t_id = entry->t_id;
673                         memcpy(&this_ent->t_counts, &entry->t_counts,
674                                    sizeof(PgStat_TableCounts));
675                         if (++this_msg->m_nentries >= PGSTAT_NUM_TABENTRIES)
676                         {
677                                 pgstat_send_tabstat(this_msg);
678                                 this_msg->m_nentries = 0;
679                         }
680                 }
681                 /* zero out TableStatus structs after use */
682                 MemSet(tsa->tsa_entries, 0,
683                            tsa->tsa_used * sizeof(PgStat_TableStatus));
684                 tsa->tsa_used = 0;
685         }
686
687         /*
688          * Send partial messages.  If force is true, make sure that any pending
689          * xact commit/abort gets counted, even if no table stats to send.
690          */
691         if (regular_msg.m_nentries > 0 ||
692                 (force && (pgStatXactCommit > 0 || pgStatXactRollback > 0)))
693                 pgstat_send_tabstat(&regular_msg);
694         if (shared_msg.m_nentries > 0)
695                 pgstat_send_tabstat(&shared_msg);
696 }
697
698 /*
699  * Subroutine for pgstat_report_tabstat: finish and send a tabstat message
700  */
701 static void
702 pgstat_send_tabstat(PgStat_MsgTabstat *tsmsg)
703 {
704         int                     n;
705         int                     len;
706
707         /* It's unlikely we'd get here with no socket, but maybe not impossible */
708         if (pgStatSock < 0)
709                 return;
710
711         /*
712          * Report accumulated xact commit/rollback whenever we send a normal
713          * tabstat message
714          */
715         if (OidIsValid(tsmsg->m_databaseid))
716         {
717                 tsmsg->m_xact_commit = pgStatXactCommit;
718                 tsmsg->m_xact_rollback = pgStatXactRollback;
719                 pgStatXactCommit = 0;
720                 pgStatXactRollback = 0;
721         }
722         else
723         {
724                 tsmsg->m_xact_commit = 0;
725                 tsmsg->m_xact_rollback = 0;
726         }
727
728         n = tsmsg->m_nentries;
729         len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
730                 n * sizeof(PgStat_TableEntry);
731
732         pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
733         pgstat_send(tsmsg, len);
734 }
735
736
737 /* ----------
738  * pgstat_vacuum_tabstat() -
739  *
740  *      Will tell the collector about objects he can get rid of.
741  * ----------
742  */
743 void
744 pgstat_vacuum_tabstat(void)
745 {
746         HTAB       *htab;
747         PgStat_MsgTabpurge msg;
748         HASH_SEQ_STATUS hstat;
749         PgStat_StatDBEntry *dbentry;
750         PgStat_StatTabEntry *tabentry;
751         int                     len;
752
753         if (pgStatSock < 0)
754                 return;
755
756         /*
757          * If not done for this transaction, read the statistics collector stats
758          * file into some hash tables.
759          */
760         backend_read_statsfile();
761
762         /*
763          * Read pg_database and make a list of OIDs of all existing databases
764          */
765         htab = pgstat_collect_oids(DatabaseRelationId);
766
767         /*
768          * Search the database hash table for dead databases and tell the
769          * collector to drop them.
770          */
771         hash_seq_init(&hstat, pgStatDBHash);
772         while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
773         {
774                 Oid                     dbid = dbentry->databaseid;
775
776                 CHECK_FOR_INTERRUPTS();
777
778                 /* the DB entry for shared tables (with InvalidOid) is never dropped */
779                 if (OidIsValid(dbid) &&
780                         hash_search(htab, (void *) &dbid, HASH_FIND, NULL) == NULL)
781                         pgstat_drop_database(dbid);
782         }
783
784         /* Clean up */
785         hash_destroy(htab);
786
787         /*
788          * Lookup our own database entry; if not found, nothing more to do.
789          */
790         dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
791                                                                                                  (void *) &MyDatabaseId,
792                                                                                                  HASH_FIND, NULL);
793         if (dbentry == NULL || dbentry->tables == NULL)
794                 return;
795
796         /*
797          * Similarly to above, make a list of all known relations in this DB.
798          */
799         htab = pgstat_collect_oids(RelationRelationId);
800
801         /*
802          * Initialize our messages table counter to zero
803          */
804         msg.m_nentries = 0;
805
806         /*
807          * Check for all tables listed in stats hashtable if they still exist.
808          */
809         hash_seq_init(&hstat, dbentry->tables);
810         while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
811         {
812                 Oid                     tabid = tabentry->tableid;
813
814                 CHECK_FOR_INTERRUPTS();
815
816                 if (hash_search(htab, (void *) &tabid, HASH_FIND, NULL) != NULL)
817                         continue;
818
819                 /*
820                  * Not there, so add this table's Oid to the message
821                  */
822                 msg.m_tableid[msg.m_nentries++] = tabid;
823
824                 /*
825                  * If the message is full, send it out and reinitialize to empty
826                  */
827                 if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
828                 {
829                         len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
830                                 +msg.m_nentries * sizeof(Oid);
831
832                         pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
833                         msg.m_databaseid = MyDatabaseId;
834                         pgstat_send(&msg, len);
835
836                         msg.m_nentries = 0;
837                 }
838         }
839
840         /*
841          * Send the rest
842          */
843         if (msg.m_nentries > 0)
844         {
845                 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
846                         +msg.m_nentries * sizeof(Oid);
847
848                 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
849                 msg.m_databaseid = MyDatabaseId;
850                 pgstat_send(&msg, len);
851         }
852
853         /* Clean up */
854         hash_destroy(htab);
855 }
856
857
858 /* ----------
859  * pgstat_collect_oids() -
860  *
861  *      Collect the OIDs of either all databases or all tables, according to
862  *      the parameter, into a temporary hash table.  Caller should hash_destroy
863  *      the result when done with it.
864  * ----------
865  */
866 static HTAB *
867 pgstat_collect_oids(Oid catalogid)
868 {
869         HTAB       *htab;
870         HASHCTL         hash_ctl;
871         Relation        rel;
872         HeapScanDesc scan;
873         HeapTuple       tup;
874
875         memset(&hash_ctl, 0, sizeof(hash_ctl));
876         hash_ctl.keysize = sizeof(Oid);
877         hash_ctl.entrysize = sizeof(Oid);
878         hash_ctl.hash = oid_hash;
879         htab = hash_create("Temporary table of OIDs",
880                                            PGSTAT_TAB_HASH_SIZE,
881                                            &hash_ctl,
882                                            HASH_ELEM | HASH_FUNCTION);
883
884         rel = heap_open(catalogid, AccessShareLock);
885         scan = heap_beginscan(rel, SnapshotNow, 0, NULL);
886         while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
887         {
888                 Oid             thisoid = HeapTupleGetOid(tup);
889
890                 CHECK_FOR_INTERRUPTS();
891
892                 (void) hash_search(htab, (void *) &thisoid, HASH_ENTER, NULL);
893         }
894         heap_endscan(scan);
895         heap_close(rel, AccessShareLock);
896
897         return htab;
898 }
899
900
901 /* ----------
902  * pgstat_drop_database() -
903  *
904  *      Tell the collector that we just dropped a database.
905  *      (If the message gets lost, we will still clean the dead DB eventually
906  *      via future invocations of pgstat_vacuum_tabstat().)
907  * ----------
908  */
909 void
910 pgstat_drop_database(Oid databaseid)
911 {
912         PgStat_MsgDropdb msg;
913
914         if (pgStatSock < 0)
915                 return;
916
917         pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
918         msg.m_databaseid = databaseid;
919         pgstat_send(&msg, sizeof(msg));
920 }
921
922
923 /* ----------
924  * pgstat_drop_relation() -
925  *
926  *      Tell the collector that we just dropped a relation.
927  *      (If the message gets lost, we will still clean the dead entry eventually
928  *      via future invocations of pgstat_vacuum_tabstat().)
929  *
930  *      Currently not used for lack of any good place to call it; we rely
931  *      entirely on pgstat_vacuum_tabstat() to clean out stats for dead rels.
932  * ----------
933  */
934 #ifdef NOT_USED
935 void
936 pgstat_drop_relation(Oid relid)
937 {
938         PgStat_MsgTabpurge msg;
939         int                     len;
940
941         if (pgStatSock < 0)
942                 return;
943
944         msg.m_tableid[0] = relid;
945         msg.m_nentries = 1;
946
947         len = offsetof(PgStat_MsgTabpurge, m_tableid[0]) +sizeof(Oid);
948
949         pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
950         msg.m_databaseid = MyDatabaseId;
951         pgstat_send(&msg, len);
952 }
953 #endif /* NOT_USED */
954
955
956 /* ----------
957  * pgstat_reset_counters() -
958  *
959  *      Tell the statistics collector to reset counters for our database.
960  * ----------
961  */
962 void
963 pgstat_reset_counters(void)
964 {
965         PgStat_MsgResetcounter msg;
966
967         if (pgStatSock < 0)
968                 return;
969
970         if (!superuser())
971                 ereport(ERROR,
972                                 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
973                                  errmsg("must be superuser to reset statistics counters")));
974
975         pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
976         msg.m_databaseid = MyDatabaseId;
977         pgstat_send(&msg, sizeof(msg));
978 }
979
980
981 /* ----------
982  * pgstat_report_autovac() -
983  *
984  *      Called from autovacuum.c to report startup of an autovacuum process.
985  *      We are called before InitPostgres is done, so can't rely on MyDatabaseId;
986  *      the db OID must be passed in, instead.
987  * ----------
988  */
989 void
990 pgstat_report_autovac(Oid dboid)
991 {
992         PgStat_MsgAutovacStart msg;
993
994         if (pgStatSock < 0)
995                 return;
996
997         pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_AUTOVAC_START);
998         msg.m_databaseid = dboid;
999         msg.m_start_time = GetCurrentTimestamp();
1000
1001         pgstat_send(&msg, sizeof(msg));
1002 }
1003
1004
1005 /* ---------
1006  * pgstat_report_vacuum() -
1007  *
1008  *      Tell the collector about the table we just vacuumed.
1009  * ---------
1010  */
1011 void
1012 pgstat_report_vacuum(Oid tableoid, bool shared,
1013                                          bool analyze, PgStat_Counter tuples)
1014 {
1015         PgStat_MsgVacuum msg;
1016
1017         if (pgStatSock < 0 || !pgstat_track_counts)
1018                 return;
1019
1020         pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_VACUUM);
1021         msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1022         msg.m_tableoid = tableoid;
1023         msg.m_analyze = analyze;
1024         msg.m_autovacuum = IsAutoVacuumWorkerProcess(); /* is this autovacuum? */
1025         msg.m_vacuumtime = GetCurrentTimestamp();
1026         msg.m_tuples = tuples;
1027         pgstat_send(&msg, sizeof(msg));
1028 }
1029
1030 /* --------
1031  * pgstat_report_analyze() -
1032  *
1033  *      Tell the collector about the table we just analyzed.
1034  * --------
1035  */
1036 void
1037 pgstat_report_analyze(Oid tableoid, bool shared, PgStat_Counter livetuples,
1038                                           PgStat_Counter deadtuples)
1039 {
1040         PgStat_MsgAnalyze msg;
1041
1042         if (pgStatSock < 0 || !pgstat_track_counts)
1043                 return;
1044
1045         pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ANALYZE);
1046         msg.m_databaseid = shared ? InvalidOid : MyDatabaseId;
1047         msg.m_tableoid = tableoid;
1048         msg.m_autovacuum = IsAutoVacuumWorkerProcess(); /* is this autovacuum? */
1049         msg.m_analyzetime = GetCurrentTimestamp();
1050         msg.m_live_tuples = livetuples;
1051         msg.m_dead_tuples = deadtuples;
1052         pgstat_send(&msg, sizeof(msg));
1053 }
1054
1055
1056 /* ----------
1057  * pgstat_ping() -
1058  *
1059  *      Send some junk data to the collector to increase traffic.
1060  * ----------
1061  */
1062 void
1063 pgstat_ping(void)
1064 {
1065         PgStat_MsgDummy msg;
1066
1067         if (pgStatSock < 0)
1068                 return;
1069
1070         pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DUMMY);
1071         pgstat_send(&msg, sizeof(msg));
1072 }
1073
1074
1075 /* ----------
1076  * pgstat_initstats() -
1077  *
1078  *      Initialize a relcache entry to count access statistics.
1079  *      Called whenever a relation is opened.
1080  *
1081  *      We assume that a relcache entry's pgstat_info field is zeroed by
1082  *      relcache.c when the relcache entry is made; thereafter it is long-lived
1083  *      data.  We can avoid repeated searches of the TabStatus arrays when the
1084  *      same relation is touched repeatedly within a transaction.
1085  * ----------
1086  */
1087 void
1088 pgstat_initstats(Relation rel)
1089 {
1090         Oid                     rel_id = rel->rd_id;
1091         char            relkind = rel->rd_rel->relkind;
1092
1093         /* We only count stats for things that have storage */
1094         if (!(relkind == RELKIND_RELATION ||
1095                   relkind == RELKIND_INDEX ||
1096                   relkind == RELKIND_TOASTVALUE))
1097         {
1098                 rel->pgstat_info = NULL;
1099                 return;
1100         }
1101
1102         if (pgStatSock < 0 || !pgstat_track_counts)
1103         {
1104                 /* We're not counting at all */
1105                 rel->pgstat_info = NULL;
1106                 return;
1107         }
1108
1109         /*
1110          * If we already set up this relation in the current transaction,
1111          * nothing to do.
1112          */
1113         if (rel->pgstat_info != NULL &&
1114                 rel->pgstat_info->t_id == rel_id)
1115                 return;
1116
1117         /* Else find or make the PgStat_TableStatus entry, and update link */
1118         rel->pgstat_info = get_tabstat_entry(rel_id, rel->rd_rel->relisshared);
1119 }
1120
1121 /*
1122  * get_tabstat_entry - find or create a PgStat_TableStatus entry for rel
1123  */
1124 static PgStat_TableStatus *
1125 get_tabstat_entry(Oid rel_id, bool isshared)
1126 {
1127         PgStat_TableStatus *entry;
1128         TabStatusArray *tsa;
1129         TabStatusArray *prev_tsa;
1130         int                     i;
1131
1132         /*
1133          * Search the already-used tabstat slots for this relation.
1134          */
1135         prev_tsa = NULL;
1136         for (tsa = pgStatTabList; tsa != NULL; prev_tsa = tsa, tsa = tsa->tsa_next)
1137         {
1138                 for (i = 0; i < tsa->tsa_used; i++)
1139                 {
1140                         entry = &tsa->tsa_entries[i];
1141                         if (entry->t_id == rel_id)
1142                                 return entry;
1143                 }
1144
1145                 if (tsa->tsa_used < TABSTAT_QUANTUM)
1146                 {
1147                         /*
1148                          * It must not be present, but we found a free slot instead.
1149                          * Fine, let's use this one.  We assume the entry was already
1150                          * zeroed, either at creation or after last use.
1151                          */
1152                         entry = &tsa->tsa_entries[tsa->tsa_used++];
1153                         entry->t_id = rel_id;
1154                         entry->t_shared = isshared;
1155                         return entry;
1156                 }
1157         }
1158
1159         /*
1160          * We ran out of tabstat slots, so allocate more.  Be sure they're zeroed.
1161          */
1162         tsa = (TabStatusArray *) MemoryContextAllocZero(TopMemoryContext,
1163                                                                                                         sizeof(TabStatusArray));
1164         if (prev_tsa)
1165                 prev_tsa->tsa_next = tsa;
1166         else
1167                 pgStatTabList = tsa;
1168
1169         /*
1170          * Use the first entry of the new TabStatusArray.
1171          */
1172         entry = &tsa->tsa_entries[tsa->tsa_used++];
1173         entry->t_id = rel_id;
1174         entry->t_shared = isshared;
1175         return entry;
1176 }
1177
1178 /*
1179  * get_tabstat_stack_level - add a new (sub)transaction stack entry if needed
1180  */
1181 static PgStat_SubXactStatus *
1182 get_tabstat_stack_level(int nest_level)
1183 {
1184         PgStat_SubXactStatus *xact_state;
1185
1186         xact_state = pgStatXactStack;
1187         if (xact_state == NULL || xact_state->nest_level != nest_level)
1188         {
1189                 xact_state = (PgStat_SubXactStatus *)
1190                         MemoryContextAlloc(TopTransactionContext,
1191                                                            sizeof(PgStat_SubXactStatus));
1192                 xact_state->nest_level = nest_level;
1193                 xact_state->prev = pgStatXactStack;
1194                 xact_state->first = NULL;
1195                 pgStatXactStack = xact_state;
1196         }
1197         return xact_state;
1198 }
1199
1200 /*
1201  * add_tabstat_xact_level - add a new (sub)transaction state record
1202  */
1203 static void
1204 add_tabstat_xact_level(PgStat_TableStatus *pgstat_info, int nest_level)
1205 {
1206         PgStat_SubXactStatus *xact_state;
1207         PgStat_TableXactStatus *trans;
1208
1209         /*
1210          * If this is the first rel to be modified at the current nest level,
1211          * we first have to push a transaction stack entry.
1212          */
1213         xact_state = get_tabstat_stack_level(nest_level);
1214
1215         /* Now make a per-table stack entry */
1216         trans = (PgStat_TableXactStatus *)
1217                 MemoryContextAllocZero(TopTransactionContext,
1218                                                            sizeof(PgStat_TableXactStatus));
1219         trans->nest_level = nest_level;
1220         trans->upper = pgstat_info->trans;
1221         trans->parent = pgstat_info;
1222         trans->next = xact_state->first;
1223         xact_state->first = trans;
1224         pgstat_info->trans = trans;
1225 }
1226
1227 /*
1228  * pgstat_count_heap_insert - count a tuple insertion
1229  */
1230 void
1231 pgstat_count_heap_insert(Relation rel)
1232 {
1233         PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1234
1235         if (pgstat_track_counts && pgstat_info != NULL)
1236         {
1237                 int             nest_level = GetCurrentTransactionNestLevel();
1238
1239                 /* t_tuples_inserted is nontransactional, so just advance it */
1240                 pgstat_info->t_counts.t_tuples_inserted++;
1241
1242                 /* We have to log the transactional effect at the proper level */
1243                 if (pgstat_info->trans == NULL ||
1244                         pgstat_info->trans->nest_level != nest_level)
1245                         add_tabstat_xact_level(pgstat_info, nest_level);
1246
1247                 pgstat_info->trans->tuples_inserted++;
1248         }
1249 }
1250
1251 /*
1252  * pgstat_count_heap_update - count a tuple update
1253  */
1254 void
1255 pgstat_count_heap_update(Relation rel, bool hot)
1256 {
1257         PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1258
1259         if (pgstat_track_counts && pgstat_info != NULL)
1260         {
1261                 int             nest_level = GetCurrentTransactionNestLevel();
1262
1263                 /* t_tuples_updated is nontransactional, so just advance it */
1264                 pgstat_info->t_counts.t_tuples_updated++;
1265                 /* ditto for the hot_update counter */
1266                 if (hot)
1267                         pgstat_info->t_counts.t_tuples_hot_updated++;
1268
1269                 /* We have to log the transactional effect at the proper level */
1270                 if (pgstat_info->trans == NULL ||
1271                         pgstat_info->trans->nest_level != nest_level)
1272                         add_tabstat_xact_level(pgstat_info, nest_level);
1273
1274                 /* An UPDATE both inserts a new tuple and deletes the old */
1275                 pgstat_info->trans->tuples_inserted++;
1276                 pgstat_info->trans->tuples_deleted++;
1277         }
1278 }
1279
1280 /*
1281  * pgstat_count_heap_delete - count a tuple deletion
1282  */
1283 void
1284 pgstat_count_heap_delete(Relation rel)
1285 {
1286         PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1287
1288         if (pgstat_track_counts && pgstat_info != NULL)
1289         {
1290                 int             nest_level = GetCurrentTransactionNestLevel();
1291
1292                 /* t_tuples_deleted is nontransactional, so just advance it */
1293                 pgstat_info->t_counts.t_tuples_deleted++;
1294
1295                 /* We have to log the transactional effect at the proper level */
1296                 if (pgstat_info->trans == NULL ||
1297                         pgstat_info->trans->nest_level != nest_level)
1298                         add_tabstat_xact_level(pgstat_info, nest_level);
1299
1300                 pgstat_info->trans->tuples_deleted++;
1301         }
1302 }
1303
1304 /*
1305  * pgstat_update_heap_dead_tuples - update dead-tuples count
1306  *
1307  * The semantics of this are that we are reporting the nontransactional
1308  * recovery of "delta" dead tuples; so t_new_dead_tuples decreases
1309  * rather than increasing, and the change goes straight into the per-table
1310  * counter, not into transactional state.
1311  */
1312 void
1313 pgstat_update_heap_dead_tuples(Relation rel, int delta)
1314 {
1315         PgStat_TableStatus *pgstat_info = rel->pgstat_info;
1316
1317         if (pgstat_track_counts && pgstat_info != NULL)
1318                 pgstat_info->t_counts.t_new_dead_tuples -= delta;
1319 }
1320
1321
1322 /* ----------
1323  * AtEOXact_PgStat
1324  *
1325  *      Called from access/transam/xact.c at top-level transaction commit/abort.
1326  * ----------
1327  */
1328 void
1329 AtEOXact_PgStat(bool isCommit)
1330 {
1331         PgStat_SubXactStatus *xact_state;
1332
1333         /*
1334          * Count transaction commit or abort.  (We use counters, not just bools,
1335          * in case the reporting message isn't sent right away.)
1336          */
1337         if (isCommit)
1338                 pgStatXactCommit++;
1339         else
1340                 pgStatXactRollback++;
1341
1342         /*
1343          * Transfer transactional insert/update counts into the base tabstat
1344          * entries.  We don't bother to free any of the transactional state,
1345          * since it's all in TopTransactionContext and will go away anyway.
1346          */
1347         xact_state = pgStatXactStack;
1348         if (xact_state != NULL)
1349         {
1350                 PgStat_TableXactStatus *trans;
1351
1352                 Assert(xact_state->nest_level == 1);
1353                 Assert(xact_state->prev == NULL);
1354                 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1355                 {
1356                         PgStat_TableStatus *tabstat;
1357
1358                         Assert(trans->nest_level == 1);
1359                         Assert(trans->upper == NULL);
1360                         tabstat = trans->parent;
1361                         Assert(tabstat->trans == trans);
1362                         if (isCommit)
1363                         {
1364                                 tabstat->t_counts.t_new_live_tuples +=
1365                                         trans->tuples_inserted - trans->tuples_deleted;
1366                                 tabstat->t_counts.t_new_dead_tuples += trans->tuples_deleted;
1367                         }
1368                         else
1369                         {
1370                                 /* inserted tuples are dead, deleted tuples are unaffected */
1371                                 tabstat->t_counts.t_new_dead_tuples += trans->tuples_inserted;
1372                         }
1373                         tabstat->trans = NULL;
1374                 }
1375         }
1376         pgStatXactStack = NULL;
1377
1378         /* Make sure any stats snapshot is thrown away */
1379         pgstat_clear_snapshot();
1380 }
1381
1382 /* ----------
1383  * AtEOSubXact_PgStat
1384  *
1385  *      Called from access/transam/xact.c at subtransaction commit/abort.
1386  * ----------
1387  */
1388 void
1389 AtEOSubXact_PgStat(bool isCommit, int nestDepth)
1390 {
1391         PgStat_SubXactStatus *xact_state;
1392
1393         /*
1394          * Transfer transactional insert/update counts into the next higher
1395          * subtransaction state.
1396          */
1397         xact_state = pgStatXactStack;
1398         if (xact_state != NULL &&
1399                 xact_state->nest_level >= nestDepth)
1400         {
1401                 PgStat_TableXactStatus *trans;
1402                 PgStat_TableXactStatus *next_trans;
1403
1404                 /* delink xact_state from stack immediately to simplify reuse case */
1405                 pgStatXactStack = xact_state->prev;
1406
1407                 for (trans = xact_state->first; trans != NULL; trans = next_trans)
1408                 {
1409                         PgStat_TableStatus *tabstat;
1410
1411                         next_trans = trans->next;
1412                         Assert(trans->nest_level == nestDepth);
1413                         tabstat = trans->parent;
1414                         Assert(tabstat->trans == trans);
1415                         if (isCommit)
1416                         {
1417                                 if (trans->upper && trans->upper->nest_level == nestDepth - 1)
1418                                 {
1419                                         trans->upper->tuples_inserted += trans->tuples_inserted;
1420                                         trans->upper->tuples_deleted += trans->tuples_deleted;
1421                                         tabstat->trans = trans->upper;
1422                                         pfree(trans);
1423                                 }
1424                                 else
1425                                 {
1426                                         /*
1427                                          * When there isn't an immediate parent state, we can
1428                                          * just reuse the record instead of going through a
1429                                          * palloc/pfree pushup (this works since it's all in
1430                                          * TopTransactionContext anyway).  We have to re-link
1431                                          * it into the parent level, though, and that might mean
1432                                          * pushing a new entry into the pgStatXactStack.
1433                                          */
1434                                         PgStat_SubXactStatus *upper_xact_state;
1435
1436                                         upper_xact_state = get_tabstat_stack_level(nestDepth - 1);
1437                                         trans->next = upper_xact_state->first;
1438                                         upper_xact_state->first = trans;
1439                                         trans->nest_level = nestDepth - 1;
1440                                 }
1441                         }
1442                         else
1443                         {
1444                                 /*
1445                                  * On abort, inserted tuples are dead (and can be bounced out
1446                                  * to the top-level tabstat), deleted tuples are unaffected
1447                                  */
1448                                 tabstat->t_counts.t_new_dead_tuples += trans->tuples_inserted;
1449                                 tabstat->trans = trans->upper;
1450                                 pfree(trans);
1451                         }
1452                 }
1453                 pfree(xact_state);
1454         }
1455 }
1456
1457
1458 /*
1459  * AtPrepare_PgStat
1460  *              Save the transactional stats state at 2PC transaction prepare.
1461  *
1462  * In this phase we just generate 2PC records for all the pending
1463  * transaction-dependent stats work.
1464  */
1465 void
1466 AtPrepare_PgStat(void)
1467 {
1468         PgStat_SubXactStatus *xact_state;
1469
1470         xact_state = pgStatXactStack;
1471         if (xact_state != NULL)
1472         {
1473                 PgStat_TableXactStatus *trans;
1474
1475                 Assert(xact_state->nest_level == 1);
1476                 Assert(xact_state->prev == NULL);
1477                 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1478                 {
1479                         PgStat_TableStatus *tabstat;
1480                         TwoPhasePgStatRecord record;
1481
1482                         Assert(trans->nest_level == 1);
1483                         Assert(trans->upper == NULL);
1484                         tabstat = trans->parent;
1485                         Assert(tabstat->trans == trans);
1486
1487                         record.tuples_inserted = trans->tuples_inserted;
1488                         record.tuples_deleted = trans->tuples_deleted;
1489                         record.t_id = tabstat->t_id;
1490                         record.t_shared = tabstat->t_shared;
1491
1492                         RegisterTwoPhaseRecord(TWOPHASE_RM_PGSTAT_ID, 0,
1493                                                                    &record, sizeof(TwoPhasePgStatRecord));
1494                 }
1495         }
1496 }
1497
1498 /*
1499  * PostPrepare_PgStat
1500  *              Clean up after successful PREPARE.
1501  *
1502  * All we need do here is unlink the transaction stats state from the
1503  * nontransactional state.  The nontransactional action counts will be
1504  * reported to the stats collector immediately, while the effects on live
1505  * and dead tuple counts are preserved in the 2PC state file.
1506  *
1507  * Note: AtEOXact_PgStat is not called during PREPARE.
1508  */
1509 void
1510 PostPrepare_PgStat(void)
1511 {
1512         PgStat_SubXactStatus *xact_state;
1513
1514         /*
1515          * We don't bother to free any of the transactional state,
1516          * since it's all in TopTransactionContext and will go away anyway.
1517          */
1518         xact_state = pgStatXactStack;
1519         if (xact_state != NULL)
1520         {
1521                 PgStat_TableXactStatus *trans;
1522
1523                 for (trans = xact_state->first; trans != NULL; trans = trans->next)
1524                 {
1525                         PgStat_TableStatus *tabstat;
1526
1527                         tabstat = trans->parent;
1528                         tabstat->trans = NULL;
1529                 }
1530         }
1531         pgStatXactStack = NULL;
1532
1533         /* Make sure any stats snapshot is thrown away */
1534         pgstat_clear_snapshot();
1535 }
1536
1537 /*
1538  * 2PC processing routine for COMMIT PREPARED case.
1539  *
1540  * Load the saved counts into our local pgstats state.
1541  */
1542 void
1543 pgstat_twophase_postcommit(TransactionId xid, uint16 info,
1544                                                    void *recdata, uint32 len)
1545 {
1546         TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
1547         PgStat_TableStatus *pgstat_info;
1548
1549         /* Find or create a tabstat entry for the rel */
1550         pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
1551
1552         pgstat_info->t_counts.t_new_live_tuples +=
1553                 rec->tuples_inserted - rec->tuples_deleted;
1554         pgstat_info->t_counts.t_new_dead_tuples += rec->tuples_deleted;
1555 }
1556
1557 /*
1558  * 2PC processing routine for ROLLBACK PREPARED case.
1559  *
1560  * Load the saved counts into our local pgstats state, but treat them
1561  * as aborted.
1562  */
1563 void
1564 pgstat_twophase_postabort(TransactionId xid, uint16 info,
1565                                                   void *recdata, uint32 len)
1566 {
1567         TwoPhasePgStatRecord *rec = (TwoPhasePgStatRecord *) recdata;
1568         PgStat_TableStatus *pgstat_info;
1569
1570         /* Find or create a tabstat entry for the rel */
1571         pgstat_info = get_tabstat_entry(rec->t_id, rec->t_shared);
1572
1573         /* inserted tuples are dead, deleted tuples are no-ops */
1574         pgstat_info->t_counts.t_new_dead_tuples += rec->tuples_inserted;
1575 }
1576
1577
1578 /* ----------
1579  * pgstat_fetch_stat_dbentry() -
1580  *
1581  *      Support function for the SQL-callable pgstat* functions. Returns
1582  *      the collected statistics for one database or NULL. NULL doesn't mean
1583  *      that the database doesn't exist, it is just not yet known by the
1584  *      collector, so the caller is better off to report ZERO instead.
1585  * ----------
1586  */
1587 PgStat_StatDBEntry *
1588 pgstat_fetch_stat_dbentry(Oid dbid)
1589 {
1590         /*
1591          * If not done for this transaction, read the statistics collector stats
1592          * file into some hash tables.
1593          */
1594         backend_read_statsfile();
1595
1596         /*
1597          * Lookup the requested database; return NULL if not found
1598          */
1599         return (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1600                                                                                           (void *) &dbid,
1601                                                                                           HASH_FIND, NULL);
1602 }
1603
1604
1605 /* ----------
1606  * pgstat_fetch_stat_tabentry() -
1607  *
1608  *      Support function for the SQL-callable pgstat* functions. Returns
1609  *      the collected statistics for one table or NULL. NULL doesn't mean
1610  *      that the table doesn't exist, it is just not yet known by the
1611  *      collector, so the caller is better off to report ZERO instead.
1612  * ----------
1613  */
1614 PgStat_StatTabEntry *
1615 pgstat_fetch_stat_tabentry(Oid relid)
1616 {
1617         Oid                     dbid;
1618         PgStat_StatDBEntry *dbentry;
1619         PgStat_StatTabEntry *tabentry;
1620
1621         /*
1622          * If not done for this transaction, read the statistics collector stats
1623          * file into some hash tables.
1624          */
1625         backend_read_statsfile();
1626
1627         /*
1628          * Lookup our database, then look in its table hash table.
1629          */
1630         dbid = MyDatabaseId;
1631         dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1632                                                                                                  (void *) &dbid,
1633                                                                                                  HASH_FIND, NULL);
1634         if (dbentry != NULL && dbentry->tables != NULL)
1635         {
1636                 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1637                                                                                                            (void *) &relid,
1638                                                                                                            HASH_FIND, NULL);
1639                 if (tabentry)
1640                         return tabentry;
1641         }
1642
1643         /*
1644          * If we didn't find it, maybe it's a shared table.
1645          */
1646         dbid = InvalidOid;
1647         dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1648                                                                                                  (void *) &dbid,
1649                                                                                                  HASH_FIND, NULL);
1650         if (dbentry != NULL && dbentry->tables != NULL)
1651         {
1652                 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1653                                                                                                            (void *) &relid,
1654                                                                                                            HASH_FIND, NULL);
1655                 if (tabentry)
1656                         return tabentry;
1657         }
1658
1659         return NULL;
1660 }
1661
1662
1663 /* ----------
1664  * pgstat_fetch_stat_beentry() -
1665  *
1666  *      Support function for the SQL-callable pgstat* functions. Returns
1667  *      our local copy of the current-activity entry for one backend.
1668  *
1669  *      NB: caller is responsible for a check if the user is permitted to see
1670  *      this info (especially the querystring).
1671  * ----------
1672  */
1673 PgBackendStatus *
1674 pgstat_fetch_stat_beentry(int beid)
1675 {
1676         pgstat_read_current_status();
1677
1678         if (beid < 1 || beid > localNumBackends)
1679                 return NULL;
1680
1681         return &localBackendStatusTable[beid - 1];
1682 }
1683
1684
1685 /* ----------
1686  * pgstat_fetch_stat_numbackends() -
1687  *
1688  *      Support function for the SQL-callable pgstat* functions. Returns
1689  *      the maximum current backend id.
1690  * ----------
1691  */
1692 int
1693 pgstat_fetch_stat_numbackends(void)
1694 {
1695         pgstat_read_current_status();
1696
1697         return localNumBackends;
1698 }
1699
1700 /*
1701  * ---------
1702  * pgstat_fetch_global() -
1703  *
1704  *  Support function for the SQL-callable pgstat* functions. Returns
1705  *  a pointer to the global statistics struct.
1706  * ---------
1707  */
1708 PgStat_GlobalStats *
1709 pgstat_fetch_global(void)
1710 {
1711         backend_read_statsfile();
1712
1713         return &globalStats;
1714 }
1715
1716
1717 /* ------------------------------------------------------------
1718  * Functions for management of the shared-memory PgBackendStatus array
1719  * ------------------------------------------------------------
1720  */
1721
1722 static PgBackendStatus *BackendStatusArray = NULL;
1723 static PgBackendStatus *MyBEEntry = NULL;
1724
1725
1726 /*
1727  * Report shared-memory space needed by CreateSharedBackendStatus.
1728  */
1729 Size
1730 BackendStatusShmemSize(void)
1731 {
1732         Size            size;
1733
1734         size = mul_size(sizeof(PgBackendStatus), MaxBackends);
1735         return size;
1736 }
1737
1738 /*
1739  * Initialize the shared status array during postmaster startup.
1740  */
1741 void
1742 CreateSharedBackendStatus(void)
1743 {
1744         Size            size = BackendStatusShmemSize();
1745         bool            found;
1746
1747         /* Create or attach to the shared array */
1748         BackendStatusArray = (PgBackendStatus *)
1749                 ShmemInitStruct("Backend Status Array", size, &found);
1750
1751         if (!found)
1752         {
1753                 /*
1754                  * We're the first - initialize.
1755                  */
1756                 MemSet(BackendStatusArray, 0, size);
1757         }
1758 }
1759
1760
1761 /* ----------
1762  * pgstat_initialize() -
1763  *
1764  *      Initialize pgstats state, and set up our on-proc-exit hook.
1765  *      Called from InitPostgres.  MyBackendId must be set,
1766  *      but we must not have started any transaction yet (since the
1767  *      exit hook must run after the last transaction exit).
1768  * ----------
1769  */
1770 void
1771 pgstat_initialize(void)
1772 {
1773         /* Initialize MyBEEntry */
1774         Assert(MyBackendId >= 1 && MyBackendId <= MaxBackends);
1775         MyBEEntry = &BackendStatusArray[MyBackendId - 1];
1776
1777         /* Set up a process-exit hook to clean up */
1778         on_shmem_exit(pgstat_beshutdown_hook, 0);
1779 }
1780
1781 /* ----------
1782  * pgstat_bestart() -
1783  *
1784  *      Initialize this backend's entry in the PgBackendStatus array.
1785  *      Called from InitPostgres.  MyDatabaseId and session userid must be set
1786  *      (hence, this cannot be combined with pgstat_initialize).
1787  * ----------
1788  */
1789 void
1790 pgstat_bestart(void)
1791 {
1792         TimestampTz proc_start_timestamp;
1793         Oid                     userid;
1794         SockAddr        clientaddr;
1795         volatile PgBackendStatus *beentry;
1796
1797         /*
1798          * To minimize the time spent modifying the PgBackendStatus entry,
1799          * fetch all the needed data first.
1800          *
1801          * If we have a MyProcPort, use its session start time (for consistency,
1802          * and to save a kernel call).
1803          */
1804         if (MyProcPort)
1805                 proc_start_timestamp = MyProcPort->SessionStartTime;
1806         else
1807                 proc_start_timestamp = GetCurrentTimestamp();
1808         userid = GetSessionUserId();
1809
1810         /*
1811          * We may not have a MyProcPort (eg, if this is the autovacuum process).
1812          * If so, use all-zeroes client address, which is dealt with specially in
1813          * pg_stat_get_backend_client_addr and pg_stat_get_backend_client_port.
1814          */
1815         if (MyProcPort)
1816                 memcpy(&clientaddr, &MyProcPort->raddr, sizeof(clientaddr));
1817         else
1818                 MemSet(&clientaddr, 0, sizeof(clientaddr));
1819
1820         /*
1821          * Initialize my status entry, following the protocol of bumping
1822          * st_changecount before and after; and make sure it's even afterwards. We
1823          * use a volatile pointer here to ensure the compiler doesn't try to get
1824          * cute.
1825          */
1826         beentry = MyBEEntry;
1827         do
1828         {
1829                 beentry->st_changecount++;
1830         } while ((beentry->st_changecount & 1) == 0);
1831
1832         beentry->st_procpid = MyProcPid;
1833         beentry->st_proc_start_timestamp = proc_start_timestamp;
1834         beentry->st_activity_start_timestamp = 0;
1835         beentry->st_xact_start_timestamp = 0;
1836         beentry->st_databaseid = MyDatabaseId;
1837         beentry->st_userid = userid;
1838         beentry->st_clientaddr = clientaddr;
1839         beentry->st_waiting = false;
1840         beentry->st_activity[0] = '\0';
1841         /* Also make sure the last byte in the string area is always 0 */
1842         beentry->st_activity[PGBE_ACTIVITY_SIZE - 1] = '\0';
1843
1844         beentry->st_changecount++;
1845         Assert((beentry->st_changecount & 1) == 0);
1846 }
1847
1848 /*
1849  * Shut down a single backend's statistics reporting at process exit.
1850  *
1851  * Flush any remaining statistics counts out to the collector.
1852  * Without this, operations triggered during backend exit (such as
1853  * temp table deletions) won't be counted.
1854  *
1855  * Lastly, clear out our entry in the PgBackendStatus array.
1856  */
1857 static void
1858 pgstat_beshutdown_hook(int code, Datum arg)
1859 {
1860         volatile PgBackendStatus *beentry = MyBEEntry;
1861
1862         pgstat_report_tabstat(true);
1863
1864         /*
1865          * Clear my status entry, following the protocol of bumping st_changecount
1866          * before and after.  We use a volatile pointer here to ensure the
1867          * compiler doesn't try to get cute.
1868          */
1869         beentry->st_changecount++;
1870
1871         beentry->st_procpid = 0;        /* mark invalid */
1872
1873         beentry->st_changecount++;
1874         Assert((beentry->st_changecount & 1) == 0);
1875 }
1876
1877
1878 /* ----------
1879  * pgstat_report_activity() -
1880  *
1881  *      Called from tcop/postgres.c to report what the backend is actually doing
1882  *      (usually "<IDLE>" or the start of the query to be executed).
1883  * ----------
1884  */
1885 void
1886 pgstat_report_activity(const char *cmd_str)
1887 {
1888         volatile PgBackendStatus *beentry = MyBEEntry;
1889         TimestampTz start_timestamp;
1890         int                     len;
1891
1892         if (!pgstat_track_activities || !beentry)
1893                 return;
1894
1895         /*
1896          * To minimize the time spent modifying the entry, fetch all the needed
1897          * data first.
1898          */
1899         start_timestamp = GetCurrentStatementStartTimestamp();
1900
1901         len = strlen(cmd_str);
1902         len = pg_mbcliplen(cmd_str, len, PGBE_ACTIVITY_SIZE - 1);
1903
1904         /*
1905          * Update my status entry, following the protocol of bumping
1906          * st_changecount before and after.  We use a volatile pointer here to
1907          * ensure the compiler doesn't try to get cute.
1908          */
1909         beentry->st_changecount++;
1910
1911         beentry->st_activity_start_timestamp = start_timestamp;
1912         memcpy((char *) beentry->st_activity, cmd_str, len);
1913         beentry->st_activity[len] = '\0';
1914
1915         beentry->st_changecount++;
1916         Assert((beentry->st_changecount & 1) == 0);
1917 }
1918
1919 /*
1920  * Report current transaction start timestamp as the specified value.
1921  * Zero means there is no active transaction.
1922  */
1923 void
1924 pgstat_report_xact_timestamp(TimestampTz tstamp)
1925 {
1926         volatile PgBackendStatus *beentry = MyBEEntry;
1927
1928         if (!pgstat_track_activities || !beentry)
1929                 return;
1930
1931         /*
1932          * Update my status entry, following the protocol of bumping
1933          * st_changecount before and after.  We use a volatile pointer
1934          * here to ensure the compiler doesn't try to get cute.
1935          */
1936         beentry->st_changecount++;
1937         beentry->st_xact_start_timestamp = tstamp;
1938         beentry->st_changecount++;
1939         Assert((beentry->st_changecount & 1) == 0);
1940 }
1941
1942 /* ----------
1943  * pgstat_report_waiting() -
1944  *
1945  *      Called from lock manager to report beginning or end of a lock wait.
1946  *
1947  * NB: this *must* be able to survive being called before MyBEEntry has been
1948  * initialized.
1949  * ----------
1950  */
1951 void
1952 pgstat_report_waiting(bool waiting)
1953 {
1954         volatile PgBackendStatus *beentry = MyBEEntry;
1955
1956         if (!pgstat_track_activities || !beentry)
1957                 return;
1958
1959         /*
1960          * Since this is a single-byte field in a struct that only this process
1961          * may modify, there seems no need to bother with the st_changecount
1962          * protocol.  The update must appear atomic in any case.
1963          */
1964         beentry->st_waiting = waiting;
1965 }
1966
1967
1968 /* ----------
1969  * pgstat_read_current_status() -
1970  *
1971  *      Copy the current contents of the PgBackendStatus array to local memory,
1972  *      if not already done in this transaction.
1973  * ----------
1974  */
1975 static void
1976 pgstat_read_current_status(void)
1977 {
1978         volatile PgBackendStatus *beentry;
1979         PgBackendStatus *localtable;
1980         PgBackendStatus *localentry;
1981         int                     i;
1982
1983         Assert(!pgStatRunningInCollector);
1984         if (localBackendStatusTable)
1985                 return;                                 /* already done */
1986
1987         pgstat_setup_memcxt();
1988
1989         localtable = (PgBackendStatus *)
1990                 MemoryContextAlloc(pgStatLocalContext,
1991                                                    sizeof(PgBackendStatus) * MaxBackends);
1992         localNumBackends = 0;
1993
1994         beentry = BackendStatusArray;
1995         localentry = localtable;
1996         for (i = 1; i <= MaxBackends; i++)
1997         {
1998                 /*
1999                  * Follow the protocol of retrying if st_changecount changes while we
2000                  * copy the entry, or if it's odd.  (The check for odd is needed to
2001                  * cover the case where we are able to completely copy the entry while
2002                  * the source backend is between increment steps.)      We use a volatile
2003                  * pointer here to ensure the compiler doesn't try to get cute.
2004                  */
2005                 for (;;)
2006                 {
2007                         int                     save_changecount = beentry->st_changecount;
2008
2009                         /*
2010                          * XXX if PGBE_ACTIVITY_SIZE is really large, it might be best to
2011                          * use strcpy not memcpy for copying the activity string?
2012                          */
2013                         memcpy(localentry, (char *) beentry, sizeof(PgBackendStatus));
2014
2015                         if (save_changecount == beentry->st_changecount &&
2016                                 (save_changecount & 1) == 0)
2017                                 break;
2018
2019                         /* Make sure we can break out of loop if stuck... */
2020                         CHECK_FOR_INTERRUPTS();
2021                 }
2022
2023                 beentry++;
2024                 /* Only valid entries get included into the local array */
2025                 if (localentry->st_procpid > 0)
2026                 {
2027                         localentry++;
2028                         localNumBackends++;
2029                 }
2030         }
2031
2032         /* Set the pointer only after completion of a valid table */
2033         localBackendStatusTable = localtable;
2034 }
2035
2036
2037 /* ------------------------------------------------------------
2038  * Local support functions follow
2039  * ------------------------------------------------------------
2040  */
2041
2042
2043 /* ----------
2044  * pgstat_setheader() -
2045  *
2046  *              Set common header fields in a statistics message
2047  * ----------
2048  */
2049 static void
2050 pgstat_setheader(PgStat_MsgHdr *hdr, StatMsgType mtype)
2051 {
2052         hdr->m_type = mtype;
2053 }
2054
2055
2056 /* ----------
2057  * pgstat_send() -
2058  *
2059  *              Send out one statistics message to the collector
2060  * ----------
2061  */
2062 static void
2063 pgstat_send(void *msg, int len)
2064 {
2065         int                     rc;
2066
2067         if (pgStatSock < 0)
2068                 return;
2069
2070         ((PgStat_MsgHdr *) msg)->m_size = len;
2071
2072         /* We'll retry after EINTR, but ignore all other failures */
2073         do
2074         {
2075                 rc = send(pgStatSock, msg, len, 0);
2076         } while (rc < 0 && errno == EINTR);
2077
2078 #ifdef USE_ASSERT_CHECKING
2079         /* In debug builds, log send failures ... */
2080         if (rc < 0)
2081                 elog(LOG, "could not send to statistics collector: %m");
2082 #endif
2083 }
2084
2085 /* ----------
2086  * pgstat_send_bgwriter() -
2087  *
2088  *      Send bgwriter statistics to the collector
2089  * ----------
2090  */
2091 void
2092 pgstat_send_bgwriter(void)
2093 {
2094         /* We assume this initializes to zeroes */
2095         static const PgStat_MsgBgWriter all_zeroes;
2096
2097         /*
2098          * This function can be called even if nothing at all has happened.
2099          * In this case, avoid sending a completely empty message to
2100          * the stats collector.
2101          */
2102         if (memcmp(&BgWriterStats, &all_zeroes, sizeof(PgStat_MsgBgWriter)) == 0)
2103                 return;
2104
2105         /*
2106          * Prepare and send the message
2107          */
2108         pgstat_setheader(&BgWriterStats.m_hdr, PGSTAT_MTYPE_BGWRITER);
2109         pgstat_send(&BgWriterStats, sizeof(BgWriterStats));
2110
2111         /*
2112          * Clear out the statistics buffer, so it can be re-used.
2113          */
2114         MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
2115 }
2116
2117
2118 /* ----------
2119  * PgstatCollectorMain() -
2120  *
2121  *      Start up the statistics collector process.      This is the body of the
2122  *      postmaster child process.
2123  *
2124  *      The argc/argv parameters are valid only in EXEC_BACKEND case.
2125  * ----------
2126  */
2127 NON_EXEC_STATIC void
2128 PgstatCollectorMain(int argc, char *argv[])
2129 {
2130         struct itimerval write_timeout;
2131         bool            need_timer = false;
2132         int                     len;
2133         PgStat_Msg      msg;
2134
2135 #ifndef WIN32
2136 #ifdef HAVE_POLL
2137         struct pollfd input_fd;
2138 #else
2139         struct timeval sel_timeout;
2140         fd_set          rfds;
2141 #endif
2142 #endif
2143
2144         IsUnderPostmaster = true;       /* we are a postmaster subprocess now */
2145
2146         MyProcPid = getpid();           /* reset MyProcPid */
2147
2148         MyStartTime = time(NULL);       /* record Start Time for logging */
2149
2150         /*
2151          * If possible, make this process a group leader, so that the postmaster
2152          * can signal any child processes too.  (pgstat probably never has
2153          * any child processes, but for consistency we make all postmaster
2154          * child processes do this.)
2155          */
2156 #ifdef HAVE_SETSID
2157         if (setsid() < 0)
2158                 elog(FATAL, "setsid() failed: %m");
2159 #endif
2160
2161         /*
2162          * Ignore all signals usually bound to some action in the postmaster,
2163          * except SIGQUIT and SIGALRM.
2164          */
2165         pqsignal(SIGHUP, SIG_IGN);
2166         pqsignal(SIGINT, SIG_IGN);
2167         pqsignal(SIGTERM, SIG_IGN);
2168         pqsignal(SIGQUIT, pgstat_exit);
2169         pqsignal(SIGALRM, force_statwrite);
2170         pqsignal(SIGPIPE, SIG_IGN);
2171         pqsignal(SIGUSR1, SIG_IGN);
2172         pqsignal(SIGUSR2, SIG_IGN);
2173         pqsignal(SIGCHLD, SIG_DFL);
2174         pqsignal(SIGTTIN, SIG_DFL);
2175         pqsignal(SIGTTOU, SIG_DFL);
2176         pqsignal(SIGCONT, SIG_DFL);
2177         pqsignal(SIGWINCH, SIG_DFL);
2178         PG_SETMASK(&UnBlockSig);
2179
2180         /*
2181          * Identify myself via ps
2182          */
2183         init_ps_display("stats collector process", "", "", "");
2184
2185         /*
2186          * Arrange to write the initial status file right away
2187          */
2188         need_statwrite = true;
2189
2190         /* Preset the delay between status file writes */
2191         MemSet(&write_timeout, 0, sizeof(struct itimerval));
2192         write_timeout.it_value.tv_sec = PGSTAT_STAT_INTERVAL / 1000;
2193         write_timeout.it_value.tv_usec = (PGSTAT_STAT_INTERVAL % 1000) * 1000;
2194
2195         /*
2196          * Read in an existing statistics stats file or initialize the stats to
2197          * zero.
2198          */
2199         pgStatRunningInCollector = true;
2200         pgStatDBHash = pgstat_read_statsfile(InvalidOid);
2201
2202         /*
2203          * Setup the descriptor set for select(2).      Since only one bit in the set
2204          * ever changes, we need not repeat FD_ZERO each time.
2205          */
2206 #if !defined(HAVE_POLL) && !defined(WIN32)
2207         FD_ZERO(&rfds);
2208 #endif
2209
2210         /*
2211          * Loop to process messages until we get SIGQUIT or detect ungraceful
2212          * death of our parent postmaster.
2213          *
2214          * For performance reasons, we don't want to do a PostmasterIsAlive() test
2215          * after every message; instead, do it at statwrite time and if
2216          * select()/poll() is interrupted by timeout.
2217          */
2218         for (;;)
2219         {
2220                 int                     got_data;
2221
2222                 /*
2223                  * Quit if we get SIGQUIT from the postmaster.
2224                  */
2225                 if (need_exit)
2226                         break;
2227
2228                 /*
2229                  * If time to write the stats file, do so.      Note that the alarm
2230                  * interrupt isn't re-enabled immediately, but only after we next
2231                  * receive a stats message; so no cycles are wasted when there is
2232                  * nothing going on.
2233                  */
2234                 if (need_statwrite)
2235                 {
2236                         /* Check for postmaster death; if so we'll write file below */
2237                         if (!PostmasterIsAlive(true))
2238                                 break;
2239
2240                         pgstat_write_statsfile();
2241                         need_statwrite = false;
2242                         need_timer = true;
2243                 }
2244
2245                 /*
2246                  * Wait for a message to arrive; but not for more than
2247                  * PGSTAT_SELECT_TIMEOUT seconds. (This determines how quickly we will
2248                  * shut down after an ungraceful postmaster termination; so it needn't
2249                  * be very fast.  However, on some systems SIGQUIT won't interrupt the
2250                  * poll/select call, so this also limits speed of response to SIGQUIT,
2251                  * which is more important.)
2252                  *
2253                  * We use poll(2) if available, otherwise select(2).
2254                  * Win32 has its own implementation.
2255                  */
2256 #ifndef WIN32
2257 #ifdef HAVE_POLL
2258                 input_fd.fd = pgStatSock;
2259                 input_fd.events = POLLIN | POLLERR;
2260                 input_fd.revents = 0;
2261
2262                 if (poll(&input_fd, 1, PGSTAT_SELECT_TIMEOUT * 1000) < 0)
2263                 {
2264                         if (errno == EINTR)
2265                                 continue;
2266                         ereport(ERROR,
2267                                         (errcode_for_socket_access(),
2268                                          errmsg("poll() failed in statistics collector: %m")));
2269                 }
2270
2271                 got_data = (input_fd.revents != 0);
2272 #else                                                   /* !HAVE_POLL */
2273
2274                 FD_SET(pgStatSock, &rfds);
2275
2276                 /*
2277                  * timeout struct is modified by select() on some operating systems,
2278                  * so re-fill it each time.
2279                  */
2280                 sel_timeout.tv_sec = PGSTAT_SELECT_TIMEOUT;
2281                 sel_timeout.tv_usec = 0;
2282
2283                 if (select(pgStatSock + 1, &rfds, NULL, NULL, &sel_timeout) < 0)
2284                 {
2285                         if (errno == EINTR)
2286                                 continue;
2287                         ereport(ERROR,
2288                                         (errcode_for_socket_access(),
2289                                          errmsg("select() failed in statistics collector: %m")));
2290                 }
2291
2292                 got_data = FD_ISSET(pgStatSock, &rfds);
2293 #endif   /* HAVE_POLL */
2294 #else /* WIN32 */
2295                 got_data = pgwin32_waitforsinglesocket(pgStatSock, FD_READ,
2296                                                                                            PGSTAT_SELECT_TIMEOUT*1000);
2297 #endif
2298
2299                 /*
2300                  * If there is a message on the socket, read it and check for
2301                  * validity.
2302                  */
2303                 if (got_data)
2304                 {
2305                         len = recv(pgStatSock, (char *) &msg,
2306                                            sizeof(PgStat_Msg), 0);
2307                         if (len < 0)
2308                         {
2309                                 if (errno == EINTR)
2310                                         continue;
2311                                 ereport(ERROR,
2312                                                 (errcode_for_socket_access(),
2313                                                  errmsg("could not read statistics message: %m")));
2314                         }
2315
2316                         /*
2317                          * We ignore messages that are smaller than our common header
2318                          */
2319                         if (len < sizeof(PgStat_MsgHdr))
2320                                 continue;
2321
2322                         /*
2323                          * The received length must match the length in the header
2324                          */
2325                         if (msg.msg_hdr.m_size != len)
2326                                 continue;
2327
2328                         /*
2329                          * O.K. - we accept this message.  Process it.
2330                          */
2331                         switch (msg.msg_hdr.m_type)
2332                         {
2333                                 case PGSTAT_MTYPE_DUMMY:
2334                                         break;
2335
2336                                 case PGSTAT_MTYPE_TABSTAT:
2337                                         pgstat_recv_tabstat((PgStat_MsgTabstat *) &msg, len);
2338                                         break;
2339
2340                                 case PGSTAT_MTYPE_TABPURGE:
2341                                         pgstat_recv_tabpurge((PgStat_MsgTabpurge *) &msg, len);
2342                                         break;
2343
2344                                 case PGSTAT_MTYPE_DROPDB:
2345                                         pgstat_recv_dropdb((PgStat_MsgDropdb *) &msg, len);
2346                                         break;
2347
2348                                 case PGSTAT_MTYPE_RESETCOUNTER:
2349                                         pgstat_recv_resetcounter((PgStat_MsgResetcounter *) &msg,
2350                                                                                          len);
2351                                         break;
2352
2353                                 case PGSTAT_MTYPE_AUTOVAC_START:
2354                                         pgstat_recv_autovac((PgStat_MsgAutovacStart *) &msg, len);
2355                                         break;
2356
2357                                 case PGSTAT_MTYPE_VACUUM:
2358                                         pgstat_recv_vacuum((PgStat_MsgVacuum *) &msg, len);
2359                                         break;
2360
2361                                 case PGSTAT_MTYPE_ANALYZE:
2362                                         pgstat_recv_analyze((PgStat_MsgAnalyze *) &msg, len);
2363                                         break;
2364
2365                                 case PGSTAT_MTYPE_BGWRITER:
2366                                         pgstat_recv_bgwriter((PgStat_MsgBgWriter *) &msg, len);
2367                                         break;
2368
2369                                 default:
2370                                         break;
2371                         }
2372
2373                         /*
2374                          * If this is the first message after we wrote the stats file the
2375                          * last time, enable the alarm interrupt to make it be written
2376                          * again later.
2377                          */
2378                         if (need_timer)
2379                         {
2380                                 if (setitimer(ITIMER_REAL, &write_timeout, NULL))
2381                                         ereport(ERROR,
2382                                         (errmsg("could not set statistics collector timer: %m")));
2383                                 need_timer = false;
2384                         }
2385                 }
2386                 else
2387                 {
2388                         /*
2389                          * We can only get here if the select/poll timeout elapsed. Check
2390                          * for postmaster death.
2391                          */
2392                         if (!PostmasterIsAlive(true))
2393                                 break;
2394                 }
2395         }                                                       /* end of message-processing loop */
2396
2397         /*
2398          * Save the final stats to reuse at next startup.
2399          */
2400         pgstat_write_statsfile();
2401
2402         exit(0);
2403 }
2404
2405
2406 /* SIGQUIT signal handler for collector process */
2407 static void
2408 pgstat_exit(SIGNAL_ARGS)
2409 {
2410         need_exit = true;
2411 }
2412
2413 /* SIGALRM signal handler for collector process */
2414 static void
2415 force_statwrite(SIGNAL_ARGS)
2416 {
2417         need_statwrite = true;
2418 }
2419
2420
2421 /*
2422  * Lookup the hash table entry for the specified database. If no hash
2423  * table entry exists, initialize it, if the create parameter is true.
2424  * Else, return NULL.
2425  */
2426 static PgStat_StatDBEntry *
2427 pgstat_get_db_entry(Oid databaseid, bool create)
2428 {
2429         PgStat_StatDBEntry *result;
2430         bool            found;
2431         HASHACTION      action = (create ? HASH_ENTER : HASH_FIND);
2432
2433         /* Lookup or create the hash table entry for this database */
2434         result = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2435                                                                                                 &databaseid,
2436                                                                                                 action, &found);
2437
2438         if (!create && !found)
2439                 return NULL;
2440
2441         /* If not found, initialize the new one. */
2442         if (!found)
2443         {
2444                 HASHCTL         hash_ctl;
2445
2446                 result->tables = NULL;
2447                 result->n_xact_commit = 0;
2448                 result->n_xact_rollback = 0;
2449                 result->n_blocks_fetched = 0;
2450                 result->n_blocks_hit = 0;
2451                 result->n_tuples_returned = 0;
2452                 result->n_tuples_fetched = 0;
2453                 result->n_tuples_inserted = 0;
2454                 result->n_tuples_updated = 0;
2455                 result->n_tuples_deleted = 0;
2456                 result->last_autovac_time = 0;
2457
2458                 memset(&hash_ctl, 0, sizeof(hash_ctl));
2459                 hash_ctl.keysize = sizeof(Oid);
2460                 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2461                 hash_ctl.hash = oid_hash;
2462                 result->tables = hash_create("Per-database table",
2463                                                                          PGSTAT_TAB_HASH_SIZE,
2464                                                                          &hash_ctl,
2465                                                                          HASH_ELEM | HASH_FUNCTION);
2466         }
2467
2468         return result;
2469 }
2470
2471
2472 /* ----------
2473  * pgstat_write_statsfile() -
2474  *
2475  *      Tell the news.
2476  * ----------
2477  */
2478 static void
2479 pgstat_write_statsfile(void)
2480 {
2481         HASH_SEQ_STATUS hstat;
2482         HASH_SEQ_STATUS tstat;
2483         PgStat_StatDBEntry *dbentry;
2484         PgStat_StatTabEntry *tabentry;
2485         FILE       *fpout;
2486         int32           format_id;
2487
2488         /*
2489          * Open the statistics temp file to write out the current values.
2490          */
2491         fpout = fopen(PGSTAT_STAT_TMPFILE, PG_BINARY_W);
2492         if (fpout == NULL)
2493         {
2494                 ereport(LOG,
2495                                 (errcode_for_file_access(),
2496                                  errmsg("could not open temporary statistics file \"%s\": %m",
2497                                                 PGSTAT_STAT_TMPFILE)));
2498                 return;
2499         }
2500
2501         /*
2502          * Write the file header --- currently just a format ID.
2503          */
2504         format_id = PGSTAT_FILE_FORMAT_ID;
2505         fwrite(&format_id, sizeof(format_id), 1, fpout);
2506
2507         /*
2508          * Write global stats struct
2509          */
2510         fwrite(&globalStats, sizeof(globalStats), 1, fpout);
2511
2512         /*
2513          * Walk through the database table.
2514          */
2515         hash_seq_init(&hstat, pgStatDBHash);
2516         while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
2517         {
2518                 /*
2519                  * Write out the DB entry including the number of live backends. We
2520                  * don't write the tables pointer since it's of no use to any other
2521                  * process.
2522                  */
2523                 fputc('D', fpout);
2524                 fwrite(dbentry, offsetof(PgStat_StatDBEntry, tables), 1, fpout);
2525
2526                 /*
2527                  * Walk through the database's access stats per table.
2528                  */
2529                 hash_seq_init(&tstat, dbentry->tables);
2530                 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
2531                 {
2532                         fputc('T', fpout);
2533                         fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
2534                 }
2535
2536                 /*
2537                  * Mark the end of this DB
2538                  */
2539                 fputc('d', fpout);
2540         }
2541
2542         /*
2543          * No more output to be done. Close the temp file and replace the old
2544          * pgstat.stat with it.  The ferror() check replaces testing for error
2545          * after each individual fputc or fwrite above.
2546          */
2547         fputc('E', fpout);
2548
2549         if (ferror(fpout))
2550         {
2551                 ereport(LOG,
2552                                 (errcode_for_file_access(),
2553                            errmsg("could not write temporary statistics file \"%s\": %m",
2554                                           PGSTAT_STAT_TMPFILE)));
2555                 fclose(fpout);
2556                 unlink(PGSTAT_STAT_TMPFILE);
2557         }
2558         else if (fclose(fpout) < 0)
2559         {
2560                 ereport(LOG,
2561                                 (errcode_for_file_access(),
2562                            errmsg("could not close temporary statistics file \"%s\": %m",
2563                                           PGSTAT_STAT_TMPFILE)));
2564                 unlink(PGSTAT_STAT_TMPFILE);
2565         }
2566         else if (rename(PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME) < 0)
2567         {
2568                 ereport(LOG,
2569                                 (errcode_for_file_access(),
2570                                  errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
2571                                                 PGSTAT_STAT_TMPFILE, PGSTAT_STAT_FILENAME)));
2572                 unlink(PGSTAT_STAT_TMPFILE);
2573         }
2574 }
2575
2576
2577 /* ----------
2578  * pgstat_read_statsfile() -
2579  *
2580  *      Reads in an existing statistics collector file and initializes the
2581  *      databases' hash table (whose entries point to the tables' hash tables).
2582  * ----------
2583  */
2584 static HTAB *
2585 pgstat_read_statsfile(Oid onlydb)
2586 {
2587         PgStat_StatDBEntry *dbentry;
2588         PgStat_StatDBEntry dbbuf;
2589         PgStat_StatTabEntry *tabentry;
2590         PgStat_StatTabEntry tabbuf;
2591         HASHCTL         hash_ctl;
2592         HTAB       *dbhash;
2593         HTAB       *tabhash = NULL;
2594         FILE       *fpin;
2595         int32           format_id;
2596         bool            found;
2597
2598         /*
2599          * The tables will live in pgStatLocalContext.
2600          */
2601         pgstat_setup_memcxt();
2602
2603         /*
2604          * Create the DB hashtable
2605          */
2606         memset(&hash_ctl, 0, sizeof(hash_ctl));
2607         hash_ctl.keysize = sizeof(Oid);
2608         hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
2609         hash_ctl.hash = oid_hash;
2610         hash_ctl.hcxt = pgStatLocalContext;
2611         dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
2612                                                  HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
2613
2614         /*
2615          * Clear out global statistics so they start from zero in case we can't
2616          * load an existing statsfile.
2617          */
2618         memset(&globalStats, 0, sizeof(globalStats));
2619
2620         /*
2621          * Try to open the status file. If it doesn't exist, the backends simply
2622          * return zero for anything and the collector simply starts from scratch
2623          * with empty counters.
2624          */
2625         if ((fpin = AllocateFile(PGSTAT_STAT_FILENAME, PG_BINARY_R)) == NULL)
2626                 return dbhash;
2627
2628         /*
2629          * Verify it's of the expected format.
2630          */
2631         if (fread(&format_id, 1, sizeof(format_id), fpin) != sizeof(format_id)
2632                 || format_id != PGSTAT_FILE_FORMAT_ID)
2633         {
2634                 ereport(pgStatRunningInCollector ? LOG : WARNING,
2635                                 (errmsg("corrupted pgstat.stat file")));
2636                 goto done;
2637         }
2638
2639         /*
2640          * Read global stats struct
2641          */
2642         if (fread(&globalStats, 1, sizeof(globalStats), fpin) != sizeof(globalStats))
2643         {
2644                 ereport(pgStatRunningInCollector ? LOG : WARNING,
2645                                 (errmsg("corrupted pgstat.stat file")));
2646                 goto done;
2647         }
2648
2649         /*
2650          * We found an existing collector stats file. Read it and put all the
2651          * hashtable entries into place.
2652          */
2653         for (;;)
2654         {
2655                 switch (fgetc(fpin))
2656                 {
2657                                 /*
2658                                  * 'D'  A PgStat_StatDBEntry struct describing a database
2659                                  * follows. Subsequently, zero to many 'T' entries will follow
2660                                  * until a 'd' is encountered.
2661                                  */
2662                         case 'D':
2663                                 if (fread(&dbbuf, 1, offsetof(PgStat_StatDBEntry, tables),
2664                                                   fpin) != offsetof(PgStat_StatDBEntry, tables))
2665                                 {
2666                                         ereport(pgStatRunningInCollector ? LOG : WARNING,
2667                                                         (errmsg("corrupted pgstat.stat file")));
2668                                         goto done;
2669                                 }
2670
2671                                 /*
2672                                  * Add to the DB hash
2673                                  */
2674                                 dbentry = (PgStat_StatDBEntry *) hash_search(dbhash,
2675                                                                                                   (void *) &dbbuf.databaseid,
2676                                                                                                                          HASH_ENTER,
2677                                                                                                                          &found);
2678                                 if (found)
2679                                 {
2680                                         ereport(pgStatRunningInCollector ? LOG : WARNING,
2681                                                         (errmsg("corrupted pgstat.stat file")));
2682                                         goto done;
2683                                 }
2684
2685                                 memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
2686                                 dbentry->tables = NULL;
2687
2688                                 /*
2689                                  * Don't collect tables if not the requested DB (or the
2690                                  * shared-table info)
2691                                  */
2692                                 if (onlydb != InvalidOid)
2693                                 {
2694                                         if (dbbuf.databaseid != onlydb &&
2695                                                 dbbuf.databaseid != InvalidOid)
2696                                                 break;
2697                                 }
2698
2699                                 memset(&hash_ctl, 0, sizeof(hash_ctl));
2700                                 hash_ctl.keysize = sizeof(Oid);
2701                                 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2702                                 hash_ctl.hash = oid_hash;
2703                                 hash_ctl.hcxt = pgStatLocalContext;
2704                                 dbentry->tables = hash_create("Per-database table",
2705                                                                                           PGSTAT_TAB_HASH_SIZE,
2706                                                                                           &hash_ctl,
2707                                                                          HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
2708
2709                                 /*
2710                                  * Arrange that following 'T's add entries to this database's
2711                                  * tables hash table.
2712                                  */
2713                                 tabhash = dbentry->tables;
2714                                 break;
2715
2716                                 /*
2717                                  * 'd'  End of this database.
2718                                  */
2719                         case 'd':
2720                                 tabhash = NULL;
2721                                 break;
2722
2723                                 /*
2724                                  * 'T'  A PgStat_StatTabEntry follows.
2725                                  */
2726                         case 'T':
2727                                 if (fread(&tabbuf, 1, sizeof(PgStat_StatTabEntry),
2728                                                   fpin) != sizeof(PgStat_StatTabEntry))
2729                                 {
2730                                         ereport(pgStatRunningInCollector ? LOG : WARNING,
2731                                                         (errmsg("corrupted pgstat.stat file")));
2732                                         goto done;
2733                                 }
2734
2735                                 /*
2736                                  * Skip if table belongs to a not requested database.
2737                                  */
2738                                 if (tabhash == NULL)
2739                                         break;
2740
2741                                 tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
2742                                                                                                         (void *) &tabbuf.tableid,
2743                                                                                                                  HASH_ENTER, &found);
2744
2745                                 if (found)
2746                                 {
2747                                         ereport(pgStatRunningInCollector ? LOG : WARNING,
2748                                                         (errmsg("corrupted pgstat.stat file")));
2749                                         goto done;
2750                                 }
2751
2752                                 memcpy(tabentry, &tabbuf, sizeof(tabbuf));
2753                                 break;
2754
2755                                 /*
2756                                  * 'E'  The EOF marker of a complete stats file.
2757                                  */
2758                         case 'E':
2759                                 goto done;
2760
2761                         default:
2762                                 ereport(pgStatRunningInCollector ? LOG : WARNING,
2763                                                 (errmsg("corrupted pgstat.stat file")));
2764                                 goto done;
2765                 }
2766         }
2767
2768 done:
2769         FreeFile(fpin);
2770
2771         return dbhash;
2772 }
2773
2774 /*
2775  * If not already done, read the statistics collector stats file into
2776  * some hash tables.  The results will be kept until pgstat_clear_snapshot()
2777  * is called (typically, at end of transaction).
2778  */
2779 static void
2780 backend_read_statsfile(void)
2781 {
2782         /* already read it? */
2783         if (pgStatDBHash)
2784                 return;
2785         Assert(!pgStatRunningInCollector);
2786
2787         /* Autovacuum launcher wants stats about all databases */
2788         if (IsAutoVacuumLauncherProcess())
2789                 pgStatDBHash = pgstat_read_statsfile(InvalidOid);
2790         else
2791                 pgStatDBHash = pgstat_read_statsfile(MyDatabaseId);
2792 }
2793
2794
2795 /* ----------
2796  * pgstat_setup_memcxt() -
2797  *
2798  *      Create pgStatLocalContext, if not already done.
2799  * ----------
2800  */
2801 static void
2802 pgstat_setup_memcxt(void)
2803 {
2804         if (!pgStatLocalContext)
2805                 pgStatLocalContext = AllocSetContextCreate(TopMemoryContext,
2806                                                                                                    "Statistics snapshot",
2807                                                                                                    ALLOCSET_SMALL_MINSIZE,
2808                                                                                                    ALLOCSET_SMALL_INITSIZE,
2809                                                                                                    ALLOCSET_SMALL_MAXSIZE);
2810 }
2811
2812
2813 /* ----------
2814  * pgstat_clear_snapshot() -
2815  *
2816  *      Discard any data collected in the current transaction.  Any subsequent
2817  *      request will cause new snapshots to be read.
2818  *
2819  *      This is also invoked during transaction commit or abort to discard
2820  *      the no-longer-wanted snapshot.
2821  * ----------
2822  */
2823 void
2824 pgstat_clear_snapshot(void)
2825 {
2826         /* Release memory, if any was allocated */
2827         if (pgStatLocalContext)
2828                 MemoryContextDelete(pgStatLocalContext);
2829
2830         /* Reset variables */
2831         pgStatLocalContext = NULL;
2832         pgStatDBHash = NULL;
2833         localBackendStatusTable = NULL;
2834         localNumBackends = 0;
2835 }
2836
2837
2838 /* ----------
2839  * pgstat_recv_tabstat() -
2840  *
2841  *      Count what the backend has done.
2842  * ----------
2843  */
2844 static void
2845 pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
2846 {
2847         PgStat_TableEntry *tabmsg = &(msg->m_entry[0]);
2848         PgStat_StatDBEntry *dbentry;
2849         PgStat_StatTabEntry *tabentry;
2850         int                     i;
2851         bool            found;
2852
2853         dbentry = pgstat_get_db_entry(msg->m_databaseid, true);
2854
2855         /*
2856          * Update database-wide stats.
2857          */
2858         dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
2859         dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
2860
2861         /*
2862          * Process all table entries in the message.
2863          */
2864         for (i = 0; i < msg->m_nentries; i++)
2865         {
2866                 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2867                                                                                                   (void *) &(tabmsg[i].t_id),
2868                                                                                                            HASH_ENTER, &found);
2869
2870                 if (!found)
2871                 {
2872                         /*
2873                          * If it's a new table entry, initialize counters to the values we
2874                          * just got.
2875                          */
2876                         tabentry->numscans = tabmsg[i].t_counts.t_numscans;
2877                         tabentry->tuples_returned = tabmsg[i].t_counts.t_tuples_returned;
2878                         tabentry->tuples_fetched = tabmsg[i].t_counts.t_tuples_fetched;
2879                         tabentry->tuples_inserted = tabmsg[i].t_counts.t_tuples_inserted;
2880                         tabentry->tuples_updated = tabmsg[i].t_counts.t_tuples_updated;
2881                         tabentry->tuples_deleted = tabmsg[i].t_counts.t_tuples_deleted;
2882                         tabentry->tuples_hot_updated = tabmsg[i].t_counts.t_tuples_hot_updated;
2883                         tabentry->n_live_tuples = tabmsg[i].t_counts.t_new_live_tuples;
2884                         tabentry->n_dead_tuples = tabmsg[i].t_counts.t_new_dead_tuples;
2885                         tabentry->blocks_fetched = tabmsg[i].t_counts.t_blocks_fetched;
2886                         tabentry->blocks_hit = tabmsg[i].t_counts.t_blocks_hit;
2887
2888                         tabentry->last_anl_tuples = 0;
2889                         tabentry->vacuum_timestamp = 0;
2890                         tabentry->autovac_vacuum_timestamp = 0;
2891                         tabentry->analyze_timestamp = 0;
2892                         tabentry->autovac_analyze_timestamp = 0;
2893                 }
2894                 else
2895                 {
2896                         /*
2897                          * Otherwise add the values to the existing entry.
2898                          */
2899                         tabentry->numscans += tabmsg[i].t_counts.t_numscans;
2900                         tabentry->tuples_returned += tabmsg[i].t_counts.t_tuples_returned;
2901                         tabentry->tuples_fetched += tabmsg[i].t_counts.t_tuples_fetched;
2902                         tabentry->tuples_inserted += tabmsg[i].t_counts.t_tuples_inserted;
2903                         tabentry->tuples_updated += tabmsg[i].t_counts.t_tuples_updated;
2904                         tabentry->tuples_deleted += tabmsg[i].t_counts.t_tuples_deleted;
2905                         tabentry->tuples_hot_updated += tabmsg[i].t_counts.t_tuples_hot_updated;
2906                         tabentry->n_live_tuples += tabmsg[i].t_counts.t_new_live_tuples;
2907                         tabentry->n_dead_tuples += tabmsg[i].t_counts.t_new_dead_tuples;
2908                         tabentry->blocks_fetched += tabmsg[i].t_counts.t_blocks_fetched;
2909                         tabentry->blocks_hit += tabmsg[i].t_counts.t_blocks_hit;
2910                 }
2911
2912                 /* Clamp n_live_tuples in case of negative new_live_tuples */
2913                 tabentry->n_live_tuples = Max(tabentry->n_live_tuples, 0);
2914                 /* Likewise for n_dead_tuples */
2915                 tabentry->n_dead_tuples = Max(tabentry->n_dead_tuples, 0);
2916
2917                 /*
2918                  * Add per-table stats to the per-database entry, too.
2919                  */
2920                 dbentry->n_tuples_returned += tabmsg[i].t_counts.t_tuples_returned;
2921                 dbentry->n_tuples_fetched += tabmsg[i].t_counts.t_tuples_fetched;
2922                 dbentry->n_tuples_inserted += tabmsg[i].t_counts.t_tuples_inserted;
2923                 dbentry->n_tuples_updated += tabmsg[i].t_counts.t_tuples_updated;
2924                 dbentry->n_tuples_deleted += tabmsg[i].t_counts.t_tuples_deleted;
2925                 dbentry->n_blocks_fetched += tabmsg[i].t_counts.t_blocks_fetched;
2926                 dbentry->n_blocks_hit += tabmsg[i].t_counts.t_blocks_hit;
2927         }
2928 }
2929
2930
2931 /* ----------
2932  * pgstat_recv_tabpurge() -
2933  *
2934  *      Arrange for dead table removal.
2935  * ----------
2936  */
2937 static void
2938 pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
2939 {
2940         PgStat_StatDBEntry *dbentry;
2941         int                     i;
2942
2943         dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2944
2945         /*
2946          * No need to purge if we don't even know the database.
2947          */
2948         if (!dbentry || !dbentry->tables)
2949                 return;
2950
2951         /*
2952          * Process all table entries in the message.
2953          */
2954         for (i = 0; i < msg->m_nentries; i++)
2955         {
2956                 /* Remove from hashtable if present; we don't care if it's not. */
2957                 (void) hash_search(dbentry->tables,
2958                                                    (void *) &(msg->m_tableid[i]),
2959                                                    HASH_REMOVE, NULL);
2960         }
2961 }
2962
2963
2964 /* ----------
2965  * pgstat_recv_dropdb() -
2966  *
2967  *      Arrange for dead database removal
2968  * ----------
2969  */
2970 static void
2971 pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len)
2972 {
2973         PgStat_StatDBEntry *dbentry;
2974
2975         /*
2976          * Lookup the database in the hashtable.
2977          */
2978         dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
2979
2980         /*
2981          * If found, remove it.
2982          */
2983         if (dbentry)
2984         {
2985                 if (dbentry->tables != NULL)
2986                         hash_destroy(dbentry->tables);
2987
2988                 if (hash_search(pgStatDBHash,
2989                                                 (void *) &(dbentry->databaseid),
2990                                                 HASH_REMOVE, NULL) == NULL)
2991                         ereport(ERROR,
2992                                         (errmsg("database hash table corrupted "
2993                                                         "during cleanup --- abort")));
2994         }
2995 }
2996
2997
2998 /* ----------
2999  * pgstat_recv_resetcounter() -
3000  *
3001  *      Reset the statistics for the specified database.
3002  * ----------
3003  */
3004 static void
3005 pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
3006 {
3007         HASHCTL         hash_ctl;
3008         PgStat_StatDBEntry *dbentry;
3009
3010         /*
3011          * Lookup the database in the hashtable.  Nothing to do if not there.
3012          */
3013         dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3014
3015         if (!dbentry)
3016                 return;
3017
3018         /*
3019          * We simply throw away all the database's table entries by recreating a
3020          * new hash table for them.
3021          */
3022         if (dbentry->tables != NULL)
3023                 hash_destroy(dbentry->tables);
3024
3025         dbentry->tables = NULL;
3026         dbentry->n_xact_commit = 0;
3027         dbentry->n_xact_rollback = 0;
3028         dbentry->n_blocks_fetched = 0;
3029         dbentry->n_blocks_hit = 0;
3030
3031         memset(&hash_ctl, 0, sizeof(hash_ctl));
3032         hash_ctl.keysize = sizeof(Oid);
3033         hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
3034         hash_ctl.hash = oid_hash;
3035         dbentry->tables = hash_create("Per-database table",
3036                                                                   PGSTAT_TAB_HASH_SIZE,
3037                                                                   &hash_ctl,
3038                                                                   HASH_ELEM | HASH_FUNCTION);
3039 }
3040
3041 /* ----------
3042  * pgstat_recv_autovac() -
3043  *
3044  *      Process an autovacuum signalling message.
3045  * ----------
3046  */
3047 static void
3048 pgstat_recv_autovac(PgStat_MsgAutovacStart *msg, int len)
3049 {
3050         PgStat_StatDBEntry *dbentry;
3051
3052         /*
3053          * Lookup the database in the hashtable.  Don't create the entry if it
3054          * doesn't exist, because autovacuum may be processing a template
3055          * database.  If this isn't the case, the database is most likely to have
3056          * an entry already.  (If it doesn't, not much harm is done anyway --
3057          * it'll get created as soon as somebody actually uses the database.)
3058          */
3059         dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3060         if (dbentry == NULL)
3061                 return;
3062
3063         /*
3064          * Store the last autovacuum time in the database entry.
3065          */
3066         dbentry->last_autovac_time = msg->m_start_time;
3067 }
3068
3069 /* ----------
3070  * pgstat_recv_vacuum() -
3071  *
3072  *      Process a VACUUM message.
3073  * ----------
3074  */
3075 static void
3076 pgstat_recv_vacuum(PgStat_MsgVacuum *msg, int len)
3077 {
3078         PgStat_StatDBEntry *dbentry;
3079         PgStat_StatTabEntry *tabentry;
3080
3081         /*
3082          * Don't create either the database or table entry if it doesn't already
3083          * exist.  This avoids bloating the stats with entries for stuff that is
3084          * only touched by vacuum and not by live operations.
3085          */
3086         dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3087         if (dbentry == NULL)
3088                 return;
3089
3090         tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
3091                                                    HASH_FIND, NULL);
3092         if (tabentry == NULL)
3093                 return;
3094
3095         if (msg->m_autovacuum)
3096                 tabentry->autovac_vacuum_timestamp = msg->m_vacuumtime;
3097         else
3098                 tabentry->vacuum_timestamp = msg->m_vacuumtime;
3099         tabentry->n_live_tuples = msg->m_tuples;
3100         /* Resetting dead_tuples to 0 is an approximation ... */
3101         tabentry->n_dead_tuples = 0;
3102         if (msg->m_analyze)
3103         {
3104                 tabentry->last_anl_tuples = msg->m_tuples;
3105                 if (msg->m_autovacuum)
3106                         tabentry->autovac_analyze_timestamp = msg->m_vacuumtime;
3107                 else
3108                         tabentry->analyze_timestamp = msg->m_vacuumtime;
3109         }
3110         else
3111         {
3112                 /* last_anl_tuples must never exceed n_live_tuples+n_dead_tuples */
3113                 tabentry->last_anl_tuples = Min(tabentry->last_anl_tuples,
3114                                                                                 msg->m_tuples);
3115         }
3116 }
3117
3118 /* ----------
3119  * pgstat_recv_analyze() -
3120  *
3121  *      Process an ANALYZE message.
3122  * ----------
3123  */
3124 static void
3125 pgstat_recv_analyze(PgStat_MsgAnalyze *msg, int len)
3126 {
3127         PgStat_StatDBEntry *dbentry;
3128         PgStat_StatTabEntry *tabentry;
3129
3130         /*
3131          * Don't create either the database or table entry if it doesn't already
3132          * exist.  This avoids bloating the stats with entries for stuff that is
3133          * only touched by analyze and not by live operations.
3134          */
3135         dbentry = pgstat_get_db_entry(msg->m_databaseid, false);
3136         if (dbentry == NULL)
3137                 return;
3138
3139         tabentry = hash_search(dbentry->tables, &(msg->m_tableoid),
3140                                                    HASH_FIND, NULL);
3141         if (tabentry == NULL)
3142                 return;
3143
3144         if (msg->m_autovacuum)
3145                 tabentry->autovac_analyze_timestamp = msg->m_analyzetime;
3146         else
3147                 tabentry->analyze_timestamp = msg->m_analyzetime;
3148         tabentry->n_live_tuples = msg->m_live_tuples;
3149         tabentry->n_dead_tuples = msg->m_dead_tuples;
3150         tabentry->last_anl_tuples = msg->m_live_tuples + msg->m_dead_tuples;
3151 }
3152
3153
3154 /* ----------
3155  * pgstat_recv_bgwriter() -
3156  *
3157  *      Process a BGWRITER message.
3158  * ----------
3159  */
3160 static void
3161 pgstat_recv_bgwriter(PgStat_MsgBgWriter *msg, int len)
3162 {
3163         globalStats.timed_checkpoints += msg->m_timed_checkpoints;
3164         globalStats.requested_checkpoints += msg->m_requested_checkpoints;
3165         globalStats.buf_written_checkpoints += msg->m_buf_written_checkpoints;
3166         globalStats.buf_written_clean += msg->m_buf_written_clean;
3167         globalStats.maxwritten_clean += msg->m_maxwritten_clean;
3168         globalStats.buf_written_backend += msg->m_buf_written_backend;
3169         globalStats.buf_alloc += msg->m_buf_alloc;
3170 }