| 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 |
use Test::More; |
| 10 |
|
| 11 |
program_help_ok('pg_resetwal'); |
| 12 |
program_version_ok('pg_resetwal'); |
| 13 |
program_options_handling_ok('pg_resetwal'); |
| 14 |
|
| 15 |
my $node = PostgreSQL::Test::Cluster->new('main'); |
| 16 |
$node->init; |
| 17 |
$node->append_conf('postgresql.conf', 'track_commit_timestamp = on'); |
| 18 |
|
| 19 |
command_like([ 'pg_resetwal', '-n', $node->data_dir ], |
| 20 |
qr/checkpoint/, 'pg_resetwal -n produces output'); |
| 21 |
|
| 22 |
|
| 23 |
# Permissions on PGDATA should be default |
| 24 |
SKIP: |
| 25 |
{ |
| 26 |
skip "unix-style permissions not supported on Windows", 1 |
| 27 |
if ($windows_os); |
| 28 |
|
| 29 |
ok(check_mode_recursive($node->data_dir, 0700, 0600), |
| 30 |
'check PGDATA permissions'); |
| 31 |
} |
| 32 |
|
| 33 |
command_ok([ 'pg_resetwal', '--pgdata' => $node->data_dir ], |
| 34 |
'pg_resetwal runs'); |
| 35 |
$node->start; |
| 36 |
is($node->safe_psql("postgres", "SELECT 1;"), |
| 37 |
1, 'server running and working after reset'); |
| 38 |
|
| 39 |
command_fails_like( |
| 40 |
[ 'pg_resetwal', $node->data_dir ], |
| 41 |
qr/lock file .* exists/, |
| 42 |
'fails if server running'); |
| 43 |
|
| 44 |
$node->stop('immediate'); |
| 45 |
command_fails_like( |
| 46 |
[ 'pg_resetwal', $node->data_dir ], |
| 47 |
qr/database server was not shut down cleanly/, |
| 48 |
'does not run after immediate shutdown'); |
| 49 |
command_ok( |
| 50 |
[ 'pg_resetwal', '--force', $node->data_dir ], |
| 51 |
'runs after immediate shutdown with force'); |
| 52 |
$node->start; |
| 53 |
is($node->safe_psql("postgres", "SELECT 1;"), |
| 54 |
1, 'server running and working after forced reset'); |
| 55 |
|
| 56 |
$node->stop; |
| 57 |
|
| 58 |
# check various command-line handling |
| 59 |
|
| 60 |
# Note: This test intends to check that a nonexistent data directory |
| 61 |
# gives a reasonable error message. Because of the way the code is |
| 62 |
# currently structured, you get an error about readings permissions, |
| 63 |
# which is perhaps suboptimal, so feel free to update this test if |
| 64 |
# this gets improved. |
| 65 |
command_fails_like( |
| 66 |
[ 'pg_resetwal', 'foo' ], |
| 67 |
qr/error: could not read permissions of directory/, |
| 68 |
'fails with nonexistent data directory'); |
| 69 |
|
| 70 |
command_fails_like( |
| 71 |
[ 'pg_resetwal', 'foo', 'bar' ], |
| 72 |
qr/too many command-line arguments/, |
| 73 |
'fails with too many command-line arguments'); |
| 74 |
|
| 75 |
$ENV{PGDATA} = $node->data_dir; # not used |
| 76 |
command_fails_like( |
| 77 |
['pg_resetwal'], |
| 78 |
qr/no data directory specified/, |
| 79 |
'fails with too few command-line arguments'); |
| 80 |
|
| 81 |
# error cases |
| 82 |
# -c |
| 83 |
command_fails_like( |
| 84 |
[ 'pg_resetwal', '-c' => 'foo', $node->data_dir ], |
| 85 |
qr/error: invalid argument for option -c/, |
| 86 |
'fails with incorrect -c option'); |
| 87 |
command_fails_like( |
| 88 |
[ 'pg_resetwal', '-c' => '10,bar', $node->data_dir ], |
| 89 |
qr/error: invalid argument for option -c/, |
| 90 |
'fails with incorrect -c option part 2'); |
| 91 |
command_fails_like( |
| 92 |
[ 'pg_resetwal', '-c' => '1,10', $node->data_dir ], |
| 93 |
qr/greater than/, |
| 94 |
'fails with -c ids value 1 part 1'); |
| 95 |
command_fails_like( |
| 96 |
[ 'pg_resetwal', '-c' => '10,1', $node->data_dir ], |
| 97 |
qr/greater than/, |
| 98 |
'fails with -c value 1 part 2'); |
| 99 |
# -e |
| 100 |
command_fails_like( |
| 101 |
[ 'pg_resetwal', '-e' => 'foo', $node->data_dir ], |
| 102 |
qr/error: invalid argument for option -e/, |
| 103 |
'fails with incorrect -e option'); |
| 104 |
command_fails_like( |
| 105 |
[ 'pg_resetwal', '-e' => '-1', $node->data_dir ], |
| 106 |
qr/must not be -1/, |
| 107 |
'fails with -e value -1'); |
| 108 |
# -l |
| 109 |
command_fails_like( |
| 110 |
[ 'pg_resetwal', '-l' => 'foo', $node->data_dir ], |
| 111 |
qr/error: invalid argument for option -l/, |
| 112 |
'fails with incorrect -l option'); |
| 113 |
# -m |
| 114 |
command_fails_like( |
| 115 |
[ 'pg_resetwal', '-m' => 'foo', $node->data_dir ], |
| 116 |
qr/error: invalid argument for option -m/, |
| 117 |
'fails with incorrect -m option'); |
| 118 |
command_fails_like( |
| 119 |
[ 'pg_resetwal', '-m' => '10,bar', $node->data_dir ], |
| 120 |
qr/error: invalid argument for option -m/, |
| 121 |
'fails with incorrect -m option part 2'); |
| 122 |
|
| 123 |
# This used to be forbidden, but nextMulti can legitimately be 0 after |
| 124 |
# wraparound, so we now accept it in pg_resetwal too. |
| 125 |
command_ok([ 'pg_resetwal', '-m' => '0,10', $node->data_dir ], |
| 126 |
'succeeds with -m value 0 part 1'); |
| 127 |
|
| 128 |
command_fails_like( |
| 129 |
[ 'pg_resetwal', '-m' => '10,0', $node->data_dir ], |
| 130 |
qr/must not be 0/, |
| 131 |
'fails with -m value 0 part 2'); |
| 132 |
# -o |
| 133 |
command_fails_like( |
| 134 |
[ 'pg_resetwal', '-o' => 'foo', $node->data_dir ], |
| 135 |
qr/error: invalid argument for option -o/, |
| 136 |
'fails with incorrect -o option'); |
| 137 |
command_fails_like( |
| 138 |
[ 'pg_resetwal', '-o' => '0', $node->data_dir ], |
| 139 |
qr/must not be 0/, |
| 140 |
'fails with -o value 0'); |
| 141 |
# -O |
| 142 |
command_fails_like( |
| 143 |
[ 'pg_resetwal', '-O' => 'foo', $node->data_dir ], |
| 144 |
qr/error: invalid argument for option -O/, |
| 145 |
'fails with incorrect -O option'); |
| 146 |
command_fails_like( |
| 147 |
[ 'pg_resetwal', '-O' => '-1', $node->data_dir ], |
| 148 |
qr/must be between 0 and 4294967295/, |
| 149 |
'fails with -O value -1'); |
| 150 |
# --wal-segsize |
| 151 |
command_fails_like( |
| 152 |
[ 'pg_resetwal', '--wal-segsize' => 'foo', $node->data_dir ], |
| 153 |
qr/error: invalid value/, |
| 154 |
'fails with incorrect --wal-segsize option'); |
| 155 |
command_fails_like( |
| 156 |
[ 'pg_resetwal', '--wal-segsize' => '13', $node->data_dir ], |
| 157 |
qr/must be a power/, |
| 158 |
'fails with invalid --wal-segsize value'); |
| 159 |
# -u |
| 160 |
command_fails_like( |
| 161 |
[ 'pg_resetwal', '-u' => 'foo', $node->data_dir ], |
| 162 |
qr/error: invalid argument for option -u/, |
| 163 |
'fails with incorrect -u option'); |
| 164 |
command_fails_like( |
| 165 |
[ 'pg_resetwal', '-u' => '1', $node->data_dir ], |
| 166 |
qr/must be greater than/, |
| 167 |
'fails with -u value too small'); |
| 168 |
# -x |
| 169 |
command_fails_like( |
| 170 |
[ 'pg_resetwal', '-x' => 'foo', $node->data_dir ], |
| 171 |
qr/error: invalid argument for option -x/, |
| 172 |
'fails with incorrect -x option'); |
| 173 |
command_fails_like( |
| 174 |
[ 'pg_resetwal', '-x' => '1', $node->data_dir ], |
| 175 |
qr/must be greater than/, |
| 176 |
'fails with -x value too small'); |
| 177 |
|
| 178 |
# --char-signedness |
| 179 |
command_fails_like( |
| 180 |
[ 'pg_resetwal', '--char-signedness', 'foo', $node->data_dir ], |
| 181 |
qr/error: invalid argument for option --char-signedness/, |
| 182 |
'fails with incorrect --char-signedness option'); |
| 183 |
|
| 184 |
# run with control override options |
| 185 |
|
| 186 |
my $out = (run_command([ 'pg_resetwal', '--dry-run', $node->data_dir ]))[0]; |
| 187 |
$out =~ /^Database block size: *(\d+)$/m or die; |
| 188 |
my $blcksz = $1; |
| 189 |
|
| 190 |
my @cmd = ('pg_resetwal', '--pgdata' => $node->data_dir); |
| 191 |
|
| 192 |
# some not-so-critical hardcoded values |
| 193 |
push @cmd, '--epoch' => 1; |
| 194 |
push @cmd, '--next-wal-file' => '00000001000000320000004B'; |
| 195 |
push @cmd, '--next-oid' => 100_000; |
| 196 |
push @cmd, '--wal-segsize' => 1; |
| 197 |
|
| 198 |
# these use the guidance from the documentation |
| 199 |
|
| 200 |
sub get_slru_files |
| 201 |
{ |
| 202 |
opendir(my $dh, $node->data_dir . '/' . $_[0]) or die $!; |
| 203 |
my @files = sort grep { /[0-9A-F]+/ } readdir $dh; |
| 204 |
closedir $dh; |
| 205 |
return @files; |
| 206 |
} |
| 207 |
|
| 208 |
my (@files, $mult); |
| 209 |
|
| 210 |
@files = get_slru_files('pg_commit_ts'); |
| 211 |
# XXX: Should there be a multiplier, similar to the other options? |
| 212 |
# -c argument is "old,new" |
| 213 |
push @cmd, |
| 214 |
'--commit-timestamp-ids' => |
| 215 |
sprintf("%d,%d", hex($files[0]) == 0 ? 3 : hex($files[0]), hex($files[-1])); |
| 216 |
|
| 217 |
@files = get_slru_files('pg_multixact/offsets'); |
| 218 |
$mult = 32 * $blcksz / 4; |
| 219 |
# --multixact-ids argument is "new,old" |
| 220 |
push @cmd, |
| 221 |
'--multixact-ids' => sprintf("%d,%d", |
| 222 |
(hex($files[-1]) + 1) * $mult, |
| 223 |
hex($files[0]) == 0 ? 1 : hex($files[0] * $mult)); |
| 224 |
|
| 225 |
@files = get_slru_files('pg_multixact/members'); |
| 226 |
$mult = 32 * int($blcksz / 20) * 4; |
| 227 |
push @cmd, '--multixact-offset' => (hex($files[-1]) + 1) * $mult; |
| 228 |
|
| 229 |
@files = get_slru_files('pg_xact'); |
| 230 |
$mult = 32 * $blcksz * 4; |
| 231 |
push @cmd, |
| 232 |
'--oldest-transaction-id' => |
| 233 |
(hex($files[0]) == 0 ? 3 : hex($files[0]) * $mult), |
| 234 |
'--next-transaction-id' => ((hex($files[-1]) + 1) * $mult); |
| 235 |
|
| 236 |
command_ok([ @cmd, '--dry-run' ], |
| 237 |
'runs with control override options, dry run'); |
| 238 |
command_ok(\@cmd, 'runs with control override options'); |
| 239 |
command_like( |
| 240 |
[ 'pg_resetwal', '--dry-run', $node->data_dir ], |
| 241 |
qr/^Latest checkpoint's NextOID: *100000$/m, |
| 242 |
'spot check that control changes were applied'); |
| 243 |
|
| 244 |
$node->start; |
| 245 |
ok(1, 'server started after reset'); |
| 246 |
|
| 247 |
done_testing(); |