14
14
# at the end.
15
15
sub check_relation_corruption
16
16
{
17
- my $node = shift ;
18
- my $table = shift ;
17
+ my $node = shift ;
18
+ my $table = shift ;
19
19
my $tablespace = shift ;
20
- my $pgdata = $node -> data_dir;
20
+ my $pgdata = $node -> data_dir;
21
21
22
- $node -> safe_psql(' postgres' ,
22
+ $node -> safe_psql(
23
+ ' postgres' ,
23
24
" SELECT a INTO $table FROM generate_series(1,10000) AS a;
24
25
ALTER TABLE $table SET (autovacuum_enabled=false);" );
25
26
26
27
$node -> safe_psql(' postgres' ,
27
- " ALTER TABLE " . $table . " SET TABLESPACE " . $tablespace . " ;" );
28
+ " ALTER TABLE " . $table . " SET TABLESPACE " . $tablespace . " ;" );
28
29
29
- my $file_corrupted = $node -> safe_psql( ' postgres ' ,
30
- " SELECT pg_relation_filepath('$table ');" );
31
- my $relfilenode_corrupted = $node -> safe_psql(' postgres' ,
30
+ my $file_corrupted =
31
+ $node -> safe_psql( ' postgres ' , " SELECT pg_relation_filepath('$table ');" );
32
+ my $relfilenode_corrupted = $node -> safe_psql(' postgres' ,
32
33
" SELECT relfilenode FROM pg_class WHERE relname = '$table ';" );
33
34
34
35
# Set page header and block size
@@ -38,9 +39,14 @@ sub check_relation_corruption
38
39
39
40
# Checksums are correct for single relfilenode as the table is not
40
41
# corrupted yet.
41
- command_ok([' pg_checksums' , ' --check' , ' -D' , $pgdata , ' -r' ,
42
- $relfilenode_corrupted ],
43
- " succeeds for single relfilenode on tablespace $tablespace with offline cluster" );
42
+ command_ok(
43
+ [
44
+ ' pg_checksums' , ' --check' ,
45
+ ' -D' , $pgdata ,
46
+ ' -r' , $relfilenode_corrupted
47
+ ],
48
+ " succeeds for single relfilenode on tablespace $tablespace with offline cluster"
49
+ );
44
50
45
51
# Time to create some corruption
46
52
open my $file , ' +<' , " $pgdata /$file_corrupted " ;
@@ -49,26 +55,32 @@ sub check_relation_corruption
49
55
close $file ;
50
56
51
57
# Checksum checks on single relfilenode fail
52
- $node -> command_checks_all([ ' pg_checksums' , ' --check' , ' -D' , $pgdata ,
53
- ' -r' , $relfilenode_corrupted ],
54
- 1,
55
- [qr / Bad checksums:.*1/ ],
56
- [qr / checksum verification failed/ ],
57
- " fails with corrupted data for single relfilenode on tablespace $tablespace " );
58
+ $node -> command_checks_all(
59
+ [
60
+ ' pg_checksums' , ' --check' ,
61
+ ' -D' , $pgdata ,
62
+ ' -r' , $relfilenode_corrupted
63
+ ],
64
+ 1,
65
+ [qr / Bad checksums:.*1/ ],
66
+ [qr / checksum verification failed/ ],
67
+ " fails with corrupted data for single relfilenode on tablespace $tablespace "
68
+ );
58
69
59
70
# Global checksum checks fail as well
60
- $node -> command_checks_all([ ' pg_checksums' , ' --check' , ' -D' , $pgdata ],
61
- 1,
62
- [qr / Bad checksums:.*1/ ],
63
- [qr / checksum verification failed/ ],
64
- " fails with corrupted data on tablespace $tablespace " );
71
+ $node -> command_checks_all(
72
+ [ ' pg_checksums' , ' --check' , ' -D' , $pgdata ],
73
+ 1,
74
+ [qr / Bad checksums:.*1/ ],
75
+ [qr / checksum verification failed/ ],
76
+ " fails with corrupted data on tablespace $tablespace " );
65
77
66
78
# Drop corrupted table again and make sure there is no more corruption.
67
79
$node -> start;
68
80
$node -> safe_psql(' postgres' , " DROP TABLE $table ;" );
69
81
$node -> stop;
70
- $node -> command_ok([' pg_checksums' , ' --check' , ' -D' , $pgdata ],
71
- " succeeds again after table drop on tablespace $tablespace " );
82
+ $node -> command_ok([ ' pg_checksums' , ' --check' , ' -D' , $pgdata ],
83
+ " succeeds again after table drop on tablespace $tablespace " );
72
84
73
85
$node -> start;
74
86
return ;
@@ -80,19 +92,20 @@ sub check_relation_corruption
80
92
my $pgdata = $node -> data_dir;
81
93
82
94
# Control file should know that checksums are disabled.
83
- command_like([' pg_controldata' , $pgdata ],
84
- qr / Data page checksum version:.*0/ ,
85
- ' checksums disabled in control file' );
95
+ command_like(
96
+ [ ' pg_controldata' , $pgdata ],
97
+ qr / Data page checksum version:.*0/ ,
98
+ ' checksums disabled in control file' );
86
99
87
100
# These are correct but empty files, so they should pass through.
88
- append_to_file " $pgdata /global/99999" , " " ;
89
- append_to_file " $pgdata /global/99999.123" , " " ;
90
- append_to_file " $pgdata /global/99999_fsm" , " " ;
91
- append_to_file " $pgdata /global/99999_init" , " " ;
92
- append_to_file " $pgdata /global/99999_vm" , " " ;
101
+ append_to_file " $pgdata /global/99999" , " " ;
102
+ append_to_file " $pgdata /global/99999.123" , " " ;
103
+ append_to_file " $pgdata /global/99999_fsm" , " " ;
104
+ append_to_file " $pgdata /global/99999_init" , " " ;
105
+ append_to_file " $pgdata /global/99999_vm" , " " ;
93
106
append_to_file " $pgdata /global/99999_init.123" , " " ;
94
- append_to_file " $pgdata /global/99999_fsm.123" , " " ;
95
- append_to_file " $pgdata /global/99999_vm.123" , " " ;
107
+ append_to_file " $pgdata /global/99999_fsm.123" , " " ;
108
+ append_to_file " $pgdata /global/99999_vm.123" , " " ;
96
109
97
110
# These are temporary files and folders with dummy contents, which
98
111
# should be ignored by the scan.
@@ -101,67 +114,75 @@ sub check_relation_corruption
101
114
append_to_file " $pgdata /global/pgsql_tmp/1.1" , " foo" ;
102
115
103
116
# Enable checksums.
104
- command_ok([' pg_checksums' , ' --enable' , ' --no-sync' , ' -D' , $pgdata ],
105
- " checksums successfully enabled in cluster" );
117
+ command_ok([ ' pg_checksums' , ' --enable' , ' --no-sync' , ' -D' , $pgdata ],
118
+ " checksums successfully enabled in cluster" );
106
119
107
120
# Successive attempt to enable checksums fails.
108
- command_fails([' pg_checksums' , ' --enable' , ' --no-sync' , ' -D' , $pgdata ],
109
- " enabling checksums fails if already enabled" );
121
+ command_fails([ ' pg_checksums' , ' --enable' , ' --no-sync' , ' -D' , $pgdata ],
122
+ " enabling checksums fails if already enabled" );
110
123
111
124
# Control file should know that checksums are enabled.
112
- command_like([' pg_controldata' , $pgdata ],
113
- qr / Data page checksum version:.*1/ ,
114
- ' checksums enabled in control file' );
125
+ command_like(
126
+ [ ' pg_controldata' , $pgdata ],
127
+ qr / Data page checksum version:.*1/ ,
128
+ ' checksums enabled in control file' );
115
129
116
130
# Disable checksums again. Flush result here as that should be cheap.
117
- command_ok([' pg_checksums' , ' --disable' , ' -D' , $pgdata ],
118
- " checksums successfully disabled in cluster" );
131
+ command_ok(
132
+ [ ' pg_checksums' , ' --disable' , ' -D' , $pgdata ],
133
+ " checksums successfully disabled in cluster" );
119
134
120
135
# Successive attempt to disable checksums fails.
121
- command_fails([' pg_checksums' , ' --disable' , ' --no-sync' , ' -D' , $pgdata ],
122
- " disabling checksums fails if already disabled" );
136
+ command_fails(
137
+ [ ' pg_checksums' , ' --disable' , ' --no-sync' , ' -D' , $pgdata ],
138
+ " disabling checksums fails if already disabled" );
123
139
124
140
# Control file should know that checksums are disabled.
125
- command_like([' pg_controldata' , $pgdata ],
126
- qr / Data page checksum version:.*0/ ,
127
- ' checksums disabled in control file' );
141
+ command_like(
142
+ [ ' pg_controldata' , $pgdata ],
143
+ qr / Data page checksum version:.*0/ ,
144
+ ' checksums disabled in control file' );
128
145
129
146
# Enable checksums again for follow-up tests.
130
- command_ok([' pg_checksums' , ' --enable' , ' --no-sync' , ' -D' , $pgdata ],
131
- " checksums successfully enabled in cluster" );
147
+ command_ok([ ' pg_checksums' , ' --enable' , ' --no-sync' , ' -D' , $pgdata ],
148
+ " checksums successfully enabled in cluster" );
132
149
133
150
# Control file should know that checksums are enabled.
134
- command_like([' pg_controldata' , $pgdata ],
135
- qr / Data page checksum version:.*1/ ,
136
- ' checksums enabled in control file' );
151
+ command_like(
152
+ [ ' pg_controldata' , $pgdata ],
153
+ qr / Data page checksum version:.*1/ ,
154
+ ' checksums enabled in control file' );
137
155
138
156
# Checksums pass on a newly-created cluster
139
- command_ok([' pg_checksums' , ' --check' , ' -D' , $pgdata ],
140
- " succeeds with offline cluster" );
157
+ command_ok([ ' pg_checksums' , ' --check' , ' -D' , $pgdata ],
158
+ " succeeds with offline cluster" );
141
159
142
160
# Checksums are verified if no other arguments are specified
143
- command_ok([' pg_checksums' , ' -D' , $pgdata ],
144
- " verifies checksums as default action" );
161
+ command_ok(
162
+ [ ' pg_checksums' , ' -D' , $pgdata ],
163
+ " verifies checksums as default action" );
145
164
146
165
# Specific relation files cannot be requested when action is --disable
147
166
# or --enable.
148
- command_fails([' pg_checksums' , ' --disable' , ' -r' , ' 1234' , ' -D' , $pgdata ],
149
- " fails when relfilenodes are requested and action is --disable" );
150
- command_fails([' pg_checksums' , ' --enable' , ' -r' , ' 1234' , ' -D' , $pgdata ],
151
- " fails when relfilenodes are requested and action is --enable" );
167
+ command_fails(
168
+ [ ' pg_checksums' , ' --disable' , ' -r' , ' 1234' , ' -D' , $pgdata ],
169
+ " fails when relfilenodes are requested and action is --disable" );
170
+ command_fails(
171
+ [ ' pg_checksums' , ' --enable' , ' -r' , ' 1234' , ' -D' , $pgdata ],
172
+ " fails when relfilenodes are requested and action is --enable" );
152
173
153
174
# Checks cannot happen with an online cluster
154
175
$node -> start;
155
- command_fails([' pg_checksums' , ' --check' , ' -D' , $pgdata ],
156
- " fails with online cluster" );
176
+ command_fails([ ' pg_checksums' , ' --check' , ' -D' , $pgdata ],
177
+ " fails with online cluster" );
157
178
158
179
# Check corruption of table on default tablespace.
159
180
check_relation_corruption($node , ' corrupt1' , ' pg_default' );
160
181
161
182
# Create tablespace to check corruptions in a non-default tablespace.
162
- my $basedir = $node -> basedir;
183
+ my $basedir = $node -> basedir;
163
184
my $tablespace_dir = " $basedir /ts_corrupt_dir" ;
164
- mkdir ($tablespace_dir );
185
+ mkdir ($tablespace_dir );
165
186
$tablespace_dir = TestLib::real_dir($tablespace_dir );
166
187
$node -> safe_psql(' postgres' ,
167
188
" CREATE TABLESPACE ts_corrupt LOCATION '$tablespace_dir ';" );
@@ -171,19 +192,20 @@ sub check_relation_corruption
171
192
# correctly-named relation files filled with some corrupted data.
172
193
sub fail_corrupt
173
194
{
174
- my $node = shift ;
175
- my $file = shift ;
195
+ my $node = shift ;
196
+ my $file = shift ;
176
197
my $pgdata = $node -> data_dir;
177
198
178
199
# Create the file with some dummy data in it.
179
200
my $file_name = " $pgdata /global/$file " ;
180
201
append_to_file $file_name , " foo" ;
181
202
182
- $node -> command_checks_all([ ' pg_checksums' , ' --check' , ' -D' , $pgdata ],
183
- 1,
184
- [qr / ^$ / ],
185
- [qr / could not read block 0 in file.*$file \" :/ ],
186
- " fails for corrupted data in $file " );
203
+ $node -> command_checks_all(
204
+ [ ' pg_checksums' , ' --check' , ' -D' , $pgdata ],
205
+ 1,
206
+ [qr / ^$ / ],
207
+ [qr / could not read block 0 in file.*$file \" :/ ],
208
+ " fails for corrupted data in $file " );
187
209
188
210
# Remove file to prevent future lookup errors on conflicts.
189
211
unlink $file_name ;
0 commit comments