| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test for archive recovery of WAL generated with wal_level=minimal |
| 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 |
# Initialize and start node with wal_level = replica and WAL archiving |
| 13 |
# enabled. |
| 14 |
my $node = PostgreSQL::Test::Cluster->new('orig'); |
| 15 |
$node->init(has_archiving => 1, allows_streaming => 1); |
| 16 |
my $replica_config = q[ |
| 17 |
wal_level = replica |
| 18 |
archive_mode = on |
| 19 |
max_wal_senders = 10 |
| 20 |
hot_standby = off |
| 21 |
]; |
| 22 |
$node->append_conf('postgresql.conf', $replica_config); |
| 23 |
$node->start; |
| 24 |
|
| 25 |
# Take backup |
| 26 |
my $backup_name = 'my_backup'; |
| 27 |
$node->backup($backup_name); |
| 28 |
|
| 29 |
# Restart node with wal_level = minimal and WAL archiving disabled |
| 30 |
# to generate WAL with that setting. Note that such WAL has not been |
| 31 |
# archived yet at this moment because WAL archiving is not enabled. |
| 32 |
$node->append_conf( |
| 33 |
'postgresql.conf', q[ |
| 34 |
wal_level = minimal |
| 35 |
archive_mode = off |
| 36 |
max_wal_senders = 0 |
| 37 |
]); |
| 38 |
$node->restart; |
| 39 |
|
| 40 |
# Restart node with wal_level = replica and WAL archiving enabled |
| 41 |
# to archive WAL previously generated with wal_level = minimal. |
| 42 |
# We ensure the WAL file containing the record indicating the change |
| 43 |
# of wal_level to minimal is archived by checking pg_stat_archiver. |
| 44 |
$node->append_conf('postgresql.conf', $replica_config); |
| 45 |
$node->restart; |
| 46 |
|
| 47 |
# Find next WAL segment to be archived |
| 48 |
my $walfile_to_be_archived = $node->safe_psql('postgres', |
| 49 |
"SELECT pg_walfile_name(pg_current_wal_lsn());"); |
| 50 |
|
| 51 |
# Make WAL segment eligible for archival |
| 52 |
$node->safe_psql('postgres', 'SELECT pg_switch_wal()'); |
| 53 |
my $archive_wait_query = |
| 54 |
"SELECT '$walfile_to_be_archived' <= last_archived_wal FROM pg_stat_archiver;"; |
| 55 |
|
| 56 |
# Wait until the WAL segment has been archived. |
| 57 |
$node->poll_query_until('postgres', $archive_wait_query) |
| 58 |
or die "Timed out while waiting for WAL segment to be archived"; |
| 59 |
|
| 60 |
$node->stop; |
| 61 |
|
| 62 |
# Initialize new node from backup, and start archive recovery. Check that |
| 63 |
# archive recovery fails with an error when it detects the WAL record |
| 64 |
# indicating the change of wal_level to minimal and node stops. |
| 65 |
sub test_recovery_wal_level_minimal |
| 66 |
{ |
| 67 |
my ($node_name, $node_text, $standby_setting) = @_; |
| 68 |
|
| 69 |
my $recovery_node = PostgreSQL::Test::Cluster->new($node_name); |
| 70 |
$recovery_node->init_from_backup( |
| 71 |
$node, $backup_name, |
| 72 |
has_restoring => 1, |
| 73 |
standby => $standby_setting); |
| 74 |
|
| 75 |
# Use run_log instead of recovery_node->start because this test expects |
| 76 |
# that the server ends with an error during recovery. |
| 77 |
run_log( |
| 78 |
[ |
| 79 |
'pg_ctl', |
| 80 |
'--pgdata' => $recovery_node->data_dir, |
| 81 |
'--log' => $recovery_node->logfile, |
| 82 |
'start', |
| 83 |
]); |
| 84 |
|
| 85 |
# wait for postgres to terminate |
| 86 |
foreach my $i (0 .. 10 * $PostgreSQL::Test::Utils::timeout_default) |
| 87 |
{ |
| 88 |
last if !-f $recovery_node->data_dir . '/postmaster.pid'; |
| 89 |
usleep(100_000); |
| 90 |
} |
| 91 |
|
| 92 |
# Confirm that the archive recovery fails with an expected error |
| 93 |
my $logfile = slurp_file($recovery_node->logfile()); |
| 94 |
ok( $logfile =~ |
| 95 |
qr/FATAL: .* WAL was generated with "wal_level=minimal", cannot continue recovering/, |
| 96 |
"$node_text ends with an error because it finds WAL generated with \"wal_level=minimal\"" |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
# Test for archive recovery |
| 101 |
test_recovery_wal_level_minimal('archive_recovery', 'archive recovery', 0); |
| 102 |
|
| 103 |
# Test for standby server |
| 104 |
test_recovery_wal_level_minimal('standby', 'standby', 1); |
| 105 |
|
| 106 |
done_testing(); |