From 0fc3878375890aef5c0a6dc57d73c30aa97c88df Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Tue, 27 Mar 2018 16:38:37 +0900
Subject: [PATCH] Fix WAL insert when updating full_page_writes for
 checkpointer

At startup or when receiving a SIGHUP signal to update its configuration,
the checkpointer may need to update the shared memory for
full_page_writes and needs to write a WAL record about it.  However,
just after a promotion, there is a small window where it is possible
that the memory context in charge of building WAL records has not been
correctly initialized, leading to a crash of the checkpointer.  With
assertions enabled, this causes a failure when allocating memory in a
critical section.  And this fails when trying to build the FPW record
without assertions.

Report and patch by Dilip Kumar, reviewed by Michael Paquier.

Discussion:
    https://postgr.es/m/CAFiTN-u4BA8KXcQUWDPNgaKAjDXC%3DC2whnzBM8TAcv%3DstckYUw%40mail.gmail.com
---
 src/backend/access/transam/xlog.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index cb9c2a29cb..aae1131977 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -9543,6 +9543,16 @@ UpdateFullPageWrites(void)
 	if (fullPageWrites == Insert->fullPageWrites)
 		return;
 
+	/*
+	 * Initialize InitXLogInsert working areas before entering the critical
+	 * section.  Normally, this is done by the first call to
+	 * RecoveryInProgress() or LocalSetXLogInsertAllowed(), but it can
+	 * be possible that this has not been initialized yet for a checkpointer
+	 * which updates the value of full_page_writes for a received SIGHUP or
+	 * at process startup.
+	 */
+	InitXLogInsert();
+
 	START_CRIT_SECTION();
 
 	/*
-- 
2.16.3

