| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test for recovery targets: name, timestamp, XID |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
use Time::HiRes qw(usleep); |
| 11 |
|
| 12 |
# Create and test a standby from given backup, with a certain recovery target. |
| 13 |
# Choose $until_lsn later than the transaction commit that causes the row |
| 14 |
# count to reach $num_rows, yet not later than the recovery target. |
| 15 |
sub test_recovery_standby |
| 16 |
{ |
| 17 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 18 |
|
| 19 |
my $test_name = shift; |
| 20 |
my $node_name = shift; |
| 21 |
my $node_primary = shift; |
| 22 |
my $recovery_params = shift; |
| 23 |
my $num_rows = shift; |
| 24 |
my $until_lsn = shift; |
| 25 |
|
| 26 |
my $node_standby = PostgreSQL::Test::Cluster->new($node_name); |
| 27 |
$node_standby->init_from_backup($node_primary, 'my_backup', |
| 28 |
has_restoring => 1); |
| 29 |
|
| 30 |
foreach my $param_item (@$recovery_params) |
| 31 |
{ |
| 32 |
$node_standby->append_conf('postgresql.conf', qq($param_item)); |
| 33 |
} |
| 34 |
|
| 35 |
$node_standby->start; |
| 36 |
|
| 37 |
# Wait until standby has replayed enough data |
| 38 |
my $caughtup_query = |
| 39 |
"SELECT '$until_lsn'::pg_lsn <= pg_last_wal_replay_lsn()"; |
| 40 |
$node_standby->poll_query_until('postgres', $caughtup_query) |
| 41 |
or die "Timed out while waiting for standby to catch up"; |
| 42 |
|
| 43 |
# Create some content on primary and check its presence in standby |
| 44 |
my $result = |
| 45 |
$node_standby->safe_psql('postgres', "SELECT count(*) FROM tab_int"); |
| 46 |
is($result, qq($num_rows), "check standby content for $test_name"); |
| 47 |
|
| 48 |
# Stop standby node |
| 49 |
$node_standby->teardown_node; |
| 50 |
|
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
# Initialize primary node |
| 55 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 56 |
$node_primary->init(has_archiving => 1, allows_streaming => 1); |
| 57 |
|
| 58 |
# Bump the transaction ID epoch. This is useful to stress the portability |
| 59 |
# of recovery_target_xid parsing. |
| 60 |
system_or_bail('pg_resetwal', '--epoch' => '1', $node_primary->data_dir); |
| 61 |
|
| 62 |
# Start it |
| 63 |
$node_primary->start; |
| 64 |
|
| 65 |
# Create data before taking the backup, aimed at testing |
| 66 |
# recovery_target = 'immediate' |
| 67 |
$node_primary->safe_psql('postgres', |
| 68 |
"CREATE TABLE tab_int AS SELECT generate_series(1,1000) AS a"); |
| 69 |
my $lsn1 = |
| 70 |
$node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();"); |
| 71 |
|
| 72 |
# Take backup from which all operations will be run |
| 73 |
$node_primary->backup('my_backup'); |
| 74 |
|
| 75 |
# Insert some data with used as a replay reference, with a recovery |
| 76 |
# target TXID. |
| 77 |
$node_primary->safe_psql('postgres', |
| 78 |
"INSERT INTO tab_int VALUES (generate_series(1001,2000))"); |
| 79 |
my $ret = $node_primary->safe_psql('postgres', |
| 80 |
"SELECT pg_current_wal_lsn(), pg_current_xact_id();"); |
| 81 |
my ($lsn2, $recovery_txid) = split /\|/, $ret; |
| 82 |
|
| 83 |
# More data, with recovery target timestamp |
| 84 |
$node_primary->safe_psql('postgres', |
| 85 |
"INSERT INTO tab_int VALUES (generate_series(2001,3000))"); |
| 86 |
my $lsn3 = |
| 87 |
$node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();"); |
| 88 |
my $recovery_time = $node_primary->safe_psql('postgres', "SELECT now()"); |
| 89 |
|
| 90 |
# Even more data, this time with a recovery target name |
| 91 |
$node_primary->safe_psql('postgres', |
| 92 |
"INSERT INTO tab_int VALUES (generate_series(3001,4000))"); |
| 93 |
my $recovery_name = "my_target"; |
| 94 |
my $lsn4 = |
| 95 |
$node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();"); |
| 96 |
$node_primary->safe_psql('postgres', |
| 97 |
"SELECT pg_create_restore_point('$recovery_name');"); |
| 98 |
|
| 99 |
# And now for a recovery target LSN |
| 100 |
$node_primary->safe_psql('postgres', |
| 101 |
"INSERT INTO tab_int VALUES (generate_series(4001,5000))"); |
| 102 |
my $lsn5 = my $recovery_lsn = |
| 103 |
$node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn()"); |
| 104 |
|
| 105 |
$node_primary->safe_psql('postgres', |
| 106 |
"INSERT INTO tab_int VALUES (generate_series(5001,6000))"); |
| 107 |
|
| 108 |
# Force archiving of WAL file |
| 109 |
$node_primary->safe_psql('postgres', "SELECT pg_switch_wal()"); |
| 110 |
|
| 111 |
# Test recovery targets |
| 112 |
my @recovery_params = ("recovery_target = 'immediate'"); |
| 113 |
test_recovery_standby('immediate target', |
| 114 |
'standby_1', $node_primary, \@recovery_params, "1000", $lsn1); |
| 115 |
@recovery_params = ("recovery_target_xid = '$recovery_txid'"); |
| 116 |
test_recovery_standby('XID', 'standby_2', $node_primary, \@recovery_params, |
| 117 |
"2000", $lsn2); |
| 118 |
@recovery_params = ("recovery_target_time = '$recovery_time'"); |
| 119 |
test_recovery_standby('time', 'standby_3', $node_primary, \@recovery_params, |
| 120 |
"3000", $lsn3); |
| 121 |
@recovery_params = ("recovery_target_name = '$recovery_name'"); |
| 122 |
test_recovery_standby('name', 'standby_4', $node_primary, \@recovery_params, |
| 123 |
"4000", $lsn4); |
| 124 |
@recovery_params = ("recovery_target_lsn = '$recovery_lsn'"); |
| 125 |
test_recovery_standby('LSN', 'standby_5', $node_primary, \@recovery_params, |
| 126 |
"5000", $lsn5); |
| 127 |
|
| 128 |
# Multiple targets |
| 129 |
# |
| 130 |
# Multiple conflicting settings are not allowed, but setting the same |
| 131 |
# parameter multiple times or unsetting a parameter and setting a |
| 132 |
# different one is allowed. |
| 133 |
|
| 134 |
@recovery_params = ( |
| 135 |
"recovery_target_name = '$recovery_name'", |
| 136 |
"recovery_target_name = ''", |
| 137 |
"recovery_target_time = '$recovery_time'"); |
| 138 |
test_recovery_standby('multiple overriding settings', |
| 139 |
'standby_6', $node_primary, \@recovery_params, "3000", $lsn3); |
| 140 |
|
| 141 |
my $node_standby = PostgreSQL::Test::Cluster->new('standby_7'); |
| 142 |
$node_standby->init_from_backup($node_primary, 'my_backup', |
| 143 |
has_restoring => 1); |
| 144 |
$node_standby->append_conf( |
| 145 |
'postgresql.conf', "recovery_target_name = '$recovery_name' |
| 146 |
recovery_target_time = '$recovery_time'"); |
| 147 |
|
| 148 |
my $res = run_log( |
| 149 |
[ |
| 150 |
'pg_ctl', |
| 151 |
'--pgdata' => $node_standby->data_dir, |
| 152 |
'--log' => $node_standby->logfile, |
| 153 |
'start', |
| 154 |
]); |
| 155 |
ok(!$res, 'invalid recovery startup fails'); |
| 156 |
|
| 157 |
my $logfile = slurp_file($node_standby->logfile()); |
| 158 |
ok($logfile =~ qr/multiple recovery targets specified/, |
| 159 |
'multiple conflicting settings'); |
| 160 |
|
| 161 |
# Check behavior when recovery ends before target is reached |
| 162 |
|
| 163 |
$node_standby = PostgreSQL::Test::Cluster->new('standby_8'); |
| 164 |
$node_standby->init_from_backup( |
| 165 |
$node_primary, 'my_backup', |
| 166 |
has_restoring => 1, |
| 167 |
standby => 0); |
| 168 |
$node_standby->append_conf('postgresql.conf', |
| 169 |
"recovery_target_name = 'does_not_exist'"); |
| 170 |
|
| 171 |
run_log( |
| 172 |
[ |
| 173 |
'pg_ctl', |
| 174 |
'--pgdata' => $node_standby->data_dir, |
| 175 |
'--log' => $node_standby->logfile, |
| 176 |
'start', |
| 177 |
]); |
| 178 |
|
| 179 |
# wait for postgres to terminate |
| 180 |
foreach my $i (0 .. 10 * $PostgreSQL::Test::Utils::timeout_default) |
| 181 |
{ |
| 182 |
last if !-f $node_standby->data_dir . '/postmaster.pid'; |
| 183 |
usleep(100_000); |
| 184 |
} |
| 185 |
$logfile = slurp_file($node_standby->logfile()); |
| 186 |
ok( $logfile =~ |
| 187 |
qr/FATAL: .* recovery ended before configured recovery target was reached/, |
| 188 |
'recovery end before target reached is a fatal error'); |
| 189 |
|
| 190 |
done_testing(); |