Skip to content

Commit 6df714b

Browse files
committed
PG-1867 Improve archiving test and fix race condition
There was a race condition in the WAL archiving tests where if the end-of-recovery checkpoint had completed the tests for the WAL contents were non-sensical and racy. Solve this by explicilty promoting the server first after we have looked at the WAL contents but still making sure to wait until all WAL has been replayed. Additionally improve the tests by actually making sure the replica starts in a good state where all WAL is encrypted and testing both the plaintext and the encrypted scenarios.
1 parent 80e0bb0 commit 6df714b

File tree

1 file changed

+64
-13
lines changed

1 file changed

+64
-13
lines changed

contrib/pg_tde/t/wal_archiving.pl

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,25 @@
3737
"SELECT pg_tde_set_server_key_using_global_key_provider('server-key', 'keyring');"
3838
);
3939

40+
# This is a quite ugly dance to make sure we have a replica starting in a stats
41+
# with encrypted WAL and without. We do this by taking a base backup while
42+
# encryption is disabled and one where it is enabled.
43+
#
44+
# We also generate some plaintext WAL and some ecrnypted WAL.
45+
46+
$primary->backup('plain_wal', backup_options => [ '-X', 'none' ]);
47+
4048
$primary->append_conf('postgresql.conf', "pg_tde.wal_encrypt = on");
4149

42-
$primary->backup('backup', backup_options => [ '-X', 'none' ]);
50+
$primary->restart;
51+
52+
$primary->backup('enc_wal', backup_options => [ '-X', 'none' ]);
53+
54+
$primary->append_conf('postgresql.conf', "pg_tde.wal_encrypt = off");
55+
56+
$primary->restart;
57+
58+
$primary->append_conf('postgresql.conf', "pg_tde.wal_encrypt = on");
4359

4460
$primary->safe_psql('postgres',
4561
"CREATE TABLE t1 AS SELECT 'foobar_plain' AS x");
@@ -71,33 +87,68 @@
7187
qr/foobar_enc/,
7288
'should find foobar_enc in archive');
7389

74-
# Test restore_command
90+
# Test restore_command with encrypted WAL
7591

76-
my $replica = PostgreSQL::Test::Cluster->new('replica');
77-
$replica->init_from_backup($primary, 'backup');
78-
$replica->append_conf('postgresql.conf',
92+
my $replica_enc = PostgreSQL::Test::Cluster->new('replica_enc');
93+
$replica_enc->init_from_backup($primary, 'enc_wal');
94+
$replica_enc->append_conf('postgresql.conf',
7995
"restore_command = 'pg_tde_restore_encrypt %f %p \"cp $archive_dir/%%f %%p\"'"
8096
);
81-
$replica->append_conf('postgresql.conf', "recovery_target_action = promote");
82-
$replica->set_recovery_mode;
83-
$replica->start;
97+
$replica_enc->set_standby_mode;
98+
$replica_enc->start;
8499

85-
$data_dir = $replica->data_dir;
100+
$replica_enc->wait_for_log("waiting for WAL to become available");
86101

87-
like(
102+
$data_dir = $replica_enc->data_dir;
103+
104+
unlike(
88105
`strings $data_dir/pg_wal/0000000100000000000000??`,
89106
qr/foobar_plain/,
90-
'should find foobar_plain in WAL since we use the same key file');
107+
'should not find foobar_plain in WAL since it is encrypted');
91108
unlike(
92109
`strings $data_dir/pg_wal/0000000100000000000000??`,
93110
qr/foobar_enc/,
94111
'should not find foobar_enc in WAL since it is encrypted');
95112

96-
my $result = $replica->safe_psql('postgres',
113+
$replica_enc->promote;
114+
115+
my $result = $replica_enc->safe_psql('postgres',
116+
'SELECT * FROM t1 UNION ALL SELECT * FROM t2');
117+
118+
is($result, "foobar_plain\nfoobar_enc", 'b');
119+
120+
$replica_enc->stop;
121+
122+
# Test restore_command with plain WAL
123+
124+
my $replica_plain = PostgreSQL::Test::Cluster->new('replica_plain');
125+
$replica_plain->init_from_backup($primary, 'plain_wal');
126+
$replica_plain->append_conf('postgresql.conf',
127+
"restore_command = 'pg_tde_restore_encrypt %f %p \"cp $archive_dir/%%f %%p\"'"
128+
);
129+
$replica_plain->set_standby_mode;
130+
$replica_plain->start;
131+
132+
$replica_plain->wait_for_log("waiting for WAL to become available");
133+
134+
$data_dir = $replica_plain->data_dir;
135+
136+
like(
137+
`strings $data_dir/pg_wal/0000000100000000000000??`,
138+
qr/foobar_plain/,
139+
'should find foobar_plain in WAL since it is not encrypted');
140+
like(
141+
`strings $data_dir/pg_wal/0000000100000000000000??`,
142+
qr/foobar_enc/,
143+
'should find foobar_enc in WAL since it is not encrypted');
144+
145+
$replica_plain->promote;
146+
147+
$result = $replica_plain->safe_psql('postgres',
97148
'SELECT * FROM t1 UNION ALL SELECT * FROM t2');
98149

99150
is($result, "foobar_plain\nfoobar_enc", 'b');
100151

101-
$replica->stop;
152+
$replica_plain->stop;
102153

103154
done_testing();

0 commit comments

Comments
 (0)