| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test for timeline switch |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
# Ensure that a cascading standby is able to follow a newly-promoted standby |
| 12 |
# on a new timeline. |
| 13 |
|
| 14 |
# Initialize primary node |
| 15 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 16 |
$node_primary->init(allows_streaming => 1); |
| 17 |
$node_primary->start; |
| 18 |
|
| 19 |
# Take backup |
| 20 |
my $backup_name = 'my_backup'; |
| 21 |
$node_primary->backup($backup_name); |
| 22 |
|
| 23 |
# Create two standbys linking to it |
| 24 |
my $node_standby_1 = PostgreSQL::Test::Cluster->new('standby_1'); |
| 25 |
$node_standby_1->init_from_backup($node_primary, $backup_name, |
| 26 |
has_streaming => 1); |
| 27 |
$node_standby_1->start; |
| 28 |
my $node_standby_2 = PostgreSQL::Test::Cluster->new('standby_2'); |
| 29 |
$node_standby_2->init_from_backup($node_primary, $backup_name, |
| 30 |
has_streaming => 1); |
| 31 |
$node_standby_2->start; |
| 32 |
|
| 33 |
# Wait for standby_1 and standby_2 connection to the primary. |
| 34 |
$node_primary->poll_query_until('postgres', |
| 35 |
"SELECT count(1) = 2 FROM pg_stat_replication"); |
| 36 |
|
| 37 |
# Create some content on primary |
| 38 |
$node_primary->safe_psql('postgres', |
| 39 |
"CREATE TABLE tab_int AS SELECT generate_series(1,1000) AS a"); |
| 40 |
|
| 41 |
# Cleanly stop and remove primary. A clean stop is required so as all |
| 42 |
# the records generated on the primary are received and flushed by the two |
| 43 |
# standbys. |
| 44 |
$node_primary->stop; |
| 45 |
|
| 46 |
# promote standby 1 using "pg_promote", switching it to a new timeline |
| 47 |
my $psql_out = ''; |
| 48 |
$node_standby_1->psql( |
| 49 |
'postgres', |
| 50 |
"SELECT pg_promote(wait_seconds => 300)", |
| 51 |
stdout => \$psql_out); |
| 52 |
is($psql_out, 't', "promotion of standby with pg_promote"); |
| 53 |
|
| 54 |
# Switch standby 2 to replay from standby 1. During the timeline switch, |
| 55 |
# the WAL receiver process on standby 2 should not be stopped, and the |
| 56 |
# new primary connection string should not be visible |
| 57 |
# in pg_stat_wal_receiver. |
| 58 |
my $secret = 'dont_show_me'; |
| 59 |
my $connstr_1 = $node_standby_1->connstr; |
| 60 |
$node_standby_2->append_conf( |
| 61 |
'postgresql.conf', qq( |
| 62 |
primary_conninfo='$connstr_1 password=$secret' |
| 63 |
)); |
| 64 |
|
| 65 |
# Rotate logfile before restarting, for the log checks done below. |
| 66 |
$node_standby_2->rotate_logfile; |
| 67 |
$node_standby_2->restart; |
| 68 |
|
| 69 |
# Wait for walreceiver to reconnect after the restart. We want to |
| 70 |
# verify that after reconnection, the walreceiver stays alive during |
| 71 |
# the timeline switch. |
| 72 |
$node_standby_2->poll_query_until('postgres', |
| 73 |
"SELECT EXISTS(SELECT 1 FROM pg_stat_wal_receiver)"); |
| 74 |
my $wr_pid_before_switch = $node_standby_2->safe_psql('postgres', |
| 75 |
"SELECT pid FROM pg_stat_wal_receiver"); |
| 76 |
|
| 77 |
# Insert some data in standby 1 and check its presence in standby 2 |
| 78 |
# to ensure that the timeline switch has been done. |
| 79 |
$node_standby_1->safe_psql('postgres', |
| 80 |
"INSERT INTO tab_int VALUES (generate_series(1001,2000))"); |
| 81 |
$node_standby_1->wait_for_catchup($node_standby_2); |
| 82 |
|
| 83 |
my $result = |
| 84 |
$node_standby_2->safe_psql('postgres', "SELECT count(*) FROM tab_int"); |
| 85 |
is($result, qq(2000), 'check content of standby 2'); |
| 86 |
|
| 87 |
# Check the logs, WAL receiver should not have been stopped while |
| 88 |
# transitioning to its new timeline. There is no need to rely on an |
| 89 |
# offset in this check of the server logs: a new log file is used on |
| 90 |
# node restart when primary_conninfo is updated above. |
| 91 |
ok( !$node_standby_2->log_contains( |
| 92 |
"FATAL: .* terminating walreceiver process due to administrator command" |
| 93 |
), |
| 94 |
'WAL receiver should not be stopped across timeline jumps'); |
| 95 |
|
| 96 |
# Verify that the walreceiver process stayed alive across the timeline |
| 97 |
# switch, check its PID. |
| 98 |
my $wr_pid_after_switch = $node_standby_2->safe_psql('postgres', |
| 99 |
"SELECT pid FROM pg_stat_wal_receiver"); |
| 100 |
|
| 101 |
is($wr_pid_before_switch, $wr_pid_after_switch, |
| 102 |
'WAL receiver PID matches across timeline jumps'); |
| 103 |
|
| 104 |
my $raw_conninfo_count = $node_standby_2->safe_psql('postgres', |
| 105 |
"SELECT count(*) FROM pg_stat_wal_receiver WHERE conninfo LIKE '%$secret%'" |
| 106 |
); |
| 107 |
|
| 108 |
is($raw_conninfo_count, '0', |
| 109 |
'pg_stat_wal_receiver.conninfo not updated across timeline jumps'); |
| 110 |
|
| 111 |
# Ensure that a standby is able to follow a primary on a newer timeline |
| 112 |
# when WAL archiving is enabled. |
| 113 |
|
| 114 |
# Initialize primary node |
| 115 |
my $node_primary_2 = PostgreSQL::Test::Cluster->new('primary_2'); |
| 116 |
$node_primary_2->init(allows_streaming => 1, has_archiving => 1); |
| 117 |
$node_primary_2->append_conf( |
| 118 |
'postgresql.conf', qq( |
| 119 |
wal_keep_size = 512MB |
| 120 |
)); |
| 121 |
$node_primary_2->start; |
| 122 |
|
| 123 |
# Take backup |
| 124 |
$node_primary_2->backup($backup_name); |
| 125 |
|
| 126 |
# Create standby node |
| 127 |
my $node_standby_3 = PostgreSQL::Test::Cluster->new('standby_3'); |
| 128 |
$node_standby_3->init_from_backup($node_primary_2, $backup_name, |
| 129 |
has_streaming => 1); |
| 130 |
|
| 131 |
# Restart primary node in standby mode and promote it, switching it |
| 132 |
# to a new timeline. |
| 133 |
$node_primary_2->set_standby_mode; |
| 134 |
$node_primary_2->restart; |
| 135 |
$node_primary_2->promote; |
| 136 |
|
| 137 |
# Start standby node, create some content on primary and check its presence |
| 138 |
# in standby, to ensure that the timeline switch has been done. |
| 139 |
$node_standby_3->start; |
| 140 |
$node_primary_2->safe_psql('postgres', |
| 141 |
"CREATE TABLE tab_int AS SELECT 1 AS a"); |
| 142 |
$node_primary_2->wait_for_catchup($node_standby_3); |
| 143 |
|
| 144 |
my $result_2 = |
| 145 |
$node_standby_3->safe_psql('postgres', "SELECT count(*) FROM tab_int"); |
| 146 |
is($result_2, qq(1), 'check content of standby 3'); |
| 147 |
|
| 148 |
done_testing(); |