| 1 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings FATAL => 'all'; |
| 5 |
use File::Compare qw(compare_text); |
| 6 |
use PostgreSQL::Test::Cluster; |
| 7 |
use PostgreSQL::Test::Utils; |
| 8 |
use Test::More; |
| 9 |
|
| 10 |
my $tempdir = PostgreSQL::Test::Utils::tempdir_short(); |
| 11 |
|
| 12 |
# Can be changed to test the other modes. |
| 13 |
my $mode = $ENV{PG_TEST_PG_COMBINEBACKUP_MODE} || '--copy'; |
| 14 |
|
| 15 |
note "testing using mode $mode"; |
| 16 |
|
| 17 |
# Set up a new database instance. |
| 18 |
my $primary = PostgreSQL::Test::Cluster->new('primary'); |
| 19 |
$primary->init(has_archiving => 1, allows_streaming => 1); |
| 20 |
$primary->append_conf('postgresql.conf', 'summarize_wal = on'); |
| 21 |
$primary->start; |
| 22 |
my $tsprimary = $tempdir . '/ts'; |
| 23 |
mkdir($tsprimary) || die "mkdir $tsprimary: $!"; |
| 24 |
|
| 25 |
# Create some test tables, each containing one row of data, plus a whole |
| 26 |
# extra database. |
| 27 |
$primary->safe_psql('postgres', <<EOM); |
| 28 |
CREATE TABLE will_change (a int, b text); |
| 29 |
INSERT INTO will_change VALUES (1, 'initial test row'); |
| 30 |
CREATE TABLE will_grow (a int, b text); |
| 31 |
INSERT INTO will_grow VALUES (1, 'initial test row'); |
| 32 |
CREATE TABLE will_shrink (a int, b text); |
| 33 |
INSERT INTO will_shrink VALUES (1, 'initial test row'); |
| 34 |
CREATE TABLE will_get_vacuumed (a int, b text); |
| 35 |
INSERT INTO will_get_vacuumed VALUES (1, 'initial test row'); |
| 36 |
CREATE TABLE will_get_dropped (a int, b text); |
| 37 |
INSERT INTO will_get_dropped VALUES (1, 'initial test row'); |
| 38 |
CREATE TABLE will_get_rewritten (a int, b text); |
| 39 |
INSERT INTO will_get_rewritten VALUES (1, 'initial test row'); |
| 40 |
CREATE DATABASE db_will_get_dropped; |
| 41 |
CREATE TABLESPACE ts1 LOCATION '$tsprimary'; |
| 42 |
CREATE TABLE will_not_change_in_ts (a int, b text) TABLESPACE ts1; |
| 43 |
INSERT INTO will_not_change_in_ts VALUES (1, 'initial test row'); |
| 44 |
CREATE TABLE will_change_in_ts (a int, b text) TABLESPACE ts1; |
| 45 |
INSERT INTO will_change_in_ts VALUES (1, 'initial test row'); |
| 46 |
CREATE TABLE will_get_dropped_in_ts (a int, b text); |
| 47 |
INSERT INTO will_get_dropped_in_ts VALUES (1, 'initial test row'); |
| 48 |
EOM |
| 49 |
|
| 50 |
# Read list of tablespace OIDs. There should be just one. |
| 51 |
my @tsoids = grep { /^\d+/ } slurp_dir($primary->data_dir . '/pg_tblspc'); |
| 52 |
is(0 + @tsoids, 1, "exactly one user-defined tablespace"); |
| 53 |
my $tsoid = $tsoids[0]; |
| 54 |
|
| 55 |
# Take a full backup. |
| 56 |
my $backup1path = $primary->backup_dir . '/backup1'; |
| 57 |
my $tsbackup1path = $tempdir . '/ts1backup'; |
| 58 |
mkdir($tsbackup1path) || die "mkdir $tsbackup1path: $!"; |
| 59 |
$primary->command_ok( |
| 60 |
[ |
| 61 |
'pg_basebackup', |
| 62 |
'--no-sync', |
| 63 |
'--pgdata' => $backup1path, |
| 64 |
'--checkpoint' => 'fast', |
| 65 |
'--tablespace-mapping' => "${tsprimary}=${tsbackup1path}" |
| 66 |
], |
| 67 |
"full backup"); |
| 68 |
|
| 69 |
# Now make some database changes. |
| 70 |
$primary->safe_psql('postgres', <<EOM); |
| 71 |
UPDATE will_change SET b = 'modified value' WHERE a = 1; |
| 72 |
UPDATE will_change_in_ts SET b = 'modified value' WHERE a = 1; |
| 73 |
INSERT INTO will_grow |
| 74 |
SELECT g, 'additional row' FROM generate_series(2, 5000) g; |
| 75 |
TRUNCATE will_shrink; |
| 76 |
VACUUM will_get_vacuumed; |
| 77 |
DROP TABLE will_get_dropped; |
| 78 |
DROP TABLE will_get_dropped_in_ts; |
| 79 |
CREATE TABLE newly_created (a int, b text); |
| 80 |
INSERT INTO newly_created VALUES (1, 'row for new table'); |
| 81 |
CREATE TABLE newly_created_in_ts (a int, b text) TABLESPACE ts1; |
| 82 |
INSERT INTO newly_created_in_ts VALUES (1, 'row for new table'); |
| 83 |
VACUUM FULL will_get_rewritten; |
| 84 |
DROP DATABASE db_will_get_dropped; |
| 85 |
CREATE DATABASE db_newly_created; |
| 86 |
EOM |
| 87 |
|
| 88 |
# Take an incremental backup. |
| 89 |
my $backup2path = $primary->backup_dir . '/backup2'; |
| 90 |
my $tsbackup2path = $tempdir . '/tsbackup2'; |
| 91 |
mkdir($tsbackup2path) || die "mkdir $tsbackup2path: $!"; |
| 92 |
$primary->command_ok( |
| 93 |
[ |
| 94 |
'pg_basebackup', |
| 95 |
'--no-sync', |
| 96 |
'--pgdata' => $backup2path, |
| 97 |
'--checkpoint' => 'fast', |
| 98 |
'--tablespace-mapping' => "${tsprimary}=${tsbackup2path}", |
| 99 |
'--incremental' => $backup1path . '/backup_manifest' |
| 100 |
], |
| 101 |
"incremental backup"); |
| 102 |
|
| 103 |
# Find an LSN to which either backup can be recovered. |
| 104 |
my $lsn = $primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();"); |
| 105 |
|
| 106 |
# Make sure that the WAL segment containing that LSN has been archived. |
| 107 |
# PostgreSQL won't issue two consecutive XLOG_SWITCH records, and the backup |
| 108 |
# just issued one, so call txid_current() to generate some WAL activity |
| 109 |
# before calling pg_switch_wal(). |
| 110 |
$primary->safe_psql('postgres', 'SELECT txid_current();'); |
| 111 |
$primary->safe_psql('postgres', 'SELECT pg_switch_wal()'); |
| 112 |
|
| 113 |
# Now wait for the LSN we chose above to be archived. |
| 114 |
my $archive_wait_query = |
| 115 |
"SELECT pg_walfile_name('$lsn') <= last_archived_wal FROM pg_stat_archiver;"; |
| 116 |
$primary->poll_query_until('postgres', $archive_wait_query) |
| 117 |
or die "Timed out while waiting for WAL segment to be archived"; |
| 118 |
|
| 119 |
# Perform PITR from the full backup. Disable archive_mode so that the archive |
| 120 |
# doesn't find out about the new timeline; that way, the later PITR below will |
| 121 |
# choose the same timeline. |
| 122 |
my $tspitr1path = $tempdir . '/tspitr1'; |
| 123 |
my $pitr1 = PostgreSQL::Test::Cluster->new('pitr1'); |
| 124 |
$pitr1->init_from_backup( |
| 125 |
$primary, 'backup1', |
| 126 |
standby => 1, |
| 127 |
has_restoring => 1, |
| 128 |
tablespace_map => { $tsoid => $tspitr1path }); |
| 129 |
$pitr1->append_conf( |
| 130 |
'postgresql.conf', qq{ |
| 131 |
recovery_target_lsn = '$lsn' |
| 132 |
recovery_target_action = 'promote' |
| 133 |
archive_mode = 'off' |
| 134 |
}); |
| 135 |
$pitr1->start(); |
| 136 |
|
| 137 |
# Perform PITR to the same LSN from the incremental backup. Use the same |
| 138 |
# basic configuration as before. |
| 139 |
my $tspitr2path = $tempdir . '/tspitr2'; |
| 140 |
my $pitr2 = PostgreSQL::Test::Cluster->new('pitr2'); |
| 141 |
$pitr2->init_from_backup( |
| 142 |
$primary, 'backup2', |
| 143 |
standby => 1, |
| 144 |
has_restoring => 1, |
| 145 |
combine_with_prior => ['backup1'], |
| 146 |
tablespace_map => { $tsbackup2path => $tspitr2path }, |
| 147 |
combine_mode => $mode); |
| 148 |
$pitr2->append_conf( |
| 149 |
'postgresql.conf', qq{ |
| 150 |
recovery_target_lsn = '$lsn' |
| 151 |
recovery_target_action = 'promote' |
| 152 |
archive_mode = 'off' |
| 153 |
}); |
| 154 |
$pitr2->start(); |
| 155 |
|
| 156 |
# Wait until both servers exit recovery. |
| 157 |
$pitr1->poll_query_until('postgres', "SELECT NOT pg_is_in_recovery();") |
| 158 |
or die "Timed out while waiting apply to reach LSN $lsn"; |
| 159 |
$pitr2->poll_query_until('postgres', "SELECT NOT pg_is_in_recovery();") |
| 160 |
or die "Timed out while waiting apply to reach LSN $lsn"; |
| 161 |
|
| 162 |
# Perform a logical dump of each server, and check that they match. |
| 163 |
# It would be much nicer if we could physically compare the data files, but |
| 164 |
# that doesn't really work. The contents of the page hole aren't guaranteed to |
| 165 |
# be identical, and there can be other discrepancies as well. To make this work |
| 166 |
# we'd need the equivalent of each AM's rm_mask function written or at least |
| 167 |
# callable from Perl, and that doesn't seem practical. |
| 168 |
# |
| 169 |
# NB: We're just using the primary's backup directory for scratch space here. |
| 170 |
# This could equally well be any other directory we wanted to pick. |
| 171 |
my $backupdir = $primary->backup_dir; |
| 172 |
my $dump1 = $backupdir . '/pitr1.dump'; |
| 173 |
my $dump2 = $backupdir . '/pitr2.dump'; |
| 174 |
$pitr1->command_ok( |
| 175 |
[ |
| 176 |
'pg_dumpall', |
| 177 |
'--restrict-key=test', |
| 178 |
'--no-sync', |
| 179 |
'--no-unlogged-table-data', |
| 180 |
'--file' => $dump1, |
| 181 |
'--dbname' => $pitr1->connstr('postgres'), |
| 182 |
], |
| 183 |
'dump from PITR 1'); |
| 184 |
$pitr2->command_ok( |
| 185 |
[ |
| 186 |
'pg_dumpall', |
| 187 |
'--restrict-key=test', |
| 188 |
'--no-sync', |
| 189 |
'--no-unlogged-table-data', |
| 190 |
'--file' => $dump2, |
| 191 |
'--dbname' => $pitr2->connstr('postgres'), |
| 192 |
], |
| 193 |
'dump from PITR 2'); |
| 194 |
|
| 195 |
# Compare the two dumps, there should be no differences other than |
| 196 |
# the tablespace paths. |
| 197 |
compare_files( |
| 198 |
$dump1, $dump2, |
| 199 |
"contents of dumps match for both PITRs", |
| 200 |
sub { |
| 201 |
s{create tablespace .* location .*\btspitr\K[12]}{N}i for @_; |
| 202 |
return $_[0] ne $_[1]; |
| 203 |
}); |
| 204 |
|
| 205 |
done_testing(); |