| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test pg_verifybackup's WAL verification. |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings FATAL => 'all'; |
| 8 |
use PostgreSQL::Test::Cluster; |
| 9 |
use PostgreSQL::Test::Utils; |
| 10 |
use Test::More; |
| 11 |
|
| 12 |
# Start up the server and take a backup. |
| 13 |
my $primary = PostgreSQL::Test::Cluster->new('primary'); |
| 14 |
$primary->init(allows_streaming => 1); |
| 15 |
$primary->start; |
| 16 |
my $backup_path = $primary->backup_dir . '/test_wal'; |
| 17 |
$primary->command_ok( |
| 18 |
[ |
| 19 |
'pg_basebackup', |
| 20 |
'--pgdata' => $backup_path, |
| 21 |
'--no-sync', |
| 22 |
'--checkpoint' => 'fast' |
| 23 |
], |
| 24 |
"base backup ok"); |
| 25 |
|
| 26 |
# Rename pg_wal. |
| 27 |
my $original_pg_wal = $backup_path . '/pg_wal'; |
| 28 |
my $relocated_pg_wal = $primary->backup_dir . '/relocated_pg_wal'; |
| 29 |
rename($original_pg_wal, $relocated_pg_wal) || die "rename pg_wal: $!"; |
| 30 |
|
| 31 |
# WAL verification should fail. |
| 32 |
command_fails_like( |
| 33 |
[ 'pg_verifybackup', $backup_path ], |
| 34 |
qr/WAL parsing failed for timeline 1/, |
| 35 |
'missing pg_wal causes failure'); |
| 36 |
|
| 37 |
# Should work if we skip WAL verification. |
| 38 |
command_ok([ 'pg_verifybackup', '--no-parse-wal', $backup_path ], |
| 39 |
'missing pg_wal OK if not verifying WAL'); |
| 40 |
|
| 41 |
# Should also work if we specify the correct WAL location. |
| 42 |
command_ok( |
| 43 |
[ |
| 44 |
'pg_verifybackup', |
| 45 |
'--wal-directory' => $relocated_pg_wal, |
| 46 |
$backup_path |
| 47 |
], |
| 48 |
'--wal-directory can be used to specify WAL directory'); |
| 49 |
|
| 50 |
# Move directory back to original location. |
| 51 |
rename($relocated_pg_wal, $original_pg_wal) || die "rename pg_wal back: $!"; |
| 52 |
|
| 53 |
# Get a list of files in that directory that look like WAL files. |
| 54 |
my @walfiles = grep { /^[0-9A-F]{24}$/ } slurp_dir($original_pg_wal); |
| 55 |
|
| 56 |
# Replace the contents of one of the files with garbage of equal length. |
| 57 |
my $wal_corruption_target = $original_pg_wal . '/' . $walfiles[0]; |
| 58 |
my $wal_size = -s $wal_corruption_target; |
| 59 |
open(my $fh, '>', $wal_corruption_target) |
| 60 |
|| die "open $wal_corruption_target: $!"; |
| 61 |
print $fh 'w' x $wal_size; |
| 62 |
close($fh); |
| 63 |
|
| 64 |
# WAL verification should fail. |
| 65 |
command_fails_like( |
| 66 |
[ 'pg_verifybackup', $backup_path ], |
| 67 |
qr/WAL parsing failed for timeline 1/, |
| 68 |
'corrupt WAL file causes failure'); |
| 69 |
|
| 70 |
# Check that WAL-Ranges has correct values with a history file and |
| 71 |
# a timeline > 1. Rather than plugging in a new standby, do a |
| 72 |
# self-promotion of this node. |
| 73 |
$primary->stop; |
| 74 |
$primary->append_conf('standby.signal', ''); |
| 75 |
$primary->start; |
| 76 |
$primary->promote; |
| 77 |
$primary->safe_psql('postgres', 'SELECT pg_switch_wal()'); |
| 78 |
my $backup_path2 = $primary->backup_dir . '/test_tli'; |
| 79 |
# The base backup run below does a checkpoint, that removes the first segment |
| 80 |
# of the current timeline. |
| 81 |
$primary->command_ok( |
| 82 |
[ |
| 83 |
'pg_basebackup', |
| 84 |
'--pgdata' => $backup_path2, |
| 85 |
'--no-sync', |
| 86 |
'--checkpoint' => 'fast' |
| 87 |
], |
| 88 |
"base backup 2 ok"); |
| 89 |
command_ok( |
| 90 |
[ 'pg_verifybackup', $backup_path2 ], |
| 91 |
'valid base backup with timeline > 1'); |
| 92 |
|
| 93 |
done_testing(); |