| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# test for archiving with hot standby |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
use File::Copy; |
| 11 |
|
| 12 |
# Initialize primary node, doing archives |
| 13 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 14 |
$node_primary->init( |
| 15 |
has_archiving => 1, |
| 16 |
allows_streaming => 1); |
| 17 |
my $backup_name = 'my_backup'; |
| 18 |
|
| 19 |
# Start it |
| 20 |
$node_primary->start; |
| 21 |
|
| 22 |
# Take backup for standby |
| 23 |
$node_primary->backup($backup_name); |
| 24 |
|
| 25 |
# Initialize standby node from backup, fetching WAL from archives |
| 26 |
my $node_standby = PostgreSQL::Test::Cluster->new('standby'); |
| 27 |
# Note that this makes the standby store its contents on the archives |
| 28 |
# of the primary. |
| 29 |
$node_standby->init_from_backup($node_primary, $backup_name, |
| 30 |
has_restoring => 1); |
| 31 |
$node_standby->append_conf('postgresql.conf', |
| 32 |
"wal_retrieve_retry_interval = '100ms'"); |
| 33 |
|
| 34 |
# Set archive_cleanup_command and recovery_end_command, checking their |
| 35 |
# execution by the backend with dummy commands. |
| 36 |
my $data_dir = $node_standby->data_dir; |
| 37 |
my $archive_cleanup_command_file = "archive_cleanup_command.done"; |
| 38 |
my $recovery_end_command_file = "recovery_end_command.done"; |
| 39 |
$node_standby->append_conf( |
| 40 |
'postgresql.conf', qq( |
| 41 |
archive_cleanup_command = 'echo archive_cleanup_done > $archive_cleanup_command_file' |
| 42 |
recovery_end_command = 'echo recovery_ended_done > $recovery_end_command_file' |
| 43 |
)); |
| 44 |
$node_standby->start; |
| 45 |
|
| 46 |
# Create some content on primary |
| 47 |
$node_primary->safe_psql('postgres', |
| 48 |
"CREATE TABLE tab_int AS SELECT generate_series(1,1000) AS a"); |
| 49 |
|
| 50 |
# Note the presence of this checkpoint for the archive_cleanup_command |
| 51 |
# check done below, before switching to a new segment. |
| 52 |
$node_primary->safe_psql('postgres', "CHECKPOINT"); |
| 53 |
|
| 54 |
# Done after the checkpoint to ensure that it is replayed on the standby, |
| 55 |
# for archive_cleanup_command. |
| 56 |
my $current_lsn = |
| 57 |
$node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();"); |
| 58 |
|
| 59 |
# Force archiving of WAL file to make it present on primary |
| 60 |
$node_primary->safe_psql('postgres', "SELECT pg_switch_wal()"); |
| 61 |
|
| 62 |
# Add some more content, it should not be present on standby |
| 63 |
$node_primary->safe_psql('postgres', |
| 64 |
"INSERT INTO tab_int VALUES (generate_series(1001,2000))"); |
| 65 |
|
| 66 |
# Wait until necessary replay has been done on standby |
| 67 |
my $caughtup_query = |
| 68 |
"SELECT '$current_lsn'::pg_lsn <= pg_last_wal_replay_lsn()"; |
| 69 |
$node_standby->poll_query_until('postgres', $caughtup_query) |
| 70 |
or die "Timed out while waiting for standby to catch up"; |
| 71 |
|
| 72 |
my $result = |
| 73 |
$node_standby->safe_psql('postgres', "SELECT count(*) FROM tab_int"); |
| 74 |
is($result, qq(1000), 'check content from archives'); |
| 75 |
|
| 76 |
# archive_cleanup_command is executed after generating a restart point, |
| 77 |
# with a checkpoint. |
| 78 |
$node_standby->safe_psql('postgres', q{CHECKPOINT}); |
| 79 |
ok( -f "$data_dir/$archive_cleanup_command_file", |
| 80 |
'archive_cleanup_command executed on checkpoint'); |
| 81 |
ok( !-f "$data_dir/$recovery_end_command_file", |
| 82 |
'recovery_end_command not executed yet'); |
| 83 |
|
| 84 |
# Check the presence of temporary files specifically generated during |
| 85 |
# archive recovery. To ensure the presence of the temporary history |
| 86 |
# file, switch to a timeline large enough to allow a standby to recover |
| 87 |
# a history file from an archive. As this requires at least two timeline |
| 88 |
# switches, promote the existing standby first. Then create a second |
| 89 |
# standby based on the primary, using its archives. Finally, the second |
| 90 |
# standby is promoted. |
| 91 |
$node_standby->promote; |
| 92 |
|
| 93 |
# Wait until the history file has been stored on the archives of the |
| 94 |
# primary once the promotion of the standby completes. This ensures that |
| 95 |
# the second standby created below will be able to restore this file, |
| 96 |
# creating a RECOVERYHISTORY. |
| 97 |
my $primary_archive = $node_primary->archive_dir; |
| 98 |
$caughtup_query = |
| 99 |
"SELECT size IS NOT NULL FROM pg_stat_file('$primary_archive/00000002.history', true)"; |
| 100 |
$node_primary->poll_query_until('postgres', $caughtup_query) |
| 101 |
or die "Timed out while waiting for archiving of 00000002.history"; |
| 102 |
|
| 103 |
# recovery_end_command should have been triggered on promotion. |
| 104 |
ok( -f "$data_dir/$recovery_end_command_file", |
| 105 |
'recovery_end_command executed after promotion'); |
| 106 |
|
| 107 |
my $node_standby2 = PostgreSQL::Test::Cluster->new('standby2'); |
| 108 |
$node_standby2->init_from_backup($node_primary, $backup_name, |
| 109 |
has_restoring => 1); |
| 110 |
|
| 111 |
# Make execution of recovery_end_command fail. This should not affect |
| 112 |
# promotion, and its failure should be logged. |
| 113 |
$node_standby2->append_conf( |
| 114 |
'postgresql.conf', qq( |
| 115 |
recovery_end_command = 'echo recovery_end_failed > missing_dir/xyz.file' |
| 116 |
)); |
| 117 |
|
| 118 |
$node_standby2->start; |
| 119 |
|
| 120 |
# Save the log location, to see the failure of recovery_end_command. |
| 121 |
my $log_location = -s $node_standby2->logfile; |
| 122 |
|
| 123 |
# Now promote standby2, and check that temporary files specifically |
| 124 |
# generated during archive recovery are removed by the end of recovery. |
| 125 |
$node_standby2->promote; |
| 126 |
|
| 127 |
# Check the logs of the standby to see that the commands have failed. |
| 128 |
my $log_contents = slurp_file($node_standby2->logfile, $log_location); |
| 129 |
my $node_standby2_data = $node_standby2->data_dir; |
| 130 |
|
| 131 |
like( |
| 132 |
$log_contents, |
| 133 |
qr/restored log file "00000002.history" from archive/s, |
| 134 |
"00000002.history retrieved from the archives"); |
| 135 |
ok( !-f "$node_standby2_data/pg_wal/RECOVERYHISTORY", |
| 136 |
"RECOVERYHISTORY removed after promotion"); |
| 137 |
ok( !-f "$node_standby2_data/pg_wal/RECOVERYXLOG", |
| 138 |
"RECOVERYXLOG removed after promotion"); |
| 139 |
like( |
| 140 |
$log_contents, |
| 141 |
qr/WARNING:.*recovery_end_command/s, |
| 142 |
"recovery_end_command failure detected in logs after promotion"); |
| 143 |
|
| 144 |
done_testing(); |