From e3d415d4a3d691d2bd7864fdb1ec088d445209e5 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 5 Oct 2018 16:57:40 +0900
Subject: [PATCH 10/10] Remove stats collector process

Since stats are directly updated by every backend stats collector is
no longer useful. Remove it.
---
 src/backend/bootstrap/bootstrap.c   |   8 --
 src/backend/postmaster/pgstat.c     | 205 ++++--------------------------------
 src/backend/postmaster/postmaster.c |  78 ++------------
 src/backend/storage/ipc/dsm.c       |  15 ++-
 src/include/pgstat.h                |   7 +-
 src/include/storage/dsm.h           |   1 +
 6 files changed, 42 insertions(+), 272 deletions(-)

diff --git a/src/backend/bootstrap/bootstrap.c b/src/backend/bootstrap/bootstrap.c
index ece200877c..578af2e66d 100644
--- a/src/backend/bootstrap/bootstrap.c
+++ b/src/backend/bootstrap/bootstrap.c
@@ -336,9 +336,6 @@ AuxiliaryProcessMain(int argc, char *argv[])
 			case WalReceiverProcess:
 				statmsg = pgstat_get_backend_desc(B_WAL_RECEIVER);
 				break;
-			case StatsCollectorProcess:
-				statmsg = pgstat_get_backend_desc(B_STATS_COLLECTOR);
-				break;
 			default:
 				statmsg = "??? process";
 				break;
@@ -473,11 +470,6 @@ AuxiliaryProcessMain(int argc, char *argv[])
 			WalReceiverMain();
 			proc_exit(1);		/* should never return */
 
-		case StatsCollectorProcess:
-			/* don't set signals, stats collector has its own agenda */
-			PgstatCollectorMain();
-			proc_exit(1);		/* should never return */
-
 		default:
 			elog(PANIC, "unrecognized process type: %d", (int) MyAuxProcType);
 			proc_exit(1);
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c
index cef3097da9..c8818af5b9 100644
--- a/src/backend/postmaster/pgstat.c
+++ b/src/backend/postmaster/pgstat.c
@@ -129,8 +129,6 @@ PgStat_BgWriter BgWriterStats;
  * Local data
  * ----------
  */
