Debian Codegraph @

contrib/amcheck/t/006_verify_gin.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 my $blksize;
14
15 # to get the split fast, we want tuples to be as large as possible, but the same time we don't want them to be toasted.
16 my $filler_size = 1900;
17
18 #
19 # Test set-up
20 #
21 $node = PostgreSQL::Test::Cluster->new('test');
22 $node->init(no_data_checksums => 1);
23 $node->append_conf('postgresql.conf', 'autovacuum=off');
24 $node->start;
25 $blksize = int($node->safe_psql('postgres', 'SHOW block_size;'));
26 $node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
27 $node->safe_psql(
28 'postgres', q(
29 CREATE OR REPLACE FUNCTION random_string( INT ) RETURNS text AS $$
30 SELECT string_agg(substring('0123456789abcdefghijklmnopqrstuvwxyz', ceil(random() * 36)::integer, 1), '') from generate_series(1, $1);
31 $$ LANGUAGE SQL;));
32
33 # Tests
34 invalid_entry_order_leaf_page_test();
35 invalid_entry_order_inner_page_test();
36 invalid_entry_columns_order_test();
37 inconsistent_with_parent_key__parent_key_corrupted_test();
38 inconsistent_with_parent_key__child_key_corrupted_test();
39 inconsistent_with_parent_key__parent_key_corrupted_posting_tree_test();
40
41 sub invalid_entry_order_leaf_page_test
42 {
43 my $relname = "test";
44 my $indexname = "test_gin_idx";
45
46 $node->safe_psql(
47 'postgres', qq(
48 DROP TABLE IF EXISTS $relname;
49 CREATE TABLE $relname (a text[]);
50 INSERT INTO $relname (a) VALUES ('{aaaaa,bbbbb}');
51 CREATE INDEX $indexname ON $relname USING gin (a);
52 ));
53 my $relpath = relation_filepath($indexname);
54
55 $node->stop;
56
57 my $blkno = 1; # root
58
59 # produce wrong order by replacing aaaaa with ccccc
60 string_replace_block($relpath, 'aaaaa', 'ccccc', $blkno);
61
62 $node->start;
63
64 my ($result, $stdout, $stderr) =
65 $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
66 my $expected =
67 "index \"$indexname\" has wrong tuple order on entry tree page, block 1, offset 2, rightlink 4294967295";
68 like($stderr, qr/$expected/);
69 }
70
71 sub invalid_entry_order_inner_page_test
72 {
73 my $relname = "test";
74 my $indexname = "test_gin_idx";
75
76 # to break the order in the inner page we need at least 3 items (rightmost key in the inner level is not checked for the order)
77 # so fill table until we have 2 splits
78 $node->safe_psql(
79 'postgres', qq(
80 DROP TABLE IF EXISTS $relname;
81 CREATE TABLE $relname (a text[]);
82 INSERT INTO $relname (a) VALUES (('{' || 'pppppppppp' || random_string($filler_size) ||'}')::text[]);
83 INSERT INTO $relname (a) VALUES (('{' || 'qqqqqqqqqq' || random_string($filler_size) ||'}')::text[]);
84 INSERT INTO $relname (a) VALUES (('{' || 'rrrrrrrrrr' || random_string($filler_size) ||'}')::text[]);
85 INSERT INTO $relname (a) VALUES (('{' || 'ssssssssss' || random_string($filler_size) ||'}')::text[]);
86 INSERT INTO $relname (a) VALUES (('{' || 'tttttttttt' || random_string($filler_size) ||'}')::text[]);
87 INSERT INTO $relname (a) VALUES (('{' || 'uuuuuuuuuu' || random_string($filler_size) ||'}')::text[]);
88 INSERT INTO $relname (a) VALUES (('{' || 'vvvvvvvvvv' || random_string($filler_size) ||'}')::text[]);
89 INSERT INTO $relname (a) VALUES (('{' || 'wwwwwwwwww' || random_string($filler_size) ||'}')::text[]);
90 CREATE INDEX $indexname ON $relname USING gin (a);
91 ));
92 my $relpath = relation_filepath($indexname);
93
94 $node->stop;
95
96 my $blkno = 1; # root
97
98 # we have rrrrrrrrr... and tttttttttt... as keys in the root, so produce wrong order by replacing rrrrrrrrrr....
99 string_replace_block($relpath, 'rrrrrrrrrr', 'zzzzzzzzzz', $blkno);
100
101 $node->start;
102
103 my ($result, $stdout, $stderr) =
104 $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
105 my $expected =
106 "index \"$indexname\" has wrong tuple order on entry tree page, block 1, offset 2, rightlink 4294967295";
107 like($stderr, qr/$expected/);
108 }
109
110 sub invalid_entry_columns_order_test
111 {
112 my $relname = "test";
113 my $indexname = "test_gin_idx";
114
115 $node->safe_psql(
116 'postgres', qq(
117 DROP TABLE IF EXISTS $relname;
118 CREATE TABLE $relname (a text[],b text[]);
119 INSERT INTO $relname (a,b) VALUES ('{aaa}','{bbb}');
120 CREATE INDEX $indexname ON $relname USING gin (a,b);
121 ));
122 my $relpath = relation_filepath($indexname);
123
124 $node->stop;
125
126 my $blkno = 1; # root
127
128 # mess column numbers
129 # root items order before: (1,aaa), (2,bbb)
130 # root items order after: (2,aaa), (1,bbb)
131 my $attrno_1 = pack('s', 1);
132 my $attrno_2 = pack('s', 2);
133
134 my $find = qr/($attrno_1)(.)(aaa)/s;
135 my $replace = $attrno_2 . '$2$3';
136 string_replace_block($relpath, $find, $replace, $blkno);
137
138 $find = qr/($attrno_2)(.)(bbb)/s;
139 $replace = $attrno_1 . '$2$3';
140 string_replace_block($relpath, $find, $replace, $blkno);
141
142 $node->start;
143
144 my ($result, $stdout, $stderr) =
145 $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
146 my $expected =
147 "index \"$indexname\" has wrong tuple order on entry tree page, block 1, offset 2, rightlink 4294967295";
148 like($stderr, qr/$expected/);
149 }
150
151 sub inconsistent_with_parent_key__parent_key_corrupted_test
152 {
153 my $relname = "test";
154 my $indexname = "test_gin_idx";
155
156 # fill the table until we have a split
157 $node->safe_psql(
158 'postgres', qq(
159 DROP TABLE IF EXISTS $relname;
160 CREATE TABLE $relname (a text[]);
161 INSERT INTO $relname (a) VALUES (('{' || 'llllllllll' || random_string($filler_size) ||'}')::text[]);
162 INSERT INTO $relname (a) VALUES (('{' || 'mmmmmmmmmm' || random_string($filler_size) ||'}')::text[]);
163 INSERT INTO $relname (a) VALUES (('{' || 'nnnnnnnnnn' || random_string($filler_size) ||'}')::text[]);
164 INSERT INTO $relname (a) VALUES (('{' || 'xxxxxxxxxx' || random_string($filler_size) ||'}')::text[]);
165 INSERT INTO $relname (a) VALUES (('{' || 'yyyyyyyyyy' || random_string($filler_size) ||'}')::text[]);
166 CREATE INDEX $indexname ON $relname USING gin (a);
167 ));
168 my $relpath = relation_filepath($indexname);
169
170 $node->stop;
171
172 my $blkno = 1; # root
173
174 # we have nnnnnnnnnn... as parent key in the root, so replace it with something smaller then child's keys
175 string_replace_block($relpath, 'nnnnnnnnnn', 'aaaaaaaaaa', $blkno);
176
177 $node->start;
178
179 my ($result, $stdout, $stderr) =
180 $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
181 my $expected =
182 "index \"$indexname\" has inconsistent records on page 3 offset 3";
183 like($stderr, qr/$expected/);
184 }
185
186 sub inconsistent_with_parent_key__child_key_corrupted_test
187 {
188 my $relname = "test";
189 my $indexname = "test_gin_idx";
190
191 # fill the table until we have a split
192 $node->safe_psql(
193 'postgres', qq(
194 DROP TABLE IF EXISTS $relname;
195 CREATE TABLE $relname (a text[]);
196 INSERT INTO $relname (a) VALUES (('{' || 'llllllllll' || random_string($filler_size) ||'}')::text[]);
197 INSERT INTO $relname (a) VALUES (('{' || 'mmmmmmmmmm' || random_string($filler_size) ||'}')::text[]);
198 INSERT INTO $relname (a) VALUES (('{' || 'nnnnnnnnnn' || random_string($filler_size) ||'}')::text[]);
199 INSERT INTO $relname (a) VALUES (('{' || 'xxxxxxxxxx' || random_string($filler_size) ||'}')::text[]);
200 INSERT INTO $relname (a) VALUES (('{' || 'yyyyyyyyyy' || random_string($filler_size) ||'}')::text[]);
201 CREATE INDEX $indexname ON $relname USING gin (a);
202 ));
203 my $relpath = relation_filepath($indexname);
204
205 $node->stop;
206
207 my $blkno = 3; # leaf
208
209 # we have nnnnnnnnnn... as parent key in the root, so replace child key with something bigger
210 string_replace_block($relpath, 'nnnnnnnnnn', 'pppppppppp', $blkno);
211
212 $node->start;
213
214 my ($result, $stdout, $stderr) =
215 $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
216 my $expected =
217 "index \"$indexname\" has inconsistent records on page 3 offset 3";
218 like($stderr, qr/$expected/);
219 }
220
221 sub inconsistent_with_parent_key__parent_key_corrupted_posting_tree_test
222 {
223 my $relname = "test";
224 my $indexname = "test_gin_idx";
225
226 $node->safe_psql(
227 'postgres', qq(
228 DROP TABLE IF EXISTS $relname;
229 CREATE TABLE $relname (a text[]);
230 INSERT INTO $relname (a) select ('{aaaaa}') from generate_series(1,10000);
231 CREATE INDEX $indexname ON $relname USING gin (a);
232 ));
233 my $relpath = relation_filepath($indexname);
234
235 $node->stop;
236
237 my $blkno = 2; # posting tree root
238
239 # we have a posting tree for 'aaaaa' key with the root at 2nd block
240 # and two leaf pages 3 and 4. replace 4th page's high key with (1,1)
241 # so that there are tid's in leaf page that are larger then the new high key.
242 my $find = pack('S*', 0, 4, 0) . '....';
243 my $replace = pack('S*', 0, 4, 0, 1, 1);
244 string_replace_block($relpath, $find, $replace, $blkno);
245
246 $node->start;
247
248 my ($result, $stdout, $stderr) =
249 $node->psql('postgres', qq(SELECT gin_index_check('$indexname')));
250 my $expected =
251 "index \"$indexname\": tid exceeds parent's high key in postingTree leaf on block 4";
252 like($stderr, qr/$expected/);
253 }
254
255
256 # Returns the filesystem path for the named relation.
257 sub relation_filepath
258 {
259 my ($relname) = @_;
260
261 my $pgdata = $node->data_dir;
262 my $rel = $node->safe_psql('postgres',
263 qq(SELECT pg_relation_filepath('$relname')));
264 die "path not found for relation $relname" unless defined $rel;
265 return "$pgdata/$rel";
266 }
267
268 # substitute pattern 'find' with 'replace' within the block with number 'blkno' in the file 'filename'
269 sub string_replace_block
270 {
271 my ($filename, $find, $replace, $blkno) = @_;
272
273 my $fh;
274 open($fh, '+<', $filename) or BAIL_OUT("open failed: $!");
275 binmode $fh;
276
277 my $offset = $blkno * $blksize;
278 my $buffer;
279
280 sysseek($fh, $offset, 0) or BAIL_OUT("seek failed: $!");
281 sysread($fh, $buffer, $blksize) or BAIL_OUT("read failed: $!");
282
283 $buffer =~ s/$find/'"' . $replace . '"'/gee;
284
285 sysseek($fh, $offset, 0) or BAIL_OUT("seek failed: $!");
286 syswrite($fh, $buffer) or BAIL_OUT("write failed: $!");
287
288 close($fh) or BAIL_OUT("close failed: $!");
289
290 return;
291 }
292
293 done_testing();