| 1 |
|
| 2 |
# Copyright (c) 2022-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
use strict; |
| 5 |
use warnings FATAL => 'all'; |
| 6 |
use File::Basename; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::RecursiveCopy; |
| 9 |
use PostgreSQL::Test::Utils; |
| 10 |
use Test::More; |
| 11 |
|
| 12 |
my ($blocksize, $walfile_name); |
| 13 |
|
| 14 |
# Function to extract the LSN from the given block structure |
| 15 |
sub get_block_lsn |
| 16 |
{ |
| 17 |
my $path = shift; |
| 18 |
my $blocksize = shift; |
| 19 |
my $block; |
| 20 |
|
| 21 |
open my $fh, '<', $path or die "couldn't open file: $path\n"; |
| 22 |
die "could not read block\n" |
| 23 |
if $blocksize != read($fh, $block, $blocksize); |
| 24 |
my ($lsn_hi, $lsn_lo) = unpack('LL', $block); |
| 25 |
|
| 26 |
$lsn_hi = sprintf('%08X', $lsn_hi); |
| 27 |
$lsn_lo = sprintf('%08X', $lsn_lo); |
| 28 |
|
| 29 |
return ($lsn_hi, $lsn_lo); |
| 30 |
} |
| 31 |
|
| 32 |
my $node = PostgreSQL::Test::Cluster->new('main'); |
| 33 |
$node->init; |
| 34 |
$node->append_conf( |
| 35 |
'postgresql.conf', q{ |
| 36 |
wal_level = 'replica' |
| 37 |
max_wal_senders = 4 |
| 38 |
}); |
| 39 |
$node->start; |
| 40 |
|
| 41 |
# Generate data/WAL to examine that will have full pages in them. |
| 42 |
$node->safe_psql( |
| 43 |
'postgres', |
| 44 |
"SELECT 'init' FROM pg_create_physical_replication_slot('regress_pg_waldump_slot', true, false); |
| 45 |
CREATE TABLE test_table AS SELECT generate_series(1,100) a; |
| 46 |
-- Force FPWs on the next writes. |
| 47 |
CHECKPOINT; |
| 48 |
UPDATE test_table SET a = a + 1; |
| 49 |
"); |
| 50 |
|
| 51 |
($walfile_name, $blocksize) = split '\|' => $node->safe_psql('postgres', |
| 52 |
"SELECT pg_walfile_name(pg_switch_wal()), current_setting('block_size')"); |
| 53 |
|
| 54 |
# Get the relation node, etc for the new table |
| 55 |
my $relation = $node->safe_psql( |
| 56 |
'postgres', |
| 57 |
q{SELECT format( |
| 58 |
'%s/%s/%s', |
| 59 |
CASE WHEN reltablespace = 0 THEN dattablespace ELSE reltablespace END, |
| 60 |
pg_database.oid, |
| 61 |
pg_relation_filenode(pg_class.oid)) |
| 62 |
FROM pg_class, pg_database |
| 63 |
WHERE relname = 'test_table' AND |
| 64 |
datname = current_database()} |
| 65 |
); |
| 66 |
|
| 67 |
my $walfile = $node->data_dir . '/pg_wal/' . $walfile_name; |
| 68 |
my $tmp_folder = PostgreSQL::Test::Utils::tempdir; |
| 69 |
|
| 70 |
ok(-f $walfile, "Got a WAL file"); |
| 71 |
|
| 72 |
$node->command_ok( |
| 73 |
[ |
| 74 |
'pg_waldump', |
| 75 |
'--quiet', |
| 76 |
'--save-fullpage' => "$tmp_folder/raw", |
| 77 |
'--relation' => $relation, |
| 78 |
$walfile |
| 79 |
], |
| 80 |
'pg_waldump with --save-fullpage runs'); |
| 81 |
|
| 82 |
# This regexp will match filenames formatted as: |
| 83 |
# TLI-LSNh-LSNl.TBLSPCOID.DBOID.NODEOID.dd_fork with the components being: |
| 84 |
# - Timeline ID in hex format. |
| 85 |
# - WAL LSN in hex format, as two 8-character numbers. |
| 86 |
# - Tablespace OID (0 for global). |
| 87 |
# - Database OID. |
| 88 |
# - Relfilenode. |
| 89 |
# - Block number. |
| 90 |
# - Fork this block came from (vm, init, fsm, or main). |
| 91 |
my $file_re = |
| 92 |
qr/^[0-9A-F]{8}-([0-9A-F]{8})-([0-9A-F]{8})[.][0-9]+[.][0-9]+[.][0-9]+[.][0-9]+(?:_vm|_init|_fsm|_main)?$/; |
| 93 |
|
| 94 |
my $file_count = 0; |
| 95 |
|
| 96 |
# Verify filename format matches --save-fullpage. |
| 97 |
for my $fullpath (glob "$tmp_folder/raw/*") |
| 98 |
{ |
| 99 |
my $file = File::Basename::basename($fullpath); |
| 100 |
|
| 101 |
like($file, $file_re, "verify filename format for file $file"); |
| 102 |
$file_count++; |
| 103 |
|
| 104 |
my ($hi_lsn_fn, $lo_lsn_fn) = ($file =~ $file_re); |
| 105 |
my ($hi_lsn_bk, $lo_lsn_bk) = get_block_lsn($fullpath, $blocksize); |
| 106 |
|
| 107 |
# The LSN on the block comes before the file's LSN. |
| 108 |
ok( $hi_lsn_fn . $lo_lsn_fn ge $hi_lsn_bk . $lo_lsn_bk, |
| 109 |
"LSN stored in the file $hi_lsn_fn/$lo_lsn_fn precedes the one stored in the block $hi_lsn_bk/$lo_lsn_bk" |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
ok($file_count > 0, 'verify that at least one block has been saved'); |
| 114 |
|
| 115 |
done_testing(); |