From 17fa4c321ccf9693de406faffe6b235e949aa25f Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Thu, 6 Jun 2013 10:15:45 -0400
Subject: [PATCH] Ensure that XLOG_HEAP2_VISIBLE always targets an initialized
 page.

Andres Freund
---
 src/backend/commands/vacuumlazy.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c
index 9254bcd6868..ff8764b4a14 100644
--- a/src/backend/commands/vacuumlazy.c
+++ b/src/backend/commands/vacuumlazy.c
@@ -650,6 +650,24 @@ lazy_scan_heap(Relation onerel, LVRelStats *vacrelstats,
 			/* empty pages are always all-visible */
 			if (!PageIsAllVisible(page))
 			{
+				/*
+				 * It's possible that another backend has extended the heap,
+				 * initialized the page, and then failed to WAL-log the page
+				 * due to an ERROR.  Since heap extension is not WAL-logged,
+				 * recovery might try to replay our record setting the
+				 * page all-visible and find that the page isn't initialized,
+				 * which will cause a PANIC.  To prevent that, check whether
+				 * the page has been previously WAL-logged, and if not, do that
+				 * now.
+				 *
+				 * XXX: It would be nice to use a logging method supporting
+				 * standard buffers here since log_newpage_buffer() will write
+				 * the full block instead of omitting the hole.
+				 */
+				if (RelationNeedsWAL(onerel) &&
+					XLByteEQ(PageGetLSN(page), InvalidXLogRecPtr))
+					log_newpage_buffer(buf);
+
 				PageSetAllVisible(page);
 				MarkBufferDirty(buf);
 				visibilitymap_set(onerel, blkno, InvalidXLogRecPtr, vmbuffer,
-- 
GitLab