From f447c576d580c52ca604658f416e2996d9aad052 Mon Sep 17 00:00:00 2001 From: Jan Nidzwetzki Date: Thu, 3 Dec 2026 12:08:04 +0100 Subject: [PATCH] pg_rewind: Add test for removal of the checkpoint's redo segment --- src/bin/pg_rewind/Makefile | 4 + src/bin/pg_rewind/meson.build | 4 + src/bin/pg_rewind/t/012_keep_redo_wals.pl | 144 ++++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/bin/pg_rewind/t/012_keep_redo_wals.pl diff --git a/src/bin/pg_rewind/Makefile b/src/bin/pg_rewind/Makefile index 32a35c57612..d23ec11b445 100644 --- a/src/bin/pg_rewind/Makefile +++ b/src/bin/pg_rewind/Makefile @@ -11,10 +11,14 @@ PGFILEDESC = "pg_rewind - synchronize a data directory with another one forked from" PGAPPICON = win32 +EXTRA_INSTALL = src/test/modules/injection_points + subdir = src/bin/pg_rewind top_builddir = ../../.. include $(top_builddir)/src/Makefile.global +export enable_injection_points + override CPPFLAGS := -I$(libpq_srcdir) -DFRONTEND $(CPPFLAGS) LDFLAGS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport) diff --git a/src/bin/pg_rewind/meson.build b/src/bin/pg_rewind/meson.build index 52a6ab0a515..7c6c8cd20ed 100644 --- a/src/bin/pg_rewind/meson.build +++ b/src/bin/pg_rewind/meson.build @@ -33,6 +33,9 @@ tests += { 'sd': meson.current_source_dir(), 'bd': meson.current_build_dir(), 'tap': { + 'env': { + 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no', + }, 'tests': [ 't/001_basic.pl', 't/002_databases.pl', @@ -45,6 +48,7 @@ tests += { 't/009_growing_files.pl', 't/010_keep_recycled_wals.pl', 't/011_wal_copy.pl', + 't/012_keep_redo_wals.pl', ], }, } diff --git a/src/bin/pg_rewind/t/012_keep_redo_wals.pl b/src/bin/pg_rewind/t/012_keep_redo_wals.pl new file mode 100644 index 00000000000..32ff2b85226 --- /dev/null +++ b/src/bin/pg_rewind/t/012_keep_redo_wals.pl @@ -0,0 +1,144 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group +# +# Test that pg_rewind keeps the target's WAL segments from the redo +# location of the last common checkpoint up to its checkpoint record, +# when they are in different segments and the source has removed them. +# + +use strict; +use warnings FATAL => 'all'; +use File::Copy; +use PostgreSQL::Test::Utils; +use Test::More; + +use FindBin; +use lib $FindBin::RealBin; +use RewindTest; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +RewindTest::setup_cluster(); +RewindTest::start_primary(); + +if (!$node_primary->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} +$node_primary->safe_psql('postgres', 'CREATE EXTENSION injection_points'); +RewindTest::primary_psql("CREATE TABLE t (a int)"); + +RewindTest::create_standby(); + +# Unlike the old primary, the new primary must not retain old WAL. +$node_standby->append_conf('postgresql.conf', 'wal_keep_size = 0'); +$node_standby->reload; + +# Last common checkpoint: stop it after its redo location has been +# determined and switch WAL twice, so that the checkpoint record ends up +# two segments after the redo location. See 050_redo_segment_missing.pl +# for the use of two injection points. +$node_primary->append_conf('postgresql.conf', 'log_checkpoints = on'); +$node_primary->reload; +$node_primary->safe_psql('postgres', + q{SELECT injection_points_attach('create-checkpoint-initial', 'wait')}); +$node_primary->safe_psql('postgres', + q{SELECT injection_points_attach('create-checkpoint-run', 'wait')}); + +my $checkpoint = $node_primary->background_psql('postgres'); +$checkpoint->query_until( + qr/starting_checkpoint/, + q(\echo starting_checkpoint +CHECKPOINT; +)); + +$node_primary->wait_for_event('checkpointer', 'create-checkpoint-initial'); +$node_primary->safe_psql('postgres', + q{SELECT injection_points_wakeup('create-checkpoint-initial')}); +$node_primary->wait_for_event('checkpointer', 'create-checkpoint-run'); + +RewindTest::primary_psql("INSERT INTO t VALUES (1)"); +RewindTest::primary_psql("SELECT pg_switch_wal()"); +RewindTest::primary_psql("INSERT INTO t VALUES (2)"); +RewindTest::primary_psql("SELECT pg_switch_wal()"); + +my $log_offset = -s $node_primary->logfile; +$node_primary->safe_psql('postgres', + q{SELECT injection_points_wakeup('create-checkpoint-run')}); +$node_primary->wait_for_log(qr/checkpoint complete/, $log_offset); +$checkpoint->quit; + +$node_primary->safe_psql('postgres', + q{SELECT injection_points_detach('create-checkpoint-initial')}); +$node_primary->safe_psql('postgres', + q{SELECT injection_points_detach('create-checkpoint-run')}); + +my $redo_walfile = $node_primary->safe_psql('postgres', + 'SELECT redo_wal_file FROM pg_control_checkpoint()'); +my $chkpt_walfile = $node_primary->safe_psql('postgres', + 'SELECT pg_walfile_name(checkpoint_lsn) FROM pg_control_checkpoint()'); +isnt($redo_walfile, $chkpt_walfile, + 'redo location and checkpoint record are in different segments'); + +RewindTest::primary_psql("INSERT INTO t VALUES (3)"); + +RewindTest::promote_standby(); + +# Diverge on the old primary. +RewindTest::primary_psql("INSERT INTO t VALUES (100)"); + +# The new primary removes the segments before its first checkpoint. +RewindTest::standby_psql("INSERT INTO t VALUES (200)"); +RewindTest::standby_psql("CHECKPOINT"); +ok( !-f $node_standby->data_dir . "/pg_wal/$redo_walfile", + 'source has removed the segment holding the redo location'); + +$node_primary->stop; +$node_standby->stop; + +# pg_rewind overwrites postgresql.conf with the source's copy, keep ours. +my $primary_pgdata = $node_primary->data_dir; +my $tmp_folder = PostgreSQL::Test::Utils::tempdir; +copy("$primary_pgdata/postgresql.conf", "$tmp_folder/postgresql.conf") + or die "could not copy postgresql.conf: $!"; + +command_ok( + [ + 'pg_rewind', '--debug', + '--source-pgdata' => $node_standby->data_dir, + '--target-pgdata' => $primary_pgdata, + '--no-sync', + ], + 'pg_rewind'); + +copy("$tmp_folder/postgresql.conf", "$primary_pgdata/postgresql.conf") + or die "could not copy postgresql.conf: $!"; + +ok(-f "$primary_pgdata/pg_wal/$redo_walfile", + 'segment holding the redo location kept on the target'); + +SKIP: +{ + # Without the segment the rewound node would wait for it forever. + skip 'redo segment missing', 1 + unless -f "$primary_pgdata/pg_wal/$redo_walfile"; + + $node_standby->start; + my $port_standby = $node_standby->port; + $node_primary->append_conf('postgresql.conf', + "primary_conninfo = 'port=$port_standby'"); + $node_primary->set_standby_mode(); + $node_primary->start; + $node_standby->wait_for_catchup($node_primary); + + is( $node_primary->safe_psql('postgres', 'SELECT a FROM t ORDER BY a'), + qq(1 +2 +3 +200), + 'rewound node replayed from the redo location'); +} + +done_testing(); -- 2.47.3