From 2c7a48b926787bad592e91d3270706eab1425077 Mon Sep 17 00:00:00 2001 From: Nisha Moond Date: Thu, 30 Jan 2025 21:07:12 +0530 Subject: [PATCH v65 3/4] Add TAP test for slot invalidation based on inactive timeout. This test uses injection points to bypass the time overhead caused by the idle_replication_slot_timeout GUC, which has a minimum value of one minute. --- src/backend/replication/slot.c | 5 + src/test/recovery/meson.build | 1 + .../t/044_invalidate_inactive_slots.pl | 176 ++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 src/test/recovery/t/044_invalidate_inactive_slots.pl diff --git a/src/backend/replication/slot.c b/src/backend/replication/slot.c index 2ebf366785..2191033e5c 100644 --- a/src/backend/replication/slot.c +++ b/src/backend/replication/slot.c @@ -55,6 +55,7 @@ #include "storage/proc.h" #include "storage/procarray.h" #include "utils/builtins.h" +#include "utils/injection_point.h" #include "utils/guc_hooks.h" #include "utils/varlena.h" @@ -1711,6 +1712,10 @@ InvalidatePossiblyObsoleteSlot(ReplicationSlotInvalidationCause cause, case RS_INVAL_IDLE_TIMEOUT: Assert(now > 0); + /* For testing timeout slot invalidation */ + if (IS_INJECTION_POINT_ATTACHED("slot-time-out-inval")) + s->inactive_since = 1; + /* * Check if the slot needs to be invalidated due to * idle_replication_slot_timeout GUC. diff --git a/src/test/recovery/meson.build b/src/test/recovery/meson.build index 0428704dbf..057bcde143 100644 --- a/src/test/recovery/meson.build +++ b/src/test/recovery/meson.build @@ -52,6 +52,7 @@ tests += { 't/041_checkpoint_at_promote.pl', 't/042_low_level_backup.pl', 't/043_no_contrecord_switch.pl', + 't/044_invalidate_inactive_slots.pl', ], }, } diff --git a/src/test/recovery/t/044_invalidate_inactive_slots.pl b/src/test/recovery/t/044_invalidate_inactive_slots.pl new file mode 100644 index 0000000000..ab948aba85 --- /dev/null +++ b/src/test/recovery/t/044_invalidate_inactive_slots.pl @@ -0,0 +1,176 @@ +# Copyright (c) 2025, PostgreSQL Global Development Group + +# Test for replication slots invalidation +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Utils; +use PostgreSQL::Test::Cluster; +use Test::More; + +if ($ENV{enable_injection_points} ne 'yes') +{ + plan skip_all => 'Injection points not supported by this build'; +} + +# ============================================================================= +# Testcase start +# +# Test invalidation of streaming standby slot and logical failover slot on the +# primary due to idle timeout. Also, test logical failover slot synced to +# the standby from the primary doesn't get invalidated on its own, but gets the +# invalidated state from the primary. + +# Initialize primary +my $primary = PostgreSQL::Test::Cluster->new('primary'); +$primary->init(allows_streaming => 'logical'); + +# Avoid unpredictability +$primary->append_conf( + 'postgresql.conf', qq{ +checkpoint_timeout = 1h +}); +$primary->start; + +# Take backup +my $backup_name = 'my_backup'; +$primary->backup($backup_name); + +# Create sync slot on the primary +$primary->psql('postgres', + q{SELECT pg_create_logical_replication_slot('sync_slot1', 'test_decoding', false, false, true);} +); + +# Create standby slot on the primary +$primary->safe_psql( + 'postgres', qq[ + SELECT pg_create_physical_replication_slot(slot_name := 'sb_slot1', immediately_reserve := true); +]); + +# Create standby +my $standby1 = PostgreSQL::Test::Cluster->new('standby1'); +$standby1->init_from_backup($primary, $backup_name, has_streaming => 1); + +my $connstr = $primary->connstr; +$standby1->append_conf( + 'postgresql.conf', qq( +hot_standby_feedback = on +primary_slot_name = 'sb_slot1' +idle_replication_slot_timeout = 1 +primary_conninfo = '$connstr dbname=postgres' +)); +$standby1->start; + +# Wait until the standby has replayed enough data +$primary->wait_for_catchup($standby1); + +# Sync the primary slots to the standby +$standby1->safe_psql('postgres', "SELECT pg_sync_replication_slots();"); + +# Confirm that the logical failover slot is created on the standby +is( $standby1->safe_psql( + 'postgres', + q{SELECT count(*) = 1 FROM pg_replication_slots + WHERE slot_name = 'sync_slot1' AND synced + AND NOT temporary + AND invalidation_reason IS NULL;} + ), + 't', + 'logical slot sync_slot1 is synced to standby'); + +my $logstart = -s $primary->logfile; + +# Set timeout GUC so that the next checkpoint will invalidate inactive slots +$primary->safe_psql( + 'postgres', qq[ + ALTER SYSTEM SET idle_replication_slot_timeout TO '1min'; +]); +$primary->reload; + +# Register an injection point on the primary to forcibly cause a slot timeout +$primary->safe_psql('postgres', 'CREATE EXTENSION injection_points;'); + +# Check if the extension injection_points is available, as it may be +# possible that this script is run with installcheck, where the module +# would not be installed by default. +if (!$primary->check_extension('injection_points')) +{ + plan skip_all => 'Extension injection_points not installed'; +} + +$primary->safe_psql('postgres', + "SELECT injection_points_attach('slot-time-out-inval', 'error');"); + +# Wait for logical failover slot to become inactive on the primary. Note that +# nobody has acquired the slot yet, so it must get invalidated due to +# idle timeout. +wait_for_slot_invalidation($primary, 'sync_slot1', $logstart); + +# Re-sync the primary slots to the standby. Note that the primary slot was +# already invalidated (above) due to idle timeout. The standby must just +# sync the invalidated state. +$standby1->safe_psql('postgres', "SELECT pg_sync_replication_slots();"); + +is( $standby1->safe_psql( + 'postgres', + q{SELECT count(*) = 1 FROM pg_replication_slots + WHERE slot_name = 'sync_slot1' + AND invalidation_reason = 'idle_timeout';} + ), + "t", + 'check that invalidation of synced slot sync_slot1 is synced on standby'); + +# Make the standby slot on the primary inactive and check for invalidation +$standby1->stop; +wait_for_slot_invalidation($primary, 'sb_slot1', $logstart); + +# Testcase end +# ============================================================================= + +# Wait for slot to first become idle and then get invalidated +sub wait_for_slot_invalidation +{ + my ($node, $slot, $offset) = @_; + my $node_name = $node->name; + + trigger_slot_invalidation($node, $slot, $offset); + + # Check that an invalidated slot cannot be acquired + my ($result, $stdout, $stderr); + ($result, $stdout, $stderr) = $node->psql( + 'postgres', qq[ + SELECT pg_replication_slot_advance('$slot', '0/1'); + ]); + ok( $stderr =~ /can no longer access replication slot "$slot"/, + "detected error upon trying to acquire invalidated slot $slot on node $node_name" + ) + or die + "could not detect error upon trying to acquire invalidated slot $slot on node $node_name"; +} + +# Trigger slot invalidation and confirm it in the server log +sub trigger_slot_invalidation +{ + my ($node, $slot, $offset) = @_; + my $node_name = $node->name; + my $invalidated = 0; + + # Run a checkpoint + $node->safe_psql('postgres', "CHECKPOINT"); + + # The slot's invalidation should be logged + $node->wait_for_log(qr/invalidating obsolete replication slot \"$slot\"/, + $offset); + + # Check that the invalidation reason is 'idle_timeout' + $node->poll_query_until( + 'postgres', qq[ + SELECT COUNT(slot_name) = 1 FROM pg_replication_slots + WHERE slot_name = '$slot' AND + invalidation_reason = 'idle_timeout'; + ]) + or die + "Timed out while waiting for invalidation reason of slot $slot to be set on node $node_name"; +} + +done_testing(); -- 2.34.1