| 1 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 2 |
# |
| 3 |
# Test whether WAL summaries are complete such that incremental backup |
| 4 |
# can be performed after promoting a standby at an arbitrary LSN. |
| 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->append_conf('postgresql.conf', 'log_min_messages = debug1'); |
| 23 |
$node1->start; |
| 24 |
|
| 25 |
# Create a table and insert a test row into it. |
| 26 |
$node1->safe_psql('postgres', <<EOM); |
| 27 |
CREATE TABLE mytable (a int, b text); |
| 28 |
INSERT INTO mytable VALUES (1, 'avocado'); |
| 29 |
EOM |
| 30 |
|
| 31 |
# Take a full backup. |
| 32 |
my $backup1path = $node1->backup_dir . '/backup1'; |
| 33 |
$node1->command_ok( |
| 34 |
[ |
| 35 |
'pg_basebackup', |
| 36 |
'--pgdata' => $backup1path, |
| 37 |
'--no-sync', |
| 38 |
'--checkpoint' => 'fast', |
| 39 |
], |
| 40 |
"full backup from node1"); |
| 41 |
|
| 42 |
# Checkpoint and record LSN after. |
| 43 |
$node1->safe_psql('postgres', 'CHECKPOINT'); |
| 44 |
my $lsn = $node1->safe_psql('postgres', 'SELECT pg_current_wal_insert_lsn()'); |
| 45 |
|
| 46 |
# Insert a second row on the original node. |
| 47 |
$node1->safe_psql('postgres', <<EOM); |
| 48 |
INSERT INTO mytable VALUES (2, 'beetle'); |
| 49 |
EOM |
| 50 |
|
| 51 |
# Now create a second node. We want this to stream from the first node and |
| 52 |
# then stop recovery at some arbitrary LSN, not just when it hits the end of |
| 53 |
# WAL, so use a recovery target. |
| 54 |
my $node2 = PostgreSQL::Test::Cluster->new('node2'); |
| 55 |
$node2->init_from_backup($node1, 'backup1', has_streaming => 1); |
| 56 |
$node2->append_conf('postgresql.conf', <<EOM); |
| 57 |
recovery_target_lsn = '$lsn' |
| 58 |
recovery_target_action = 'pause' |
| 59 |
EOM |
| 60 |
$node2->start(); |
| 61 |
|
| 62 |
# Wait until recovery pauses, then promote. |
| 63 |
$node2->poll_query_until('postgres', |
| 64 |
"SELECT pg_get_wal_replay_pause_state() = 'paused';"); |
| 65 |
$node2->safe_psql('postgres', "SELECT pg_promote()"); |
| 66 |
|
| 67 |
# Once promotion occurs, insert a second row on the new node. |
| 68 |
$node2->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';"); |
| 69 |
$node2->safe_psql('postgres', <<EOM); |
| 70 |
INSERT INTO mytable VALUES (2, 'blackberry'); |
| 71 |
EOM |
| 72 |
|
| 73 |
# Now take an incremental backup. If WAL summarization didn't follow the |
| 74 |
# timeline change correctly, something should break at this point. |
| 75 |
my $backup2path = $node1->backup_dir . '/backup2'; |
| 76 |
$node2->command_ok( |
| 77 |
[ |
| 78 |
'pg_basebackup', |
| 79 |
'--pgdata' => $backup2path, |
| 80 |
'--no-sync', |
| 81 |
'--checkpoint' => 'fast', |
| 82 |
'--incremental' => $backup1path . '/backup_manifest', |
| 83 |
], |
| 84 |
"incremental backup from node2"); |
| 85 |
|
| 86 |
# Restore the incremental backup and use it to create a new node. |
| 87 |
my $node3 = PostgreSQL::Test::Cluster->new('node3'); |
| 88 |
$node3->init_from_backup($node1, 'backup2', |
| 89 |
combine_with_prior => ['backup1']); |
| 90 |
$node3->start(); |
| 91 |
|
| 92 |
done_testing(); |