aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_combinebackup/t/003_timeline.pl
blob: 0205a59f927ef7bcca34f6474183b51f062cc5a6 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Copyright (c) 2021-2025, PostgreSQL Global Development Group
#
# This test aims to validate that restoring an incremental backup works
# properly even when the reference backup is on a different timeline.

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

# Can be changed to test the other modes.
my $mode = $ENV{PG_TEST_PG_COMBINEBACKUP_MODE} || '--copy';

note "testing using mode $mode";

# Set up a new database instance.
my $node1 = PostgreSQL::Test::Cluster->new('node1');
$node1->init(has_archiving => 1, allows_streaming => 1);
$node1->append_conf('postgresql.conf', 'summarize_wal = on');
$node1->start;

# Create a table and insert a test row into it.
$node1->safe_psql('postgres', <<EOM);
CREATE TABLE mytable (a int, b text);
INSERT INTO mytable VALUES (1, 'aardvark');
EOM

# Take a full backup.
my $backup1path = $node1->backup_dir . '/backup1';
$node1->command_ok(
	[
		'pg_basebackup',
		'--pgdata' => $backup1path,
		'--no-sync',
		'--checkpoint' => 'fast'
	],
	"full backup from node1");

# Insert a second row on the original node.
$node1->safe_psql('postgres', <<EOM);
INSERT INTO mytable VALUES (2, 'beetle');
EOM

# Now take an incremental backup.
my $backup2path = $node1->backup_dir . '/backup2';
$node1->command_ok(
	[
		'pg_basebackup',
		'--pgdata' => $backup2path,
		'--no-sync',
		'--checkpoint' => 'fast',
		'--incremental' => $backup1path . '/backup_manifest'
	],
	"incremental backup from node1");

# Restore the incremental backup and use it to create a new node.
my $node2 = PostgreSQL::Test::Cluster->new('node2');
$node2->init_from_backup($node1, 'backup2',
	combine_with_prior => ['backup1']);
$node2->start();

# Insert rows on both nodes.
$node1->safe_psql('postgres', <<EOM);
INSERT INTO mytable VALUES (3, 'crab');
EOM
$node2->safe_psql('postgres', <<EOM);
INSERT INTO mytable VALUES (4, 'dingo');
EOM

# Take another incremental backup, from node2, based on backup2 from node1.
my $backup3path = $node1->backup_dir . '/backup3';
$node2->command_ok(
	[
		'pg_basebackup',
		'--pgdata' => $backup3path,
		'--no-sync',
		'--checkpoint' => 'fast',
		'--incremental' => $backup2path . '/backup_manifest'
	],
	"incremental backup from node2");

# Restore the incremental backup and use it to create a new node.
my $node3 = PostgreSQL::Test::Cluster->new('node3');
$node3->init_from_backup(
	$node1, 'backup3',
	combine_with_prior => [ 'backup1', 'backup2' ],
	combine_mode => $mode);
$node3->start();

# Let's insert one more row.
$node3->safe_psql('postgres', <<EOM);
INSERT INTO mytable VALUES (5, 'elephant');
EOM

# Now check that we have the expected rows.
my $result = $node3->safe_psql('postgres', <<EOM);
select string_agg(a::text, ':'), string_agg(b, ':') from mytable;
EOM
is($result, '1:2:4:5|aardvark:beetle:dingo:elephant');

# Let's also verify all the backups.
for my $backup_name (qw(backup1 backup2 backup3))
{
	$node1->command_ok(
		[ 'pg_verifybackup', $node1->backup_dir . '/' . $backup_name ],
		"verify backup $backup_name");
}

# OK, that's all.
done_testing();