-static time_t last_pgstat_start_time;
-
 /* Shared stats bootstrap infomation */
 typedef struct StatsShmemStruct {
 	dsa_handle stats_dsa_handle;
@@ -286,11 +284,6 @@ static PgStat_ArchiverStats *snapshot_archiverStats;
 static PgStat_GlobalStats *shared_globalStats;
 static PgStat_GlobalStats *snapshot_globalStats;
 
-/* Signal handler flags */
-static volatile bool need_exit = false;
-static volatile bool got_SIGHUP = false;
-static volatile bool got_SIGTERM = false;
-
 /*
  * Total time charged to functions so far in the current backend.
  * We use this to help separate "self" and "other" time charges.
@@ -303,14 +296,8 @@ static instr_time total_func_time;
  * Local function forward declarations
  * ----------
  */
-#ifdef EXEC_BACKEND
-static pid_t pgstat_forkexec(void);
-#endif
-
-static void pgstat_shutdown_handler(SIGNAL_ARGS);
-static void pgstat_quickdie_handler(SIGNAL_ARGS);
+/* functions used in backends */
 static void pgstat_beshutdown_hook(int code, Datum arg);
-static void pgstat_sighup_handler(SIGNAL_ARGS);
 
 static PgStat_StatDBEntry *pgstat_get_db_entry(Oid databaseid, int op,
 									pg_stat_table_result_status *status);
@@ -357,27 +344,25 @@ static bool pgstat_update_tabentry(dshash_table *tabhash,
 static void pgstat_update_dbentry(PgStat_StatDBEntry *dbentry,
 								  PgStat_TableStatus *stat);
 
-/* file in/out functions */
-static void pgstat_read_statsfiles(void);
-static void pgstat_write_statsfiles(void);
-
 /* ------------------------------------------------------------
  * Public functions called from postmaster follow
  * ------------------------------------------------------------
  */
 
 /* ----------
- * pgstat_init() -
+ * pgstat_child_init() -
  *
- *	Called from postmaster at startup. Create the resources required
- *	by the statistics collector process.  If unable to do so, do not
- *	fail --- better to let the postmaster start with stats collection
- *	disabled.
+ *	Called from InitPostmasterChild at process startup. Initialize static
+ *	variables.
  * ----------
  */
 void
-pgstat_init(void)
+pgstat_child_init(void)
 {
+	StatsShmem->stats_dsa_handle = DSM_HANDLE_INVALID;
+	area = NULL;
+
+	return;
 }
 
 /*
@@ -437,12 +422,6 @@ pgstat_reset_all(void)
 	pgstat_reset_remove_files(PGSTAT_STAT_PERMANENT_DIRECTORY);
 }
 
-void
-allow_immediate_pgstat_restart(void)
-{
-	last_pgstat_start_time = 0;
-}
-
 /* ----------
  * pgstat_attach_shared_stats() -
  *
@@ -4384,154 +4363,6 @@ pgstat_update_bgwriter(void)
 	MemSet(&BgWriterStats, 0, sizeof(BgWriterStats));
 }
 
-
-/* ----------
- * PgstatCollectorMain() -
- *
- *	Start up the statistics collector process.  This is the body of the
- *	postmaster child process.
- *
- *	The argc/argv parameters are valid only in EXEC_BACKEND case.
- * ----------
- */
-void
-PgstatCollectorMain(void)
-{
-	int			wr;
-
-	/*
-	 * Ignore all signals usually bound to some action in the postmaster,
-	 * except SIGHUP and SIGQUIT.  Note we don't need a SIGUSR1 handler to
-	 * support latch operations, because we only use a local latch.
-	 */
-	pqsignal(SIGHUP, pgstat_sighup_handler);
-	pqsignal(SIGINT, SIG_IGN);
-	pqsignal(SIGTERM, pgstat_shutdown_handler);
-	pqsignal(SIGQUIT, pgstat_quickdie_handler);
-	pqsignal(SIGALRM, SIG_IGN);
-	pqsignal(SIGPIPE, SIG_IGN);
-	pqsignal(SIGUSR1, SIG_IGN);
-	pqsignal(SIGUSR2, SIG_IGN);
-	pqsignal(SIGCHLD, SIG_DFL);
-	pqsignal(SIGTTIN, SIG_DFL);
-	pqsignal(SIGTTOU, SIG_DFL);
-	pqsignal(SIGCONT, SIG_DFL);
-	pqsignal(SIGWINCH, SIG_DFL);
-
-	PG_SETMASK(&UnBlockSig);
-
-	/*
-	 * Read in existing stats files or initialize the stats to zero.
-	 */
-	pgStatRunningInCollector = true;
-	pgstat_read_statsfiles();
-
-	/*
-	 * Loop to process messages until we get SIGQUIT or detect ungraceful
-	 * death of our parent postmaster.
-	 *
-	 * For performance reasons, we don't want to do ResetLatch/WaitLatch after
-	 * every message; instead, do that only after a recv() fails to obtain a
-	 * message.  (This effectively means that if backends are sending us stuff
-	 * like mad, we won't notice postmaster death until things slack off a
-	 * bit; which seems fine.)	To do that, we have an inner loop that
-	 * iterates as long as recv() succeeds.  We do recognize got_SIGHUP inside
-	 * the inner loop, which means that such interrupts will get serviced but
-	 * the latch won't get cleared until next time there is a break in the
-	 * action.
-	 */
-	for (;;)
-	{
-		/* Clear any already-pending wakeups */
-		ResetLatch(MyLatch);
-
-		/*
-		 * Quit if we get SIGQUIT from the postmaster.
-		 */
-		if (got_SIGTERM)
-			break;
-
-		/*
-		 * Reload configuration if we got SIGHUP from the postmaster.
-		 */
-		if (got_SIGHUP)
-		{
-			got_SIGHUP = false;
-			ProcessConfigFile(PGC_SIGHUP);
-		}
-
-		wr = WaitLatch(MyLatch,
-					   WL_LATCH_SET | WL_POSTMASTER_DEATH,
-					   -1L, WAIT_EVENT_PGSTAT_MAIN);
-
-		/*
-		 * Emergency bailout if postmaster has died.  This is to avoid the
-		 * necessity for manual cleanup of all postmaster children.
-		 */
-		if (wr & WL_POSTMASTER_DEATH)
-			break;
-	}							/* end of outer loop */
-
-	/*
-	 * Save the final stats to reuse at next startup.
-	 */
-	pgstat_write_statsfiles();
-
-	exit(0);
-}
-
-
-/* SIGQUIT signal handler for collector process */
-static void
-pgstat_quickdie_handler(SIGNAL_ARGS)
-{
-	PG_SETMASK(&BlockSig);
-
-	/*
-	 * We DO NOT want to run proc_exit() callbacks -- we're here because
-	 * shared memory may be corrupted, so we don't want to try to clean up our
-	 * transaction.  Just nail the windows shut and get out of town.  Now that
-	 * there's an atexit callback to prevent third-party code from breaking
-	 * things by calling exit() directly, we have to reset the callbacks
-	 * explicitly to make this work as intended.
-	 */
-	on_exit_reset();
-
-	/*
-	 * Note we do exit(2) not exit(0).  This is to force the postmaster into a
-	 * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
-	 * backend.  This is necessary precisely because we don't clean up our
-	 * shared memory state.  (The "dead man switch" mechanism in pmsignal.c
-	 * should ensure the postmaster sees this as a crash, too, but no harm in
-	 * being doubly sure.)
-	 */
-	exit(2);
-}
-
-/* SIGHUP handler for collector process */
-static void
-pgstat_sighup_handler(SIGNAL_ARGS)
-{
-	int			save_errno = errno;
-
-	got_SIGHUP = true;
-	SetLatch(MyLatch);
-
-	errno = save_errno;
-}
-
-static void
-pgstat_shutdown_handler(SIGNAL_ARGS)
-{
-	int save_errno = errno;
-
-	got_SIGTERM = true;
-
-	SetLatch(MyLatch);
-
-	errno = save_errno;
-}
-
 /*
  * Subroutine to reset stats in a shared database entry
  *
@@ -4700,7 +4531,7 @@ pgstat_get_tab_entry(dshash_table *table, Oid tableoid, bool create)
  *		Write the global statistics file, as well as DB files.
  * ----------
  */
-static void
+void
 pgstat_write_statsfiles(void)
 {
 	dshash_seq_status hstat;
@@ -4711,8 +4542,8 @@ pgstat_write_statsfiles(void)
 	const char *statfile = PGSTAT_STAT_PERMANENT_FILENAME;
 	int			rc;
 
-	/* should be called in stats collector  */
-	Assert(pgStatRunningInCollector);
+	/* should be called from postmaster  */
+	Assert(!IsUnderPostmaster);
 
 	elog(DEBUG2, "writing stats file \"%s\"", statfile);
 
@@ -4855,8 +4686,6 @@ pgstat_write_db_statsfile(PgStat_StatDBEntry *dbentry)
 	char		statfile[MAXPGPATH];
 	dshash_table *tbl;
 
-	Assert(pgStatRunningInCollector);
-
 	get_dbstat_filename(true, dbid, tmpfile, MAXPGPATH);
 	get_dbstat_filename(false, dbid, statfile, MAXPGPATH);
 
@@ -4950,7 +4779,7 @@ pgstat_write_db_statsfile(PgStat_StatDBEntry *dbentry)
  *
  * ----------
  */
-static void
+void
 pgstat_read_statsfiles(void)
 {
 	PgStat_StatDBEntry *dbentry;
@@ -4962,7 +4791,9 @@ pgstat_read_statsfiles(void)
 	dshash_table *tblstats = NULL;
 	dshash_table *funcstats = NULL;
 
-	Assert(pgStatRunningInCollector);
+	/* should be called from postmaster  */
+	Assert(!IsUnderPostmaster);
+
 	/*
 	 * The tables will live in pgStatLocalContext.
 	 */
@@ -5173,7 +5004,9 @@ pgstat_read_db_statsfile(Oid databaseid,
 	bool		found;
 	char		statfile[MAXPGPATH];
 
-	Assert(pgStatRunningInCollector);
+	/* should be called from postmaster  */
+	Assert(!IsUnderPostmaster);
+
 	get_dbstat_filename(false, databaseid, statfile, MAXPGPATH);
 
 	/*
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 798df0767d..edf6e64e25 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -145,8 +145,7 @@
 #define BACKEND_TYPE_AUTOVAC	0x0002	/* autovacuum worker process */
 #define BACKEND_TYPE_WALSND		0x0004	/* walsender process */
 #define BACKEND_TYPE_BGWORKER	0x0008	/* bgworker process */
-#define BACKEND_TYPE_STATS		0x0010	/* bgworker process */
-#define BACKEND_TYPE_ALL		0x001F	/* OR of all the above */
+#define BACKEND_TYPE_ALL		0x000F	/* OR of all the above */
 
 #define BACKEND_TYPE_WORKER		(BACKEND_TYPE_AUTOVAC | BACKEND_TYPE_BGWORKER)
 
@@ -254,7 +253,6 @@ static pid_t StartupPID = 0,
 			WalReceiverPID = 0,
 			AutoVacPID = 0,
 			PgArchPID = 0,
-			PgStatPID = 0,
 			SysLoggerPID = 0;
 
 /* Startup process's status */
@@ -552,7 +550,6 @@ static void ShmemBackendArrayRemove(Backend *bn);
 #define StartCheckpointer()		StartChildProcess(CheckpointerProcess)
 #define StartWalWriter()		StartChildProcess(WalWriterProcess)
 #define StartWalReceiver()		StartChildProcess(WalReceiverProcess)
-#define StartStatsCollector()	StartChildProcess(StatsCollectorProcess)
 
 /* Macros to check exit status of a child process */
 #define EXIT_STATUS_0(st)  ((st) == 0)
@@ -1304,12 +1301,6 @@ PostmasterMain(int argc, char *argv[])
 
 	whereToSendOutput = DestNone;
 
-	/*
-	 * Initialize stats collection subsystem (this does NOT start the
-	 * collector process!)
-	 */
-	pgstat_init();
-
 	/*
 	 * Initialize the autovacuum subsystem (again, no process start yet)
 	 */
@@ -1381,6 +1372,9 @@ PostmasterMain(int argc, char *argv[])
 	/* Some workers may be scheduled to start now */
 	maybe_start_bgworkers();
 
+	/* Activate stats collector facility */
+	pgstat_read_statsfiles();
+
 	status = ServerLoop();
 
 	/*
@@ -1762,11 +1756,6 @@ ServerLoop(void)
 				start_autovac_launcher = false; /* signal processed */
 		}
 
-		/* If we have lost the stats collector, try to start a new one */
-		if (PgStatPID == 0 &&
-			(pmState == PM_RUN || pmState == PM_HOT_STANDBY))
-			PgStatPID = StartStatsCollector();
-
 		/* If we have lost the archiver, try to start a new one. */
 		if (PgArchPID == 0 && PgArchStartupAllowed())
 			PgArchPID = pgarch_start();
@@ -2565,8 +2554,6 @@ SIGHUP_handler(SIGNAL_ARGS)
 			signal_child(PgArchPID, SIGHUP);
 		if (SysLoggerPID != 0)
 			signal_child(SysLoggerPID, SIGHUP);
-		if (PgStatPID != 0)
-			signal_child(PgStatPID, SIGHUP);
 
 		/* Reload authentication config files too */
 		if (!load_hba())
@@ -2897,8 +2884,6 @@ reaper(SIGNAL_ARGS)
 				AutoVacPID = StartAutoVacLauncher();
 			if (PgArchStartupAllowed() && PgArchPID == 0)
 				PgArchPID = pgarch_start();
-			if (PgStatPID == 0)
-				PgStatPID = StartStatsCollector();
 
 			/* workers may be scheduled to start now */
 			maybe_start_bgworkers();
@@ -2970,8 +2955,7 @@ reaper(SIGNAL_ARGS)
 				 * We can also shut down the stats collector now; there's
 				 * nothing left for it to do.
 				 */
-				if (PgStatPID != 0)
-					signal_child(PgStatPID, SIGTERM);
+				pgstat_write_statsfiles();
 			}
 			else
 			{
@@ -3048,22 +3032,6 @@ reaper(SIGNAL_ARGS)
 			continue;
 		}
 
-		/*
-		 * Was it the statistics collector?  If so, just try to start a new
-		 * one; no need to force reset of the rest of the system.  (If fail,
-		 * we'll try again in future cycles of the main loop.)
-		 */
-		if (pid == PgStatPID)
-		{
-			PgStatPID = 0;
-			if (!EXIT_STATUS_0(exitstatus))
-				HandleChildCrash(pid, exitstatus,
-								 _("statistics collector process"));
-			if (pmState == PM_RUN || pmState == PM_HOT_STANDBY)
-				PgStatPID = StartStatsCollector();
-			continue;
-		}
-
 		/* Was it the system logger?  If so, try to start a new one */
 		if (pid == SysLoggerPID)
 		{
@@ -3510,22 +3478,6 @@ HandleChildCrash(int pid, int exitstatus, const char *procname)
 		signal_child(PgArchPID, SIGQUIT);
 	}
 
-	/*
-	 * Force a power-cycle of the pgstat process too.  (This isn't absolutely
-	 * necessary, but it seems like a good idea for robustness, and it
-	 * simplifies the state-machine logic in the case where a shutdown request
-	 * arrives during crash processing.)
-	 */
-	if (PgStatPID != 0 && take_action)
-	{
-		ereport(DEBUG2,
-				(errmsg_internal("sending %s to process %d",
-								 "SIGQUIT",
-								 (int) PgStatPID)));
-		signal_child(PgStatPID, SIGQUIT);
-		allow_immediate_pgstat_restart();
-	}
-
 	/* We do NOT restart the syslogger */
 
 	if (Shutdown != ImmediateShutdown)
@@ -3729,8 +3681,6 @@ PostmasterStateMachine(void)
 					SignalChildren(SIGQUIT);
 					if (PgArchPID != 0)
 						signal_child(PgArchPID, SIGQUIT);
-					if (PgStatPID != 0)
-						signal_child(PgStatPID, SIGQUIT);
 				}
 			}
 		}
