| 1 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
# Tests for already-propagated WAL segments ending in incomplete WAL records. |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
|
| 8 |
use FindBin; |
| 9 |
use PostgreSQL::Test::Cluster; |
| 10 |
use PostgreSQL::Test::Utils; |
| 11 |
use Test::More; |
| 12 |
|
| 13 |
# Test: Create a physical replica that's missing the last WAL file, |
| 14 |
# then restart the primary to create a divergent WAL file and observe |
| 15 |
# that the replica replays the "overwrite contrecord" from that new |
| 16 |
# file and the standby promotes successfully. |
| 17 |
|
| 18 |
my $node = PostgreSQL::Test::Cluster->new('primary'); |
| 19 |
$node->init(allows_streaming => 1); |
| 20 |
# We need these settings for stability of WAL behavior. |
| 21 |
$node->append_conf( |
| 22 |
'postgresql.conf', qq( |
| 23 |
autovacuum = off |
| 24 |
wal_keep_size = 1GB |
| 25 |
)); |
| 26 |
$node->start; |
| 27 |
|
| 28 |
$node->safe_psql('postgres', 'create table filler (a int, b text)'); |
| 29 |
|
| 30 |
# Now consume all remaining room in the current WAL segment, leaving |
| 31 |
# space enough only for the start of a largish record. |
| 32 |
$node->safe_psql( |
| 33 |
'postgres', q{ |
| 34 |
DO $$ |
| 35 |
DECLARE |
| 36 |
wal_segsize int := setting::int FROM pg_settings WHERE name = 'wal_segment_size'; |
| 37 |
remain int; |
| 38 |
iters int := 0; |
| 39 |
BEGIN |
| 40 |
LOOP |
| 41 |
INSERT into filler |
| 42 |
select g, repeat(encode(sha256(g::text::bytea), 'hex'), (random() * 15 + 1)::int) |
| 43 |
from generate_series(1, 10) g; |
| 44 |
|
| 45 |
remain := wal_segsize - (pg_current_wal_insert_lsn() - '0/0') % wal_segsize; |
| 46 |
IF remain < 2 * setting::int from pg_settings where name = 'block_size' THEN |
| 47 |
RAISE log 'exiting after % iterations, % bytes to end of WAL segment', iters, remain; |
| 48 |
EXIT; |
| 49 |
END IF; |
| 50 |
iters := iters + 1; |
| 51 |
END LOOP; |
| 52 |
END |
| 53 |
$$; |
| 54 |
}); |
| 55 |
|
| 56 |
my $initfile = $node->safe_psql('postgres', |
| 57 |
'SELECT pg_walfile_name(pg_current_wal_insert_lsn())'); |
| 58 |
$node->safe_psql('postgres', |
| 59 |
qq{SELECT pg_logical_emit_message(true, 'test 026', repeat('xyzxz', 123456))} |
| 60 |
); |
| 61 |
#$node->safe_psql('postgres', qq{create table foo ()}); |
| 62 |
my $endfile = $node->safe_psql('postgres', |
| 63 |
'SELECT pg_walfile_name(pg_current_wal_insert_lsn())'); |
| 64 |
ok($initfile ne $endfile, "$initfile differs from $endfile"); |
| 65 |
|
| 66 |
# Now stop abruptly, to avoid a stop checkpoint. We can remove the tail file |
| 67 |
# afterwards, and on startup the large message should be overwritten with new |
| 68 |
# contents |
| 69 |
$node->stop('immediate'); |
| 70 |
|
| 71 |
unlink $node->basedir . "/pgdata/pg_wal/$endfile" |
| 72 |
or die "could not unlink " . $node->basedir . "/pgdata/pg_wal/$endfile: $!"; |
| 73 |
|
| 74 |
# OK, create a standby at this spot. |
| 75 |
$node->backup_fs_cold('backup'); |
| 76 |
my $node_standby = PostgreSQL::Test::Cluster->new('standby'); |
| 77 |
$node_standby->init_from_backup($node, 'backup', has_streaming => 1); |
| 78 |
|
| 79 |
$node_standby->start; |
| 80 |
$node->start; |
| 81 |
|
| 82 |
$node->safe_psql('postgres', |
| 83 |
qq{create table foo (a text); insert into foo values ('hello')}); |
| 84 |
$node->safe_psql('postgres', |
| 85 |
qq{SELECT pg_logical_emit_message(true, 'test 026', 'AABBCC')}); |
| 86 |
|
| 87 |
my $until_lsn = $node->safe_psql('postgres', "SELECT pg_current_wal_lsn()"); |
| 88 |
my $caughtup_query = |
| 89 |
"SELECT '$until_lsn'::pg_lsn <= pg_last_wal_replay_lsn()"; |
| 90 |
$node_standby->poll_query_until('postgres', $caughtup_query) |
| 91 |
or die "Timed out while waiting for standby to catch up"; |
| 92 |
|
| 93 |
ok($node_standby->safe_psql('postgres', 'select * from foo') eq 'hello', |
| 94 |
'standby replays past overwritten contrecord'); |
| 95 |
|
| 96 |
# Verify message appears in standby's log |
| 97 |
my $log = slurp_file($node_standby->logfile); |
| 98 |
like( |
| 99 |
$log, |
| 100 |
qr[successfully skipped missing contrecord at], |
| 101 |
"found log line in standby"); |
| 102 |
|
| 103 |
# Verify promotion is successful |
| 104 |
$node_standby->promote; |
| 105 |
|
| 106 |
$node->stop; |
| 107 |
$node_standby->stop; |
| 108 |
|
| 109 |
done_testing(); |