4 * All the statistics collector stuff hacked up in one big, ugly file.
6 * TODO: - Separate collector, postmaster and backend stuff
7 * into different files.
9 * - Add some automatic call for pgstat vacuuming.
11 * - Add a pgstat config column to pg_database, so this
12 * entire thing can be enabled/disabled on a per db base.
14 * Copyright (c) 2001-2003, PostgreSQL Global Development Group
16 * $PostgreSQL: pgsql/src/backend/postmaster/pgstat.c,v 1.60 2004/03/10 21:12:46 momjian Exp $
23 #include <sys/param.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
35 #include "access/xact.h"
36 #include "access/heapam.h"
37 #include "catalog/catname.h"
38 #include "catalog/pg_shadow.h"
39 #include "catalog/pg_database.h"
40 #include "libpq/pqsignal.h"
41 #include "libpq/libpq.h"
42 #include "mb/pg_wchar.h"
43 #include "miscadmin.h"
44 #include "utils/memutils.h"
45 #include "storage/backendid.h"
46 #include "storage/ipc.h"
47 #include "storage/pg_shmem.h"
48 #include "tcop/tcopprot.h"
49 #include "utils/rel.h"
50 #include "utils/hsearch.h"
51 #include "utils/ps_status.h"
52 #include "utils/syscache.h"
55 #include "utils/guc.h"
59 extern pid_t win32_forkexec(const char* path, char *argv[]);
66 bool pgstat_collect_startcollector = true;
67 bool pgstat_collect_resetonpmstart = true;
68 bool pgstat_collect_querystring = false;
69 bool pgstat_collect_tuplelevel = false;
70 bool pgstat_collect_blocklevel = false;
73 * Other global variables
76 bool pgstat_is_running = false;
82 NON_EXEC_STATIC int pgStatSock = -1;
83 static int pgStatPipe[2];
84 static struct sockaddr_storage pgStatAddr;
85 static int pgStatPmPipe[2] = {-1, -1};
88 static time_t last_pgstat_start_time;
90 static long pgStatNumMessages = 0;
92 static bool pgStatRunningInCollector = FALSE;
94 static int pgStatTabstatAlloc = 0;
95 static int pgStatTabstatUsed = 0;
96 static PgStat_MsgTabstat **pgStatTabstatMessages = NULL;
98 #define TABSTAT_QUANTUM 4 /* we alloc this many at a time */
100 static int pgStatXactCommit = 0;
101 static int pgStatXactRollback = 0;
103 static TransactionId pgStatDBHashXact = InvalidTransactionId;
104 static HTAB *pgStatDBHash = NULL;
105 static HTAB *pgStatBeDead = NULL;
106 static PgStat_StatBeEntry *pgStatBeTable = NULL;
107 static int pgStatNumBackends = 0;
109 static char pgStat_tmpfname[MAXPGPATH];
110 static char pgStat_fname[MAXPGPATH];
114 * Local function forward declarations
118 static pid_t pgstat_forkexec(STATS_PROCESS_TYPE procType);
119 static void pgstat_parseArgs(PGSTAT_FORK_ARGS);
121 NON_EXEC_STATIC void pgstat_main(PGSTAT_FORK_ARGS);
122 NON_EXEC_STATIC void pgstat_mainChild(PGSTAT_FORK_ARGS);
123 static void pgstat_mainInit(void);
124 static void pgstat_recvbuffer(void);
125 static void pgstat_die(SIGNAL_ARGS);
127 static int pgstat_add_backend(PgStat_MsgHdr *msg);
128 static void pgstat_sub_backend(int procpid);
129 static void pgstat_drop_database(Oid databaseid);
130 static void pgstat_write_statsfile(void);
131 static void pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
132 PgStat_StatBeEntry **betab,
135 static void pgstat_setheader(PgStat_MsgHdr *hdr, int mtype);
136 static void pgstat_send(void *msg, int len);
138 static void pgstat_recv_bestart(PgStat_MsgBestart *msg, int len);
139 static void pgstat_recv_beterm(PgStat_MsgBeterm *msg, int len);
140 static void pgstat_recv_activity(PgStat_MsgActivity *msg, int len);
141 static void pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len);
142 static void pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len);
143 static void pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len);
144 static void pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len);
147 * WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
148 * so for that platform we use socket() instead of pipe().
151 #define pgpipe(a) pipe(a)
152 #define piperead(a,b,c) read(a,b,c)
153 #define pipewrite(a,b,c) write(a,b,c)
155 extern int pgpipe(int handles[2]); /* pgpipe() is in /src/port */
156 #define piperead(a,b,c) recv(a,b,c,0)
157 #define pipewrite(a,b,c) send(a,b,c,0)
160 /* ------------------------------------------------------------
161 * Public functions called from postmaster follow
162 * ------------------------------------------------------------
168 pgstat_init_forkexec_backend(void)
170 Assert(DataDir != NULL);
171 snprintf(pgStat_fname, MAXPGPATH,
172 PGSTAT_STAT_FILENAME, DataDir);
180 * Called from postmaster at startup. Create the resources required
181 * by the statistics collector process. If unable to do so, do not
182 * fail --- better to let the postmaster start with stats collection
189 ACCEPT_TYPE_ARG3 alen;
190 struct addrinfo *addrs = NULL,
196 * Force start of collector daemon if something to collect
198 if (pgstat_collect_querystring ||
199 pgstat_collect_tuplelevel ||
200 pgstat_collect_blocklevel)
201 pgstat_collect_startcollector = true;
204 * Initialize the filenames for the status reports.
206 snprintf(pgStat_tmpfname, MAXPGPATH,
207 PGSTAT_STAT_TMPFILE, DataDir, getpid());
208 snprintf(pgStat_fname, MAXPGPATH,
209 PGSTAT_STAT_FILENAME, DataDir);
212 * If we don't have to start a collector or should reset the collected
213 * statistics on postmaster start, simply remove the file.
215 if (!pgstat_collect_startcollector || pgstat_collect_resetonpmstart)
216 unlink(pgStat_fname);
219 * Nothing else required if collector will not get started
221 if (!pgstat_collect_startcollector)
225 * Create the UDP socket for sending and receiving statistic messages
227 hints.ai_flags = AI_PASSIVE;
228 hints.ai_family = PF_UNSPEC;
229 hints.ai_socktype = SOCK_DGRAM;
230 hints.ai_protocol = 0;
231 hints.ai_addrlen = 0;
232 hints.ai_addr = NULL;
233 hints.ai_canonname = NULL;
234 hints.ai_next = NULL;
235 ret = getaddrinfo_all("localhost", NULL, &hints, &addrs);
239 (errmsg("could not resolve \"localhost\": %s",
240 gai_strerror(ret))));
245 * On some platforms, getaddrinfo_all() may return multiple addresses
246 * only one of which will actually work (eg, both IPv6 and IPv4 addresses
247 * when kernel will reject IPv6). Worse, the failure may occur at the
248 * bind() or perhaps even connect() stage. So we must loop through the
249 * results till we find a working combination. We will generate LOG
250 * messages, but no error, for bogus combinations.
252 for (addr = addrs; addr; addr = addr->ai_next)
254 #ifdef HAVE_UNIX_SOCKETS
255 /* Ignore AF_UNIX sockets, if any are returned. */
256 if (addr->ai_family == AF_UNIX)
262 if ((pgStatSock = socket(addr->ai_family, SOCK_DGRAM, 0)) < 0)
265 (errcode_for_socket_access(),
266 errmsg("could not create socket for statistics collector: %m")));
271 * Bind it to a kernel assigned port on localhost and get the assigned
272 * port via getsockname().
274 if (bind(pgStatSock, addr->ai_addr, addr->ai_addrlen) < 0)
277 (errcode_for_socket_access(),
278 errmsg("could not bind socket for statistics collector: %m")));
279 closesocket(pgStatSock);
284 alen = sizeof(pgStatAddr);
285 if (getsockname(pgStatSock, (struct sockaddr *) &pgStatAddr, &alen) < 0)
288 (errcode_for_socket_access(),
289 errmsg("could not get address of socket for statistics collector: %m")));
290 closesocket(pgStatSock);
296 * Connect the socket to its own address. This saves a few cycles by
297 * not having to respecify the target address on every send. This also
298 * provides a kernel-level check that only packets from this same
299 * address will be received.
301 if (connect(pgStatSock, (struct sockaddr *) &pgStatAddr, alen) < 0)
304 (errcode_for_socket_access(),
305 errmsg("could not connect socket for statistics collector: %m")));
306 closesocket(pgStatSock);
311 /* If we get here, we have a working socket */
315 /* Did we find a working address? */
316 if (!addr || pgStatSock < 0)
319 (errcode_for_socket_access(),
320 errmsg("disabling statistics collector for lack of working socket")));
325 * Set the socket to non-blocking IO. This ensures that if the
326 * collector falls behind (despite the buffering process), statistics
327 * messages will be discarded; backends won't block waiting to send
328 * messages to the collector.
330 if (!set_noblock(pgStatSock))
333 (errcode_for_socket_access(),
334 errmsg("could not set statistics collector socket to nonblocking mode: %m")));
339 * Create the pipe that controls the statistics collector shutdown
341 if (pgpipe(pgStatPmPipe) < 0)
344 (errcode_for_socket_access(),
345 errmsg("could not create pipe for statistics collector: %m")));
349 freeaddrinfo_all(hints.ai_family, addrs);
355 freeaddrinfo_all(hints.ai_family, addrs);
358 closesocket(pgStatSock);
361 /* Adjust GUC variables to suppress useless activity */
362 pgstat_collect_startcollector = false;
363 pgstat_collect_querystring = false;
364 pgstat_collect_tuplelevel = false;
365 pgstat_collect_blocklevel = false;
372 * pgstat_forkexec() -
374 * Used to format up the arglist for, then fork and exec, statistics
375 * (buffer and collector) processes
379 pgstat_forkexec(STATS_PROCESS_TYPE procType)
383 int ac = 0, bufc = 0, i;
384 char pgstatBuf[10][MAXPGPATH];
386 av[ac++] = "postgres";
389 case STAT_PROC_BUFFER:
390 av[ac++] = "-statBuf";
393 case STAT_PROC_COLLECTOR:
394 av[ac++] = "-statCol";
401 /* Sockets + pipes */
403 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatSock);
404 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatPmPipe[0]);
405 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatPmPipe[1]);
406 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatPipe[0]);
407 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",pgStatPipe[1]);
410 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%d",MaxBackends);
412 /* + the pstat file names, and postgres pathname */
413 /* FIXME: [fork/exec] whitespaces in directories? */
414 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%s",pgStat_tmpfname);
415 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%s",pgStat_fname);
416 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%s",pg_pathname);
417 snprintf(pgstatBuf[bufc++],MAXPGPATH,"%s",DataDir);
419 /* Add to the arg list */
420 Assert(bufc <= lengthof(pgstatBuf));
421 for (i = 0; i < bufc; i++)
422 av[ac++] = pgstatBuf[i];
425 Assert(ac <= lengthof(av));
427 /* Fire off execv in child */
429 pid = win32_forkexec(pg_pathname,av);
431 if ((pid = fork()) == 0 && (execv(pg_pathname,av) == -1))
432 /* FIXME: [fork/exec] suggestions for what to do here? Can't call elog... */
435 return pid; /* Parent returns pid */
440 * pgstat_parseArgs() -
442 * Used to unformat the arglist for exec'ed statistics
443 * (buffer and collector) processes
447 pgstat_parseArgs(PGSTAT_FORK_ARGS)
451 pgStatSock = atoi(argv[argc++]);
452 pgStatPmPipe[0] = atoi(argv[argc++]);
453 pgStatPmPipe[1] = atoi(argv[argc++]);
454 pgStatPipe[0] = atoi(argv[argc++]);
455 pgStatPipe[1] = atoi(argv[argc++]);
456 MaxBackends = atoi(argv[argc++]);
457 strncpy(pgStat_tmpfname,argv[argc++],MAXPGPATH);
458 strncpy(pgStat_fname, argv[argc++],MAXPGPATH);
459 strncpy(pg_pathname, argv[argc++],MAXPGPATH);
460 DataDir = strdup(argv[argc++]);
462 read_nondefault_variables();
470 * Called from postmaster at startup or after an existing collector
471 * died. Attempt to fire up a fresh statistics collector.
473 * Note: if fail, we will be called again from the postmaster main loop.
482 * Do nothing if no collector needed
484 if (pgstat_is_running || !pgstat_collect_startcollector)
488 * Do nothing if too soon since last collector start. This is a
489 * safety valve to protect against continuous respawn attempts if the
490 * collector is dying immediately at launch. Note that since we will
491 * be re-called from the postmaster main loop, we will get another
494 curtime = time(NULL);
495 if ((unsigned int) (curtime - last_pgstat_start_time) <
496 (unsigned int) PGSTAT_RESTART_INTERVAL)
498 last_pgstat_start_time = curtime;
501 * Check that the socket is there, else pgstat_init failed.
506 (errmsg("statistics collector startup skipped")));
509 * We can only get here if someone tries to manually turn
510 * pgstat_collect_startcollector on after it had been off.
512 pgstat_collect_startcollector = false;
517 * Okay, fork off the collector. Remember its PID for
525 /* Specific beos actions before backend startup */
526 beos_before_backend_startup();
530 switch ((pgStatPid = (int) pgstat_forkexec(STAT_PROC_BUFFER)))
532 switch ((pgStatPid = (int) fork()))
537 /* Specific beos actions */
538 beos_backend_startup_failed();
541 (errmsg("could not fork statistics buffer: %m")));
546 /* in postmaster child ... */
548 /* Specific beos actions after backend startup */
549 beos_backend_startup();
551 /* Close the postmaster's sockets, except for pgstat link */
552 ClosePostmasterPorts(false);
554 /* Drop our connection to postmaster's shared memory, as well */
555 PGSharedMemoryDetach();
562 pgstat_is_running = true;
569 * pgstat_ispgstat() -
571 * Called from postmaster to check if a terminated child process
572 * was the statistics collector.
576 pgstat_ispgstat(int pid)
578 if (!pgstat_is_running)
581 if (pgStatPid != pid)
585 pgstat_is_running = false;
592 * pgstat_close_sockets() -
594 * Called when postmaster forks a non-pgstat child process, to close off
595 * file descriptors that should not be held open in child processes.
599 pgstat_close_sockets(void)
601 if (pgStatPmPipe[0] >= 0)
602 closesocket(pgStatPmPipe[0]);
603 pgStatPmPipe[0] = -1;
604 if (pgStatPmPipe[1] >= 0)
605 closesocket(pgStatPmPipe[1]);
606 pgStatPmPipe[1] = -1;
613 * Called from postmaster to tell collector a backend terminated.
617 pgstat_beterm(int pid)
619 PgStat_MsgBeterm msg;
624 MemSet(&(msg.m_hdr), 0, sizeof(msg.m_hdr));
625 msg.m_hdr.m_type = PGSTAT_MTYPE_BETERM;
626 msg.m_hdr.m_procpid = pid;
628 pgstat_send(&msg, sizeof(msg));
632 /* ------------------------------------------------------------
633 * Public functions used by backends follow
634 *------------------------------------------------------------
641 * Tell the collector that this new backend is soon ready to process
642 * queries. Called from tcop/postgres.c before entering the mainloop.
648 PgStat_MsgBestart msg;
653 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_BESTART);
654 pgstat_send(&msg, sizeof(msg));
659 * pgstat_report_activity() -
661 * Called in tcop/postgres.c to tell the collector what the backend
662 * is actually doing (usually "<IDLE>" or the start of the query to
667 pgstat_report_activity(const char *what)
669 PgStat_MsgActivity msg;
672 if (!pgstat_collect_querystring || pgStatSock < 0)
676 len = pg_mbcliplen((const unsigned char *) what, len,
677 PGSTAT_ACTIVITY_SIZE - 1);
679 memcpy(msg.m_what, what, len);
680 msg.m_what[len] = '\0';
681 len += offsetof(PgStat_MsgActivity, m_what) +1;
683 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_ACTIVITY);
684 pgstat_send(&msg, len);
689 * pgstat_report_tabstat() -
691 * Called from tcop/postgres.c to send the so far collected
692 * per table access statistics to the collector.
696 pgstat_report_tabstat(void)
700 if (pgStatSock < 0 ||
701 !(pgstat_collect_querystring ||
702 pgstat_collect_tuplelevel ||
703 pgstat_collect_blocklevel))
705 /* Not reporting stats, so just flush whatever we have */
706 pgStatTabstatUsed = 0;
711 * For each message buffer used during the last query set the header
712 * fields and send it out.
714 for (i = 0; i < pgStatTabstatUsed; i++)
716 PgStat_MsgTabstat *tsmsg = pgStatTabstatMessages[i];
720 n = tsmsg->m_nentries;
721 len = offsetof(PgStat_MsgTabstat, m_entry[0]) +
722 n * sizeof(PgStat_TableEntry);
724 tsmsg->m_xact_commit = pgStatXactCommit;
725 tsmsg->m_xact_rollback = pgStatXactRollback;
726 pgStatXactCommit = 0;
727 pgStatXactRollback = 0;
729 pgstat_setheader(&tsmsg->m_hdr, PGSTAT_MTYPE_TABSTAT);
730 pgstat_send(tsmsg, len);
733 pgStatTabstatUsed = 0;
738 * pgstat_vacuum_tabstat() -
740 * Will tell the collector about objects he can get rid of.
744 pgstat_vacuum_tabstat(void)
752 HASH_SEQ_STATUS hstat;
753 PgStat_StatDBEntry *dbentry;
754 PgStat_StatTabEntry *tabentry;
757 PgStat_MsgTabpurge msg;
765 * If not done for this transaction, read the statistics collector
766 * stats file into some hash tables.
768 if (!TransactionIdEquals(pgStatDBHashXact, GetCurrentTransactionId()))
770 pgstat_read_statsfile(&pgStatDBHash, MyDatabaseId,
771 &pgStatBeTable, &pgStatNumBackends);
772 pgStatDBHashXact = GetCurrentTransactionId();
776 * Lookup our own database entry
778 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
779 (void *) &MyDatabaseId,
784 if (dbentry->tables == NULL)
788 * Initialize our messages table counter to zero
793 * Check for all tables if they still exist.
795 hash_seq_init(&hstat, dbentry->tables);
796 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&hstat)) != NULL)
799 * Check if this relation is still alive by looking up it's
800 * pg_class tuple in the system catalog cache.
802 reltup = SearchSysCache(RELOID,
803 ObjectIdGetDatum(tabentry->tableid),
805 if (HeapTupleIsValid(reltup))
807 ReleaseSysCache(reltup);
812 * Add this tables Oid to the message
814 msg.m_tableid[msg.m_nentries++] = tabentry->tableid;
818 * If the message is full, send it out and reinitialize ot zero
820 if (msg.m_nentries >= PGSTAT_NUM_TABPURGE)
822 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
823 +msg.m_nentries * sizeof(Oid);
825 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
826 pgstat_send(&msg, len);
835 if (msg.m_nentries > 0)
837 len = offsetof(PgStat_MsgTabpurge, m_tableid[0])
838 +msg.m_nentries * sizeof(Oid);
840 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_TABPURGE);
841 pgstat_send(&msg, len);
845 * Read pg_database and remember the Oid's of all existing databases
849 dbidlist = (Oid *) palloc(sizeof(Oid) * dbidalloc);
851 dbrel = heap_openr(DatabaseRelationName, AccessShareLock);
852 dbscan = heap_beginscan(dbrel, SnapshotNow, 0, NULL);
853 while ((dbtup = heap_getnext(dbscan, ForwardScanDirection)) != NULL)
855 if (dbidused >= dbidalloc)
858 dbidlist = (Oid *) repalloc((char *) dbidlist,
859 sizeof(Oid) * dbidalloc);
861 dbidlist[dbidused++] = HeapTupleGetOid(dbtup);
863 heap_endscan(dbscan);
864 heap_close(dbrel, AccessShareLock);
867 * Search the database hash table for dead databases and tell the
868 * collector to drop them as well.
870 hash_seq_init(&hstat, pgStatDBHash);
871 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
873 Oid dbid = dbentry->databaseid;
875 for (i = 0; i < dbidused; i++)
877 if (dbidlist[i] == dbid)
884 if (dbid != InvalidOid)
887 pgstat_drop_database(dbid);
892 * Free the dbid list.
894 pfree((char *) dbidlist);
897 * Tell the caller how many removeable objects we found
904 * pgstat_drop_database() -
906 * Tell the collector that we just dropped a database.
907 * This is the only message that shouldn't get lost in space. Otherwise
908 * the collector will keep the statistics for the dead DB until his
909 * stats file got removed while the postmaster is down.
913 pgstat_drop_database(Oid databaseid)
915 PgStat_MsgDropdb msg;
920 msg.m_databaseid = databaseid;
922 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DROPDB);
923 pgstat_send(&msg, sizeof(msg));
928 * pgstat_reset_counters() -
930 * Tell the statistics collector to reset counters for our database.
934 pgstat_reset_counters(void)
936 PgStat_MsgResetcounter msg;
943 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
944 errmsg("must be superuser to reset statistics counters")));
946 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_RESETCOUNTER);
947 pgstat_send(&msg, sizeof(msg));
954 * Send some junk data to the collector to increase traffic.
965 pgstat_setheader(&msg.m_hdr, PGSTAT_MTYPE_DUMMY);
966 pgstat_send(&msg, sizeof(msg));
970 * Create or enlarge the pgStatTabstatMessages array
973 more_tabstat_space(void)
975 PgStat_MsgTabstat *newMessages;
976 PgStat_MsgTabstat **msgArray;
977 int newAlloc = pgStatTabstatAlloc + TABSTAT_QUANTUM;
980 /* Create (another) quantum of message buffers */
981 newMessages = (PgStat_MsgTabstat *)
982 malloc(sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
983 if (newMessages == NULL)
986 (errcode(ERRCODE_OUT_OF_MEMORY),
987 errmsg("out of memory")));
991 /* Create or enlarge the pointer array */
992 if (pgStatTabstatMessages == NULL)
993 msgArray = (PgStat_MsgTabstat **)
994 malloc(sizeof(PgStat_MsgTabstat *) * newAlloc);
996 msgArray = (PgStat_MsgTabstat **)
997 realloc(pgStatTabstatMessages,
998 sizeof(PgStat_MsgTabstat *) * newAlloc);
999 if (msgArray == NULL)
1003 (errcode(ERRCODE_OUT_OF_MEMORY),
1004 errmsg("out of memory")));
1008 MemSet(newMessages, 0, sizeof(PgStat_MsgTabstat) * TABSTAT_QUANTUM);
1009 for (i = 0; i < TABSTAT_QUANTUM; i++)
1010 msgArray[pgStatTabstatAlloc + i] = newMessages++;
1011 pgStatTabstatMessages = msgArray;
1012 pgStatTabstatAlloc = newAlloc;
1018 * pgstat_initstats() -
1020 * Called from various places usually dealing with initialization
1021 * of Relation or Scan structures. The data placed into these
1022 * structures from here tell where later to count for buffer reads,
1023 * scans and tuples fetched.
1027 pgstat_initstats(PgStat_Info *stats, Relation rel)
1029 Oid rel_id = rel->rd_id;
1030 PgStat_TableEntry *useent;
1031 PgStat_MsgTabstat *tsmsg;
1036 * Initialize data not to count at all.
1038 stats->tabentry = NULL;
1039 stats->no_stats = FALSE;
1040 stats->heap_scan_counted = FALSE;
1041 stats->index_scan_counted = FALSE;
1043 if (pgStatSock < 0 ||
1044 !(pgstat_collect_tuplelevel ||
1045 pgstat_collect_blocklevel))
1047 stats->no_stats = TRUE;
1052 * Search the already-used message slots for this relation.
1054 for (mb = 0; mb < pgStatTabstatUsed; mb++)
1056 tsmsg = pgStatTabstatMessages[mb];
1058 for (i = tsmsg->m_nentries; --i >= 0; )
1060 if (tsmsg->m_entry[i].t_id == rel_id)
1062 stats->tabentry = (void *) &(tsmsg->m_entry[i]);
1067 if (tsmsg->m_nentries >= PGSTAT_NUM_TABENTRIES)
1071 * Not found, but found a message buffer with an empty slot
1072 * instead. Fine, let's use this one.
1074 i = tsmsg->m_nentries++;
1075 useent = &tsmsg->m_entry[i];
1076 MemSet(useent, 0, sizeof(PgStat_TableEntry));
1077 useent->t_id = rel_id;
1078 stats->tabentry = (void *) useent;
1083 * If we ran out of message buffers, we just allocate more.
1085 if (pgStatTabstatUsed >= pgStatTabstatAlloc)
1087 if (!more_tabstat_space())
1089 stats->no_stats = TRUE;
1092 Assert(pgStatTabstatUsed < pgStatTabstatAlloc);
1096 * Use the first entry of the next message buffer.
1098 mb = pgStatTabstatUsed++;
1099 tsmsg = pgStatTabstatMessages[mb];
1100 tsmsg->m_nentries = 1;
1101 useent = &tsmsg->m_entry[0];
1102 MemSet(useent, 0, sizeof(PgStat_TableEntry));
1103 useent->t_id = rel_id;
1104 stats->tabentry = (void *) useent;
1109 * pgstat_count_xact_commit() -
1111 * Called from access/transam/xact.c to count transaction commits.
1115 pgstat_count_xact_commit(void)
1117 if (!(pgstat_collect_querystring ||
1118 pgstat_collect_tuplelevel ||
1119 pgstat_collect_blocklevel))
1125 * If there was no relation activity yet, just make one existing
1126 * message buffer used without slots, causing the next report to tell
1127 * new xact-counters.
1129 if (pgStatTabstatAlloc == 0)
1131 if (!more_tabstat_space())
1134 if (pgStatTabstatUsed == 0)
1136 pgStatTabstatUsed++;
1137 pgStatTabstatMessages[0]->m_nentries = 0;
1143 * pgstat_count_xact_rollback() -
1145 * Called from access/transam/xact.c to count transaction rollbacks.
1149 pgstat_count_xact_rollback(void)
1151 if (!(pgstat_collect_querystring ||
1152 pgstat_collect_tuplelevel ||
1153 pgstat_collect_blocklevel))
1156 pgStatXactRollback++;
1159 * If there was no relation activity yet, just make one existing
1160 * message buffer used without slots, causing the next report to tell
1161 * new xact-counters.
1163 if (pgStatTabstatAlloc == 0)
1165 if (!more_tabstat_space())
1168 if (pgStatTabstatUsed == 0)
1170 pgStatTabstatUsed++;
1171 pgStatTabstatMessages[0]->m_nentries = 0;
1177 * pgstat_fetch_stat_dbentry() -
1179 * Support function for the SQL-callable pgstat* functions. Returns
1180 * the collected statistics for one database or NULL. NULL doesn't mean
1181 * that the database doesn't exist, it is just not yet known by the
1182 * collector, so the caller is better off to report ZERO instead.
1185 PgStat_StatDBEntry *
1186 pgstat_fetch_stat_dbentry(Oid dbid)
1188 PgStat_StatDBEntry *dbentry;
1191 * If not done for this transaction, read the statistics collector
1192 * stats file into some hash tables. Be careful with the
1193 * read_statsfile() call below!
1195 if (!TransactionIdEquals(pgStatDBHashXact, GetCurrentTransactionId()))
1197 pgstat_read_statsfile(&pgStatDBHash, MyDatabaseId,
1198 &pgStatBeTable, &pgStatNumBackends);
1199 pgStatDBHashXact = GetCurrentTransactionId();
1203 * Lookup the requested database
1205 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1208 if (dbentry == NULL)
1216 * pgstat_fetch_stat_tabentry() -
1218 * Support function for the SQL-callable pgstat* functions. Returns
1219 * the collected statistics for one table or NULL. NULL doesn't mean
1220 * that the table doesn't exist, it is just not yet known by the
1221 * collector, so the caller is better off to report ZERO instead.
1224 PgStat_StatTabEntry *
1225 pgstat_fetch_stat_tabentry(Oid relid)
1227 PgStat_StatDBEntry *dbentry;
1228 PgStat_StatTabEntry *tabentry;
1231 * If not done for this transaction, read the statistics collector
1232 * stats file into some hash tables. Be careful with the
1233 * read_statsfile() call below!
1235 if (!TransactionIdEquals(pgStatDBHashXact, GetCurrentTransactionId()))
1237 pgstat_read_statsfile(&pgStatDBHash, MyDatabaseId,
1238 &pgStatBeTable, &pgStatNumBackends);
1239 pgStatDBHashXact = GetCurrentTransactionId();
1243 * Lookup our database.
1245 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
1246 (void *) &MyDatabaseId,
1248 if (dbentry == NULL)
1252 * Now inside the DB's table hash table lookup the requested one.
1254 if (dbentry->tables == NULL)
1256 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
1259 if (tabentry == NULL)
1267 * pgstat_fetch_stat_beentry() -
1269 * Support function for the SQL-callable pgstat* functions. Returns
1270 * the actual activity slot of one active backend. The caller is
1271 * responsible for a check if the actual user is permitted to see
1272 * that info (especially the querystring).
1275 PgStat_StatBeEntry *
1276 pgstat_fetch_stat_beentry(int beid)
1278 if (!TransactionIdEquals(pgStatDBHashXact, GetCurrentTransactionId()))
1280 pgstat_read_statsfile(&pgStatDBHash, MyDatabaseId,
1281 &pgStatBeTable, &pgStatNumBackends);
1282 pgStatDBHashXact = GetCurrentTransactionId();
1285 if (beid < 1 || beid > pgStatNumBackends)
1288 return &pgStatBeTable[beid - 1];
1293 * pgstat_fetch_stat_numbackends() -
1295 * Support function for the SQL-callable pgstat* functions. Returns
1296 * the maximum current backend id.
1300 pgstat_fetch_stat_numbackends(void)
1302 if (!TransactionIdEquals(pgStatDBHashXact, GetCurrentTransactionId()))
1304 pgstat_read_statsfile(&pgStatDBHash, MyDatabaseId,
1305 &pgStatBeTable, &pgStatNumBackends);
1306 pgStatDBHashXact = GetCurrentTransactionId();
1309 return pgStatNumBackends;
1314 /* ------------------------------------------------------------
1315 * Local support functions follow
1316 * ------------------------------------------------------------
1321 * pgstat_setheader() -
1323 * Set common header fields in a statistics message
1327 pgstat_setheader(PgStat_MsgHdr *hdr, int mtype)
1329 hdr->m_type = mtype;
1330 hdr->m_backendid = MyBackendId;
1331 hdr->m_procpid = MyProcPid;
1332 hdr->m_databaseid = MyDatabaseId;
1333 hdr->m_userid = GetSessionUserId();
1340 * Send out one statistics message to the collector
1344 pgstat_send(void *msg, int len)
1349 ((PgStat_MsgHdr *) msg)->m_size = len;
1351 send(pgStatSock, msg, len, 0);
1352 /* We deliberately ignore any error from send() */
1356 /* ------------------------------------------------------------
1357 * Local functions implementing the statistics collector itself follow
1358 *------------------------------------------------------------
1362 pgstat_mainInit(void)
1364 IsUnderPostmaster = true; /* we are a postmaster subprocess now */
1367 /* In EXEC case we will not have inherited these settings */
1368 IsPostmasterEnvironment = true;
1369 whereToSendOutput = None;
1371 /* Setup global context */
1372 MemoryContextInit(); /* before any elog'ing can occur */
1373 InitializeGUCOptions();
1376 MyProcPid = getpid(); /* reset MyProcPid */
1378 /* Lose the postmaster's on-exit routines */
1382 * Ignore all signals usually bound to some action in the postmaster,
1383 * except for SIGCHLD --- see pgstat_recvbuffer.
1385 pqsignal(SIGHUP, SIG_IGN);
1386 pqsignal(SIGINT, SIG_IGN);
1387 pqsignal(SIGTERM, SIG_IGN);
1388 pqsignal(SIGQUIT, SIG_IGN);
1389 pqsignal(SIGALRM, SIG_IGN);
1390 pqsignal(SIGPIPE, SIG_IGN);
1391 pqsignal(SIGUSR1, SIG_IGN);
1392 pqsignal(SIGUSR2, SIG_IGN);
1393 pqsignal(SIGCHLD, pgstat_die);
1394 pqsignal(SIGTTIN, SIG_DFL);
1395 pqsignal(SIGTTOU, SIG_DFL);
1396 pqsignal(SIGCONT, SIG_DFL);
1397 pqsignal(SIGWINCH, SIG_DFL);
1404 * Start up the statistics collector itself. This is the body of the
1405 * postmaster child process.
1408 NON_EXEC_STATIC void
1409 pgstat_main(PGSTAT_FORK_ARGS)
1411 pgstat_mainInit(); /* Note: for *both* EXEC_BACKEND and regular cases */
1413 pgstat_parseArgs(argc,argv);
1417 * Close the writing end of the postmaster pipe, so we'll see it
1418 * closing when the postmaster terminates and can terminate as well.
1420 closesocket(pgStatPmPipe[1]);
1421 pgStatPmPipe[1] = -1;
1424 * Start a buffering process to read from the socket, so we have a
1425 * little more time to process incoming messages.
1427 * NOTE: the process structure is: postmaster is parent of buffer process
1428 * is parent of collector process. This way, the buffer can detect
1429 * collector failure via SIGCHLD, whereas otherwise it wouldn't notice
1430 * collector failure until it tried to write on the pipe. That would
1431 * mean that after the postmaster started a new collector, we'd have
1432 * two buffer processes competing to read from the UDP socket --- not
1435 if (pgpipe(pgStatPipe) < 0)
1438 (errcode_for_socket_access(),
1439 errmsg("could not create pipe for statistics buffer: %m")));
1444 /* child becomes collector process */
1445 switch (pgstat_forkexec(STAT_PROC_COLLECTOR))
1452 (errmsg("could not fork statistics collector: %m")));
1455 #ifndef EXEC_BACKEND
1457 /* child becomes collector process */
1463 /* parent becomes buffer process */
1464 closesocket(pgStatPipe[0]);
1465 pgstat_recvbuffer();
1471 NON_EXEC_STATIC void
1472 pgstat_mainChild(PGSTAT_FORK_ARGS)
1481 struct timeval timeout;
1482 struct timeval next_statwrite;
1483 bool need_statwrite;
1487 pgstat_mainInit(); /* Note: only in EXEC_BACKEND case */
1488 pgstat_parseArgs(argc,argv);
1490 MyProcPid = getpid(); /* reset MyProcPid */
1493 closesocket(pgStatPipe[1]);
1494 closesocket(pgStatSock);
1495 pmPipe = pgStatPmPipe[0];
1498 * In the child we can have default SIGCHLD handling (in case we want
1499 * to call system() here...)
1501 pqsignal(SIGCHLD, SIG_DFL);
1504 * Identify myself via ps
1506 init_ps_display("stats collector process", "", "");
1510 * Arrange to write the initial status file right away
1512 gettimeofday(&next_statwrite, NULL);
1513 need_statwrite = TRUE;
1516 * Read in an existing statistics stats file or initialize the stats
1519 pgStatRunningInCollector = TRUE;
1520 pgstat_read_statsfile(&pgStatDBHash, InvalidOid, NULL, NULL);
1523 * Create the dead backend hashtable
1525 memset(&hash_ctl, 0, sizeof(hash_ctl));
1526 hash_ctl.keysize = sizeof(int);
1527 hash_ctl.entrysize = sizeof(PgStat_StatBeDead);
1528 hash_ctl.hash = tag_hash;
1529 pgStatBeDead = hash_create("Dead Backends", PGSTAT_BE_HASH_SIZE,
1530 &hash_ctl, HASH_ELEM | HASH_FUNCTION);
1531 if (pgStatBeDead == NULL)
1533 /* assume the problem is out-of-memory */
1535 (errcode(ERRCODE_OUT_OF_MEMORY),
1536 errmsg("out of memory in statistics collector --- abort")));
1541 * Create the known backends table
1543 pgStatBeTable = (PgStat_StatBeEntry *) malloc(
1544 sizeof(PgStat_StatBeEntry) * MaxBackends);
1545 if (pgStatBeTable == NULL)
1548 (errcode(ERRCODE_OUT_OF_MEMORY),
1549 errmsg("out of memory in statistics collector --- abort")));
1552 memset(pgStatBeTable, 0, sizeof(PgStat_StatBeEntry) * MaxBackends);
1554 readPipe = pgStatPipe[0];
1557 * Process incoming messages and handle all the reporting stuff until
1558 * there are no more messages.
1563 * If we need to write the status file again (there have been
1564 * changes in the statistics since we wrote it last) calculate the
1565 * timeout until we have to do so.
1571 gettimeofday(&now, NULL);
1572 /* avoid assuming that tv_sec is signed */
1573 if (now.tv_sec > next_statwrite.tv_sec ||
1574 (now.tv_sec == next_statwrite.tv_sec &&
1575 now.tv_usec >= next_statwrite.tv_usec))
1578 timeout.tv_usec = 0;
1582 timeout.tv_sec = next_statwrite.tv_sec - now.tv_sec;
1583 timeout.tv_usec = next_statwrite.tv_usec - now.tv_usec;
1584 if (timeout.tv_usec < 0)
1587 timeout.tv_usec += 1000000;
1593 * Setup the descriptor set for select(2)
1596 FD_SET(readPipe, &rfds);
1597 FD_SET(pmPipe, &rfds);
1599 if (readPipe > pmPipe)
1605 * Now wait for something to do.
1607 nready = select(maxfd + 1, &rfds, NULL, NULL,
1608 (need_statwrite) ? &timeout : NULL);
1614 (errcode_for_socket_access(),
1615 errmsg("select() failed in statistics collector: %m")));
1620 * If there are no descriptors ready, our timeout for writing the
1621 * stats file happened.
1625 pgstat_write_statsfile();
1626 need_statwrite = FALSE;
1632 * Check if there is a new statistics message to collect.
1634 if (FD_ISSET(readPipe, &rfds))
1637 * We may need to issue multiple read calls in case the buffer
1638 * process didn't write the message in a single write, which
1639 * is possible since it dumps its buffer bytewise. In any
1640 * case, we'd need two reads since we don't know the message
1644 int targetlen = sizeof(PgStat_MsgHdr); /* initial */
1646 while (nread < targetlen)
1648 len = piperead(readPipe,
1649 ((char *) &msg) + nread,
1656 (errcode_for_socket_access(),
1657 errmsg("could not read from statistics collector pipe: %m")));
1660 if (len == 0) /* EOF on the pipe! */
1663 if (nread == sizeof(PgStat_MsgHdr))
1665 /* we have the header, compute actual msg length */
1666 targetlen = msg.msg_hdr.m_size;
1667 if (targetlen < (int) sizeof(PgStat_MsgHdr) ||
1668 targetlen > (int) sizeof(msg))
1671 * Bogus message length implies that we got out of
1672 * sync with the buffer process somehow. Abort so
1673 * that we can restart both processes.
1676 (errmsg("invalid statistics message length")));
1683 * EOF on the pipe implies that the buffer process exited.
1684 * Fall out of outer loop.
1690 * Distribute the message to the specific function handling
1693 switch (msg.msg_hdr.m_type)
1695 case PGSTAT_MTYPE_DUMMY:
1698 case PGSTAT_MTYPE_BESTART:
1699 pgstat_recv_bestart((PgStat_MsgBestart *) &msg, nread);
1702 case PGSTAT_MTYPE_BETERM:
1703 pgstat_recv_beterm((PgStat_MsgBeterm *) &msg, nread);
1706 case PGSTAT_MTYPE_TABSTAT:
1707 pgstat_recv_tabstat((PgStat_MsgTabstat *) &msg, nread);
1710 case PGSTAT_MTYPE_TABPURGE:
1711 pgstat_recv_tabpurge((PgStat_MsgTabpurge *) &msg, nread);
1714 case PGSTAT_MTYPE_ACTIVITY:
1715 pgstat_recv_activity((PgStat_MsgActivity *) &msg, nread);
1718 case PGSTAT_MTYPE_DROPDB:
1719 pgstat_recv_dropdb((PgStat_MsgDropdb *) &msg, nread);
1722 case PGSTAT_MTYPE_RESETCOUNTER:
1723 pgstat_recv_resetcounter((PgStat_MsgResetcounter *) &msg,
1732 * Globally count messages.
1734 pgStatNumMessages++;
1737 * If this is the first message after we wrote the stats file
1738 * the last time, setup the timeout that it'd be written.
1740 if (!need_statwrite)
1742 gettimeofday(&next_statwrite, NULL);
1743 next_statwrite.tv_usec += ((PGSTAT_STAT_INTERVAL) * 1000);
1744 next_statwrite.tv_sec += (next_statwrite.tv_usec / 1000000);
1745 next_statwrite.tv_usec %= 1000000;
1746 need_statwrite = TRUE;
1751 * Note that we do NOT check for postmaster exit inside the loop;
1752 * only EOF on the buffer pipe causes us to fall out. This
1753 * ensures we don't exit prematurely if there are still a few
1754 * messages in the buffer or pipe at postmaster shutdown.
1759 * Okay, we saw EOF on the buffer pipe, so there are no more messages
1760 * to process. If the buffer process quit because of postmaster
1761 * shutdown, we want to save the final stats to reuse at next startup.
1762 * But if the buffer process failed, it seems best not to (there may
1763 * even now be a new collector firing up, and we don't want it to read
1764 * a partially- rewritten stats file). We can tell whether the
1765 * postmaster is still alive by checking to see if the postmaster pipe
1766 * is still open. If it is read-ready (ie, EOF), the postmaster must
1769 if (FD_ISSET(pmPipe, &rfds))
1770 pgstat_write_statsfile();
1775 * pgstat_recvbuffer() -
1777 * This is the body of the separate buffering process. Its only
1778 * purpose is to receive messages from the UDP socket as fast as
1779 * possible and forward them over a pipe into the collector itself.
1780 * If the collector is slow to absorb messages, they are buffered here.
1784 pgstat_recvbuffer(void)
1788 int writePipe = pgStatPipe[1];
1789 int pmPipe = pgStatPmPipe[0];
1795 PgStat_Msg input_buffer;
1797 int msg_send = 0; /* next send index in buffer */
1798 int msg_recv = 0; /* next receive index */
1799 int msg_have = 0; /* number of bytes stored */
1800 bool overflow = false;
1803 * Identify myself via ps
1805 init_ps_display("stats buffer process", "", "");
1809 * We want to die if our child collector process does. There are two
1810 * ways we might notice that it has died: receive SIGCHLD, or get a
1811 * write failure on the pipe leading to the child. We can set SIGPIPE
1812 * to kill us here. Our SIGCHLD handler was already set up before we
1813 * forked (must do it that way, else it's a race condition).
1815 pqsignal(SIGPIPE, SIG_DFL);
1816 PG_SETMASK(&UnBlockSig);
1819 * Set the write pipe to nonblock mode, so that we cannot block when
1820 * the collector falls behind.
1822 if (!set_noblock(writePipe))
1825 (errcode_for_socket_access(),
1826 errmsg("could not set statistics collector pipe to nonblocking mode: %m")));
1831 * Allocate the message buffer
1833 msgbuffer = (char *) malloc(PGSTAT_RECVBUFFERSZ);
1834 if (msgbuffer == NULL)
1837 (errcode(ERRCODE_OUT_OF_MEMORY),
1838 errmsg("out of memory in statistics collector --- abort")));
1852 * As long as we have buffer space we add the socket to the read
1855 if (msg_have <= (int) (PGSTAT_RECVBUFFERSZ - sizeof(PgStat_Msg)))
1857 FD_SET(pgStatSock, &rfds);
1866 (errmsg("statistics buffer is full")));
1872 * If we have messages to write out, we add the pipe to the write
1873 * descriptor set. Otherwise, we check if the postmaster might
1878 FD_SET(writePipe, &wfds);
1879 if (writePipe > maxfd)
1884 FD_SET(pmPipe, &rfds);
1890 * Wait for some work to do.
1892 nready = select(maxfd + 1, &rfds, &wfds, NULL, NULL);
1898 (errcode_for_socket_access(),
1899 errmsg("select() failed in statistics buffer: %m")));
1904 * If there is a message on the socket, read it and check for
1907 if (FD_ISSET(pgStatSock, &rfds))
1909 len = recv(pgStatSock, (char *) &input_buffer,
1910 sizeof(PgStat_Msg), 0);
1914 (errcode_for_socket_access(),
1915 errmsg("could not read statistics message: %m")));
1920 * We ignore messages that are smaller than our common header
1922 if (len < sizeof(PgStat_MsgHdr))
1926 * The received length must match the length in the header
1928 if (input_buffer.msg_hdr.m_size != len)
1932 * O.K. - we accept this message. Copy it to the circular
1938 xfr = PGSTAT_RECVBUFFERSZ - msg_recv;
1942 memcpy(msgbuffer + msg_recv,
1943 ((char *) &input_buffer) + frm,
1946 if (msg_recv == PGSTAT_RECVBUFFERSZ)
1955 * If the collector is ready to receive, write some data into his
1956 * pipe. We may or may not be able to write all that we have.
1958 * NOTE: if what we have is less than PIPE_BUF bytes but more than
1959 * the space available in the pipe buffer, most kernels will
1960 * refuse to write any of it, and will return EAGAIN. This means
1961 * we will busy-loop until the situation changes (either because
1962 * the collector caught up, or because more data arrives so that
1963 * we have more than PIPE_BUF bytes buffered). This is not good,
1964 * but is there any way around it? We have no way to tell when
1965 * the collector has caught up...
1967 if (FD_ISSET(writePipe, &wfds))
1969 xfr = PGSTAT_RECVBUFFERSZ - msg_send;
1973 len = pipewrite(writePipe, msgbuffer + msg_send, xfr);
1976 if (errno == EINTR || errno == EAGAIN)
1977 continue; /* not enough space in pipe */
1979 (errcode_for_socket_access(),
1980 errmsg("could not write to statistics collector pipe: %m")));
1983 /* NB: len < xfr is okay */
1985 if (msg_send == PGSTAT_RECVBUFFERSZ)
1991 * Make sure we forwarded all messages before we check for
1992 * Postmaster termination.
1994 if (msg_have != 0 || FD_ISSET(pgStatSock, &rfds))
1998 * If the pipe from the postmaster is ready for reading, the
1999 * kernel must have closed it on exit() (the postmaster never
2000 * really writes to it). So we've done our job.
2002 if (FD_ISSET(pmPipe, &rfds))
2008 pgstat_die(SIGNAL_ARGS)
2015 * pgstat_add_backend() -
2017 * Support function to keep our backend list up to date.
2021 pgstat_add_backend(PgStat_MsgHdr *msg)
2023 PgStat_StatDBEntry *dbentry;
2024 PgStat_StatBeEntry *beentry;
2025 PgStat_StatBeDead *deadbe;
2029 * Check that the backend ID is valid
2031 if (msg->m_backendid < 1 || msg->m_backendid > MaxBackends)
2034 (errmsg("invalid server process ID %d", msg->m_backendid)));
2039 * Get the slot for this backendid.
2041 beentry = &pgStatBeTable[msg->m_backendid - 1];
2042 if (beentry->databaseid != InvalidOid)
2045 * If the slot contains the PID of this backend, everything is
2046 * fine and we got nothing to do.
2048 if (beentry->procpid == msg->m_procpid)
2053 * Lookup if this backend is known to be dead. This can be caused due
2054 * to messages arriving in the wrong order - i.e. Postmaster's BETERM
2055 * message might have arrived before we received all the backends
2056 * stats messages, or even a new backend with the same backendid was
2057 * faster in sending his BESTART.
2059 * If the backend is known to be dead, we ignore this add.
2061 deadbe = (PgStat_StatBeDead *) hash_search(pgStatBeDead,
2062 (void *) &(msg->m_procpid),
2068 * Backend isn't known to be dead. If it's slot is currently used, we
2069 * have to kick out the old backend.
2071 if (beentry->databaseid != InvalidOid)
2072 pgstat_sub_backend(beentry->procpid);
2075 * Put this new backend into the slot.
2077 beentry->databaseid = msg->m_databaseid;
2078 beentry->procpid = msg->m_procpid;
2079 beentry->userid = msg->m_userid;
2080 beentry->activity_start_sec = 0;
2081 beentry->activity_start_usec = 0;
2082 MemSet(beentry->activity, 0, PGSTAT_ACTIVITY_SIZE);
2085 * Lookup or create the database entry for this backends DB.
2087 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2088 (void *) &(msg->m_databaseid),
2089 HASH_ENTER, &found);
2090 if (dbentry == NULL)
2093 (errcode(ERRCODE_OUT_OF_MEMORY),
2094 errmsg("out of memory in statistics collector --- abort")));
2099 * If not found, initialize the new one.
2105 dbentry->tables = NULL;
2106 dbentry->n_xact_commit = 0;
2107 dbentry->n_xact_rollback = 0;
2108 dbentry->n_blocks_fetched = 0;
2109 dbentry->n_blocks_hit = 0;
2110 dbentry->n_connects = 0;
2111 dbentry->destroy = 0;
2113 memset(&hash_ctl, 0, sizeof(hash_ctl));
2114 hash_ctl.keysize = sizeof(Oid);
2115 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2116 hash_ctl.hash = tag_hash;
2117 dbentry->tables = hash_create("Per-database table",
2118 PGSTAT_TAB_HASH_SIZE,
2120 HASH_ELEM | HASH_FUNCTION);
2121 if (dbentry->tables == NULL)
2123 /* assume the problem is out-of-memory */
2125 (errcode(ERRCODE_OUT_OF_MEMORY),
2126 errmsg("out of memory in statistics collector --- abort")));
2132 * Count number of connects to the database
2134 dbentry->n_connects++;
2141 * pgstat_sub_backend() -
2143 * Remove a backend from the actual backends list.
2147 pgstat_sub_backend(int procpid)
2150 PgStat_StatBeDead *deadbe;
2154 * Search in the known-backends table for the slot containing this
2157 for (i = 0; i < MaxBackends; i++)
2159 if (pgStatBeTable[i].databaseid != InvalidOid &&
2160 pgStatBeTable[i].procpid == procpid)
2163 * That's him. Add an entry to the known to be dead backends.
2164 * Due to possible misorder in the arrival of UDP packets it's
2165 * possible that even if we know the backend is dead, there
2166 * could still be messages queued that arrive later. Those
2167 * messages must not cause our number of backends statistics
2168 * to get screwed up, so we remember for a couple of seconds
2169 * that this PID is dead and ignore them (only the counting of
2170 * backends, not the table access stats they sent).
2172 deadbe = (PgStat_StatBeDead *) hash_search(pgStatBeDead,
2179 (errcode(ERRCODE_OUT_OF_MEMORY),
2180 errmsg("out of memory in statistics collector --- abort")));
2185 deadbe->backendid = i + 1;
2186 deadbe->destroy = PGSTAT_DESTROY_COUNT;
2190 * Declare the backend slot empty.
2192 pgStatBeTable[i].databaseid = InvalidOid;
2198 * No big problem if not found. This can happen if UDP messages arrive
2199 * out of order here.
2205 * pgstat_write_statsfile() -
2211 pgstat_write_statsfile(void)
2213 HASH_SEQ_STATUS hstat;
2214 HASH_SEQ_STATUS tstat;
2215 PgStat_StatDBEntry *dbentry;
2216 PgStat_StatTabEntry *tabentry;
2217 PgStat_StatBeDead *deadbe;
2222 * Open the statistics temp file to write out the current values.
2224 fpout = fopen(pgStat_tmpfname, PG_BINARY_W);
2228 (errcode_for_file_access(),
2229 errmsg("could not open temporary statistics file \"%s\": %m",
2235 * Walk through the database table.
2237 hash_seq_init(&hstat, pgStatDBHash);
2238 while ((dbentry = (PgStat_StatDBEntry *) hash_seq_search(&hstat)) != NULL)
2241 * If this database is marked destroyed, count down and do so if
2244 if (dbentry->destroy > 0)
2246 if (--(dbentry->destroy) == 0)
2248 if (dbentry->tables != NULL)
2249 hash_destroy(dbentry->tables);
2251 if (hash_search(pgStatDBHash,
2252 (void *) &(dbentry->databaseid),
2253 HASH_REMOVE, NULL) == NULL)
2256 (errmsg("database hash table corrupted "
2257 "during cleanup --- abort")));
2263 * Don't include statistics for it.
2269 * Write out the DB line including the number of life backends.
2272 fwrite(dbentry, sizeof(PgStat_StatDBEntry), 1, fpout);
2275 * Walk through the databases access stats per table.
2277 hash_seq_init(&tstat, dbentry->tables);
2278 while ((tabentry = (PgStat_StatTabEntry *) hash_seq_search(&tstat)) != NULL)
2281 * If table entry marked for destruction, same as above for
2282 * the database entry.
2284 if (tabentry->destroy > 0)
2286 if (--(tabentry->destroy) == 0)
2288 if (hash_search(dbentry->tables,
2289 (void *) &(tabentry->tableid),
2290 HASH_REMOVE, NULL) == NULL)
2293 (errmsg("tables hash table for "
2294 "database %u corrupted during "
2295 "cleanup --- abort",
2296 dbentry->databaseid)));
2304 * At least we think this is still a life table. Print it's
2308 fwrite(tabentry, sizeof(PgStat_StatTabEntry), 1, fpout);
2312 * Mark the end of this DB
2318 * Write out the known running backends to the stats file.
2322 fwrite(&i, sizeof(i), 1, fpout);
2324 for (i = 0; i < MaxBackends; i++)
2326 if (pgStatBeTable[i].databaseid != InvalidOid)
2329 fwrite(&pgStatBeTable[i], sizeof(PgStat_StatBeEntry), 1, fpout);
2334 * No more output to be done. Close the temp file and replace the old
2335 * pgstat.stat with it.
2338 if (fclose(fpout) < 0)
2341 (errcode_for_file_access(),
2342 errmsg("could not close temporary statistics file \"%s\": %m",
2347 if (rename(pgStat_tmpfname, pgStat_fname) < 0)
2350 (errcode_for_file_access(),
2351 errmsg("could not rename temporary statistics file \"%s\" to \"%s\": %m",
2352 pgStat_tmpfname, pgStat_fname)));
2357 * Clear out the dead backends table
2359 hash_seq_init(&hstat, pgStatBeDead);
2360 while ((deadbe = (PgStat_StatBeDead *) hash_seq_search(&hstat)) != NULL)
2363 * Count down the destroy delay and remove entries where it
2366 if (--(deadbe->destroy) <= 0)
2368 if (hash_search(pgStatBeDead,
2369 (void *) &(deadbe->procpid),
2370 HASH_REMOVE, NULL) == NULL)
2373 (errmsg("dead-server-process hash table corrupted "
2374 "during cleanup --- abort")));
2383 * pgstat_read_statsfile() -
2385 * Reads in an existing statistics collector and initializes the
2386 * databases hash table (who's entries point to the tables hash tables)
2387 * and the current backend table.
2391 pgstat_read_statsfile(HTAB **dbhash, Oid onlydb,
2392 PgStat_StatBeEntry **betab, int *numbackends)
2394 PgStat_StatDBEntry *dbentry;
2395 PgStat_StatDBEntry dbbuf;
2396 PgStat_StatTabEntry *tabentry;
2397 PgStat_StatTabEntry tabbuf;
2399 HTAB *tabhash = NULL;
2401 int maxbackends = 0;
2402 int havebackends = 0;
2404 MemoryContext use_mcxt;
2408 * If running in the collector we use the DynaHashCxt memory context.
2409 * If running in a backend, we use the TopTransactionContext instead,
2410 * so the caller must only know the last XactId when this call
2411 * happened to know if his tables are still valid or already gone!
2413 if (pgStatRunningInCollector)
2420 use_mcxt = TopTransactionContext;
2421 mcxt_flags = HASH_CONTEXT;
2425 * Create the DB hashtable
2427 memset(&hash_ctl, 0, sizeof(hash_ctl));
2428 hash_ctl.keysize = sizeof(Oid);
2429 hash_ctl.entrysize = sizeof(PgStat_StatDBEntry);
2430 hash_ctl.hash = tag_hash;
2431 hash_ctl.hcxt = use_mcxt;
2432 *dbhash = hash_create("Databases hash", PGSTAT_DB_HASH_SIZE, &hash_ctl,
2433 HASH_ELEM | HASH_FUNCTION | mcxt_flags);
2434 if (*dbhash == NULL)
2436 /* assume the problem is out-of-memory */
2437 if (pgStatRunningInCollector)
2440 (errcode(ERRCODE_OUT_OF_MEMORY),
2441 errmsg("out of memory in statistics collector --- abort")));
2444 /* in backend, can do normal error */
2446 (errcode(ERRCODE_OUT_OF_MEMORY),
2447 errmsg("out of memory")));
2451 * Initialize the number of known backends to zero, just in case we do
2452 * a silent error return below.
2454 if (numbackends != NULL)
2460 * Try to open the status file. If it doesn't exist, the backends
2461 * simply return zero for anything and the collector simply starts
2462 * from scratch with empty counters.
2464 if ((fpin = fopen(pgStat_fname, PG_BINARY_R)) == NULL)
2468 * We found an existing collector stats file. Read it and put all the
2469 * hashtable entries into place.
2473 switch (fgetc(fpin))
2476 * 'D' A PgStat_StatDBEntry struct describing a database
2477 * follows. Subsequently, zero to many 'T' entries will
2478 * follow until a 'd' is encountered.
2481 if (fread(&dbbuf, 1, sizeof(dbbuf), fpin) != sizeof(dbbuf))
2483 ereport(pgStatRunningInCollector ? LOG : WARNING,
2484 (errmsg("corrupted pgstat.stat file")));
2490 * Add to the DB hash
2492 dbentry = (PgStat_StatDBEntry *) hash_search(*dbhash,
2493 (void *) &dbbuf.databaseid,
2496 if (dbentry == NULL)
2498 if (pgStatRunningInCollector)
2501 (errcode(ERRCODE_OUT_OF_MEMORY),
2502 errmsg("out of memory in statistics collector --- abort")));
2509 (errcode(ERRCODE_OUT_OF_MEMORY),
2510 errmsg("out of memory")));
2515 ereport(pgStatRunningInCollector ? LOG : WARNING,
2516 (errmsg("corrupted pgstat.stat file")));
2521 memcpy(dbentry, &dbbuf, sizeof(PgStat_StatDBEntry));
2522 dbentry->tables = NULL;
2523 dbentry->destroy = 0;
2524 dbentry->n_backends = 0;
2527 * Don't collect tables if not the requested DB
2529 if (onlydb != InvalidOid && onlydb != dbbuf.databaseid)
2532 memset(&hash_ctl, 0, sizeof(hash_ctl));
2533 hash_ctl.keysize = sizeof(Oid);
2534 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
2535 hash_ctl.hash = tag_hash;
2536 hash_ctl.hcxt = use_mcxt;
2537 dbentry->tables = hash_create("Per-database table",
2538 PGSTAT_TAB_HASH_SIZE,
2540 HASH_ELEM | HASH_FUNCTION | mcxt_flags);
2541 if (dbentry->tables == NULL)
2543 /* assume the problem is out-of-memory */
2544 if (pgStatRunningInCollector)
2547 (errcode(ERRCODE_OUT_OF_MEMORY),
2548 errmsg("out of memory in statistics collector --- abort")));
2551 /* in backend, can do normal error */
2554 (errcode(ERRCODE_OUT_OF_MEMORY),
2555 errmsg("out of memory")));
2559 * Arrange that following 'T's add entries to this
2560 * databases tables hash table.
2562 tabhash = dbentry->tables;
2566 * 'd' End of this database.
2573 * 'T' A PgStat_StatTabEntry follows.
2576 if (fread(&tabbuf, 1, sizeof(tabbuf), fpin) != sizeof(tabbuf))
2578 ereport(pgStatRunningInCollector ? LOG : WARNING,
2579 (errmsg("corrupted pgstat.stat file")));
2585 * Skip if table belongs to a not requested database.
2587 if (tabhash == NULL)
2590 tabentry = (PgStat_StatTabEntry *) hash_search(tabhash,
2591 (void *) &tabbuf.tableid,
2592 HASH_ENTER, &found);
2593 if (tabentry == NULL)
2595 if (pgStatRunningInCollector)
2598 (errcode(ERRCODE_OUT_OF_MEMORY),
2599 errmsg("out of memory in statistics collector --- abort")));
2602 /* in backend, can do normal error */
2605 (errcode(ERRCODE_OUT_OF_MEMORY),
2606 errmsg("out of memory")));
2611 ereport(pgStatRunningInCollector ? LOG : WARNING,
2612 (errmsg("corrupted pgstat.stat file")));
2617 memcpy(tabentry, &tabbuf, sizeof(tabbuf));
2621 * 'M' The maximum number of backends to expect follows.
2624 if (betab == NULL || numbackends == NULL)
2629 if (fread(&maxbackends, 1, sizeof(maxbackends), fpin) !=
2630 sizeof(maxbackends))
2632 ereport(pgStatRunningInCollector ? LOG : WARNING,
2633 (errmsg("corrupted pgstat.stat file")));
2637 if (maxbackends == 0)
2644 * Allocate space (in TopTransactionContext too) for the
2647 if (use_mcxt == NULL)
2648 *betab = (PgStat_StatBeEntry *) malloc(
2649 sizeof(PgStat_StatBeEntry) * maxbackends);
2651 *betab = (PgStat_StatBeEntry *) MemoryContextAlloc(
2653 sizeof(PgStat_StatBeEntry) * maxbackends);
2657 * 'B' A PgStat_StatBeEntry follows.
2660 if (betab == NULL || numbackends == NULL)
2672 * Read it directly into the table.
2674 if (fread(&(*betab)[havebackends], 1,
2675 sizeof(PgStat_StatBeEntry), fpin) !=
2676 sizeof(PgStat_StatBeEntry))
2678 ereport(pgStatRunningInCollector ? LOG : WARNING,
2679 (errmsg("corrupted pgstat.stat file")));
2685 * Count backends per database here.
2687 dbentry = (PgStat_StatDBEntry *) hash_search(*dbhash,
2688 (void *) &((*betab)[havebackends].databaseid),
2691 dbentry->n_backends++;
2694 if (numbackends != 0)
2695 *numbackends = havebackends;
2696 if (havebackends >= maxbackends)
2704 * 'E' The EOF marker of a complete stats file.
2711 ereport(pgStatRunningInCollector ? LOG : WARNING,
2712 (errmsg("corrupted pgstat.stat file")));
2723 * pgstat_recv_bestart() -
2725 * Process a backend starup message.
2729 pgstat_recv_bestart(PgStat_MsgBestart *msg, int len)
2731 pgstat_add_backend(&msg->m_hdr);
2736 * pgstat_recv_beterm() -
2738 * Process a backend termination message.
2742 pgstat_recv_beterm(PgStat_MsgBeterm *msg, int len)
2744 pgstat_sub_backend(msg->m_hdr.m_procpid);
2749 * pgstat_recv_activity() -
2751 * Remember what the backend is doing.
2755 pgstat_recv_activity(PgStat_MsgActivity *msg, int len)
2757 PgStat_StatBeEntry *entry;
2760 * Here we check explicitly for 0 return, since we don't want to
2761 * mangle the activity of an active backend by a delayed packed from a
2764 if (pgstat_add_backend(&msg->m_hdr) != 0)
2767 entry = &(pgStatBeTable[msg->m_hdr.m_backendid - 1]);
2769 strncpy(entry->activity, msg->m_what, PGSTAT_ACTIVITY_SIZE);
2771 entry->activity_start_sec =
2772 GetCurrentAbsoluteTimeUsec(&entry->activity_start_usec);
2777 * pgstat_recv_tabstat() -
2779 * Count what the backend has done.
2783 pgstat_recv_tabstat(PgStat_MsgTabstat *msg, int len)
2785 PgStat_TableEntry *tabmsg = &(msg->m_entry[0]);
2786 PgStat_StatDBEntry *dbentry;
2787 PgStat_StatTabEntry *tabentry;
2792 * Make sure the backend is counted for.
2794 if (pgstat_add_backend(&msg->m_hdr) < 0)
2798 * Lookup the database in the hashtable.
2800 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2801 (void *) &(msg->m_hdr.m_databaseid),
2807 * If the database is marked for destroy, this is a delayed UDP packet
2808 * and not worth being counted.
2810 if (dbentry->destroy > 0)
2813 dbentry->n_xact_commit += (PgStat_Counter) (msg->m_xact_commit);
2814 dbentry->n_xact_rollback += (PgStat_Counter) (msg->m_xact_rollback);
2817 * Process all table entries in the message.
2819 for (i = 0; i < msg->m_nentries; i++)
2821 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2822 (void *) &(tabmsg[i].t_id),
2823 HASH_ENTER, &found);
2824 if (tabentry == NULL)
2827 (errcode(ERRCODE_OUT_OF_MEMORY),
2828 errmsg("out of memory in statistics collector --- abort")));
2835 * If it's a new table entry, initialize counters to the
2836 * values we just got.
2838 tabentry->numscans = tabmsg[i].t_numscans;
2839 tabentry->tuples_returned = tabmsg[i].t_tuples_returned;
2840 tabentry->tuples_fetched = tabmsg[i].t_tuples_fetched;
2841 tabentry->tuples_inserted = tabmsg[i].t_tuples_inserted;
2842 tabentry->tuples_updated = tabmsg[i].t_tuples_updated;
2843 tabentry->tuples_deleted = tabmsg[i].t_tuples_deleted;
2844 tabentry->blocks_fetched = tabmsg[i].t_blocks_fetched;
2845 tabentry->blocks_hit = tabmsg[i].t_blocks_hit;
2847 tabentry->destroy = 0;
2852 * Otherwise add the values to the existing entry.
2854 tabentry->numscans += tabmsg[i].t_numscans;
2855 tabentry->tuples_returned += tabmsg[i].t_tuples_returned;
2856 tabentry->tuples_fetched += tabmsg[i].t_tuples_fetched;
2857 tabentry->tuples_inserted += tabmsg[i].t_tuples_inserted;
2858 tabentry->tuples_updated += tabmsg[i].t_tuples_updated;
2859 tabentry->tuples_deleted += tabmsg[i].t_tuples_deleted;
2860 tabentry->blocks_fetched += tabmsg[i].t_blocks_fetched;
2861 tabentry->blocks_hit += tabmsg[i].t_blocks_hit;
2865 * And add the block IO to the database entry.
2867 dbentry->n_blocks_fetched += tabmsg[i].t_blocks_fetched;
2868 dbentry->n_blocks_hit += tabmsg[i].t_blocks_hit;
2874 * pgstat_recv_tabpurge() -
2876 * Arrange for dead table removal.
2880 pgstat_recv_tabpurge(PgStat_MsgTabpurge *msg, int len)
2882 PgStat_StatDBEntry *dbentry;
2883 PgStat_StatTabEntry *tabentry;
2887 * Make sure the backend is counted for.
2889 if (pgstat_add_backend(&msg->m_hdr) < 0)
2893 * Lookup the database in the hashtable.
2895 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2896 (void *) &(msg->m_hdr.m_databaseid),
2902 * If the database is marked for destroy, this is a delayed UDP packet
2903 * and the tables will go away at DB destruction.
2905 if (dbentry->destroy > 0)
2909 * Process all table entries in the message.
2911 for (i = 0; i < msg->m_nentries; i++)
2913 tabentry = (PgStat_StatTabEntry *) hash_search(dbentry->tables,
2914 (void *) &(msg->m_tableid[i]),
2917 tabentry->destroy = PGSTAT_DESTROY_COUNT;
2923 * pgstat_recv_dropdb() -
2925 * Arrange for dead database removal
2929 pgstat_recv_dropdb(PgStat_MsgDropdb *msg, int len)
2931 PgStat_StatDBEntry *dbentry;
2934 * Make sure the backend is counted for.
2936 if (pgstat_add_backend(&msg->m_hdr) < 0)
2940 * Lookup the database in the hashtable.
2942 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2943 (void *) &(msg->m_databaseid),
2949 * Mark the database for destruction.
2951 dbentry->destroy = PGSTAT_DESTROY_COUNT;
2956 * pgstat_recv_dropdb() -
2958 * Arrange for dead database removal
2962 pgstat_recv_resetcounter(PgStat_MsgResetcounter *msg, int len)
2965 PgStat_StatDBEntry *dbentry;
2968 * Make sure the backend is counted for.
2970 if (pgstat_add_backend(&msg->m_hdr) < 0)
2974 * Lookup the database in the hashtable.
2976 dbentry = (PgStat_StatDBEntry *) hash_search(pgStatDBHash,
2977 (void *) &(msg->m_hdr.m_databaseid),
2983 * We simply throw away all the databases table entries by recreating
2984 * a new hash table for them.
2986 if (dbentry->tables != NULL)
2987 hash_destroy(dbentry->tables);
2989 dbentry->tables = NULL;
2990 dbentry->n_xact_commit = 0;
2991 dbentry->n_xact_rollback = 0;
2992 dbentry->n_blocks_fetched = 0;
2993 dbentry->n_blocks_hit = 0;
2994 dbentry->n_connects = 0;
2995 dbentry->destroy = 0;
2997 memset(&hash_ctl, 0, sizeof(hash_ctl));
2998 hash_ctl.keysize = sizeof(Oid);
2999 hash_ctl.entrysize = sizeof(PgStat_StatTabEntry);
3000 hash_ctl.hash = tag_hash;
3001 dbentry->tables = hash_create("Per-database table",
3002 PGSTAT_TAB_HASH_SIZE,
3004 HASH_ELEM | HASH_FUNCTION);
3005 if (dbentry->tables == NULL)
3007 /* assume the problem is out-of-memory */
3009 (errcode(ERRCODE_OUT_OF_MEMORY),
3010 errmsg("out of memory in statistics collector --- abort")));