aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_verifybackup/t/007_wal.pl
blob: babc4f0a86b85b1584d9de1986951b61f2dcae70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# Copyright (c) 2021-2025, PostgreSQL Global Development Group

# Test pg_verifybackup's WAL verification.

use strict;
use warnings FATAL => 'all';
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;

# Start up the server and take a backup.
my $primary = PostgreSQL::Test::Cluster->new('primary');
$primary->init(allows_streaming => 1);
$primary->start;
my $backup_path = $primary->backup_dir . '/test_wal';
$primary->command_ok(
	[
		'pg_basebackup',
		'--pgdata' => $backup_path,
		'--no-sync',
		'--checkpoint' => 'fast'
	],
	"base backup ok");

# Rename pg_wal.
my $original_pg_wal = $backup_path . '/pg_wal';
my $relocated_pg_wal = $primary->backup_dir . '/relocated_pg_wal';
rename($original_pg_wal, $relocated_pg_wal) || die "rename pg_wal: $!";

# WAL verification should fail.
command_fails_like(
	[ 'pg_verifybackup', $backup_path ],
	qr/WAL parsing failed for timeline 1/,
	'missing pg_wal causes failure');

# Should work if we skip WAL verification.
command_ok([ 'pg_verifybackup', '--no-parse-wal', $backup_path ],
	'missing pg_wal OK if not verifying WAL');

# Should also work if we specify the correct WAL location.
command_ok(
	[
		'pg_verifybackup',
		'--wal-directory' => $relocated_pg_wal,
		$backup_path
	],
	'--wal-directory can be used to specify WAL directory');

# Move directory back to original location.
rename($relocated_pg_wal, $original_pg_wal) || die "rename pg_wal back: $!";

# Get a list of files in that directory that look like WAL files.
my @walfiles = grep { /^[0-9A-F]{24}$/ } slurp_dir($original_pg_wal);

# Replace the contents of one of the files with garbage of equal length.
my $wal_corruption_target = $original_pg_wal . '/' . $walfiles[0];
my $wal_size = -s $wal_corruption_target;
open(my $fh, '>', $wal_corruption_target)
  || die "open $wal_corruption_target: $!";
print $fh 'w' x $wal_size;
close($fh);

# WAL verification should fail.
command_fails_like(
	[ 'pg_verifybackup', $backup_path ],
	qr/WAL parsing failed for timeline 1/,
	'corrupt WAL file causes failure');

# Check that WAL-Ranges has correct values with a history file and
# a timeline > 1.  Rather than plugging in a new standby, do a
# self-promotion of this node.
$primary->stop;
$primary->append_conf('standby.signal', '');
$primary->start;
$primary->promote;
$primary->safe_psql('postgres', 'SELECT pg_switch_wal()');
my $backup_path2 = $primary->backup_dir . '/test_tli';
# The base backup run below does a checkpoint, that removes the first segment
# of the current timeline.
$primary->command_ok(
	[
		'pg_basebackup',
		'--pgdata' => $backup_path2,
		'--no-sync',
		'--checkpoint' => 'fast'
	],
	"base backup 2 ok");
command_ok(
	[ 'pg_verifybackup', $backup_path2 ],
	'valid base backup with timeline > 1');

done_testing();