|
| 1 | +# Test for point-in-time-recovery (PITR) with prepared transactions |
| 2 | +use strict; |
| 3 | +use warnings; |
| 4 | +use PostgresNode; |
| 5 | +use TestLib; |
| 6 | +use Test::More tests => 1; |
| 7 | +use File::Compare; |
| 8 | + |
| 9 | +# Initialize and start primary node with WAL archiving |
| 10 | +my $node_primary = get_new_node('primary'); |
| 11 | +$node_primary->init(has_archiving => 1); |
| 12 | +$node_primary->append_conf( |
| 13 | + 'postgresql.conf', qq{ |
| 14 | +max_wal_senders = 10 |
| 15 | +wal_level = 'replica' |
| 16 | +max_prepared_transactions = 10}); |
| 17 | +$node_primary->start; |
| 18 | + |
| 19 | +# Take backup |
| 20 | +my $backup_name = 'my_backup'; |
| 21 | +$node_primary->backup($backup_name); |
| 22 | + |
| 23 | +# Initialize node for PITR targeting a very specific restore point, just |
| 24 | +# after a PREPARE TRANSACTION is issued so as we finish with a promoted |
| 25 | +# node where this 2PC transaction needs an explicit COMMIT PREPARED. |
| 26 | +my $node_pitr = get_new_node('node_pitr'); |
| 27 | +$node_pitr->init_from_backup( |
| 28 | + $node_primary, $backup_name, |
| 29 | + standby => 0, |
| 30 | + has_restoring => 1); |
| 31 | +$node_pitr->append_conf( |
| 32 | + 'postgresql.conf', qq{ |
| 33 | +max_prepared_transactions = 10 |
| 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; |
0 commit comments