@@ -3769,8 +3719,7 @@ PostmasterStateMachine(void)
 		 * normal state transition leading up to PM_WAIT_DEAD_END, or during
 		 * FatalError processing.
 		 */
-		if (dlist_is_empty(&BackendList) &&
-			PgArchPID == 0 && PgStatPID == 0)
+		if (dlist_is_empty(&BackendList) && PgArchPID == 0)
 		{
 			/* These other guys should be dead already */
 			Assert(StartupPID == 0);
@@ -3970,8 +3919,6 @@ TerminateChildren(int signal)
 		signal_child(AutoVacPID, signal);
 	if (PgArchPID != 0)
 		signal_child(PgArchPID, signal);
-	if (PgStatPID != 0)
-		signal_child(PgStatPID, signal);
 }
 
 /*
@@ -4969,16 +4916,6 @@ SubPostmasterMain(int argc, char *argv[])
 
 		PgArchiverMain(argc, argv); /* does not return */
 	}
-	if (strcmp(argv[1], "--forkcol") == 0)
-	{
-		/* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
-		InitProcess();
-
-		/* Attach process to shared data structures */
-		CreateSharedMemoryAndSemaphores(false, 0);
-
-		PgstatCollectorMain(argc, argv);	/* does not return */
-	}
 	if (strcmp(argv[1], "--forklog") == 0)
 	{
 		/* Do not want to attach to shared memory */
@@ -5094,8 +5031,7 @@ sigusr1_handler(SIGNAL_ARGS)
 		/*
 		 * Likewise, start other special children as needed.
 		 */
-		Assert(PgStatPID == 0);
-		PgStatPID = StartStatsCollector();
+		pgstat_read_statsfiles();
 
 		ereport(LOG,
 				(errmsg("database system is ready to accept read only connections")));
diff --git a/src/backend/storage/ipc/dsm.c b/src/backend/storage/ipc/dsm.c
index ff0ad4cce3..32508353b7 100644
--- a/src/backend/storage/ipc/dsm.c
+++ b/src/backend/storage/ipc/dsm.c
@@ -432,6 +432,15 @@ dsm_set_control_handle(dsm_handle h)
 }
 #endif
 
