| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Testing streaming replication where standby is promoted and a new cascading |
| 5 |
# standby (without WAL) is connected to the promoted standby. Both archiving |
| 6 |
# and streaming are enabled, but only the history file is available from the |
| 7 |
# archive, so the WAL files all have to be streamed. Test that the cascading |
| 8 |
# standby can follow the new primary (promoted standby). |
| 9 |
use strict; |
| 10 |
use warnings FATAL => 'all'; |
| 11 |
use PostgreSQL::Test::Cluster; |
| 12 |
use PostgreSQL::Test::Utils; |
| 13 |
|
| 14 |
use File::Basename; |
| 15 |
use FindBin; |
| 16 |
use Test::More; |
| 17 |
|
| 18 |
# Initialize primary node |
| 19 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 20 |
|
| 21 |
# Set up an archive command that will copy the history file but not the WAL |
| 22 |
# files. No real archive command should behave this way; the point is to |
| 23 |
# simulate a race condition where the new cascading standby starts up after |
| 24 |
# the timeline history file reaches the archive but before any of the WAL files |
| 25 |
# get there. |
| 26 |
$node_primary->init(allows_streaming => 1, has_archiving => 1); |
| 27 |
|
| 28 |
# Note: consistent use of forward slashes here avoids any escaping problems |
| 29 |
# that arise from use of backslashes. That means we need to double-quote all |
| 30 |
# the paths in the archive_command |
| 31 |
my $perlbin = $^X; |
| 32 |
$perlbin =~ s!\\!/!g if $PostgreSQL::Test::Utils::windows_os; |
| 33 |
my $archivedir_primary = $node_primary->archive_dir; |
| 34 |
$archivedir_primary =~ s!\\!/!g if $PostgreSQL::Test::Utils::windows_os; |
| 35 |
$node_primary->append_conf( |
| 36 |
'postgresql.conf', qq( |
| 37 |
archive_command = '"$perlbin" "$FindBin::RealBin/cp_history_files" "%p" "$archivedir_primary/%f"' |
| 38 |
wal_keep_size=128MB |
| 39 |
)); |
| 40 |
# Make sure that Msys perl doesn't complain about difficulty in setting locale |
| 41 |
# when called from the archive_command. |
| 42 |
local $ENV{PERL_BADLANG} = 0; |
| 43 |
$node_primary->start; |
| 44 |
|
| 45 |
# Take backup from primary |
| 46 |
my $backup_name = 'my_backup'; |
| 47 |
$node_primary->backup($backup_name); |
| 48 |
|
| 49 |
# Create streaming standby linking to primary |
| 50 |
my $node_standby = PostgreSQL::Test::Cluster->new('standby'); |
| 51 |
$node_standby->init_from_backup( |
| 52 |
$node_primary, $backup_name, |
| 53 |
allows_streaming => 1, |
| 54 |
has_streaming => 1, |
| 55 |
has_archiving => 1); |
| 56 |
$node_standby->start; |
| 57 |
|
| 58 |
# Take backup of standby, use -Xnone so that pg_wal is empty. |
| 59 |
$node_standby->backup($backup_name, backup_options => ['-Xnone']); |
| 60 |
|
| 61 |
# Create cascading standby but don't start it yet. |
| 62 |
# Must set up both streaming and archiving. |
| 63 |
my $node_cascade = PostgreSQL::Test::Cluster->new('cascade'); |
| 64 |
$node_cascade->init_from_backup($node_standby, $backup_name, |
| 65 |
has_streaming => 1); |
| 66 |
$node_cascade->enable_restoring($node_primary); |
| 67 |
$node_cascade->append_conf( |
| 68 |
'postgresql.conf', qq( |
| 69 |
recovery_target_timeline='latest' |
| 70 |
)); |
| 71 |
|
| 72 |
# Promote the standby. |
| 73 |
$node_standby->promote; |
| 74 |
|
| 75 |
# Wait for promotion to complete |
| 76 |
$node_standby->poll_query_until('postgres', "SELECT NOT pg_is_in_recovery();") |
| 77 |
or die "Timed out while waiting for promotion"; |
| 78 |
|
| 79 |
# Find next WAL segment to be archived |
| 80 |
my $walfile_to_be_archived = $node_standby->safe_psql('postgres', |
| 81 |
"SELECT pg_walfile_name(pg_current_wal_lsn());"); |
| 82 |
|
| 83 |
# Make WAL segment eligible for archival |
| 84 |
$node_standby->safe_psql('postgres', 'SELECT pg_switch_wal()'); |
| 85 |
|
| 86 |
# Wait until the WAL segment has been archived. |
| 87 |
# Since the history file gets created on promotion and is archived before any |
| 88 |
# WAL segment, this is enough to guarantee that the history file was |
| 89 |
# archived. |
| 90 |
my $archive_wait_query = |
| 91 |
"SELECT '$walfile_to_be_archived' <= last_archived_wal FROM pg_stat_archiver"; |
| 92 |
$node_standby->poll_query_until('postgres', $archive_wait_query) |
| 93 |
or die "Timed out while waiting for WAL segment to be archived"; |
| 94 |
my $last_archived_wal_file = $walfile_to_be_archived; |
| 95 |
|
| 96 |
# Start cascade node |
| 97 |
$node_cascade->start; |
| 98 |
|
| 99 |
# Create some content on promoted standby and check its presence on the |
| 100 |
# cascading standby. |
| 101 |
$node_standby->safe_psql('postgres', "CREATE TABLE tab_int AS SELECT 1 AS a"); |
| 102 |
|
| 103 |
# Wait for the replication to catch up |
| 104 |
$node_standby->wait_for_catchup($node_cascade); |
| 105 |
|
| 106 |
# Check that cascading standby has the new content |
| 107 |
my $result = |
| 108 |
$node_cascade->safe_psql('postgres', "SELECT count(*) FROM tab_int"); |
| 109 |
print "cascade: $result\n"; |
| 110 |
is($result, 1, 'check streamed content on cascade standby'); |
| 111 |
|
| 112 |
done_testing(); |