From 23eef7ed170c9766b49a40684390d7a2811064a7 Mon Sep 17 00:00:00 2001
From: Srinath Reddy Sadipiralla <srinath2133@gmail.com>
Date: Fri, 13 Nov 2026 21:23:26 +0530
Subject: [PATCH v3 1/2] Fix recovery_target = 'immediate' when WAL ends at the
 consistency point

recoveryStopsAfter() returned early for anything that isn't a
transaction record, so for most records it never got to its check for
recovery_target = 'immediate'.  Usually that doesn't matter, because
recoveryStopsBefore() then stops recovery at the next record.  But when
there is no next record, as in a backup taken from a standby, recovery
runs off the end of the WAL and fails with "recovery ended before
configured recovery target was reached".

The early return came in with commit 4f1b890b137.  Make the rmgr check
part of the condition again, as it was before that.

Reported-by: Haruka Takatsuka <harukat@sraoss.co.jp>
Discussion: https://postgr.es/m/19897-60cf65e39604f958@postgresql.org
---
 src/backend/access/transam/xlogrecovery.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index 52ac6d58423..3c828f13bce 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -2789,15 +2789,17 @@ recoveryStopsAfter(XLogReaderState *record)
 		return true;
 	}
 
-	if (rmid != RM_XACT_ID)
-		return false;
-
+	/*
+	 * Don't return early for other record types: the immediate-target check
+	 * at the bottom must be reached for every record.
+	 */
 	xact_info = info & XLOG_XACT_OPMASK;
 
-	if (xact_info == XLOG_XACT_COMMIT ||
-		xact_info == XLOG_XACT_COMMIT_PREPARED ||
-		xact_info == XLOG_XACT_ABORT ||
-		xact_info == XLOG_XACT_ABORT_PREPARED)
+	if (rmid == RM_XACT_ID &&
+		(xact_info == XLOG_XACT_COMMIT ||
+		 xact_info == XLOG_XACT_COMMIT_PREPARED ||
+		 xact_info == XLOG_XACT_ABORT ||
+		 xact_info == XLOG_XACT_ABORT_PREPARED))
 	{
 		TransactionId recordXid;
 
-- 
2.56.0

