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