|
| 1 | + |
| 2 | +# Copyright (c) 2021, 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; |
| 11 | +use PostgresNode; |
| 12 | +use TestLib; |
| 13 | +use FindBin; |
| 14 | +use Test::More tests => 1; |
| 15 | + |
| 16 | +# Initialize primary node |
| 17 | +my $node_primary = get_new_node('primary'); |
| 18 | + |
| 19 | +# Set up an archive command that will copy the history file but not the WAL |
| 20 | +# files. No real archive command should behave this way; the point is to |
| 21 | +# simulate a race condition where the new cascading standby starts up after |
| 22 | +# the timeline history file reaches the archive but before any of the WAL files |
| 23 | +# get there. |
| 24 | +$node_primary->init(allows_streaming => 1, has_archiving => 1); |
| 25 | +my $perlbin = $^X; |
| 26 | +$perlbin =~ s{\\}{\\\\}g if ($TestLib::windows_os); |
| 27 | +my $archivedir_primary = $node_primary->archive_dir; |
| 28 | +$node_primary->append_conf('postgresql.conf', qq( |
| 29 | +archive_command = '$perlbin "$FindBin::RealBin/cp_history_files" "%p" "$archivedir_primary/%f"' |
| 30 | +)); |
| 31 | +$node_primary->start; |
| 32 | + |
| 33 | +# Take backup from primary |
| 34 | +my $backup_name = 'my_backup'; |
| 35 | +$node_primary->backup($backup_name); |
| 36 | + |
| 37 | +# Create streaming standby linking to primary |
| 38 | +my $node_standby = get_new_node('standby'); |
| 39 | +$node_standby->init_from_backup($node_primary, $backup_name, |
| 40 | + allows_streaming => 1, has_streaming => 1, has_archiving => 1); |
| 41 | +$node_standby->start; |
| 42 | + |
| 43 | +# Take backup of standby, use -Xnone so that pg_wal is empty. |
| 44 | +$node_standby->backup($backup_name, backup_options => ['-Xnone']); |
| 45 | + |
| 46 | +# Create cascading standby but don't start it yet. |
| 47 | +# Must set up both streaming and archiving. |
| 48 | +my $node_cascade = get_new_node('cascade'); |
| 49 | +$node_cascade->init_from_backup($node_standby, $backup_name, |
| 50 | + has_streaming => 1); |
| 51 | +$node_cascade->enable_restoring($node_primary); |
| 52 | +$node_cascade->append_conf('postgresql.conf', qq( |
| 53 | +recovery_target_timeline='latest' |
| 54 | +)); |
| 55 | + |
| 56 | +# Promote the standby. |
| 57 | +$node_standby->promote; |
| 58 | + |
| 59 | +# Wait for promotion to complete |
| 60 | +$node_standby->poll_query_until('postgres', |
| 61 | + "SELECT NOT pg_is_in_recovery();") |
| 62 | + or die "Timed out while waiting for promotion"; |
| 63 | + |
| 64 | +# Find next WAL segment to be archived |
| 65 | +my $walfile_to_be_archived = $node_standby->safe_psql('postgres', |
| 66 | + "SELECT pg_walfile_name(pg_current_wal_lsn());"); |
| 67 | + |
| 68 | +# Make WAL segment eligible for archival |
| 69 | +$node_standby->safe_psql('postgres', 'SELECT pg_switch_wal()'); |
| 70 | + |
| 71 | +# Wait until the WAL segment has been archived. |
| 72 | +# Since the history file gets created on promotion and is archived before any |
| 73 | +# WAL segment, this is enough to guarantee that the history file was |
| 74 | +# archived. |
| 75 | +my $archive_wait_query = |
| 76 | + "SELECT '$walfile_to_be_archived' <= last_archived_wal FROM pg_stat_archiver;"; |
| 77 | +$node_standby->poll_query_until('postgres', $archive_wait_query) |
| 78 | + or die "Timed out while waiting for WAL segment to be archived"; |
| 79 | +my $last_archived_wal_file = $walfile_to_be_archived; |
| 80 | + |
| 81 | +# Start cascade node |
| 82 | +$node_cascade->start; |
| 83 | + |
| 84 | +# Create some content on promoted standby and check its presence on the |
| 85 | +# cascading standby. |
| 86 | +$node_standby->safe_psql('postgres', "CREATE TABLE tab_int AS SELECT 1 AS a"); |
| 87 | + |
| 88 | +# Wait for the replication to catch up |
| 89 | +$node_standby->wait_for_catchup($node_cascade, 'replay', |
| 90 | + $node_standby->lsn('insert')); |
| 91 | + |
| 92 | +# Check that cascading standby has the new content |
| 93 | +my $result = |
| 94 | + $node_cascade->safe_psql('postgres', "SELECT count(*) FROM tab_int"); |
| 95 | +print "cascade: $result\n"; |
| 96 | +is($result, 1, 'check streamed content on cascade standby'); |
0 commit comments