On Fri, Nov 20, 2026 at 2:48 AM Daniel Gustafsson <[email protected]> wrote:
> > On 16 Nov 2026, at 20:13, Stepan Neretin <[email protected]> wrote: > > > Attached is a small rebase on current master, with the TAP coverage > > folded into "005_same_timeline.pl". No behaviour change outside > > assertion builds. > > use warnings FATAL => 'all'; > +use PostgreSQL::Test::Cluster; > use PostgreSQL::Test::Utils; > > The proposed TAP test breaks the current model where tests use the > infrastructure in the RewindTest module. Is there a reason why this test > should be different? > > > Does this still seem worth committing? > > From reading over the thread in which Heikki concluded it was a bogus > Assert, > I'd say it's still worth committing. I do think the TAP test should be in > line > with the other pg_rewind tests though. > > -- > Daniel Gustafsson > Hi Daniel, Thanks for looking. Folding this into 005 was a mistake. RewindTest is the usual primary/standby/promote/rewind cycle, and this needs an older base backup as the target. Same reason 008 skips it. v2 leaves 005 alone and puts the coverage in 012_target_is_ancestor.pl, along the lines of Heikki's earlier test. Assert fix itself is unchanged. Attached. Regards, Stepan Neretin
From 1de65d7771acf90bcc032a87690017608efc1e6f Mon Sep 17 00:00:00 2001 From: Stepan Neretin <[email protected]> Date: Fri, 20 Nov 2026 14:29:34 +0700 Subject: [PATCH v2 1/2] Remove bogus Assert in pg_rewind ancestor check When the target is a direct ancestor of the source, its WAL can end before the calculated divergence LSN, for example after an older base backup of the primary. That used to trip Assert(target_wal_endrec == divergerec) on assertion builds. Drop it; we already report "no rewind required". Discussion: https://postgr.es/m/CA+Yyo5Raa0ePMeN64EPEBwt5n9T2FUr=+o3-lcnbo_5cjua...@mail.gmail.com --- src/bin/pg_rewind/pg_rewind.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c index d2521dab333..c4ac20be5e0 100644 --- a/src/bin/pg_rewind/pg_rewind.c +++ b/src/bin/pg_rewind/pg_rewind.c @@ -447,16 +447,9 @@ main(int argc, char **argv) * in the target that needs rewinding. */ if (target_wal_endrec > divergerec) - { rewind_needed = true; - } else - { - /* the last common checkpoint record must be part of target WAL */ - Assert(target_wal_endrec == divergerec); - rewind_needed = false; - } } if (!rewind_needed) -- 2.56.0
From 2d7636a2cbb79a48c0b989a4e4e16c21994c495d Mon Sep 17 00:00:00 2001 From: Stepan Neretin <[email protected]> Date: Fri, 20 Nov 2026 14:29:34 +0700 Subject: [PATCH v2 2/2] Add TAP test for ancestor no-rewind cases Cover targets restored from backups before and at the timeline switchpoint ("no rewind required"), plus a divergent target. Discussion: https://postgr.es/m/CA+Yyo5Raa0ePMeN64EPEBwt5n9T2FUr=+o3-lcnbo_5cjua...@mail.gmail.com --- src/bin/pg_rewind/meson.build | 1 + src/bin/pg_rewind/t/012_target_is_ancestor.pl | 125 ++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/bin/pg_rewind/t/012_target_is_ancestor.pl diff --git a/src/bin/pg_rewind/meson.build b/src/bin/pg_rewind/meson.build index 52a6ab0a515..b093cfa63fb 100644 --- a/src/bin/pg_rewind/meson.build +++ b/src/bin/pg_rewind/meson.build @@ -45,6 +45,7 @@ tests += { 't/009_growing_files.pl', 't/010_keep_recycled_wals.pl', 't/011_wal_copy.pl', + 't/012_target_is_ancestor.pl', ], }, } diff --git a/src/bin/pg_rewind/t/012_target_is_ancestor.pl b/src/bin/pg_rewind/t/012_target_is_ancestor.pl new file mode 100644 index 00000000000..4d06face8ce --- /dev/null +++ b/src/bin/pg_rewind/t/012_target_is_ancestor.pl @@ -0,0 +1,125 @@ +# Copyright (c) 2021-2026, PostgreSQL Global Development Group + +# +# Test cases where the target is a direct ancestor of the source. +# +# We set up two timelines like this: +# +# -------------- TLI 2 (source) +# / +# ---A----B-------C------- TLI 1 +# +# +# On TLI 1, we take a backup at points A, B and C. We use a server +# restored from each of those points as the target, and a primary +# server running on TLI 2 as the source. +# +# - A is a direct ancestor of the source, so no rewind is required. +# Target WAL ends before the calculated divergence LSN; that used +# to trip Assert(target_wal_endrec == divergerec). +# - B is also a direct ancestor, at the divergence point. No rewind. +# - C is not a direct ancestor, so rewind is required. +# + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +use File::Copy; + +my $tmp_folder = PostgreSQL::Test::Utils::tempdir; + +my $orig_node = PostgreSQL::Test::Cluster->new('orig_node'); +$orig_node->init(allows_streaming => 1); +$orig_node->append_conf( + 'postgresql.conf', qq( +wal_log_hints = on +)); +$orig_node->start; +$orig_node->safe_psql('postgres', 'CREATE TABLE test_tab (t TEXT)'); +$orig_node->safe_psql('postgres', + "INSERT INTO test_tab VALUES ('at the beginning')"); +$orig_node->stop; +$orig_node->backup_fs_cold('backup_A'); + +$orig_node->start; +$orig_node->safe_psql('postgres', + "INSERT INTO test_tab VALUES ('after backup A')"); +$orig_node->stop; +$orig_node->backup_fs_cold('backup_B'); + +# Restore backup B to create Timeline 2 +my $node_2 = PostgreSQL::Test::Cluster->new('node_2'); +$node_2->init_from_backup($orig_node, 'backup_B', has_streaming => 1); +$node_2->start; +$node_2->promote; +$node_2->safe_psql('postgres', "INSERT INTO test_tab VALUES ('in node_2')"); +$node_2->stop; + +$orig_node->start; +$orig_node->safe_psql('postgres', + "INSERT INTO test_tab VALUES ('this will be lost on rewind')"); +$orig_node->stop; +$orig_node->backup_fs_cold('backup_C'); + +my $source_node = $node_2; +my $source_pgdata = $source_node->data_dir; + +sub rewind_from_backup +{ + my ($backup, $expected_stderr) = @_; + + my $target_node = PostgreSQL::Test::Cluster->new('restored_' . $backup); + $target_node->init_from_backup($orig_node, $backup, has_streaming => 1); + my $target_pgdata = $target_node->data_dir; + + # Keep a temporary postgresql.conf or it would be overwritten during the rewind. + copy("$target_pgdata/postgresql.conf", + "$tmp_folder/postgresql.conf.tmp"); + + command_checks_all( + [ + 'pg_rewind', + '--debug', + '--no-sync', + '--source-pgdata' => $source_pgdata, + '--target-pgdata' => $target_pgdata, + ], + 0, + [], + [$expected_stderr], + "pg_rewind on node restored from $backup"); + + # Now move back postgresql.conf with old settings + move("$tmp_folder/postgresql.conf.tmp", + "$target_pgdata/postgresql.conf"); + + my $node_2_connstr = $node_2->connstr(); + $target_node->append_conf( + 'postgresql.conf', qq( +primary_conninfo = '$node_2_connstr' +)); + $target_node->set_standby_mode(); + $target_node->start; + + $node_2->start; + $node_2->wait_for_catchup($target_node->name); + my $result = + $target_node->safe_psql('postgres', "SELECT * FROM test_tab"); + is( $result, qq(at the beginning +after backup A +in node_2), + "check query after rewind from $backup"); + $target_node->stop; + + $node_2->stop; +} + +rewind_from_backup('backup_C', + qr/pg_rewind: rewinding from last common checkpoint at .* on timeline 1/); +rewind_from_backup('backup_B', qr/pg_rewind: no rewind required/); +rewind_from_backup('backup_A', qr/pg_rewind: no rewind required/); + +done_testing(); -- 2.56.0
