| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use PostgreSQL::Test::Cluster; |
| 7 |
use PostgreSQL::Test::Utils; |
| 8 |
use Test::More; |
| 9 |
|
| 10 |
program_help_ok('pg_waldump'); |
| 11 |
program_version_ok('pg_waldump'); |
| 12 |
program_options_handling_ok('pg_waldump'); |
| 13 |
|
| 14 |
# wrong number of arguments |
| 15 |
command_fails_like([ 'pg_waldump', ], qr/error: no arguments/, |
| 16 |
'no arguments'); |
| 17 |
command_fails_like( |
| 18 |
[ 'pg_waldump', 'foo', 'bar', 'baz' ], |
| 19 |
qr/error: too many command-line arguments/, |
| 20 |
'too many arguments'); |
| 21 |
|
| 22 |
# invalid option arguments |
| 23 |
command_fails_like( |
| 24 |
[ 'pg_waldump', '--block' => 'bad' ], |
| 25 |
qr/error: invalid block number/, |
| 26 |
'invalid block number'); |
| 27 |
command_fails_like( |
| 28 |
[ 'pg_waldump', '--fork' => 'bad' ], |
| 29 |
qr/error: invalid fork name/, |
| 30 |
'invalid fork name'); |
| 31 |
command_fails_like( |
| 32 |
[ 'pg_waldump', '--limit' => 'bad' ], |
| 33 |
qr/error: invalid value/, |
| 34 |
'invalid limit'); |
| 35 |
command_fails_like( |
| 36 |
[ 'pg_waldump', '--relation' => 'bad' ], |
| 37 |
qr/error: invalid relation/, |
| 38 |
'invalid relation specification'); |
| 39 |
command_fails_like( |
| 40 |
[ 'pg_waldump', '--rmgr' => 'bad' ], |
| 41 |
qr/error: resource manager .* does not exist/, |
| 42 |
'invalid rmgr name'); |
| 43 |
command_fails_like( |
| 44 |
[ 'pg_waldump', '--start' => 'bad' ], |
| 45 |
qr/error: invalid WAL location/, |
| 46 |
'invalid start LSN'); |
| 47 |
command_fails_like( |
| 48 |
[ 'pg_waldump', '--end' => 'bad' ], |
| 49 |
qr/error: invalid WAL location/, |
| 50 |
'invalid end LSN'); |
| 51 |
|
| 52 |
# rmgr list: If you add one to the list, consider also adding a test |
| 53 |
# case exercising the new rmgr below. |
| 54 |
command_like( |
| 55 |
[ 'pg_waldump', '--rmgr=list' ], qr/^XLOG |
| 56 |
Transaction |
| 57 |
Storage |
| 58 |
CLOG |
| 59 |
Database |
| 60 |
Tablespace |
| 61 |
MultiXact |
| 62 |
RelMap |
| 63 |
Standby |
| 64 |
Heap2 |
| 65 |
Heap |
| 66 |
Btree |
| 67 |
Hash |
| 68 |
Gin |
| 69 |
Gist |
| 70 |
Sequence |
| 71 |
SPGist |
| 72 |
BRIN |
| 73 |
CommitTs |
| 74 |
ReplicationOrigin |
| 75 |
Generic |
| 76 |
LogicalMessage$/, |
| 77 |
'rmgr list'); |
| 78 |
|
| 79 |
|
| 80 |
my $node = PostgreSQL::Test::Cluster->new('main'); |
| 81 |
$node->init; |
| 82 |
$node->append_conf( |
| 83 |
'postgresql.conf', q{ |
| 84 |
autovacuum = off |
| 85 |
checkpoint_timeout = 1h |
| 86 |
|
| 87 |
# for standbydesc |
| 88 |
archive_mode=on |
| 89 |
archive_command='' |
| 90 |
|
| 91 |
# for XLOG_HEAP_TRUNCATE |
| 92 |
wal_level=logical |
| 93 |
}); |
| 94 |
$node->start; |
| 95 |
|
| 96 |
my ($start_lsn, $start_walfile) = split /\|/, |
| 97 |
$node->safe_psql('postgres', |
| 98 |
q{SELECT pg_current_wal_insert_lsn(), pg_walfile_name(pg_current_wal_insert_lsn())} |
| 99 |
); |
| 100 |
|
| 101 |
$node->safe_psql( |
| 102 |
'postgres', q{ |
| 103 |
-- heap, btree, hash, sequence |
| 104 |
CREATE TABLE t1 (a int GENERATED ALWAYS AS IDENTITY, b text); |
| 105 |
CREATE INDEX i1a ON t1 USING btree (a); |
| 106 |
CREATE INDEX i1b ON t1 USING hash (b); |
| 107 |
INSERT INTO t1 VALUES (default, 'one'), (default, 'two'); |
| 108 |
DELETE FROM t1 WHERE b = 'one'; |
| 109 |
TRUNCATE t1; |
| 110 |
|
| 111 |
-- abort |
| 112 |
START TRANSACTION; |
| 113 |
INSERT INTO t1 VALUES (default, 'three'); |
| 114 |
ROLLBACK; |
| 115 |
|
| 116 |
-- unlogged/init fork |
| 117 |
CREATE UNLOGGED TABLE t2 (x int); |
| 118 |
CREATE INDEX i2 ON t2 USING btree (x); |
| 119 |
INSERT INTO t2 SELECT generate_series(1, 10); |
| 120 |
|
| 121 |
-- gin |
| 122 |
CREATE TABLE gin_idx_tbl (id bigserial PRIMARY KEY, data jsonb); |
| 123 |
CREATE INDEX gin_idx ON gin_idx_tbl USING gin (data); |
| 124 |
INSERT INTO gin_idx_tbl |
| 125 |
WITH random_json AS ( |
| 126 |
SELECT json_object_agg(key, trunc(random() * 10)) as json_data |
| 127 |
FROM unnest(array['a', 'b', 'c']) as u(key)) |
| 128 |
SELECT generate_series(1,500), json_data FROM random_json; |
| 129 |
|
| 130 |
-- gist, spgist |
| 131 |
CREATE TABLE gist_idx_tbl (p point); |
| 132 |
CREATE INDEX gist_idx ON gist_idx_tbl USING gist (p); |
| 133 |
CREATE INDEX spgist_idx ON gist_idx_tbl USING spgist (p); |
| 134 |
INSERT INTO gist_idx_tbl (p) VALUES (point '(1, 1)'), (point '(3, 2)'), (point '(6, 3)'); |
| 135 |
|
| 136 |
-- brin |
| 137 |
CREATE TABLE brin_idx_tbl (col1 int, col2 text, col3 text ); |
| 138 |
CREATE INDEX brin_idx ON brin_idx_tbl USING brin (col1, col2, col3) WITH (autosummarize=on); |
| 139 |
INSERT INTO brin_idx_tbl SELECT generate_series(1, 10000), 'dummy', 'dummy'; |
| 140 |
UPDATE brin_idx_tbl SET col2 = 'updated' WHERE col1 BETWEEN 1 AND 5000; |
| 141 |
SELECT brin_summarize_range('brin_idx', 0); |
| 142 |
SELECT brin_desummarize_range('brin_idx', 0); |
| 143 |
|
| 144 |
VACUUM; |
| 145 |
|
| 146 |
-- logical message |
| 147 |
SELECT pg_logical_emit_message(true, 'foo', 'bar'); |
| 148 |
|
| 149 |
-- relmap |
| 150 |
VACUUM FULL pg_authid; |
| 151 |
|
| 152 |
-- database |
| 153 |
CREATE DATABASE d1; |
| 154 |
DROP DATABASE d1; |
| 155 |
}); |
| 156 |
|
| 157 |
my $tblspc_path = PostgreSQL::Test::Utils::tempdir_short(); |
| 158 |
|
| 159 |
$node->safe_psql( |
| 160 |
'postgres', qq{ |
| 161 |
CREATE TABLESPACE ts1 LOCATION '$tblspc_path'; |
| 162 |
DROP TABLESPACE ts1; |
| 163 |
}); |
| 164 |
|
| 165 |
my ($end_lsn, $end_walfile) = split /\|/, |
| 166 |
$node->safe_psql('postgres', |
| 167 |
q{SELECT pg_current_wal_insert_lsn(), pg_walfile_name(pg_current_wal_insert_lsn())} |
| 168 |
); |
| 169 |
|
| 170 |
my $default_ts_oid = $node->safe_psql('postgres', |
| 171 |
q{SELECT oid FROM pg_tablespace WHERE spcname = 'pg_default'}); |
| 172 |
my $postgres_db_oid = $node->safe_psql('postgres', |
| 173 |
q{SELECT oid FROM pg_database WHERE datname = 'postgres'}); |
| 174 |
my $rel_t1_oid = $node->safe_psql('postgres', |
| 175 |
q{SELECT oid FROM pg_class WHERE relname = 't1'}); |
| 176 |
my $rel_i1a_oid = $node->safe_psql('postgres', |
| 177 |
q{SELECT oid FROM pg_class WHERE relname = 'i1a'}); |
| 178 |
|
| 179 |
$node->stop; |
| 180 |
|
| 181 |
|
| 182 |
# various ways of specifying WAL range |
| 183 |
command_fails_like( |
| 184 |
[ 'pg_waldump', 'foo', 'bar' ], |
| 185 |
qr/error: could not locate WAL file "foo"/, |
| 186 |
'start file not found'); |
| 187 |
command_like([ 'pg_waldump', $node->data_dir . '/pg_wal/' . $start_walfile ], |
| 188 |
qr/./, 'runs with start segment specified'); |
| 189 |
command_fails_like( |
| 190 |
[ 'pg_waldump', $node->data_dir . '/pg_wal/' . $start_walfile, 'bar' ], |
| 191 |
qr/error: could not open file "bar"/, |
| 192 |
'end file not found'); |
| 193 |
command_like( |
| 194 |
[ |
| 195 |
'pg_waldump', |
| 196 |
$node->data_dir . '/pg_wal/' . $start_walfile, |
| 197 |
$node->data_dir . '/pg_wal/' . $end_walfile |
| 198 |
], |
| 199 |
qr/./, |
| 200 |
'runs with start and end segment specified'); |
| 201 |
command_fails_like( |
| 202 |
[ 'pg_waldump', '--path' => $node->data_dir ], |
| 203 |
qr/error: no start WAL location given/, |
| 204 |
'path option requires start location'); |
| 205 |
command_like( |
| 206 |
[ |
| 207 |
'pg_waldump', |
| 208 |
'--path' => $node->data_dir, |
| 209 |
'--start' => $start_lsn, |
| 210 |
'--end' => $end_lsn, |
| 211 |
], |
| 212 |
qr/./, |
| 213 |
'runs with path option and start and end locations'); |
| 214 |
command_fails_like( |
| 215 |
[ |
| 216 |
'pg_waldump', |
| 217 |
'--path' => $node->data_dir, |
| 218 |
'--start' => $start_lsn, |
| 219 |
], |
| 220 |
qr/error: error in WAL record at/, |
| 221 |
'falling off the end of the WAL results in an error'); |
| 222 |
|
| 223 |
command_like( |
| 224 |
[ |
| 225 |
'pg_waldump', '--quiet', |
| 226 |
$node->data_dir . '/pg_wal/' . $start_walfile |
| 227 |
], |
| 228 |
qr/^$/, |
| 229 |
'no output with --quiet option'); |
| 230 |
command_fails_like( |
| 231 |
[ |
| 232 |
'pg_waldump', '--quiet', |
| 233 |
'--path' => $node->data_dir, |
| 234 |
'--start' => $start_lsn |
| 235 |
], |
| 236 |
qr/error: error in WAL record at/, |
| 237 |
'errors are shown with --quiet'); |
| 238 |
|
| 239 |
|
| 240 |
# Test for: Display a message that we're skipping data if `from` |
| 241 |
# wasn't a pointer to the start of a record. |
| 242 |
{ |
| 243 |
# Construct a new LSN that is one byte past the original |
| 244 |
# start_lsn. |
| 245 |
my ($part1, $part2) = split qr{/}, $start_lsn; |
| 246 |
my $lsn2 = hex $part2; |
| 247 |
$lsn2++; |
| 248 |
my $new_start = sprintf("%s/%X", $part1, $lsn2); |
| 249 |
|
| 250 |
my ($stdout, $stderr); |
| 251 |
|
| 252 |
my $result = IPC::Run::run [ |
| 253 |
'pg_waldump', |
| 254 |
'--start' => $new_start, |
| 255 |
$node->data_dir . '/pg_wal/' . $start_walfile |
| 256 |
], |
| 257 |
'>' => \$stdout, |
| 258 |
'2>' => \$stderr; |
| 259 |
ok($result, "runs with start segment and start LSN specified"); |
| 260 |
like($stderr, qr/first record is after/, 'info message printed'); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
# Helper function to test various options. Pass options as arguments. |
| 265 |
# Output lines are returned as array. |
| 266 |
sub test_pg_waldump |
| 267 |
{ |
| 268 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 269 |
my @opts = @_; |
| 270 |
|
| 271 |
my ($stdout, $stderr); |
| 272 |
|
| 273 |
my $result = IPC::Run::run [ |
| 274 |
'pg_waldump', |
| 275 |
'--path' => $node->data_dir, |
| 276 |
'--start' => $start_lsn, |
| 277 |
'--end' => $end_lsn, |
| 278 |
@opts |
| 279 |
], |
| 280 |
'>' => \$stdout, |
| 281 |
'2>' => \$stderr; |
| 282 |
ok($result, "pg_waldump @opts: runs ok"); |
| 283 |
is($stderr, '', "pg_waldump @opts: no stderr"); |
| 284 |
my @lines = split /\n/, $stdout; |
| 285 |
ok(@lines > 0, "pg_waldump @opts: some lines are output"); |
| 286 |
return @lines; |
| 287 |
} |
| 288 |
|
| 289 |
my @lines; |
| 290 |
|
| 291 |
@lines = test_pg_waldump; |
| 292 |
is(grep(!/^rmgr: \w/, @lines), 0, 'all output lines are rmgr lines'); |
| 293 |
|
| 294 |
@lines = test_pg_waldump('--limit' => 6); |
| 295 |
is(@lines, 6, 'limit option observed'); |
| 296 |
|
| 297 |
@lines = test_pg_waldump('--fullpage'); |
| 298 |
is(grep(!/^rmgr:.*\bFPW\b/, @lines), 0, 'all output lines are FPW'); |
| 299 |
|
| 300 |
@lines = test_pg_waldump('--stats'); |
| 301 |
like($lines[0], qr/WAL statistics/, "statistics on stdout"); |
| 302 |
is(grep(/^rmgr:/, @lines), 0, 'no rmgr lines output'); |
| 303 |
|
| 304 |
@lines = test_pg_waldump('--stats=record'); |
| 305 |
like($lines[0], qr/WAL statistics/, "statistics on stdout"); |
| 306 |
is(grep(/^rmgr:/, @lines), 0, 'no rmgr lines output'); |
| 307 |
|
| 308 |
@lines = test_pg_waldump('--rmgr' => 'Btree'); |
| 309 |
is(grep(!/^rmgr: Btree/, @lines), 0, 'only Btree lines'); |
| 310 |
|
| 311 |
@lines = test_pg_waldump('--fork' => 'init'); |
| 312 |
is(grep(!/fork init/, @lines), 0, 'only init fork lines'); |
| 313 |
|
| 314 |
@lines = test_pg_waldump( |
| 315 |
'--relation' => "$default_ts_oid/$postgres_db_oid/$rel_t1_oid"); |
| 316 |
is(grep(!/rel $default_ts_oid\/$postgres_db_oid\/$rel_t1_oid/, @lines), |
| 317 |
0, 'only lines for selected relation'); |
| 318 |
|
| 319 |
@lines = test_pg_waldump( |
| 320 |
'--relation' => "$default_ts_oid/$postgres_db_oid/$rel_i1a_oid", |
| 321 |
'--block' => 1); |
| 322 |
is(grep(!/\bblk 1\b/, @lines), 0, 'only lines for selected block'); |
| 323 |
|
| 324 |
|
| 325 |
done_testing(); |