| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Test for point-in-time recovery (PITR) with prepared transactions |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
use File::Compare; |
| 11 |
|
| 12 |
# Initialize and start primary node with WAL archiving |
| 13 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 14 |
$node_primary->init(has_archiving => 1, allows_streaming => 1); |
| 15 |
$node_primary->append_conf( |
| 16 |
'postgresql.conf', qq{ |
| 17 |
max_prepared_transactions = 10}); |
| 18 |
$node_primary->start; |
| 19 |
|
| 20 |
# Take backup |
| 21 |
my $backup_name = 'my_backup'; |
| 22 |
$node_primary->backup($backup_name); |
| 23 |
|
| 24 |
# Initialize node for PITR targeting a very specific restore point, just |
| 25 |
# after a PREPARE TRANSACTION is issued so as we finish with a promoted |
| 26 |
# node where this 2PC transaction needs an explicit COMMIT PREPARED. |
| 27 |
my $node_pitr = PostgreSQL::Test::Cluster->new('node_pitr'); |
| 28 |
$node_pitr->init_from_backup( |
| 29 |
$node_primary, $backup_name, |
| 30 |
standby => 0, |
| 31 |
has_restoring => 1); |
| 32 |
$node_pitr->append_conf( |
| 33 |
'postgresql.conf', qq{ |
| 34 |
recovery_target_name = 'rp' |
| 35 |
recovery_target_action = 'promote'}); |
| 36 |
|
| 37 |
# Workload with a prepared transaction and the target restore point. |
| 38 |
$node_primary->psql( |
| 39 |
'postgres', qq{ |
| 40 |
CREATE TABLE foo(i int); |
| 41 |
BEGIN; |
| 42 |
INSERT INTO foo VALUES(1); |
| 43 |
PREPARE TRANSACTION 'fooinsert'; |
| 44 |
SELECT pg_create_restore_point('rp'); |
| 45 |
INSERT INTO foo VALUES(2); |
| 46 |
}); |
| 47 |
|
| 48 |
# Find next WAL segment to be archived |
| 49 |
my $walfile_to_be_archived = $node_primary->safe_psql('postgres', |
| 50 |
"SELECT pg_walfile_name(pg_current_wal_lsn());"); |
| 51 |
|
| 52 |
# Make WAL segment eligible for archival |
| 53 |
$node_primary->safe_psql('postgres', 'SELECT pg_switch_wal()'); |
| 54 |
|
| 55 |
# Wait until the WAL segment has been archived. |
| 56 |
my $archive_wait_query = |
| 57 |
"SELECT '$walfile_to_be_archived' <= last_archived_wal FROM pg_stat_archiver;"; |
| 58 |
$node_primary->poll_query_until('postgres', $archive_wait_query) |
| 59 |
or die "Timed out while waiting for WAL segment to be archived"; |
| 60 |
my $last_archived_wal_file = $walfile_to_be_archived; |
| 61 |
|
| 62 |
# Now start the PITR node. |
| 63 |
$node_pitr->start; |
| 64 |
|
| 65 |
# Wait until the PITR node exits recovery. |
| 66 |
$node_pitr->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';") |
| 67 |
or die "Timed out while waiting for PITR promotion"; |
| 68 |
|
| 69 |
# Commit the prepared transaction in the latest timeline and check its |
| 70 |
# result. There should only be one row in the table, coming from the |
| 71 |
# prepared transaction. The row from the INSERT after the restore point |
| 72 |
# should not show up, since our recovery target was older than the second |
| 73 |
# INSERT done. |
| 74 |
$node_pitr->psql('postgres', qq{COMMIT PREPARED 'fooinsert';}); |
| 75 |
my $result = $node_pitr->safe_psql('postgres', "SELECT * FROM foo;"); |
| 76 |
is($result, qq{1}, "check table contents after COMMIT PREPARED"); |
| 77 |
|
| 78 |
# Insert more data and do a checkpoint. These should be generated on the |
| 79 |
# timeline chosen after the PITR promotion. |
| 80 |
$node_pitr->psql( |
| 81 |
'postgres', qq{ |
| 82 |
INSERT INTO foo VALUES(3); |
| 83 |
CHECKPOINT; |
| 84 |
}); |
| 85 |
|
| 86 |
# Enforce recovery, the checkpoint record generated previously should |
| 87 |
# still be found. |
| 88 |
$node_pitr->stop('immediate'); |
| 89 |
$node_pitr->start; |
| 90 |
|
| 91 |
done_testing(); |