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