From 8196b1b996c6d4866cb05e3fd8443ed68174ead0 Mon Sep 17 00:00:00 2001 From: Hayato Kuroda Date: Mon, 10 Nov 2025 21:23:31 +0900 Subject: [PATCH] Invalidate newly synchronized slots --- src/backend/replication/logical/slotsync.c | 3 + src/test/recovery/meson.build | 3 +- .../recovery/t/110_slot_sync_invalidation.pl | 89 +++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/test/recovery/t/110_slot_sync_invalidation.pl diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c index 051b1c866b5..69d2fbf2036 100644 --- a/src/backend/replication/logical/slotsync.c +++ b/src/backend/replication/logical/slotsync.c @@ -67,6 +67,7 @@ #include "storage/procarray.h" #include "tcop/tcopprot.h" #include "utils/builtins.h" +#include "utils/injection_point.h" #include "utils/pg_lsn.h" #include "utils/ps_status.h" #include "utils/timeout.h" @@ -486,6 +487,8 @@ reserve_wal_for_local_slot(XLogRecPtr restart_lsn) slot->data.restart_lsn = restart_lsn; SpinLockRelease(&slot->mutex); + INJECTION_POINT("slotsync-reserve-wal"); + /* Prevent WAL removal as fast as possible */ ReplicationSlotsComputeRequiredLSN(); diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build index 9f01d71dc3b..9a9635149c9 100644 --- a/src/test/recovery/meson.build +++ b/src/test/recovery/meson.build @@ -55,7 +55,8 @@ tests += { 't/045_archive_restartpoint.pl', 't/046_checkpoint_logical_slot.pl', 't/047_checkpoint_physical_slot.pl', - 't/048_vacuum_horizon_floor.pl' + 't/048_vacuum_horizon_floor.pl', + 't/110_slot_sync_invalidation.pl' ], }, } diff --git a/src/test/recovery/t/110_slot_sync_invalidation.pl b/src/test/recovery/t/110_slot_sync_invalidation.pl new file mode 100644 index 00000000000..688eb8aa64a --- /dev/null +++ b/src/test/recovery/t/110_slot_sync_invalidation.pl @@ -0,0 +1,89 @@ + +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +# Create primary +my $primary = PostgreSQL::Test::Cluster->new('primary'); +$primary->init(allows_streaming => 'logical'); +$primary->append_conf( + 'postgresql.conf', qq( +autovacuum = off +checkpoint_timeout = 1h +)); +$primary->start; + +# Create a standby from the backup +my $backup_name = 'backup'; +$primary->backup($backup_name); + +my $standby = PostgreSQL::Test::Cluster->new('standby'); +$standby->init_from_backup( + $primary, $backup_name, + has_streaming => 1, + has_restoring => 1); + +my $connstr = $primary->connstr; +$standby->append_conf( + 'postgresql.conf', qq( +hot_standby_feedback = on +primary_slot_name = 'sb1_slot' +primary_conninfo = '$connstr dbname=postgres' +)); + +# Install injection_point +$primary->safe_psql('postgres', q{CREATE EXTENSION injection_points;}); + +# Create a physical replication slot to synchronize logical slots +$primary->safe_psql('postgres', + q{SELECT pg_create_physical_replication_slot('sb1_slot');}); + +# Start the standby so that slot syncing can begin +$standby->start; + +$primary->wait_for_replay_catchup($standby); + +# Create a logical replication slot to be synchronized +$primary->safe_psql('postgres', + q{SELECT pg_create_logical_replication_slot('lsub1_slot', 'pgoutput', false, false, true);}); + +$primary->advance_wal(6); + +$primary->wait_for_replay_catchup($standby); + + +# Attach an injection point an try to synchronize the slot. It would stuck. +$standby->safe_psql('postgres', + q{SELECT injection_points_attach('slotsync-reserve-wal', 'wait')}); +my $background = $standby->background_psql('postgres'); +$background->query_until( + qr/slotsync-reserve-wal/, + q(\echo slotsync-reserve-wal + select pg_sync_replication_slots(); + \q +)); + +$standby->wait_for_event('client backend', 'slotsync-reserve-wal'); + +my $offset = -s $standby->logfile; + +# Run CHECKPOINT on both nodes to allow the standby server recycling files +$primary->safe_psql('postgres', q{CHECKPOINT}); +$standby->safe_psql('postgres', q{CHECKPOINT}); + +$standby->safe_psql('postgres', + q{SELECT injection_points_detach('slotsync-reserve-wal')}); + +my $result = $standby->safe_psql('postgres', + q(SELECT slot_name FROM pg_replication_slots;)); + +is($result, 'lsub1_slot', 'logical replication slot can be synchronized'); +done_testing(); -- 2.47.3