Skip to content

Commit 54d131a

Browse files
committed
TAP t
1 parent 4e4e38e commit 54d131a

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

contrib/pg_tde/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ tap_tests = [
120120
't/pg_waldump_fullpage.pl',
121121
't/replication.pl',
122122
't/rotate_key.pl',
123+
't/standby_source.pl',
123124
't/tde_heap.pl',
124125
't/unlogged_tables.pl',
125126
't/wal_encrypt.pl',

contrib/pg_tde/t/standby_source.pl

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
2+
# Copyright (c) 2021-2024, PostgreSQL Global Development Group
3+
4+
#
5+
# Test using a standby server as the source.
6+
#
7+
# This sets up three nodes: A, B and C. First, A is the primary,
8+
# B follows A, and C follows B:
9+
#
10+
# A (primary) <--- B (standby) <--- C (standby)
11+
#
12+
#
13+
# Then we promote C, and insert some divergent rows in A and C:
14+
#
15+
# A (primary) <--- B (standby) C (primary)
16+
#
17+
#
18+
# Finally, we run pg_rewind on C, to re-point it at B again:
19+
#
20+
# A (primary) <--- B (standby) <--- C (standby)
21+
#
22+
#
23+
# The test is similar to the basic tests, but since we're dealing with
24+
# three nodes, not two, we cannot use most of the RewindTest functions
25+
# as is.
26+
27+
use strict;
28+
use warnings FATAL => 'all';
29+
use PostgreSQL::Test::Utils;
30+
use Test::More;
31+
32+
use FindBin;
33+
use lib $FindBin::RealBin;
34+
use File::Copy;
35+
use PostgreSQL::Test::Cluster;
36+
use RewindTest;
37+
38+
my $tmp_folder = PostgreSQL::Test::Utils::tempdir;
39+
40+
my $node_a;
41+
my $node_b;
42+
my $node_c;
43+
44+
# Set up node A, as primary
45+
#
46+
# A (primary)
47+
48+
setup_cluster('a');
49+
start_primary();
50+
$node_a = $node_primary;
51+
52+
# Create a test table and insert a row in primary.
53+
$node_a->safe_psql('postgres', "CREATE TABLE tbl1 (d text)");
54+
$node_a->safe_psql('postgres', "INSERT INTO tbl1 VALUES ('in A')");
55+
primary_psql("CHECKPOINT");
56+
57+
# Set up node B and C, as cascaded standbys
58+
#
59+
# A (primary) <--- B (standby) <--- C (standby)
60+
$node_a->backup('my_backup');
61+
$node_b = PostgreSQL::Test::Cluster->new('node_b');
62+
$node_b->init_from_backup($node_a, 'my_backup', has_streaming => 1);
63+
$node_b->set_standby_mode();
64+
$node_b->start;
65+
66+
$node_b->backup('my_backup');
67+
$node_c = PostgreSQL::Test::Cluster->new('node_c');
68+
$node_c->init_from_backup($node_b, 'my_backup', has_streaming => 1);
69+
$node_c->set_standby_mode();
70+
$node_c->start;
71+
72+
# Insert additional data on A, and wait for both standbys to catch up.
73+
$node_a->safe_psql('postgres',
74+
"INSERT INTO tbl1 values ('in A, before promotion')");
75+
$node_a->safe_psql('postgres', 'CHECKPOINT');
76+
77+
my $lsn = $node_a->lsn('write');
78+
$node_a->wait_for_catchup('node_b', 'write', $lsn);
79+
$node_b->wait_for_catchup('node_c', 'write', $lsn);
80+
81+
# Promote C
82+
#
83+
# A (primary) <--- B (standby) C (primary)
84+
85+
$node_c->promote;
86+
87+
88+
# Insert a row in A. This causes A/B and C to have "diverged", so that it's
89+
# no longer possible to just apply the standby's logs over primary directory
90+
# - you need to rewind.
91+
$node_a->safe_psql('postgres',
92+
"INSERT INTO tbl1 VALUES ('in A, after C was promoted')");
93+
94+
# make sure it's replicated to B before we continue
95+
$node_a->wait_for_catchup('node_b');
96+
97+
# Also insert a new row in the standby, which won't be present in the
98+
# old primary.
99+
$node_c->safe_psql('postgres',
100+
"INSERT INTO tbl1 VALUES ('in C, after C was promoted')");
101+
102+
103+
#
104+
# All set up. We're ready to run pg_rewind.
105+
#
106+
my $node_c_pgdata = $node_c->data_dir;
107+
108+
# Stop the node and be ready to perform the rewind.
109+
$node_c->stop('fast');
110+
111+
# Keep a temporary postgresql.conf or it would be overwritten during the rewind.
112+
copy(
113+
"$node_c_pgdata/postgresql.conf",
114+
"$tmp_folder/node_c-postgresql.conf.tmp");
115+
116+
{
117+
# Temporarily unset PGAPPNAME so that the server doesn't
118+
# inherit it. Otherwise this could affect libpqwalreceiver
119+
# connections in confusing ways.
120+
local %ENV = %ENV;
121+
delete $ENV{PGAPPNAME};
122+
123+
# Do rewind using a remote connection as source, generating
124+
# recovery configuration automatically.
125+
command_ok(
126+
[
127+
'pg_rewind', "--debug",
128+
"--source-server", $node_b->connstr('postgres'),
129+
"--target-pgdata=$node_c_pgdata", "--no-sync",
130+
"--write-recovery-conf"
131+
],
132+
'pg_rewind remote');
133+
}
134+
135+
# Now move back postgresql.conf with old settings
136+
move(
137+
"$tmp_folder/node_c-postgresql.conf.tmp",
138+
"$node_c_pgdata/postgresql.conf");
139+
140+
# Restart the node.
141+
$node_c->start;
142+
143+
# set RewindTest::node_primary to point to the rewound node, so that we can
144+
# use check_query()
145+
$node_primary = $node_c;
146+
147+
# Run some checks to verify that C has been successfully rewound,
148+
# and connected back to follow B.
149+
150+
check_query(
151+
'SELECT * FROM tbl1',
152+
qq(in A
153+
in A, before promotion
154+
in A, after C was promoted
155+
),
156+
'table content after rewind');
157+
158+
# Insert another row, and observe that it's cascaded from A to B to C.
159+
$node_a->safe_psql('postgres',
160+
"INSERT INTO tbl1 values ('in A, after rewind')");
161+
162+
$node_b->wait_for_replay_catchup('node_c', $node_a);
163+
164+
check_query(
165+
'SELECT * FROM tbl1',
166+
qq(in A
167+
in A, before promotion
168+
in A, after C was promoted
169+
in A, after rewind
170+
),
171+
'table content after rewind and insert');
172+
173+
# clean up
174+
$node_a->teardown_node;
175+
$node_b->teardown_node;
176+
$node_c->teardown_node;
177+
178+
done_testing();

0 commit comments

Comments
 (0)