From 63a9b77b614461abc8741fd3819b62a7d3bec313 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Wed, 25 Nov 2026 16:46:44 +0900
Subject: [PATCH v3 2/2] Check the immediate recovery target at end of WAL

Recovery may reach consistency while attempting to read the next WAL
record rather than while applying a record.  In this case, the main
redo loop exits without checking whether the immediate recovery target
has been reached.

Allow recoveryStopsAfter() to be called without a valid WAL record and
call it again after the main redo loop if no recovery target has been
reached yet.

Discussion: https://postgr.es/m/19897-60cf65e39604f958@postgresql.org
---
 src/backend/access/transam/xlogrecovery.c   | 45 ++++++++++++++++-----
 src/test/recovery/t/003_recovery_targets.pl | 26 ++++++++++++
 2 files changed, 60 insertions(+), 11 deletions(-)

diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c
index bb697362bdf..4117b5e7044 100644
--- a/src/backend/access/transam/xlogrecovery.c
+++ b/src/backend/access/transam/xlogrecovery.c
@@ -1831,6 +1831,10 @@ PerformWalRecovery(void)
 		 * end of main redo apply loop
 		 */
 
+		/* Consistency may have been reached at end of WAL. */
+		if (!reachedRecoveryTarget)
+			reachedRecoveryTarget = recoveryStopsAfter(NULL);
+
 		if (reachedRecoveryTarget)
 		{
 			if (!reachedConsistency)
@@ -2730,10 +2734,19 @@ recoveryStopsBefore(XLogReaderState *record)
 static bool
 recoveryStopsAfter(XLogReaderState *record)
 {
-	uint8		info;
-	uint8		xact_info;
-	uint8		rmid;
+	uint8		info = 0;
+	uint8		xact_info = 0;
+	uint8		rmid = 0;
 	TimestampTz recordXtime = 0;
+	bool		valid_record;
+
+	/*
+	 * We may reach consistency when we run out of WAL, even without a valid
+	 * record.  Only the immediate recovery target can be reached in this case.
+	 * The caller must pass NULL when there is no valid record.
+	 */
+	Assert(record == NULL || record->record != NULL);
+	valid_record = (record != NULL);
 
 	/*
 	 * Ignore recovery target settings when not in archive recovery (meaning
@@ -2742,14 +2755,20 @@ recoveryStopsAfter(XLogReaderState *record)
 	if (!ArchiveRecoveryRequested)
 		return false;
 
-	info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
-	rmid = XLogRecGetRmid(record);
+	/* Set up some info for valid records */
+	if (valid_record)
+	{
+		info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
+		rmid = XLogRecGetRmid(record);
+		xact_info = info & XLOG_XACT_OPMASK;
+	}
 
 	/*
 	 * There can be many restore points that share the same name; we stop at
 	 * the first one.
 	 */
-	if (recoveryTarget == RECOVERY_TARGET_NAME &&
+	if (valid_record &&
+		recoveryTarget == RECOVERY_TARGET_NAME &&
 		rmid == RM_XLOG_ID && info == XLOG_RESTORE_POINT)
 	{
 		xl_restore_point *recordRestorePointData;
@@ -2777,9 +2796,8 @@ recoveryStopsAfter(XLogReaderState *record)
 	 * targets, so that the latest transaction timestamp is updated even if we
 	 * stop at such a record.
 	 */
-	xact_info = info & XLOG_XACT_OPMASK;
-
-	if (rmid == RM_XACT_ID &&
+	if (valid_record &&
+		rmid == RM_XACT_ID &&
 		(xact_info == XLOG_XACT_COMMIT ||
 		 xact_info == XLOG_XACT_COMMIT_PREPARED ||
 		 xact_info == XLOG_XACT_ABORT ||
@@ -2853,8 +2871,13 @@ recoveryStopsAfter(XLogReaderState *record)
 		}
 	}
 
-	/* Check if the target LSN has been reached */
-	if (recoveryTarget == RECOVERY_TARGET_LSN &&
+	/*
+	 * Check if the target LSN has been reached.  If no valid record is
+	 * available, we have already performed this check for the last valid
+	 * record.
+	 */
+	if (valid_record &&
+		recoveryTarget == RECOVERY_TARGET_LSN &&
 		recoveryTargetInclusive &&
 		record->ReadRecPtr >= recoveryTargetLSN)
 	{
diff --git a/src/test/recovery/t/003_recovery_targets.pl b/src/test/recovery/t/003_recovery_targets.pl
index 1fa74062a08..210efddb4af 100644
--- a/src/test/recovery/t/003_recovery_targets.pl
+++ b/src/test/recovery/t/003_recovery_targets.pl
@@ -356,4 +356,30 @@ if (ok($node_immediate->start(fail_ok => 1),
 	$node_immediate->stop;
 }
 
+# Test recovery to an immediate target after a crash, where consistency
+# is reached only when we run out of WAL.  Clear the XID target left
+# over from the preceding tests.
+$node_primary->append_conf(
+	'postgresql.conf', "
+restore_command = 'false'
+recovery_target_xid = ''
+recovery_target = 'immediate'
+recovery_target_action = 'promote'");
+
+$node_primary->set_recovery_mode;
+
+if (ok($node_primary->start(fail_ok => 1),
+		"start recovery to immediate target after crash"))
+{
+	ok($node_primary->poll_query_until('postgres',
+			"SELECT NOT pg_is_in_recovery()"),
+		"promotion after reaching immediate target at end of WAL");
+
+	ok($node_primary->log_contains(
+			"recovery stopping after reaching consistency"),
+		"immediate target reached at end of WAL");
+
+	$node_primary->stop;
+}
+
 done_testing();
-- 
2.52.0

