Debian Codegraph @

src/test/recovery/t/016_min_consistency.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Test for checking consistency of on-disk pages for a cluster with
5 # the minimum recovery LSN, ensuring that the updates happen across
6 # all processes. In this test, the updates from the startup process
7 # and the checkpointer (which triggers non-startup code paths) are
8 # both checked.
9
10 use strict;
11 use warnings FATAL => 'all';
12 use PostgreSQL::Test::Cluster;
13 use PostgreSQL::Test::Utils;
14 use Test::More;
15
16 # Find the largest LSN in the set of pages part of the given relation
17 # file. This is used for offline checks of page consistency. The LSN
18 # is historically stored as a set of two numbers of 4 byte-length
19 # located at the beginning of each page.
20 sub find_largest_lsn
21 {
22 my $blocksize = int(shift);
23 my $filename = shift;
24 my ($max_hi, $max_lo) = (0, 0);
25 open(my $fh, "<:raw", $filename)
26 or die "failed to open $filename: $!";
27 my ($buf, $len);
28 while ($len = read($fh, $buf, $blocksize))
29 {
30 $len == $blocksize
31 or die "read only $len of $blocksize bytes from $filename";
32 my ($hi, $lo) = unpack("LL", $buf);
33
34 if ($hi > $max_hi or ($hi == $max_hi and $lo > $max_lo))
35 {
36 ($max_hi, $max_lo) = ($hi, $lo);
37 }
38 }
39 defined($len) or die "read error on $filename: $!";
40 close($fh);
41
42 return sprintf("%X/%X", $max_hi, $max_lo);
43 }
44
45 # Initialize primary node
46 my $primary = PostgreSQL::Test::Cluster->new('primary');
47 $primary->init(allows_streaming => 1);
48
49 # Set shared_buffers to a very low value to enforce discard and flush
50 # of PostgreSQL buffers on standby, enforcing other processes than the
51 # startup process to update the minimum recovery LSN in the control
52 # file. Autovacuum is disabled so as there is no risk of having other
53 # processes than the checkpointer doing page flushes.
54 $primary->append_conf("postgresql.conf", <<EOF);
55 shared_buffers = 128kB
56 autovacuum = off
57 EOF
58
59 # Start the primary
60 $primary->start;
61
62 # setup/start a standby
63 $primary->backup('bkp');
64 my $standby = PostgreSQL::Test::Cluster->new('standby');
65 $standby->init_from_backup($primary, 'bkp', has_streaming => 1);
66 $standby->start;
67
68 # Create base table whose data consistency is checked.
69 $primary->safe_psql(
70 'postgres', "
71 CREATE TABLE test1 (a int) WITH (fillfactor = 10);
72 INSERT INTO test1 SELECT generate_series(1, 10000);");
73
74 # Take a checkpoint and enforce post-checkpoint full page writes
75 # which makes the startup process replay those pages, updating
76 # minRecoveryPoint.
77 $primary->safe_psql('postgres', 'CHECKPOINT;');
78 $primary->safe_psql('postgres', 'UPDATE test1 SET a = a + 1;');
79
80 # Wait for last record to have been replayed on the standby.
81 $primary->wait_for_catchup($standby);
82
83 # Fill in the standby's shared buffers with the data filled in
84 # previously.
85 $standby->safe_psql('postgres', 'SELECT count(*) FROM test1;');
86
87 # Update the table again, this does not generate full page writes so
88 # the standby will replay records associated with it, but the startup
89 # process will not flush those pages.
90 $primary->safe_psql('postgres', 'UPDATE test1 SET a = a + 1;');
91
92 # Extract from the relation the last block created and its relation
93 # file, this will be used at the end of the test for sanity checks.
94 my $blocksize = $primary->safe_psql('postgres',
95 "SELECT setting::int FROM pg_settings WHERE name = 'block_size';");
96 my $last_block = $primary->safe_psql('postgres',
97 "SELECT pg_relation_size('test1')::int / $blocksize - 1;");
98 my $relfilenode = $primary->safe_psql('postgres',
99 "SELECT pg_relation_filepath('test1'::regclass);");
100
101 # Wait for last record to have been replayed on the standby.
102 $primary->wait_for_catchup($standby);
103
104 # Issue a restart point on the standby now, which makes the checkpointer
105 # update minRecoveryPoint.
106 $standby->safe_psql('postgres', 'CHECKPOINT;');
107
108 # Now shut down the primary violently so as the standby does not
109 # receive the shutdown checkpoint, making sure that the startup
110 # process does not flush any pages on its side. The standby is
111 # cleanly stopped, which makes the checkpointer update minRecoveryPoint
112 # with the restart point created at shutdown.
113 $primary->stop('immediate');
114 $standby->stop('fast');
115
116 # Check the data consistency of the instance while offline. This is
117 # done by directly scanning the on-disk relation blocks and what
118 # pg_controldata lets know.
119 my $standby_data = $standby->data_dir;
120 my $offline_max_lsn =
121 find_largest_lsn($blocksize, "$standby_data/$relfilenode");
122
123 # Fetch minRecoveryPoint from the control file itself
124 my ($stdout, $stderr) = run_command([ 'pg_controldata', $standby_data ]);
125 my @control_data = split("\n", $stdout);
126 my $offline_recovery_lsn = undef;
127 foreach (@control_data)
128 {
129 if ($_ =~ /^Minimum recovery ending location:\s*(.*)$/mg)
130 {
131 $offline_recovery_lsn = $1;
132 last;
133 }
134 }
135 die "No minRecoveryPoint in control file found\n"
136 unless defined($offline_recovery_lsn);
137
138 # minRecoveryPoint should never be older than the maximum LSN for all
139 # the pages on disk.
140 ok($offline_recovery_lsn ge $offline_max_lsn,
141 "Check offline that table data is consistent with minRecoveryPoint");
142
143 done_testing();