From cd6f3162ed9ed678a61b6080a40f004920684b55 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 20 Apr 2018 17:27:49 +0900
Subject: [PATCH 2/3] step1

---
 src/backend/access/transam/xlog.c     | 19 ++++++++-----
 src/backend/postmaster/checkpointer.c | 51 ++++++++++++++++++++++++-----------
 src/include/catalog/pg_control.h      |  2 +-
 src/include/postmaster/bgwriter.h     |  1 +
 4 files changed, 50 insertions(+), 23 deletions(-)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 27753f7321..72cc880139 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -7580,14 +7580,15 @@ StartupXLOG(void)
 	oldestActiveXID = PrescanPreparedTransactions(NULL, NULL);
 
 	/*
-	 * Update full_page_writes in shared memory and write an XLOG_FPW_CHANGE
-	 * record before resource manager writes cleanup WAL records or checkpoint
-	 * record is written.
+	 * Update full_page_writes in shared memory with the lastest value before
+	 * resource manager writes cleanup WAL records or checkpoint record is
+	 * written. We don't need to write XLOG_FPW_CHANGE since this just
+	 * reflects the status at the last redo'ed record. No lock is required
+	 * since startup is the only updator of the flag at this
+	 * point. Checkpointer will take over after SharedRecoveryInProgress is
+	 * turned false.
 	 */
 	Insert->fullPageWrites = lastFullPageWrites;
-	LocalSetXLogInsertAllowed();
-	UpdateFullPageWrites();
-	LocalXLogInsertAllowed = -1;
 
 	if (InRecovery)
 	{
@@ -7829,10 +7830,14 @@ StartupXLOG(void)
 	 * If this was a fast promotion, request an (online) checkpoint now. This
 	 * isn't required for consistency, but the last restartpoint might be far
 	 * back, and in case of a crash, recovering from it might take a longer
-	 * than is appropriate now that we're not in standby mode anymore.
+	 * than is appropriate now that we're not in standby mode anymore.  If
+	 * checkpoint is not needed, tell checkpointer to update shared
+	 * configuration instead.
 	 */
 	if (fast_promoted)
 		RequestCheckpoint(CHECKPOINT_FORCE);
+	else
+		WakeupCheckpointer();
 }
 
 /*
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index 4b452e7cee..23d404f082 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -368,20 +368,26 @@ CheckpointerMain(void)
 		{
 			got_SIGHUP = false;
 			ProcessConfigFile(PGC_SIGHUP);
-
-			/*
-			 * Checkpointer is the last process to shut down, so we ask it to
-			 * hold the keys for a range of other tasks required most of which
-			 * have nothing to do with checkpointing at all.
-			 *
-			 * For various reasons, some config values can change dynamically
-			 * so the primary copy of them is held in shared memory to make
-			 * sure all backends see the same value.  We make Checkpointer
-			 * responsible for updating the shared memory copy if the
-			 * parameter setting changes because of SIGHUP.
-			 */
-			UpdateSharedMemoryConfig();
 		}
+
+		/*
+		 * Checkpointer is the last process to shut down, so we ask it to hold
+		 * the keys for a range of other tasks required most of which have
+		 * nothing to do with checkpointing at all.
+		 *
+		 * For various reasons, some config values can change dynamically so
+		 * the primary copy of them is held in shared memory to make sure all
+		 * backends see the same value.  We make Checkpointer responsible for
+		 * updating the shared memory copy if the parameter setting changes
+		 * because of SIGHUP.
+		 *
+		 * Since some of the shared memory copy are in sync only after
+ 		 * recovery ends, skipped updated are not refrected until the next
+ 		 * SIGHUP. So call UpdateSharedMemoryConfig() regardless of SIGHUP in
+ 		 * order to sync them without waiting the next reload.
+		 */
+		UpdateSharedMemoryConfig();
+
 		if (checkpoint_requested)
 		{
 			checkpoint_requested = false;
@@ -1090,6 +1096,19 @@ RequestCheckpoint(int flags)
 	}
 }
 
+/*
+ * WakeupCheckpointer
+ *
+ * Requests checkpointer to wake up. This causes per-loop processing
+ * regardless of the necessity of a checkpoint.
+ */
+void
+WakeupCheckpointer(void)
+{
+	if (IsUnderPostmaster && CheckpointerShmem->checkpointer_pid != 0)
+		kill(CheckpointerShmem->checkpointer_pid, SIGINT);
+}
+
 /*
  * ForwardFsyncRequest
  *		Forward a file-fsync request from a backend to the checkpointer
@@ -1361,9 +1380,11 @@ UpdateSharedMemoryConfig(void)
 
 	/*
 	 * If full_page_writes has been changed by SIGHUP, we update it in shared
-	 * memory and write an XLOG_FPW_CHANGE record.
+	 * memory and write an XLOG_FPW_CHANGE record. We don't do this until
+	 * recovery ends.
 	 */
-	UpdateFullPageWrites();
+	if (!RecoveryInProgress())
+		UpdateFullPageWrites();
 
 	elog(DEBUG2, "checkpointer updated shared memory configuration values");
 }
diff --git a/src/include/catalog/pg_control.h b/src/include/catalog/pg_control.h
index 773d9e6eba..a7e22e0f5f 100644
--- a/src/include/catalog/pg_control.h
+++ b/src/include/catalog/pg_control.h
@@ -38,7 +38,7 @@ typedef struct CheckPoint
 	TimeLineID	ThisTimeLineID; /* current TLI */
 	TimeLineID	PrevTimeLineID; /* previous TLI, if this record begins a new
 								 * timeline (equals ThisTimeLineID otherwise) */
-	bool		fullPageWrites; /* current full_page_writes */
+	bool		fullPageWrites; /* full_page_writes at REDO start point */
 	uint32		nextXidEpoch;	/* higher-order bits of nextXid */
 	TransactionId nextXid;		/* next free XID */
 	Oid			nextOid;		/* next free OID */
diff --git a/src/include/postmaster/bgwriter.h b/src/include/postmaster/bgwriter.h
index 941c6aba7d..3aac1606a0 100644
--- a/src/include/postmaster/bgwriter.h
+++ b/src/include/postmaster/bgwriter.h
@@ -29,6 +29,7 @@ extern void BackgroundWriterMain(void) pg_attribute_noreturn();
 extern void CheckpointerMain(void) pg_attribute_noreturn();
 
 extern void RequestCheckpoint(int flags);
+extern void WakeupCheckpointer(void);
 extern void CheckpointWriteDelay(int flags, double progress);
 
 extern bool ForwardFsyncRequest(RelFileNode rnode, ForkNumber forknum,
-- 
2.16.3

