Debian Codegraph @

src/bin/pg_resetwal/t/002_corrupted.pl

raw
1
2 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
3
4 # Tests for handling a corrupted pg_control
5
6 use strict;
7 use warnings FATAL => 'all';
8
9 use PostgreSQL::Test::Cluster;
10 use PostgreSQL::Test::Utils;
11 use Test::More;
12
13 my $node = PostgreSQL::Test::Cluster->new('main');
14 $node->init;
15
16 my $pg_control = $node->data_dir . '/global/pg_control';
17 my $size = -s $pg_control;
18
19 # Read out the head of the file to get PG_CONTROL_VERSION in
20 # particular.
21 my $data;
22 open my $fh, '<', $pg_control or BAIL_OUT($!);
23 binmode $fh;
24 read $fh, $data, 16 or die $!;
25 close $fh;
26
27 # Fill pg_control with zeros
28 open $fh, '>', $pg_control or BAIL_OUT($!);
29 binmode $fh;
30 print $fh pack("x[$size]");
31 close $fh;
32
33 command_checks_all(
34 [ 'pg_resetwal', '--dry-run', $node->data_dir ],
35 0,
36 [qr/pg_control version number/],
37 [
38 qr/pg_resetwal: warning: pg_control exists but is broken or wrong version; ignoring it/
39 ],
40 'processes corrupted pg_control all zeroes');
41
42 # Put in the previously saved header data. This uses a different code
43 # path internally, allowing us to process a zero WAL segment size.
44 open $fh, '>', $pg_control or BAIL_OUT($!);
45 binmode $fh;
46 print $fh $data, pack("x[" . ($size - 16) . "]");
47 close $fh;
48
49 command_checks_all(
50 [ 'pg_resetwal', '--dry-run', $node->data_dir ],
51 0,
52 [qr/pg_control version number/],
53 [
54 qr/\Qpg_resetwal: warning: pg_control specifies invalid WAL segment size (0 bytes); proceed with caution\E/
55 ],
56 'processes zero WAL segment size');
57
58 # now try to run it
59 command_fails_like(
60 [ 'pg_resetwal', $node->data_dir ],
61 qr/not proceeding because control file values were guessed/,
62 'does not run when control file values were guessed');
63 command_ok(
64 [ 'pg_resetwal', '--force', $node->data_dir ],
65 'runs with force when control file values were guessed');
66
67 done_testing();