From ca3e957b7684c5e67ff8868f6d53cbcf9e4e4d1f Mon Sep 17 00:00:00 2001
From: Matthias van de Meent <boekewurm+postgres@gmail.com>
Date: Mon, 13 May 2024 13:48:04 +0200
Subject: [PATCH v1] Fix logging of non-standard pages in
 RelationCopyStorageUsingBuffer

If the data in the page is non-standard (e.g. the VM, FSM, or an access
method that doesn't update PageHeader's fields as required) then we may
fail to WAL-log the data on that page. When the server crashes after
flushing the WAL record, but before flushing the file writes, this data
may be permanently lost.

Reported-By: Konstantin Knizhnik
---
 src/backend/storage/buffer/bufmgr.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c
index 49637284f9..6a3b9357db 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -4695,9 +4695,13 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
 		memcpy(dstPage, srcPage, BLCKSZ);
 		MarkBufferDirty(dstBuf);
 
-		/* WAL-log the copied page. */
+		/*
+		 * WAL-log the copied page.
+		 * Note that we don't know about the type of data contained in the
+		 * page, so we can't report that the buffer is a standard page.
+		 */
 		if (use_wal)
-			log_newpage_buffer(dstBuf, true);
+			log_newpage_buffer(dstBuf, false);
 
 		END_CRIT_SECTION();
 
-- 
2.40.1

