From 8cb3bb6cf49d64eee0f27fb9f2264abf0cf89f70 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 20 Apr 2018 17:28:56 +0900
Subject: [PATCH 3/3] step2

---
 doc/src/sgml/config.sgml              |  5 ++++-
 src/backend/access/transam/xlog.c     | 15 ++++++++++++---
 src/backend/postmaster/checkpointer.c | 14 ++++----------
 src/include/access/xlog.h             |  2 +-
 4 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 5d5f2d23c4..d39f0c12d4 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -2598,7 +2598,10 @@ include_dir 'conf.d'
        <para>
         This parameter can only be set in the <filename>postgresql.conf</filename>
         file or on the server command line.
-        The default is <literal>on</literal>.
+
+        The default is <literal>on</literal>. Turning of of this variable is
+        immediately takes effect and turning on takes effect at the next
+        checkpoint time.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 72cc880139..efb3abd979 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -8678,6 +8678,9 @@ CreateCheckPoint(int flags)
 	 */
 	last_important_lsn = GetLastImportantRecPtr();
 
+	/* update fullPageWrites in shared memory if turned on */
+	UpdateFullPageWrites(true);
+
 	/*
 	 * We must block concurrent insertions while examining insert state to
 	 * determine the checkpoint REDO pointer.
@@ -9554,11 +9557,13 @@ XLogReportParameters(void)
  * Update full_page_writes in shared memory, and write an
  * XLOG_FPW_CHANGE record if necessary.
  *
- * Note: this function assumes there is no other process running
- * concurrently that could update it.
+ * Processes only turning on if turn_on is true or turning off if not.
+ *
+ * Note: this function assumes there is no other process running concurrently
+ * that could update it.
  */
 void
-UpdateFullPageWrites(void)
+UpdateFullPageWrites(bool turn_on)
 {
 	XLogCtlInsert *Insert = &XLogCtl->Insert;
 
@@ -9572,6 +9577,10 @@ UpdateFullPageWrites(void)
 	if (fullPageWrites == Insert->fullPageWrites)
 		return;
 
+	/* return if we don't handle the change now  */
+	if ((turn_on && !fullPageWrites) || (!turn_on && fullPageWrites))
+		return;
+
 	START_CRIT_SECTION();
 
 	/*
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index 23d404f082..60d1f6b3b0 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -332,12 +332,6 @@ CheckpointerMain(void)
 	 */
 	PG_SETMASK(&UnBlockSig);
 
-	/*
-	 * Ensure all shared memory values are set correctly for the config. Doing
-	 * this here ensures no race conditions from other concurrent updaters.
-	 */
-	UpdateSharedMemoryConfig();
-
 	/*
 	 * Advertise our latch that backends can use to wake us up while we're
 	 * sleeping.
@@ -1379,12 +1373,12 @@ UpdateSharedMemoryConfig(void)
 	SyncRepUpdateSyncStandbysDefined();
 
 	/*
-	 * If full_page_writes has been changed by SIGHUP, we update it in shared
-	 * memory and write an XLOG_FPW_CHANGE record. We don't do this until
-	 * recovery ends.
+	 * If full_page_writes has been turned off by SIGHUP, we update it in
+	 * shared memory and write an XLOG_FPW_CHANGE record. We don't do this
+	 * until recovery ends.
 	 */
 	if (!RecoveryInProgress())
-		UpdateFullPageWrites();
+		UpdateFullPageWrites(false);
 
 	elog(DEBUG2, "checkpointer updated shared memory configuration values");
 }
diff --git a/src/include/access/xlog.h b/src/include/access/xlog.h
index 421ba6d775..dc90d64a2c 100644
--- a/src/include/access/xlog.h
+++ b/src/include/access/xlog.h
@@ -270,7 +270,7 @@ extern void CreateCheckPoint(int flags);
 extern bool CreateRestartPoint(int flags);
 extern void XLogPutNextOid(Oid nextOid);
 extern XLogRecPtr XLogRestorePoint(const char *rpName);
-extern void UpdateFullPageWrites(void);
+extern void UpdateFullPageWrites(bool turn_on);
 extern void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p);
 extern XLogRecPtr GetRedoRecPtr(void);
 extern XLogRecPtr GetInsertRecPtr(void);
-- 
2.16.3

