Debian Codegraph @

src/test/recovery/t/043_no_contrecord_switch.pl

raw
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;
7
8 use File::Copy;
9 use PostgreSQL::Test::Cluster;
10 use Test::More;
11 use Fcntl qw(SEEK_SET);
12
13 use integer; # causes / operator to use integer math
14
15 # Values queried from the server
16 my $WAL_SEGMENT_SIZE;
17 my $WAL_BLOCK_SIZE;
18 my $TLI;
19
20 # Build name of a WAL segment, used when filtering the contents of the server
21 # logs.
22 sub wal_segment_name
23 {
24 my $tli = shift;
25 my $segment = shift;
26 return sprintf("%08X%08X%08X", $tli, 0, $segment);
27 }
28
29 # Calculate from a LSN (in bytes) its segment number and its offset, used
30 # when filtering the contents of the server logs.
31 sub lsn_to_segment_and_offset
32 {
33 my $lsn = shift;
34 return ($lsn / $WAL_SEGMENT_SIZE, $lsn % $WAL_SEGMENT_SIZE);
35 }
36
37 # Get GUC value, converted to an int.
38 sub get_int_setting
39 {
40 my $node = shift;
41 my $name = shift;
42 return int(
43 $node->safe_psql(
44 'postgres',
45 "SELECT setting FROM pg_settings WHERE name = '$name'"));
46 }
47
48 # Find the start of a WAL page, based on an LSN in bytes.
49 sub start_of_page
50 {
51 my $lsn = shift;
52 return $lsn & ~($WAL_BLOCK_SIZE - 1);
53 }
54
55 my $primary = PostgreSQL::Test::Cluster->new('primary');
56 $primary->init(allows_streaming => 1, has_archiving => 1);
57
58 # The configuration is chosen here to minimize the friction with
59 # concurrent WAL activity. checkpoint_timeout avoids noise with
60 # checkpoint activity, and autovacuum is disabled to avoid any
61 # WAL activity generated by it.
62 $primary->append_conf(
63 'postgresql.conf', qq(
64 autovacuum = off
65 checkpoint_timeout = '30min'
66 wal_keep_size = 1GB
67 ));
68
69 $primary->start;
70 $primary->backup('backup');
71
72 $primary->safe_psql('postgres', "CREATE TABLE t AS SELECT 0");
73
74 $WAL_SEGMENT_SIZE = get_int_setting($primary, 'wal_segment_size');
75 $WAL_BLOCK_SIZE = get_int_setting($primary, 'wal_block_size');
76 $TLI = $primary->safe_psql('postgres',
77 "SELECT timeline_id FROM pg_control_checkpoint()");
78
79 # Get close to the end of the current WAL page, enough to fit the
80 # beginning of a record that spans on two pages, generating a
81 # continuation record.
82 $primary->emit_wal(0);
83 my $end_lsn =
84 $primary->advance_wal_out_of_record_splitting_zone($WAL_BLOCK_SIZE);
85
86 # Do some math to find the record size that will overflow the page, and
87 # write it.
88 my $overflow_size = $WAL_BLOCK_SIZE - ($end_lsn % $WAL_BLOCK_SIZE);
89 $end_lsn = $primary->emit_wal($overflow_size);
90 $primary->stop('immediate');
91
92 # Find the beginning of the page with the continuation record and fill
93 # the entire page with zero bytes to simulate broken replication.
94 my $start_page = start_of_page($end_lsn);
95 my $wal_file = $primary->write_wal($TLI, $start_page, $WAL_SEGMENT_SIZE,
96 "\x00" x $WAL_BLOCK_SIZE);
97
98 # Copy the file we just "hacked" to the archives.
99 copy($wal_file, $primary->archive_dir);
100
101 # Start standby nodes and make sure they replay the file "hacked" from
102 # the archives of the primary.
103 my $standby1 = PostgreSQL::Test::Cluster->new('standby1');
104 $standby1->init_from_backup(
105 $primary, 'backup',
106 standby => 1,
107 has_restoring => 1);
108
109 my $standby2 = PostgreSQL::Test::Cluster->new('standby2');
110 $standby2->init_from_backup(
111 $primary, 'backup',
112 standby => 1,
113 has_restoring => 1);
114
115 my $log_size1 = -s $standby1->logfile;
116 my $log_size2 = -s $standby2->logfile;
117
118 $standby1->start;
119 $standby2->start;
120
121 my ($segment, $offset) = lsn_to_segment_and_offset($start_page);
122 my $segment_name = wal_segment_name($TLI, $segment);
123 my $pattern =
124 qq(invalid magic number 0000 .* segment $segment_name.* offset $offset);
125
126 # We expect both standby nodes to complain about an empty page when trying to
127 # assemble the record that spans over two pages, so wait for such reports in
128 # their logs.
129 $standby1->wait_for_log($pattern, $log_size1);
130 $standby2->wait_for_log($pattern, $log_size2);
131
132 # Now check the case of a promotion with a timeline jump handled at
133 # page boundary with a continuation record.
134 $standby1->promote;
135
136 # This command forces standby2 to read a continuation record from the page
137 # that is filled with zero bytes.
138 $standby1->safe_psql('postgres', 'SELECT pg_switch_wal()');
139
140 # Make sure WAL moves forward.
141 $standby1->safe_psql('postgres',
142 'INSERT INTO t SELECT * FROM generate_series(1, 1000)');
143
144 # Configure standby2 to stream from just promoted standby1 (it also pulls WAL
145 # files from the archive). It should be able to catch up.
146 $standby2->enable_streaming($standby1);
147 $standby2->reload;
148 $standby1->wait_for_replay_catchup($standby2);
149
150 my $result = $standby2->safe_psql('postgres', "SELECT count(*) FROM t");
151 print "standby2: $result\n";
152 is($result, qq(1001), 'check streamed content on standby2');
153
154 done_testing();