Debian Codegraph @

src/bin/pg_checksums/t/002_actions.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Do basic sanity checks supported by pg_checksums using
5 # an initialized cluster.
6
7 use strict;
8 use warnings FATAL => 'all';
9 use Config;
10 use PostgreSQL::Test::Cluster;
11 use PostgreSQL::Test::Utils;
12
13 use Test::More;
14
15
16 # Utility routine to create and check a table with corrupted checksums
17 # on a wanted tablespace. Note that this stops and starts the node
18 # multiple times to perform the checks, leaving the node started
19 # at the end.
20 sub check_relation_corruption
21 {
22 my $node = shift;
23 my $table = shift;
24 my $tablespace = shift;
25 my $pgdata = $node->data_dir;
26
27 # Create table and discover its filesystem location.
28 $node->safe_psql(
29 'postgres',
30 "CREATE TABLE $table AS SELECT a FROM generate_series(1,10000) AS a;
31 ALTER TABLE $table SET (autovacuum_enabled=false);");
32
33 $node->safe_psql('postgres',
34 "ALTER TABLE " . $table . " SET TABLESPACE " . $tablespace . ";");
35
36 my $file_corrupted =
37 $node->safe_psql('postgres', "SELECT pg_relation_filepath('$table');");
38 my $relfilenode_corrupted = $node->safe_psql('postgres',
39 "SELECT relfilenode FROM pg_class WHERE relname = '$table';");
40
41 $node->stop;
42
43 # Checksums are correct for single relfilenode as the table is not
44 # corrupted yet.
45 command_ok(
46 [
47 'pg_checksums',
48 '--check',
49 '--pgdata' => $pgdata,
50 '--filenode' => $relfilenode_corrupted,
51 ],
52 "succeeds for single relfilenode on tablespace $tablespace with offline cluster"
53 );
54
55 # Time to create some corruption
56 $node->corrupt_page_checksum($file_corrupted, 0);
57
58 # Checksum checks on single relfilenode fail
59 $node->command_checks_all(
60 [
61 'pg_checksums',
62 '--check',
63 '--pgdata' => $pgdata,
64 '--filenode' => $relfilenode_corrupted,
65 ],
66 1,
67 [qr/Bad checksums:.*1/],
68 [qr/checksum verification failed/],
69 "fails with corrupted data for single relfilenode on tablespace $tablespace"
70 );
71
72 # Global checksum checks fail as well
73 $node->command_checks_all(
74 [ 'pg_checksums', '--check', '--pgdata' => $pgdata ],
75 1,
76 [qr/Bad checksums:.*1/],
77 [qr/checksum verification failed/],
78 "fails with corrupted data on tablespace $tablespace");
79
80 # Drop corrupted table again and make sure there is no more corruption.
81 $node->start;
82 $node->safe_psql('postgres', "DROP TABLE $table;");
83 $node->stop;
84 $node->command_ok(
85 [ 'pg_checksums', '--check', '--pgdata' => $pgdata ],
86 "succeeds again after table drop on tablespace $tablespace");
87
88 $node->start;
89 return;
90 }
91
92 # Initialize node with checksums disabled.
93 my $node = PostgreSQL::Test::Cluster->new('node_checksum');
94 $node->init(no_data_checksums => 1);
95 my $pgdata = $node->data_dir;
96
97 # Control file should know that checksums are disabled.
98 command_like(
99 [ 'pg_controldata', $pgdata ],
100 qr/Data page checksum version:.*0/,
101 'checksums disabled in control file');
102
103 # These are correct but empty files, so they should pass through.
104 append_to_file "$pgdata/global/99999", "";
105 append_to_file "$pgdata/global/99999.123", "";
106 append_to_file "$pgdata/global/99999_fsm", "";
107 append_to_file "$pgdata/global/99999_init", "";
108 append_to_file "$pgdata/global/99999_vm", "";
109 append_to_file "$pgdata/global/99999_init.123", "";
110 append_to_file "$pgdata/global/99999_fsm.123", "";
111 append_to_file "$pgdata/global/99999_vm.123", "";
112
113 # These are temporary files and folders with dummy contents, which
114 # should be ignored by the scan.
115 append_to_file "$pgdata/global/pgsql_tmp_123", "foo";
116 mkdir "$pgdata/global/pgsql_tmp";
117 append_to_file "$pgdata/global/pgsql_tmp/1.1", "foo";
118 append_to_file "$pgdata/global/pg_internal.init", "foo";
119 append_to_file "$pgdata/global/pg_internal.init.123", "foo";
120
121 # These are non-postgres macOS files, which should be ignored by the scan.
122 # Only perform this test on non-macOS systems though as creating incorrect
123 # system files may have side effects on macOS.
124 append_to_file "$pgdata/global/.DS_Store", "foo"
125 unless ($Config{osname} eq 'darwin');
126
127 # Enable checksums.
128 command_ok([ 'pg_checksums', '--enable', '--no-sync', '--pgdata' => $pgdata ],
129 "checksums successfully enabled in cluster");
130
131 # Successive attempt to enable checksums fails.
132 command_fails(
133 [ 'pg_checksums', '--enable', '--no-sync', '--pgdata' => $pgdata ],
134 "enabling checksums fails if already enabled");
135
136 # Control file should know that checksums are enabled.
137 command_like(
138 [ 'pg_controldata', $pgdata ],
139 qr/Data page checksum version:.*1/,
140 'checksums enabled in control file');
141
142 # Disable checksums again. Flush result here as that should be cheap.
143 command_ok(
144 [ 'pg_checksums', '--disable', '--pgdata' => $pgdata ],
145 "checksums successfully disabled in cluster");
146
147 # Successive attempt to disable checksums fails.
148 command_fails(
149 [ 'pg_checksums', '--disable', '--no-sync', '--pgdata' => $pgdata ],
150 "disabling checksums fails if already disabled");
151
152 # Control file should know that checksums are disabled.
153 command_like(
154 [ 'pg_controldata', $pgdata ],
155 qr/Data page checksum version:.*0/,
156 'checksums disabled in control file');
157
158 # Enable checksums again for follow-up tests.
159 command_ok([ 'pg_checksums', '--enable', '--no-sync', '--pgdata' => $pgdata ],
160 "checksums successfully enabled in cluster");
161
162 # Control file should know that checksums are enabled.
163 command_like(
164 [ 'pg_controldata', $pgdata ],
165 qr/Data page checksum version:.*1/,
166 'checksums enabled in control file');
167
168 # Checksums pass on a newly-created cluster
169 command_ok([ 'pg_checksums', '--check', '--pgdata' => $pgdata ],
170 "succeeds with offline cluster");
171
172 # Checksums are verified if no other arguments are specified
173 command_ok(
174 [ 'pg_checksums', '--pgdata' => $pgdata ],
175 "verifies checksums as default action");
176
177 # Specific relation files cannot be requested when action is --disable
178 # or --enable.
179 command_fails(
180 [
181 'pg_checksums',
182 '--disable',
183 '--filenode' => '1234',
184 '--pgdata' => $pgdata
185 ],
186 "fails when relfilenodes are requested and action is --disable");
187 command_fails(
188 [
189 'pg_checksums',
190 '--enable',
191 '--filenode' => '1234',
192 '--pgdata' => $pgdata
193 ],
194 "fails when relfilenodes are requested and action is --enable");
195
196 # Test postgres -C for an offline cluster.
197 # Run-time GUCs are safe to query here. Note that a lock file is created,
198 # then removed, leading to an extra LOG entry showing in stderr. This uses
199 # log_min_messages=fatal to remove any noise. This test uses a startup
200 # wrapped with pg_ctl to allow the case where this runs under a privileged
201 # account on Windows.
202 command_checks_all(
203 [
204 'pg_ctl', 'start',
205 '--silent',
206 '--pgdata' => $pgdata,
207 '-o' => '-C data_checksums -c log_min_messages=fatal',
208 ],
209 1,
210 [qr/^on$/],
211 [qr/could not start server/],
212 'data_checksums=on is reported on an offline cluster');
213
214 # Checks cannot happen with an online cluster
215 $node->start;
216 command_fails([ 'pg_checksums', '--check', '--pgdata' => $pgdata ],
217 "fails with online cluster");
218
219 # Check corruption of table on default tablespace.
220 check_relation_corruption($node, 'corrupt1', 'pg_default');
221
222 # Create tablespace to check corruptions in a non-default tablespace.
223 my $basedir = $node->basedir;
224 my $tablespace_dir = "$basedir/ts_corrupt_dir";
225 mkdir($tablespace_dir);
226 $node->safe_psql('postgres',
227 "CREATE TABLESPACE ts_corrupt LOCATION '$tablespace_dir';");
228 check_relation_corruption($node, 'corrupt2', 'ts_corrupt');
229
230 # Utility routine to check that pg_checksums is able to detect
231 # correctly-named relation files filled with some corrupted data.
232 sub fail_corrupt
233 {
234 my $node = shift;
235 my $file = shift;
236 my $pgdata = $node->data_dir;
237
238 # Create the file with some dummy data in it.
239 my $file_name = "$pgdata/global/$file";
240 append_to_file $file_name, "foo";
241
242 $node->command_checks_all(
243 [ 'pg_checksums', '--check', '--pgdata' => $pgdata ],
244 1,
245 [qr/^$/],
246 [qr/could not read block 0 in file.*$file\":/],
247 "fails for corrupted data in $file");
248
249 # Remove file to prevent future lookup errors on conflicts.
250 unlink $file_name;
251 return;
252 }
253
254 # Stop instance for the follow-up checks.
255 $node->stop;
256
257 # Create a fake tablespace location that should not be scanned
258 # when verifying checksums.
259 mkdir "$tablespace_dir/PG_99_999999991/";
260 append_to_file "$tablespace_dir/PG_99_999999991/foo", "123";
261 command_ok([ 'pg_checksums', '--check', '--pgdata' => $pgdata ],
262 "succeeds with foreign tablespace");
263
264 # Authorized relation files filled with corrupted data cause the
265 # checksum checks to fail. Make sure to use file names different
266 # than the previous ones.
267 fail_corrupt($node, "99990");
268 fail_corrupt($node, "99990.123");
269 fail_corrupt($node, "99990_fsm");
270 fail_corrupt($node, "99990_init");
271 fail_corrupt($node, "99990_vm");
272 fail_corrupt($node, "99990_init.123");
273 fail_corrupt($node, "99990_fsm.123");
274 fail_corrupt($node, "99990_vm.123");
275
276 done_testing();