# Copyright (c) 2026, Kirillov Vyacheslav by AI, QDB Development Group # # pg_rewind on CFS-compressed relations. # # The rewind decision logic ("copy whole file / copy tail / truncate") # works on relation files; the CFS sidecars (.cfp/.cfsmap/.cfsjrn) must # follow the same decisions as the main fork they describe. After every # rewind both the table rows and the structural integrity of the # sidecars (via pg_cfs_check) are verified. # # The first section exercises the basic scenarios (diverged inserts, # truncation in both directions, a dropped table) in all three rewind # modes. The second section rewinds a target whose WAL extends beyond # both the last common checkpoint and the divergence point, the case # where minRecoveryPoint must not be ignored. use strict; use warnings FATAL => 'all'; use PostgreSQL::Test::Cluster; use PostgreSQL::Test::Utils; use Test::More; use File::Copy; use FindBin; use lib $FindBin::RealBin; use RewindTest; #----------------------------------------------------------------------- # Basic rewind scenarios for CFS relations #----------------------------------------------------------------------- { sub run_test { my $test_mode = shift; RewindTest::setup_cluster($test_mode); RewindTest::start_primary(); # Create a test table and insert a row in primary. primary_psql("CREATE TABLE tbl1 (d text) WITH (algorithm = 'lz4')"); primary_psql("INSERT INTO tbl1 VALUES ('in primary')"); # This test table will be used to test truncation, i.e. the table # is extended in the old primary after promotion primary_psql( "CREATE TABLE trunc_tbl (d text) WITH (algorithm = 'lz4')"); primary_psql("INSERT INTO trunc_tbl VALUES ('in primary')"); # This test table will be used to test the "copy-tail" case, i.e. the # table is truncated in the old primary after promotion primary_psql( "CREATE TABLE tail_tbl (id integer, d text) WITH (algorithm = 'zstd')" ); primary_psql("INSERT INTO tail_tbl VALUES (0, 'in primary')"); # This test table is dropped in the old primary after promotion. primary_psql( "CREATE TABLE drop_tbl (d text) WITH (algorithm = 'lz4')"); primary_psql("INSERT INTO drop_tbl VALUES ('in primary')"); primary_psql("CHECKPOINT"); RewindTest::create_standby($test_mode); # Insert additional data on primary that will be replicated to standby primary_psql( "INSERT INTO tbl1 values ('in primary, before promotion')"); primary_psql( "INSERT INTO trunc_tbl values ('in primary, before promotion')"); primary_psql( "INSERT INTO tail_tbl SELECT g, 'in primary, before promotion: ' || g FROM generate_series(1, 10000) g" ); primary_psql('CHECKPOINT'); RewindTest::promote_standby(); # Insert a row in the old primary. This causes the primary and standby # to have "diverged", it's no longer possible to just apply the # standby's logs over primary directory - you need to rewind. primary_psql( "INSERT INTO tbl1 VALUES ('in primary, after promotion')"); # Also insert a new row in the standby, which won't be present in the # old primary. standby_psql( "INSERT INTO tbl1 VALUES ('in standby, after promotion')"); # Insert enough rows to trunc_tbl to extend the file. pg_rewind should # truncate it back to the old size. primary_psql( "INSERT INTO trunc_tbl SELECT 'in primary, after promotion: ' || g FROM generate_series(1, 10000) g" ); # Truncate tail_tbl. pg_rewind should copy back the truncated part # (We cannot use an actual TRUNCATE command here, as that creates a # whole new relfilenode) primary_psql("DELETE FROM tail_tbl WHERE id > 10"); primary_psql("VACUUM tail_tbl"); # Drop drop_tbl. pg_rewind should copy it back. primary_psql( "insert into drop_tbl values ('in primary, after promotion')"); primary_psql("DROP TABLE drop_tbl"); RewindTest::run_pg_rewind($test_mode); check_query( 'SELECT * FROM tbl1 ORDER BY d', qq(in primary in primary, before promotion in standby, after promotion ), 'tbl1 has the standby rows after rewind'); check_query( 'SELECT * FROM trunc_tbl ORDER BY d', qq(in primary in primary, before promotion ), 'trunc_tbl extension truncated back by rewind'); check_query( 'SELECT count(*) FROM tail_tbl', qq(10001 ), 'tail_tbl truncated tail copied back by rewind'); check_query( 'SELECT * FROM drop_tbl', qq(in primary ), 'dropped drop_tbl restored by rewind'); # The CFS sidecars must be structurally valid after the rewind. check_query( q{SELECT string_agg(ok::text, ',' ORDER BY relname) FROM (SELECT relname, (pg_cfs_check(oid, true)).ok FROM pg_class WHERE relname IN ('tbl1', 'trunc_tbl', 'tail_tbl', 'drop_tbl') AND relnamespace = 'public'::regnamespace) s}, qq(true,true,true,true ), 'CFS sidecars intact after rewind'); RewindTest::clean_rewind_test(); return; } # Run the test in all three modes run_test('local'); run_test('remote'); run_test('archive'); } #----------------------------------------------------------------------- # Rewind past minRecoveryPoint with CFS relations: last checkpoint on # the target TLI is an ancestor of the source TLI #----------------------------------------------------------------------- { # Test situation where a target data directory contains # WAL records beyond both the last checkpoint and the divergence # point: # # Target WAL (TLI 2): # # backup ... Checkpoint A ... INSERT 'rewind this' # (TLI 1 -> 2) # # ^ last common ^ minRecoveryPoint # checkpoint # # Source WAL (TLI 3): # # backup ... Checkpoint A ... Checkpoint B ... INSERT 'keep this' # (TLI 1 -> 2) (TLI 2 -> 3) # # # The last common checkpoint is Checkpoint A. But there is WAL on TLI 2 # after the last common checkpoint that needs to be rewound. We used to # have a bug where minRecoveryPoint was ignored, and pg_rewind concluded # that the target doesn't need rewinding in this scenario, because the # last checkpoint on the target TLI was an ancestor of the source TLI. # # After the rewind the rewound-away rows must be gone and the sidecar # files must match the source's state. # # This test does not make use of RewindTest as it requires three # nodes. my $tmp_folder = PostgreSQL::Test::Utils::tempdir; my $node_1 = PostgreSQL::Test::Cluster->new('node_1'); $node_1->init(allows_streaming => 1); $node_1->append_conf( 'postgresql.conf', qq( wal_keep_size='100 MB' )); $node_1->start; # Create a couple of test tables $node_1->safe_psql('postgres', q{CREATE TABLE public.foo (t TEXT) WITH (algorithm = 'lz4')}); $node_1->safe_psql('postgres', q{CREATE TABLE public.bar (t TEXT) WITH (algorithm = 'zstd')}); $node_1->safe_psql('postgres', "INSERT INTO public.bar VALUES ('in both')"); # # Create node_2 and node_3 as standbys following node_1 # my $backup_name = 'my_backup'; $node_1->backup($backup_name); my $node_2 = PostgreSQL::Test::Cluster->new('node_2'); $node_2->init_from_backup($node_1, $backup_name, has_streaming => 1); $node_2->start; my $node_3 = PostgreSQL::Test::Cluster->new('node_3'); $node_3->init_from_backup($node_1, $backup_name, has_streaming => 1); $node_3->start; # Wait until node 3 has connected and caught up $node_1->wait_for_catchup('node_3'); # # Swap the roles of node_1 and node_3, so that node_1 follows node_3. # $node_1->stop('fast'); $node_3->promote; # reconfigure node_1 as a standby following node_3 my $node_3_connstr = $node_3->connstr; $node_1->append_conf( 'postgresql.conf', qq( primary_conninfo='$node_3_connstr' )); $node_1->set_standby_mode(); $node_1->start(); # also reconfigure node_2 to follow node_3 $node_2->append_conf( 'postgresql.conf', qq( primary_conninfo='$node_3_connstr' )); $node_2->restart(); # # Promote node_1, to create a split-brain scenario. # # make sure node_1 is fully caught up with node_3 first $node_3->wait_for_catchup('node_1'); $node_1->promote; # # We now have a split-brain with two primaries. Insert a row on both to # demonstratively create a split brain. After the rewind, we should only # see the insert on 1, as the insert on node 3 is rewound away. # $node_1->safe_psql('postgres', "INSERT INTO public.foo (t) VALUES ('keep this')"); # 'bar' is unmodified in node 1, so it won't be overwritten by replaying the # WAL from node 1. $node_3->safe_psql('postgres', "INSERT INTO public.bar (t) VALUES ('rewind this')"); # Insert more rows in node 1, to bump up the XID counter. Otherwise, if # rewind doesn't correctly rewind the changes made on the other node, # we might fail to notice if the inserts are invisible because the XIDs # are not marked as committed. $node_1->safe_psql('postgres', "INSERT INTO public.foo (t) VALUES ('and this')"); $node_1->safe_psql('postgres', "INSERT INTO public.foo (t) VALUES ('and this too')"); # Wait for node 2 to catch up $node_2->poll_query_until('postgres', q|SELECT COUNT(*) > 1 FROM public.bar|, 't'); # At this point node_2 will shut down without a shutdown checkpoint, # but with WAL entries beyond the preceding shutdown checkpoint. $node_2->stop('fast'); $node_3->stop('fast'); my $node_2_pgdata = $node_2->data_dir; my $node_1_connstr = $node_1->connstr; # Keep a temporary postgresql.conf or it would be overwritten during the rewind. copy( "$node_2_pgdata/postgresql.conf", "$tmp_folder/node_2-postgresql.conf.tmp"); command_ok( [ 'pg_rewind', '--source-server' => $node_1_connstr, '--target-pgdata' => $node_2_pgdata, '--debug', ], 'run pg_rewind'); # Now move back postgresql.conf with old settings move( "$tmp_folder/node_2-postgresql.conf.tmp", "$node_2_pgdata/postgresql.conf"); $node_2->start; # Check contents of the test tables after rewind. The rows inserted in node 3 # before rewind should've been overwritten with the data from node 1. my $result; $result = $node_2->safe_psql('postgres', 'SELECT * FROM public.foo'); is( $result, qq(keep this and this and this too), 'foo keeps the rows from the new primary after rewind'); $result = $node_2->safe_psql('postgres', 'SELECT * FROM public.bar'); is($result, qq(in both), 'bar rewound away the split-brain row'); # The CFS sidecars of both tables must be structurally valid on the # rewound node. for my $rel ('public.foo', 'public.bar') { is( $node_2->safe_psql( 'postgres', "SELECT ok FROM pg_cfs_check('$rel'::regclass, true)"), 't', "$rel sidecars intact after rewind"); } } done_testing();