Debian Codegraph @

contrib/amcheck/t/001_verify_heapam.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 use strict;
5 use warnings FATAL => 'all';
6
7 use PostgreSQL::Test::Cluster;
8 use PostgreSQL::Test::Utils;
9
10 use Test::More;
11
12 my $node;
13
14 #
15 # Test set-up
16 #
17 $node = PostgreSQL::Test::Cluster->new('test');
18 $node->init(no_data_checksums => 1);
19 $node->append_conf('postgresql.conf', 'autovacuum=off');
20 $node->start;
21 $node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
22
23 #
24 # Check a table with data loaded but no corruption, freezing, etc.
25 #
26 fresh_test_table('test');
27 check_all_options_uncorrupted('test', 'plain');
28
29 #
30 # Check a corrupt table
31 #
32 fresh_test_table('test');
33 corrupt_first_page('test');
34 detects_heap_corruption("verify_heapam('test')", "plain corrupted table");
35 detects_heap_corruption(
36 "verify_heapam('test', skip := 'all-visible')",
37 "plain corrupted table skipping all-visible");
38 detects_heap_corruption(
39 "verify_heapam('test', skip := 'all-frozen')",
40 "plain corrupted table skipping all-frozen");
41 detects_heap_corruption(
42 "verify_heapam('test', check_toast := false)",
43 "plain corrupted table skipping toast");
44 detects_heap_corruption(
45 "verify_heapam('test', startblock := 0, endblock := 0)",
46 "plain corrupted table checking only block zero");
47
48 #
49 # Check a corrupt table with all-frozen data
50 #
51 fresh_test_table('test');
52 $node->safe_psql('postgres', q(VACUUM (FREEZE, DISABLE_PAGE_SKIPPING) test));
53 detects_no_corruption("verify_heapam('test')",
54 "all-frozen not corrupted table");
55 corrupt_first_page('test');
56 detects_heap_corruption("verify_heapam('test')",
57 "all-frozen corrupted table");
58 detects_no_corruption(
59 "verify_heapam('test', skip := 'all-frozen')",
60 "all-frozen corrupted table skipping all-frozen");
61
62 #
63 # Check a sequence with no corruption. The current implementation of sequences
64 # doesn't require its own test setup, since sequences are really just heap
65 # tables under-the-hood. To guard against future implementation changes made
66 # without remembering to update verify_heapam, we create and exercise a
67 # sequence, checking along the way that it passes corruption checks.
68 #
69 fresh_test_sequence('test_seq');
70 check_all_options_uncorrupted('test_seq', 'plain');
71 advance_test_sequence('test_seq');
72 check_all_options_uncorrupted('test_seq', 'plain');
73 set_test_sequence('test_seq');
74 check_all_options_uncorrupted('test_seq', 'plain');
75 reset_test_sequence('test_seq');
76 check_all_options_uncorrupted('test_seq', 'plain');
77
78 # Returns the filesystem path for the named relation.
79 sub relation_filepath
80 {
81 my ($relname) = @_;
82
83 my $pgdata = $node->data_dir;
84 my $rel = $node->safe_psql('postgres',
85 qq(SELECT pg_relation_filepath('$relname')));
86 die "path not found for relation $relname" unless defined $rel;
87 return "$pgdata/$rel";
88 }
89
90 # (Re)create and populate a test table of the given name.
91 sub fresh_test_table
92 {
93 my ($relname) = @_;
94
95 return $node->safe_psql(
96 'postgres', qq(
97 DROP TABLE IF EXISTS $relname CASCADE;
98 CREATE TABLE $relname (a integer, b text);
99 ALTER TABLE $relname SET (autovacuum_enabled=false);
100 ALTER TABLE $relname ALTER b SET STORAGE external;
101 INSERT INTO $relname (a, b)
102 (SELECT gs, repeat('b',gs*10) FROM generate_series(1,1000) gs);
103 BEGIN;
104 SAVEPOINT s1;
105 SELECT 1 FROM $relname WHERE a = 42 FOR UPDATE;
106 UPDATE $relname SET b = b WHERE a = 42;
107 RELEASE s1;
108 SAVEPOINT s1;
109 SELECT 1 FROM $relname WHERE a = 42 FOR UPDATE;
110 UPDATE $relname SET b = b WHERE a = 42;
111 COMMIT;
112 ));
113 }
114
115 # Create a test sequence of the given name.
116 sub fresh_test_sequence
117 {
118 my ($seqname) = @_;
119
120 return $node->safe_psql(
121 'postgres', qq(
122 DROP SEQUENCE IF EXISTS $seqname CASCADE;
123 CREATE SEQUENCE $seqname
124 INCREMENT BY 13
125 MINVALUE 17
126 START WITH 23;
127 SELECT nextval('$seqname');
128 SELECT setval('$seqname', currval('$seqname') + nextval('$seqname'));
129 ));
130 }
131
132 # Call SQL functions to increment the sequence
133 sub advance_test_sequence
134 {
135 my ($seqname) = @_;
136
137 return $node->safe_psql(
138 'postgres', qq(
139 SELECT nextval('$seqname');
140 ));
141 }
142
143 # Call SQL functions to set the sequence
144 sub set_test_sequence
145 {
146 my ($seqname) = @_;
147
148 return $node->safe_psql(
149 'postgres', qq(
150 SELECT setval('$seqname', 102);
151 ));
152 }
153
154 # Call SQL functions to reset the sequence
155 sub reset_test_sequence
156 {
157 my ($seqname) = @_;
158
159 return $node->safe_psql(
160 'postgres', qq(
161 ALTER SEQUENCE $seqname RESTART WITH 51
162 ));
163 }
164
165 # Stops the test node, corrupts the first page of the named relation, and
166 # restarts the node.
167 sub corrupt_first_page
168 {
169 my ($relname) = @_;
170 my $relpath = relation_filepath($relname);
171
172 $node->stop;
173
174 my $fh;
175 open($fh, '+<', $relpath)
176 or BAIL_OUT("open failed: $!");
177 binmode $fh;
178
179 # Corrupt some line pointers. The values are chosen to hit the
180 # various line-pointer-corruption checks in verify_heapam.c
181 # on both little-endian and big-endian architectures.
182 sysseek($fh, 32, 0)
183 or BAIL_OUT("sysseek failed: $!");
184 syswrite(
185 $fh,
186 pack("L*",
187 0xAAA15550, 0xAAA0D550, 0x00010000,
188 0x00008000, 0x0000800F, 0x001e8000)
189 ) or BAIL_OUT("syswrite failed: $!");
190 close($fh)
191 or BAIL_OUT("close failed: $!");
192
193 $node->start;
194 }
195
196 sub detects_heap_corruption
197 {
198 local $Test::Builder::Level = $Test::Builder::Level + 1;
199
200 my ($function, $testname) = @_;
201
202 detects_corruption(
203 $function,
204 $testname,
205 qr/line pointer redirection to item at offset \d+ precedes minimum offset \d+/,
206 qr/line pointer redirection to item at offset \d+ exceeds maximum offset \d+/,
207 qr/line pointer to page offset \d+ is not maximally aligned/,
208 qr/line pointer length \d+ is less than the minimum tuple header size \d+/,
209 qr/line pointer to page offset \d+ with length \d+ ends beyond maximum page offset \d+/,
210 );
211 }
212
213 sub detects_corruption
214 {
215 local $Test::Builder::Level = $Test::Builder::Level + 1;
216
217 my ($function, $testname, @re) = @_;
218
219 my $result = $node->safe_psql('postgres', qq(SELECT * FROM $function));
220 like($result, $_, $testname) for (@re);
221 }
222
223 sub detects_no_corruption
224 {
225 local $Test::Builder::Level = $Test::Builder::Level + 1;
226
227 my ($function, $testname) = @_;
228
229 my $result = $node->safe_psql('postgres', qq(SELECT * FROM $function));
230 is($result, '', $testname);
231 }
232
233 # Check various options are stable (don't abort) and do not report corruption
234 # when running verify_heapam on an uncorrupted test table.
235 #
236 # The relname *must* be an uncorrupted table, or this will fail.
237 #
238 # The prefix is used to identify the test, along with the options,
239 # and should be unique.
240 sub check_all_options_uncorrupted
241 {
242 local $Test::Builder::Level = $Test::Builder::Level + 1;
243
244 my ($relname, $prefix) = @_;
245
246 for my $stop (qw(true false))
247 {
248 for my $check_toast (qw(true false))
249 {
250 for my $skip ("'none'", "'all-frozen'", "'all-visible'")
251 {
252 for my $startblock (qw(NULL 0))
253 {
254 for my $endblock (qw(NULL 0))
255 {
256 my $opts =
257 "on_error_stop := $stop, "
258 . "check_toast := $check_toast, "
259 . "skip := $skip, "
260 . "startblock := $startblock, "
261 . "endblock := $endblock";
262
263 detects_no_corruption(
264 "verify_heapam('$relname', $opts)",
265 "$prefix: $opts");
266 }
267 }
268 }
269 }
270 }
271 }
272
273 done_testing();