From e84907ec40ff5df03588bc6e7731a50bd77d8f9d Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Wed, 2 Dec 2026 14:52:54 +0900 Subject: [PATCH v1 2/2] Test snapshot cache invalidation during promotion. Add an injection point after publishing the end of recovery and before shutting down the recovery transaction environment. Use it with the existing promotion-after-wal-segment-cleanup injection point to create a backend during recovery and run its first query after recovery ends. Check that the query completes without an assertion failure, without checking its result. Without the preceding fix, it reuses an older catalog snapshot and triggers the GetSnapshotDataReuse() assertion. --- src/backend/access/transam/xlog.c | 2 + src/test/recovery/meson.build | 1 + src/test/recovery/t/062_promotion_snapshot.pl | 63 +++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 src/test/recovery/t/062_promotion_snapshot.pl diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index e7d277bf599..8ddce1e46cd 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -6951,6 +6951,8 @@ StartupXLOG(void) UpdateControlFile(); LWLockRelease(ControlFileLock); + INJECTION_POINT("promotion-before-recovery-shutdown", NULL); + /* * Wake up the checkpointer process as there might be a request to disable * logical decoding by concurrent slot drop. diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build index cb3df6ed895..f7ee2160471 100644 --- a/src/test/recovery/meson.build +++ b/src/test/recovery/meson.build @@ -70,6 +70,7 @@ tests += { 't/059_remote_apply_status_interval.pl', 't/060_multixact_crashed_updater.pl', 't/061_recovery_target_prefetch.pl', + 't/062_promotion_snapshot.pl', ], }, } diff --git a/src/test/recovery/t/062_promotion_snapshot.pl b/src/test/recovery/t/062_promotion_snapshot.pl new file mode 100644 index 00000000000..8775575ba7a --- /dev/null +++ b/src/test/recovery/t/062_promotion_snapshot.pl @@ -0,0 +1,63 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Check that a query does not trigger an assertion failure during promotion. +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +plan skip_all => 'Injection points not supported by this build' + unless $ENV{enable_injection_points} eq 'yes'; + +my $primary = PostgreSQL::Test::Cluster->new('primary'); +$primary->init(allows_streaming => 1); +$primary->start; + +if (!$primary->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} +$primary->safe_psql('postgres', + 'CREATE EXTENSION injection_points; CREATE TABLE t (i int)'); +$primary->backup('backup'); + +my $standby = PostgreSQL::Test::Cluster->new('standby'); +$standby->init_from_backup($primary, 'backup', has_streaming => 1); +$standby->start; + +# Leave a transaction running on the primary so that snapshot xmin can +# advance when recovery ends and snapshots stop including that transaction. +my $writer = $primary->background_psql('postgres'); +$writer->query_safe('BEGIN; INSERT INTO t VALUES (1)'); +$primary->safe_psql('postgres', 'SELECT pg_log_standby_snapshot()'); +$primary->wait_for_catchup($standby); + +$standby->safe_psql( + 'postgres', + q(SELECT injection_points_attach('promotion-after-wal-segment-cleanup', 'wait'); + SELECT injection_points_attach('promotion-before-recovery-shutdown', 'wait'); + SELECT pg_promote(false))); +$standby->wait_for_event('startup', 'promotion-after-wal-segment-cleanup'); + +# This backend has a cached catalog snapshot from connection setup but has +# not taken a transaction snapshot. Its first query after recovery ends +# must not reuse that older catalog snapshot with a newer transaction xmin. +my $reader = $standby->background_psql('postgres'); +$standby->safe_psql('postgres', + q(SELECT injection_points_wakeup('promotion-after-wal-segment-cleanup'))); +$standby->wait_for_event('startup', 'promotion-before-recovery-shutdown'); + +$reader->query_safe('SELECT NOT pg_is_in_recovery()'); +pass('query completes without an assertion failure after recovery ends'); + +$standby->safe_psql('postgres', + q(SELECT injection_points_wakeup('promotion-before-recovery-shutdown'))); +$reader->quit; +$writer->query_safe('ROLLBACK'); +$writer->quit; +$standby->stop; +$primary->stop; + +done_testing(); -- 2.56.1