v4-0001-Fix-missing-continuation-record-after-standby-promot.patch

application/octet-stream

Filename: v4-0001-Fix-missing-continuation-record-after-standby-promot.patch
Type: application/octet-stream
Part: 0
Message: Re: [BUG] Panic due to incorrect missingContrecPtr after promotion

Patch

Format: format-patch
Series: patch v4-0001
Subject: Fix "missing continuation record" after standby promotion
File+
src/backend/access/transam/xlogrecovery.c 4 0
src/test/recovery/t/026_overwrite_contrecord.pl 4 1
From a43c4dc01c27a07798c935cbc15260d1a668d3b9 Mon Sep 17 00:00:00 2001
From: "Sami Imseih (AWS)" <simseih@amazon.com>
Date: Fri, 25 Feb 2022 21:23:34 +0000
Subject: [PATCH 1/1] Fix "missing continuation record" after standby promotion

Invalidate abortedRecPtr and missingContrecPtr after a missing
continuation record is skipped on a standby. This fixes a PANIC
caused when a recently promoted standby attempts to write an
OVERWRITE_RECORD with an LSN of the previously read aborted record.

Author: Sami Imseih
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>, Alvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://postgr.es/m/44D259DE-7542-49C4-8A52-2AB01534DCA9@amazon.co
---
 src/backend/access/transam/xlogrecovery.c       | 4 ++++
 src/test/recovery/t/026_overwrite_contrecord.pl | 5 ++++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index f9f212680b..bbb2d7fef5 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -1948,6 +1948,10 @@ xlogrecovery_redo(XLogReaderState *record, TimeLineID replayTLI)
 				 LSN_FORMAT_ARGS(xlrec.overwritten_lsn),
 				 LSN_FORMAT_ARGS(record->overwrittenRecPtr));
 
+		/* We have safely skipped the aborted record */
+		abortedRecPtr = InvalidXLogRecPtr;
+		missingContrecPtr = InvalidXLogRecPtr;
+
 		ereport(LOG,
 				(errmsg("successfully skipped missing contrecord at %X/%X, overwritten at %s",
 						LSN_FORMAT_ARGS(xlrec.overwritten_lsn),
diff --git a/src/test/recovery/t/026_overwrite_contrecord.pl b/src/test/recovery/t/026_overwrite_contrecord.pl
index 0fd907f152..fe175f5e90 100644
--- a/src/test/recovery/t/026_overwrite_contrecord.pl
+++ b/src/test/recovery/t/026_overwrite_contrecord.pl
@@ -13,7 +13,7 @@ use Test::More;
 # Test: Create a physical replica that's missing the last WAL file,
 # then restart the primary to create a divergent WAL file and observe
 # that the replica replays the "overwrite contrecord" from that new
-# file.
+# file and the standby promotes successfully.
 
 my $node = PostgreSQL::Test::Cluster->new('primary');
 $node->init(allows_streaming => 1);
@@ -100,6 +100,9 @@ like(
 	qr[successfully skipped missing contrecord at],
 	"found log line in standby");
 
+# Verify promotion is successful
+$node_standby->safe_psql('postgres', "select pg_promote()");
+
 $node->stop;
 $node_standby->stop;
 
-- 
2.32.0