| 1 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 2 |
# |
| 3 |
# This test aims to validate that restoring an incremental backup works |
| 4 |
# properly even when the reference backup is on a different timeline. |
| 5 |
|
| 6 |
use strict; |
| 7 |
use warnings FATAL => 'all'; |
| 8 |
use File::Compare; |
| 9 |
use PostgreSQL::Test::Cluster; |
| 10 |
use PostgreSQL::Test::Utils; |
| 11 |
use Test::More; |
| 12 |
|
| 13 |
# Can be changed to test the other modes. |
| 14 |
my $mode = $ENV{PG_TEST_PG_COMBINEBACKUP_MODE} || '--copy'; |
| 15 |
|
| 16 |
note "testing using mode $mode"; |
| 17 |
|
| 18 |
# Set up a new database instance. |
| 19 |
my $node1 = PostgreSQL::Test::Cluster->new('node1'); |
| 20 |
$node1->init(has_archiving => 1, allows_streaming => 1); |
| 21 |
$node1->append_conf('postgresql.conf', 'summarize_wal = on'); |
| 22 |
$node1->start; |
| 23 |
|
| 24 |
# Create a table and insert a test row into it. |
| 25 |
$node1->safe_psql('postgres', <<EOM); |
| 26 |
CREATE TABLE mytable (a int, b text); |
| 27 |
INSERT INTO mytable VALUES (1, 'aardvark'); |
| 28 |
EOM |
| 29 |
|
| 30 |
# Take a full backup. |
| 31 |
my $backup1path = $node1->backup_dir . '/backup1'; |
| 32 |
$node1->command_ok( |
| 33 |
[ |
| 34 |
'pg_basebackup', |
| 35 |
'--pgdata' => $backup1path, |
| 36 |
'--no-sync', |
| 37 |
'--checkpoint' => 'fast' |
| 38 |
], |
| 39 |
"full backup from node1"); |
| 40 |
|
| 41 |
# Insert a second row on the original node. |
| 42 |
$node1->safe_psql('postgres', <<EOM); |
| 43 |
INSERT INTO mytable VALUES (2, 'beetle'); |
| 44 |
EOM |
| 45 |
|
| 46 |
# Now take an incremental backup. |
| 47 |
my $backup2path = $node1->backup_dir . '/backup2'; |
| 48 |
$node1->command_ok( |
| 49 |
[ |
| 50 |
'pg_basebackup', |
| 51 |
'--pgdata' => $backup2path, |
| 52 |
'--no-sync', |
| 53 |
'--checkpoint' => 'fast', |
| 54 |
'--incremental' => $backup1path . '/backup_manifest' |
| 55 |
], |
| 56 |
"incremental backup from node1"); |
| 57 |
|
| 58 |
# Restore the incremental backup and use it to create a new node. |
| 59 |
my $node2 = PostgreSQL::Test::Cluster->new('node2'); |
| 60 |
$node2->init_from_backup($node1, 'backup2', |
| 61 |
combine_with_prior => ['backup1']); |
| 62 |
$node2->start(); |
| 63 |
|
| 64 |
# Insert rows on both nodes. |
| 65 |
$node1->safe_psql('postgres', <<EOM); |
| 66 |
INSERT INTO mytable VALUES (3, 'crab'); |
| 67 |
EOM |
| 68 |
$node2->safe_psql('postgres', <<EOM); |
| 69 |
INSERT INTO mytable VALUES (4, 'dingo'); |
| 70 |
EOM |
| 71 |
|
| 72 |
# Take another incremental backup, from node2, based on backup2 from node1. |
| 73 |
my $backup3path = $node1->backup_dir . '/backup3'; |
| 74 |
$node2->command_ok( |
| 75 |
[ |
| 76 |
'pg_basebackup', |
| 77 |
'--pgdata' => $backup3path, |
| 78 |
'--no-sync', |
| 79 |
'--checkpoint' => 'fast', |
| 80 |
'--incremental' => $backup2path . '/backup_manifest' |
| 81 |
], |
| 82 |
"incremental backup from node2"); |
| 83 |
|
| 84 |
# Restore the incremental backup and use it to create a new node. |
| 85 |
my $node3 = PostgreSQL::Test::Cluster->new('node3'); |
| 86 |
$node3->init_from_backup( |
| 87 |
$node1, 'backup3', |
| 88 |
combine_with_prior => [ 'backup1', 'backup2' ], |
| 89 |
combine_mode => $mode); |
| 90 |
$node3->start(); |
| 91 |
|
| 92 |
# Let's insert one more row. |
| 93 |
$node3->safe_psql('postgres', <<EOM); |
| 94 |
INSERT INTO mytable VALUES (5, 'elephant'); |
| 95 |
EOM |
| 96 |
|
| 97 |
# Now check that we have the expected rows. |
| 98 |
my $result = $node3->safe_psql('postgres', <<EOM); |
| 99 |
select string_agg(a::text, ':'), string_agg(b, ':') from mytable; |
| 100 |
EOM |
| 101 |
is($result, '1:2:4:5|aardvark:beetle:dingo:elephant'); |
| 102 |
|
| 103 |
# Let's also verify all the backups. |
| 104 |
for my $backup_name (qw(backup1 backup2 backup3)) |
| 105 |
{ |
| 106 |
$node1->command_ok( |
| 107 |
[ 'pg_verifybackup', $node1->backup_dir . '/' . $backup_name ], |
| 108 |
"verify backup $backup_name"); |
| 109 |
} |
| 110 |
|
| 111 |
# OK, that's all. |
| 112 |
done_testing(); |