Hi Srinath,
> While i working on this reported bug [0], i observed this;
> currently, if you restore a backup where no WAL follows the
> shutdown checkpoint, PerformWalRecovery() realizes there is
> nothing to replay and takes the "redo is not required" path.
> Because this path bypasses the main WAL replay loop, neither
> recoveryStopsBefore() nor recoveryStopsAfter() gets called,
> then if the user requested an 'immediate' recovery target, the
> server exits with:
> FATAL: recovery ended before configured recovery target was reached.
>
>
Thank you for reporting the issue and the patch.
I reproduced this on master with a cold copy of a cleanly stopped cluster,
recovery.signal and recovery_target = 'immediate':
LOG: consistent recovery state reached at 0/020000B0
LOG: redo is not required
FATAL: recovery ended before configured recovery target was reached
I reproduced this on master with a cold copy of a cleanly stopped
cluster, recovery.signal and recovery_target = 'immediate':
LOG: consistent recovery state reached at 0/020000B0
LOG: redo is not required
FATAL: recovery ended before configured recovery target was reached
v1 fixes it. I tested each recovery_target_action on the no-redo path,
pause (and pg_wal_replay_resume() then promotes), promote,
shutdown (recovery.signal is kept), and pause with hot_standby = off
(turned into shutdown, as on the normal replay path).
Things that should not change didn't i.e with no recovery target the server
still
promotes, an unreachable recovery_target_name still fails with the
same FATAL, and 'immediate' on an online base backup still stops at
the backup end. The new test in 003_recovery_targets.pl passes with the
patch and fails without it.
A few comments:
1. The patch also fixes a standby case that the commit message does
not mention. A server started with standby.signal and recovery_target =
'immediate' that never receives any WAL waits for
it; if it is promoted, it takes the same "redo is not required" path.
On master that fails with the same FATAL, and the pg_promote() caller
loses its connection when the server shuts down:
LOG: received promote request
LOG: redo is not required
FATAL: recovery ended before configured recovery target was reached
With v1 it promotes. It would be worth mentioning in the commit message
and covering in the test.
2. The test only covers the default action, pause. The attached test-only
diff (applies on top of v1) adds promote, shutdown and the
standby case above; feel free to take what you like. I named it .txt so
cfbot does not pick it up.
3. The FATAL check goes back to dc788668bb2 in PG13, and the same
code is in all supported branches (I only tested master). On
REL_19_STABLE the xlogrecovery.c part of v1 applies cleanly and only
the test code conflicts and on 15-18 the code also conflicts, on
pg_fallthrough. I feel this can be back-ported.
Regards,
Surya Poondla
>
diff --git a/src/test/recovery/t/003_recovery_targets.pl
b/src/test/recovery/t/003_recovery_targets.pl
index c3a5d0ff179..4d33030020b 100644
--- a/src/test/recovery/t/003_recovery_targets.pl
+++ b/src/test/recovery/t/003_recovery_targets.pl
@@ -251,6 +251,78 @@ ok( $immediate_target_no_redo_node->log_contains(
'immediate target reached when there is no WAL to replay');
$immediate_target_no_redo_node->teardown_node;
+# Create a cluster that was shut down cleanly, set up to start archive
+# recovery (or standby mode, if $standby) from its own empty archive with
+# recovery_target = 'immediate', plus any extra settings in @conf.
+sub init_immediate_no_redo_node
+{
+ my ($name, $standby, @conf) = @_;
+
+ my $node = PostgreSQL::Test::Cluster->new($name);
+ $node->init(allows_streaming => 1);
+ $node->start;
+ $node->stop;
+ $node->enable_restoring($node, $standby);
+ $node->append_conf('postgresql.conf',
+ join("\n", "recovery_target = 'immediate'", @conf));
+ return $node;
+}
+
+# With nothing to replay, recovery_target_action = 'promote' ends recovery.
+my $no_redo_promote_node = init_immediate_no_redo_node(
+ 'immediate_target_no_redo_promote',
+ 0, "recovery_target_action = 'promote'");
+$no_redo_promote_node->start;
+$no_redo_promote_node->poll_query_until('postgres',
+ "SELECT NOT pg_is_in_recovery()")
+ or die "Timed out while waiting for promotion at consistency";
+ok( $no_redo_promote_node->log_contains(
+ 'recovery stopping after reaching consistency'),
+ 'immediate target with nothing to replay and action promote');
+$no_redo_promote_node->teardown_node;
+
+# With nothing to replay, recovery_target_action = 'shutdown' shuts the
+# server down, still in recovery. The server may accept connections briefly
+# before it exits, so start it without waiting, as above, and wait for it to
+# terminate.
+my $no_redo_shutdown_node = init_immediate_no_redo_node(
+ 'immediate_target_no_redo_shutdown',
+ 0, "recovery_target_action = 'shutdown'");
+my $log_offset = -s $no_redo_shutdown_node->logfile;
+run_log(
+ [
+ 'pg_ctl',
+ '--pgdata' => $no_redo_shutdown_node->data_dir,
+ '--log' => $no_redo_shutdown_node->logfile,
+ 'start',
+ ]);
+$no_redo_shutdown_node->wait_for_log(qr/database system is shut down/,
+ $log_offset);
+foreach my $i (0 .. 10 * $PostgreSQL::Test::Utils::timeout_default)
+{
+ last if !-f $no_redo_shutdown_node->data_dir . '/postmaster.pid';
+ usleep(100_000);
+}
+ok( $no_redo_shutdown_node->log_contains(
+ 'shutdown at recovery target', $log_offset),
+ 'immediate target with nothing to replay and action shutdown');
+ok(-f $no_redo_shutdown_node->data_dir . '/recovery.signal',
+ 'recovery.signal is kept after shutdown at recovery target');
+
+# A standby with recovery_target = 'immediate' that never receives any WAL
+# waits for it; when promoted, the target must count as reached.
+my $no_redo_standby_node =
+ init_immediate_no_redo_node('immediate_target_no_redo_standby', 1);
+$no_redo_standby_node->start;
+$no_redo_standby_node->promote;
+$no_redo_standby_node->poll_query_until('postgres',
+ "SELECT NOT pg_is_in_recovery()")
+ or die "Timed out while waiting for standby promotion";
+ok( $no_redo_standby_node->log_contains(
+ 'recovery stopping after reaching consistency'),
+ 'standby with immediate target and nothing to replay can be promoted');
+$no_redo_standby_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