# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group # # src/test/recovery/t/506_cfs_pitr.pl # # Point-in-time and archive recovery of a cluster with CFS-compressed # relations. # # A base backup is taken while CFS data is present, then the WAL written # after the backup is replayed from the archive by restored nodes. The # scenarios cover recovery to a named restore point, replay of a prepared # transaction into compressed storage, plain archive recovery, the # different recovery target types, restartpoint cycles on a standby, and # recovery across timelines. In every scenario the CFS tables must # contain exactly the rows committed up to the recovery target, read back # through the CFS decompression path, and the sidecar files # (.cfp/.cfsmap/.cfsjrn) must pass the structural pg_cfs_check(). use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; # Create and start a primary node with WAL archiving. The whole test is # skipped when the server was built without LZ4 support, because the CFS # tables below need a compression algorithm. sub init_primary_with_archive { my ($name, $extra_conf) = @_; my $node = PostgreSQL::Test::Cluster->new($name); $node->init(has_archiving => 1, allows_streaming => 1); $node->append_conf('postgresql.conf', $extra_conf) if defined $extra_conf; $node->start; my $configure = $node->safe_psql('postgres', "SELECT setting FROM pg_config WHERE name = 'CONFIGURE'"); if ($configure !~ /--with-lz4/) { plan skip_all => 'LZ4 support not built, skipping CFS recovery tests'; } return $node; } # ---------------------------------------------------------------------- # PITR to a named restore point: the restored node must show exactly the # rows committed before the restore point, read them through the CFS # path, and accept new CFS writes on its new timeline. # ---------------------------------------------------------------------- { my $primary = init_primary_with_archive('cfs_pitr_primary'); # CFS table with a first batch of rows, captured by the base backup. $primary->safe_psql('postgres', "CREATE TABLE cfs_pitr (id int, payload text) WITH (algorithm = 'lz4')" ); $primary->safe_psql('postgres', "INSERT INTO cfs_pitr SELECT g, repeat('a' || g, 100) " . "FROM generate_series(1, 300) g"); $primary->safe_psql('postgres', "CHECKPOINT"); $primary->backup('my_backup'); # Second batch, then the restore point, then a third batch that must # NOT be visible after PITR. $primary->safe_psql('postgres', "INSERT INTO cfs_pitr SELECT g, repeat('b' || g, 100) " . "FROM generate_series(301, 600) g"); $primary->safe_psql('postgres', "SELECT pg_create_restore_point('cfs_rp')"); $primary->safe_psql('postgres', "INSERT INTO cfs_pitr SELECT g, repeat('c' || g, 100) " . "FROM generate_series(601, 900) g"); # Expected state at the restore point. my $expected_checksum = $primary->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) " . "FROM cfs_pitr WHERE id <= 600"); # Archive the WAL up to the current point. my $walfile_to_be_archived = $primary->safe_psql('postgres', "SELECT pg_walfile_name(pg_current_wal_lsn())"); $primary->safe_psql('postgres', "SELECT pg_switch_wal()"); $primary->poll_query_until('postgres', "SELECT '$walfile_to_be_archived' <= last_archived_wal " . "FROM pg_stat_archiver") or die "Timed out while waiting for WAL segment to be archived"; # Restore node targeting the restore point. my $pitr = PostgreSQL::Test::Cluster->new('cfs_pitr_node'); $pitr->init_from_backup( $primary, 'my_backup', standby => 0, has_restoring => 1); $pitr->append_conf( 'postgresql.conf', qq{ recovery_target_name = 'cfs_rp' recovery_target_action = 'promote'}); $pitr->start; $pitr->poll_query_until('postgres', "SELECT NOT pg_is_in_recovery()") or die "Timed out while waiting for PITR promotion"; # Exactly the first two batches must be present, read via the CFS # path. my $count = $pitr->safe_psql('postgres', "SELECT count(*) FROM cfs_pitr"); is($count, '600', 'PITR stops at the restore point for the CFS table'); my $checksum = $pitr->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM cfs_pitr"); is($checksum, $expected_checksum, 'CFS contents at the restore point match the primary'); my $fsck = $pitr->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('cfs_pitr')"); is($fsck, 't', 'pg_cfs_check passes on the PITR node'); # The PITR node must accept new CFS writes on its new timeline. $pitr->safe_psql('postgres', "INSERT INTO cfs_pitr SELECT g, repeat('d' || g, 100) " . "FROM generate_series(901, 1000) g"); $pitr->safe_psql('postgres', "CHECKPOINT"); my $after_count = $pitr->safe_psql('postgres', "SELECT count(*) FROM cfs_pitr"); is($after_count, '700', 'CFS writes work on the promoted PITR node'); $pitr->stop('fast'); $primary->stop('fast'); } # ---------------------------------------------------------------------- # PITR targeting a restore point taken after PREPARE TRANSACTION on a # CFS table. Recovery must replay the prepared transaction into # compressed storage, and COMMIT PREPARED after promotion must see the # data through CFS decompression. # ---------------------------------------------------------------------- { my $node_primary = init_primary_with_archive('prep_primary', "max_prepared_transactions = 10"); # Take backup my $backup_name = 'my_backup'; $node_primary->backup($backup_name); # PITR node targeting a restore point after PREPARE TRANSACTION my $node_pitr = PostgreSQL::Test::Cluster->new('prep_pitr'); $node_pitr->init_from_backup( $node_primary, $backup_name, standby => 0, has_restoring => 1); $node_pitr->append_conf( 'postgresql.conf', qq{ recovery_target_name = 'rp' recovery_target_action = 'promote'}); # Workload with a prepared transaction on a CFS table and the restore # point $node_primary->psql( 'postgres', qq{ CREATE TABLE foo(i int) WITH (algorithm = 'lz4'); BEGIN; INSERT INTO foo VALUES(1); PREPARE TRANSACTION 'fooinsert'; SELECT pg_create_restore_point('rp'); INSERT INTO foo VALUES(2); }); # Switch WAL and wait for archiving my $walfile = $node_primary->safe_psql('postgres', "SELECT pg_walfile_name(pg_current_wal_lsn());"); $node_primary->safe_psql('postgres', 'SELECT pg_switch_wal()'); $node_primary->poll_query_until('postgres', "SELECT '$walfile' <= last_archived_wal FROM pg_stat_archiver;") or die "Timed out waiting for WAL archival"; # Start the PITR node and wait for promotion $node_pitr->start; $node_pitr->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';") or die "Timed out waiting for PITR promotion"; # Commit the prepared transaction — it was replayed into CFS storage $node_pitr->psql('postgres', qq{COMMIT PREPARED 'fooinsert';}); # Only the row from the prepared transaction should exist (restore # point was before the second INSERT). my $result = $node_pitr->safe_psql('postgres', "SELECT * FROM foo ORDER BY i;"); is($result, '1', 'PITR CFS table has only the prepared-xact row'); # Verify structural integrity of the CFS relation after recovery $result = $node_pitr->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('foo'::regclass, true);"); is($result, 't', 'CFS sidecars are structurally valid after PITR recovery'); $node_pitr->stop; $node_primary->stop; } # ---------------------------------------------------------------------- # Plain archive recovery: a CFS table loaded before and after the base # backup must survive backup-plus-archive recovery with its contents and # sidecars intact. # ---------------------------------------------------------------------- { my $node_primary = init_primary_with_archive('arch_primary'); # Create a CFS table with data and record checksum $node_primary->safe_psql( 'postgres', qq{ CREATE TABLE cfs_arch (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO cfs_arch SELECT g, md5(g::text) FROM generate_series(1, 500) g; }); $node_primary->safe_psql('postgres', 'CHECKPOINT;'); my $checksum_before = $node_primary->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM cfs_arch"); # Take a base backup my $backup_name = 'my_backup'; $node_primary->backup($backup_name); # Insert more data after the backup (will need WAL replay) $node_primary->safe_psql('postgres', "INSERT INTO cfs_arch SELECT g, md5(g::text) FROM generate_series(501, 1000) g;" ); my $checksum_full = $node_primary->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM cfs_arch"); # Switch WAL to archive everything $node_primary->safe_psql('postgres', 'SELECT pg_switch_wal()'); # Wait for WAL archiving $node_primary->poll_query_until('postgres', "SELECT last_archived_wal IS NOT NULL FROM pg_stat_archiver;") or die "Timed out waiting for WAL archival"; # Create a restore node from backup + archive my $node_restore = PostgreSQL::Test::Cluster->new('arch_restore'); $node_restore->init_from_backup( $node_primary, $backup_name, standby => 0, has_restoring => 1); $node_restore->start; # Wait for recovery to complete $node_restore->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';") or die "Timed out waiting for restore promotion"; # Verify the full dataset survived recovery (WAL replay into CFS # storage) my $checksum_after = $node_restore->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM cfs_arch;"); is($checksum_after, $checksum_full, 'CFS table data intact after archive recovery (WAL replay into CFS)'); # Structural check my $result = $node_restore->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('cfs_arch'::regclass, true);"); is($result, 't', 'CFS sidecars valid after archive recovery'); $node_restore->stop; $node_primary->stop; } # ---------------------------------------------------------------------- # Different recovery target types (restore point name, transaction id) # on a CFS table. At each target the table must contain exactly the # rows committed up to that WAL position. # ---------------------------------------------------------------------- { my $node_primary = init_primary_with_archive('tgt_primary'); # CFS table with an initial batch $node_primary->safe_psql( 'postgres', qq{ CREATE TABLE cfs_targets (id int, batch int) WITH (algorithm = 'lz4'); INSERT INTO cfs_targets VALUES (1, 1); CHECKPOINT; }); my $backup_name = 'my_backup'; $node_primary->backup($backup_name); # Insert batches and record the XID / LSN / restore point for each $node_primary->safe_psql('postgres', "INSERT INTO cfs_targets VALUES (2, 2);"); my $xid2 = $node_primary->safe_psql('postgres', "SELECT txid_current()::bigint::text;"); my $lsn2 = $node_primary->safe_psql('postgres', "SELECT pg_current_wal_lsn();"); $node_primary->safe_psql('postgres', "SELECT pg_create_restore_point('rp_batch2');"); $node_primary->safe_psql('postgres', "INSERT INTO cfs_targets VALUES (3, 3);"); # Archive WAL $node_primary->safe_psql('postgres', 'SELECT pg_switch_wal()'); $node_primary->poll_query_until('postgres', "SELECT last_archived_wal IS NOT NULL FROM pg_stat_archiver;") or die "Timed out waiting for WAL archival"; # --- Target 1: recovery_target_name = 'rp_batch2' --- my $node_name = PostgreSQL::Test::Cluster->new('target_name'); $node_name->init_from_backup( $node_primary, $backup_name, standby => 0, has_restoring => 1); $node_name->append_conf( 'postgresql.conf', qq{ recovery_target_name = 'rp_batch2' recovery_target_action = 'promote'}); $node_name->start; $node_name->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';") or die "Timed out waiting for target_name promotion"; my $result = $node_name->safe_psql('postgres', "SELECT count(*) FROM cfs_targets;"); is($result, '2', 'CFS recovery target by name: 2 rows visible (batch 3 not yet committed)' ); $result = $node_name->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('cfs_targets'::regclass, true);"); is($result, 't', 'CFS sidecars valid after recovery target by name'); $node_name->stop; # --- Target 2: recovery_target_xid --- my $node_xid = PostgreSQL::Test::Cluster->new('target_xid'); $node_xid->init_from_backup( $node_primary, $backup_name, standby => 0, has_restoring => 1); $node_xid->append_conf( 'postgresql.conf', qq{ recovery_target_xid = '$xid2' recovery_target_action = 'promote'}); $node_xid->start; $node_xid->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';") or die "Timed out waiting for target_xid promotion"; $result = $node_xid->safe_psql('postgres', "SELECT count(*) FROM cfs_targets;"); is($result, '2', 'CFS recovery target by xid: 2 rows visible'); $result = $node_xid->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('cfs_targets'::regclass, true);"); is($result, 't', 'CFS sidecars valid after recovery target by xid'); $node_xid->stop; $node_primary->stop; } # ---------------------------------------------------------------------- # Repeated restartpoint cycles on a standby with a CFS table. Each # restartpoint re-snapshots the .cfsmap, so after multiple cycles there # must be no resource leak (orphaned .cfsmap snapshots, stale journal # records, or growing .cfp files) and the standby contents must match # the primary exactly. # ---------------------------------------------------------------------- { my $node_primary = init_primary_with_archive('rp_primary', "checkpoint_timeout = 30s"); # CFS table with data $node_primary->safe_psql( 'postgres', qq{ CREATE TABLE cfs_restart (id int, payload text) WITH (algorithm = 'lz4'); INSERT INTO cfs_restart SELECT g, md5(g::text) FROM generate_series(1, 200) g; CHECKPOINT; }); my $backup_name = 'my_backup'; $node_primary->backup($backup_name); # Standby my $node_standby = PostgreSQL::Test::Cluster->new('rp_standby'); $node_standby->init_from_backup($node_primary, $backup_name, has_streaming => 1); $node_standby->append_conf('postgresql.conf', "checkpoint_timeout = 30s"); $node_standby->start; # Wait for catch-up $node_primary->wait_for_replay_catchup($node_standby); # Record initial checksum my $checksum = $node_standby->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM cfs_restart;"); # Run several cycles of: primary writes → standby replays → standby # checkpoint (each checkpoint on standby triggers a restartpoint that # re-snapshots .cfsmap) for my $round (1 .. 5) { $node_primary->safe_psql('postgres', "INSERT INTO cfs_restart VALUES (1000 + $round, md5('${round}text'));" ); $node_primary->wait_for_replay_catchup($node_standby); $node_standby->safe_psql('postgres', 'CHECKPOINT;'); } # Verify data integrity after 5 restartpoint cycles my $final_count = $node_standby->safe_psql('postgres', "SELECT count(*) FROM cfs_restart;"); is($final_count, '205', 'CFS table has 205 rows after 5 restartpoint cycles on standby'); my $result = $node_standby->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('cfs_restart'::regclass, true);"); is($result, 't', 'CFS sidecars valid after 5 restartpoint cycles (no leak)'); # Verify the standby data matches the primary exactly $node_primary->safe_psql('postgres', 'CHECKPOINT;'); my $primary_md5 = $node_primary->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM cfs_restart;"); my $standby_md5 = $node_standby->safe_psql('postgres', "SELECT md5(string_agg(payload, '' ORDER BY id)) FROM cfs_restart;"); is($standby_md5, $primary_md5, 'Standby CFS data matches primary after restartpoint cycles'); $node_standby->stop; $node_primary->stop; } # ---------------------------------------------------------------------- # PITR across intersecting timelines: after a promote creates a new # timeline, the PITR node recovered to a point on the earlier timeline # must see the correct CFS data and accept new writes on the new # timeline. # ---------------------------------------------------------------------- { my $node_primary = init_primary_with_archive('tl_primary'); # CFS table $node_primary->safe_psql( 'postgres', qq{ CREATE TABLE cfs_tl (id int, val int) WITH (algorithm = 'lz4'); INSERT INTO cfs_tl VALUES (1, 1); CHECKPOINT; }); my $backup_name = 'my_backup'; $node_primary->backup($backup_name); # Insert more, create a restore point $node_primary->safe_psql('postgres', "INSERT INTO cfs_tl VALUES (2, 2);"); $node_primary->safe_psql('postgres', "SELECT pg_create_restore_point('tl1_rp');"); $node_primary->safe_psql('postgres', "INSERT INTO cfs_tl VALUES (3, 3);"); # Archive WAL $node_primary->safe_psql('postgres', 'SELECT pg_switch_wal()'); $node_primary->poll_query_until('postgres', "SELECT last_archived_wal IS NOT NULL FROM pg_stat_archiver;") or die "Timed out waiting for WAL archival"; # PITR to the restore point (timeline 1) my $node_pitr = PostgreSQL::Test::Cluster->new('tl_pitr'); $node_pitr->init_from_backup( $node_primary, $backup_name, standby => 0, has_restoring => 1); $node_pitr->append_conf( 'postgresql.conf', qq{ recovery_target_name = 'tl1_rp' recovery_target_action = 'promote'}); $node_pitr->start; $node_pitr->poll_query_until('postgres', "SELECT pg_is_in_recovery() = 'f';") or die "Timed out waiting for PITR promotion"; # After promotion, PITR is on a new timeline (2) but recovered to the # point where only rows 1 and 2 exist. my $result = $node_pitr->safe_psql('postgres', "SELECT count(*) FROM cfs_tl;"); is($result, '2', 'PITR on new timeline sees 2 CFS rows (target before row 3)'); # Insert on the new timeline $node_pitr->safe_psql('postgres', "INSERT INTO cfs_tl VALUES (4, 4);"); $node_pitr->safe_psql('postgres', 'CHECKPOINT;'); $result = $node_pitr->safe_psql('postgres', "SELECT ok FROM pg_cfs_check('cfs_tl'::regclass, true);"); is($result, 't', 'CFS sidecars valid after timeline switch + new writes'); $result = $node_pitr->safe_psql('postgres', "SELECT count(*) FROM cfs_tl;"); is($result, '3', 'PITR CFS table has 3 rows after timeline-2 insert'); $node_pitr->stop; $node_primary->stop; } done_testing();