From 88002a04e64388f49d48c67ef6beec3ce835f2c3 Mon Sep 17 00:00:00 2001 From: Shlomi Noach Date: Mon, 30 Nov 2026 11:32:34 +0200 Subject: [PATCH v1 2/2] Add a test for the xmin of a snapshot exported by a replication slot Add an injection point in ReplicationSlotRelease() right after the slot's temporary data xmin is reset, and a TAP test that stops the walsender there while it creates a temporary logical slot with an exported snapshot. Meanwhile, another session updates a row and vacuums the table. The exported snapshot must still show the old row version. Without the previous commit, the test fails: the walsender is still flagged PROC_IN_LOGICAL_DECODING at the injection point, so its xmin does not hold back the horizon. VACUUM removes the old row version, and the snapshot shows the updated row instead. --- src/backend/replication/slot.c | 1 + src/test/recovery/meson.build | 1 + .../t/062_slot_export_snapshot_xmin.pl | 69 +++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/test/recovery/t/062_slot_export_snapshot_xmin.pl diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 080cae21672..5aea246902c 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -824,6 +824,7 @@ ReplicationSlotRelease(void) slot->effective_xmin = InvalidTransactionId; SpinLockRelease(&slot->mutex); ReplicationSlotsComputeRequiredXmin(false); + INJECTION_POINT("replication-slot-release-after-xmin-reset", NULL); } /* diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build index cb3df6ed895..e4dbe535cae 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_slot_export_snapshot_xmin.pl', ], }, } diff --git a/src/test/recovery/t/062_slot_export_snapshot_xmin.pl b/src/test/recovery/t/062_slot_export_snapshot_xmin.pl new file mode 100644 index 00000000000..6129c12f853 --- /dev/null +++ b/src/test/recovery/t/062_slot_export_snapshot_xmin.pl @@ -0,0 +1,69 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# A snapshot exported by CREATE_REPLICATION_SLOT ... (SNAPSHOT 'export') must +# keep the tuples it needs after the slot resets its data xmin at the end of +# the command. The walsender stops right after that reset, another session +# replaces a row and vacuums the table, and the exported snapshot must still +# show the old row version. +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 $node = PostgreSQL::Test::Cluster->new('primary'); +$node->init(allows_streaming => 'logical'); +$node->append_conf('postgresql.conf', 'autovacuum = off'); +$node->start; + +if (!$node->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$node->safe_psql('postgres', q(CREATE EXTENSION injection_points)); +$node->safe_psql( + 'postgres', + q(CREATE TABLE tab(i int PRIMARY KEY, j int); + INSERT INTO tab VALUES (1, 1))); + +my $point = 'replication-slot-release-after-xmin-reset'; +$node->safe_psql('postgres', + "SELECT injection_points_attach('$point', 'wait')"); + +my $walsender = $node->background_psql('postgres', replication => 'database'); +$walsender->query_until( + qr/started/, q(\echo started +CREATE_REPLICATION_SLOT slot_export TEMPORARY LOGICAL test_decoding (SNAPSHOT 'export'); +)); +$node->wait_for_event('walsender', $point); + +# The snapshot is exported, and the slot no longer holds back the xmin +# horizon. Replace the row version, and let VACUUM remove what it can. +$node->safe_psql('postgres', q(UPDATE tab SET j = j + 1 WHERE i = 1)); +$node->safe_psql('postgres', q(VACUUM tab)); + +$node->safe_psql('postgres', "SELECT injection_points_wakeup('$point')"); +$node->safe_psql('postgres', "SELECT injection_points_detach('$point')"); + +my $result = $walsender->query_until(qr/test_decoding/, ''); +my ($snapshot) = $result =~ /\|([0-9A-F]+-[0-9A-F]+-[0-9]+)\|/; +ok(defined $snapshot, 'slot creation returned a snapshot name') + or diag("output: $result"); + +# The walsender keeps the snapshot exported until its next command. +my $rows = $node->safe_psql( + 'postgres', qq(BEGIN ISOLATION LEVEL REPEATABLE READ; +SET TRANSACTION SNAPSHOT '$snapshot'; +SELECT i, j FROM tab; +COMMIT;)); +is($rows, '1|1', 'exported snapshot shows the row version it needs'); + +$walsender->quit; +$node->stop; + +done_testing(); -- 2.50.1 (Apple Git-155)