+/*
+ * Return if dsm feature is available on this process.
+ */
+bool
+dsm_is_available(void)
+{
+	return dsm_control != NULL;
+}
+
 /*
  * Create a new dynamic shared memory segment.
  *
@@ -449,8 +458,7 @@ dsm_create(Size size, int flags)
 	uint32		i;
 	uint32		nitems;
 
-	/* Unsafe in postmaster (and pointless in a stand-alone backend). */
-	Assert(IsUnderPostmaster);
+	Assert(dsm_is_available());
 
 	if (!dsm_init_done)
 		dsm_backend_startup();
@@ -546,8 +554,7 @@ dsm_attach(dsm_handle h)
 	uint32		i;
 	uint32		nitems;
 
-	/* Unsafe in postmaster (and pointless in a stand-alone backend). */
-	Assert(IsUnderPostmaster);
+	Assert(dsm_is_available());
 
 	if (!dsm_init_done)
 		dsm_backend_startup();
diff --git a/src/include/pgstat.h b/src/include/pgstat.h
index 5db631c54c..dad3440568 100644
--- a/src/include/pgstat.h
+++ b/src/include/pgstat.h
@@ -800,10 +800,8 @@ extern PgStat_Counter pgStatBlockWriteTime;
  * Functions called from postmaster
  * ----------
  */
-extern void pgstat_init(void);
-extern int	pgstat_start(void);
+extern void pgstat_child_init(void);
 extern void pgstat_reset_all(void);
-extern void allow_immediate_pgstat_restart(void);
 
 /* ----------
  * Functions called from backends
@@ -1005,4 +1003,7 @@ extern void CreateSharedBackendStatus(void);
 extern Size StatsShmemSize(void);
 extern void StatsShmemInit(void);
 
+extern void pgstat_read_statsfiles(void);
+extern void pgstat_write_statsfiles(void);
+
 #endif							/* PGSTAT_H */
diff --git a/src/include/storage/dsm.h b/src/include/storage/dsm.h
index 3c3da7ace6..be50f43db0 100644
--- a/src/include/storage/dsm.h
+++ b/src/include/storage/dsm.h
@@ -34,6 +34,7 @@ extern void dsm_detach_all(void);
 extern void dsm_set_control_handle(dsm_handle h);
 #endif
 
+extern bool dsm_is_available(void);
 /* Functions that create, update, or remove mappings. */
 extern dsm_segment *dsm_create(Size size, int flags);
 extern dsm_segment *dsm_attach(dsm_handle h);
-- 
2.16.3

