From ec3d221b6a31ad1493c8032e75cd92a9ff26c6b9 Mon Sep 17 00:00:00 2001 From: Srinath Reddy Sadipiralla Date: Fri, 13 Nov 2026 21:26:07 +0530 Subject: [PATCH v1] Treat an immediate recovery target as reached when there's nothing to replay If archive recovery is already consistent before any record has been replayed, as when it starts from a cleanly shut down cluster, and no WAL follows the checkpoint, PerformWalRecovery() takes the "redo is not required" path. Neither recoveryStopsBefore() nor recoveryStopsAfter() is called on that path, so with recovery_target = 'immediate' recovery ended with FATAL: recovery ended before configured recovery target was reached even though the target had been reached. Recognize that case explicitly, and perform the configured recovery_target_action for it. Just skipping the error would not be enough, as the server would then promote even if asked to pause or shut down. To avoid duplicating that code, move it into a new function, PerformRecoveryTargetAction(). --- src/backend/access/transam/xlogrecovery.c | 88 +++++++++++++-------- src/test/recovery/t/003_recovery_targets.pl | 22 ++++++ 2 files changed, 78 insertions(+), 32 deletions(-) diff --git a/src/backend/access/transam/xlogrecovery.c b/src/backend/access/transam/xlogrecovery.c index 3c828f13bce..36a24db852d 100644 --- a/src/backend/access/transam/xlogrecovery.c +++ b/src/backend/access/transam/xlogrecovery.c @@ -363,6 +363,7 @@ static void verifyBackupPageConsistency(XLogReaderState *record); static bool recoveryStopsBefore(XLogReaderState *record); static bool recoveryStopsAfter(XLogReaderState *record); static char *getRecoveryStopReason(void); +static void PerformRecoveryTargetAction(void); static void recoveryPausesHere(bool endOfRecovery); static bool recoveryApplyDelay(XLogReaderState *record); static void ConfirmRecoveryPaused(void); @@ -1833,38 +1834,7 @@ PerformWalRecovery(void) */ if (reachedRecoveryTarget) - { - if (!reachedConsistency) - ereport(FATAL, - (errmsg("requested recovery stop point is before consistent recovery point"))); - - /* - * This is the last point where we can restart recovery with a new - * recovery target, if we shutdown and begin again. After this, - * Resource Managers may choose to do permanent corrective actions - * at end of recovery. - */ - switch (recoveryTargetAction) - { - case RECOVERY_TARGET_ACTION_SHUTDOWN: - - /* - * exit with special return code to request shutdown of - * postmaster. Log messages issued from postmaster. - */ - proc_exit(3); - - case RECOVERY_TARGET_ACTION_PAUSE: - SetRecoveryPause(true); - recoveryPausesHere(true); - - /* drop into promote */ - pg_fallthrough; - - case RECOVERY_TARGET_ACTION_PROMOTE: - break; - } - } + PerformRecoveryTargetAction(); RmgrCleanup(); @@ -1885,6 +1855,21 @@ PerformWalRecovery(void) /* there are no WAL records following the checkpoint */ ereport(LOG, (errmsg("redo is not required"))); + + /* + * If we were consistent before replaying anything, an immediate + * recovery target has been reached, even though there is no record + * for recoveryStopsBefore() to stop at. + */ + if (ArchiveRecoveryRequested && + recoveryTarget == RECOVERY_TARGET_IMMEDIATE && + reachedConsistency) + { + ereport(LOG, + (errmsg("recovery stopping after reaching consistency"))); + reachedRecoveryTarget = true; + PerformRecoveryTargetAction(); + } } /* @@ -1899,6 +1884,45 @@ PerformWalRecovery(void) errmsg("recovery ended before configured recovery target was reached"))); } +/* + * Perform the action requested by recovery_target_action, once the recovery + * target has been reached. + */ +static void +PerformRecoveryTargetAction(void) +{ + if (!reachedConsistency) + ereport(FATAL, + (errmsg("requested recovery stop point is before consistent recovery point"))); + + /* + * This is the last point where we can restart recovery with a new + * recovery target, if we shutdown and begin again. After this, Resource + * Managers may choose to do permanent corrective actions at end of + * recovery. + */ + switch (recoveryTargetAction) + { + case RECOVERY_TARGET_ACTION_SHUTDOWN: + + /* + * exit with special return code to request shutdown of + * postmaster. Log messages issued from postmaster. + */ + proc_exit(3); + + case RECOVERY_TARGET_ACTION_PAUSE: + SetRecoveryPause(true); + recoveryPausesHere(true); + + /* drop into promote */ + pg_fallthrough; + + case RECOVERY_TARGET_ACTION_PROMOTE: + break; + } +} + /* * Subroutine of PerformWalRecovery, to apply one WAL record. */ diff --git a/src/test/recovery/t/003_recovery_targets.pl b/src/test/recovery/t/003_recovery_targets.pl index db4a0ea74b2..c3a5d0ff179 100644 --- a/src/test/recovery/t/003_recovery_targets.pl +++ b/src/test/recovery/t/003_recovery_targets.pl @@ -229,6 +229,28 @@ like( qr/cannot specify more than one recovery target/, 'recovery_target=immediate conflicting with another target is rejected'); +# recovery_target = 'immediate' must also be reached if there is no WAL at all +# after the checkpoint that recovery starts from, as with a cluster that was +# shut down cleanly: recovery is consistent before replaying anything. The +# node's own archive is empty. +my $immediate_target_no_redo_node = + PostgreSQL::Test::Cluster->new('immediate_target_no_redo_node'); +$immediate_target_no_redo_node->init(allows_streaming => 1); +$immediate_target_no_redo_node->start; +$immediate_target_no_redo_node->stop; +$immediate_target_no_redo_node->enable_restoring( + $immediate_target_no_redo_node, 0); +$immediate_target_no_redo_node->append_conf('postgresql.conf', + "recovery_target = 'immediate'"); +$immediate_target_no_redo_node->start; +$immediate_target_no_redo_node->poll_query_until('postgres', + "SELECT pg_get_wal_replay_pause_state() = 'paused'") + or die "Timed out while waiting for recovery to pause at consistency"; +ok( $immediate_target_no_redo_node->log_contains( + 'recovery stopping after reaching consistency'), + 'immediate target reached when there is no WAL to replay'); +$immediate_target_no_redo_node->teardown_node; + # Same-GUC set-then-clear: setting a recovery_target_* GUC and then setting the # same GUC to an empty string leaves no target, so recovery runs to the end of # WAL. Duplicate keys collapse in postgresql.conf, so "pg_ctl --options" passes -- 2.43.0