Debian Codegraph @

src/test/recovery/t/044_invalidate_inactive_slots.pl

raw
1 # Copyright (c) 2025, PostgreSQL Global Development Group
2
3 # Test for replication slots invalidation due to idle_timeout
4 use strict;
5 use warnings FATAL => 'all';
6
7 use PostgreSQL::Test::Utils;
8 use PostgreSQL::Test::Cluster;
9 use Test::More;
10
11 # This test depends on injection point that forces slot invalidation
12 # due to idle_timeout.
13 # https://www.postgresql.org/docs/current/xfunc-c.html#XFUNC-ADDIN-INJECTION-POINTS
14 if ($ENV{enable_injection_points} ne 'yes')
15 {
16 plan skip_all => 'Injection points not supported by this build';
17 }
18
19 # Wait for slot to first become idle and then get invalidated
20 sub wait_for_slot_invalidation
21 {
22 my ($node, $slot_name, $offset) = @_;
23 my $node_name = $node->name;
24
25 # The slot's invalidation should be logged
26 $node->wait_for_log(
27 qr/invalidating obsolete replication slot \"$slot_name\"/, $offset);
28
29 # Check that the invalidation reason is 'idle_timeout'
30 $node->poll_query_until(
31 'postgres', qq[
32 SELECT COUNT(slot_name) = 1 FROM pg_replication_slots
33 WHERE slot_name = '$slot_name' AND
34 invalidation_reason = 'idle_timeout';
35 ])
36 or die
37 "Timed out while waiting for invalidation reason of slot $slot_name to be set on node $node_name";
38 }
39
40 # ========================================================================
41 # Testcase start
42 #
43 # Test invalidation of physical replication slot and logical replication slot
44 # due to idle timeout.
45
46 # Initialize the node
47 my $node = PostgreSQL::Test::Cluster->new('node');
48 $node->init(allows_streaming => 'logical');
49
50 # Avoid unpredictability
51 $node->append_conf(
52 'postgresql.conf', qq{
53 checkpoint_timeout = 1h
54 idle_replication_slot_timeout = 1min
55 });
56 $node->start;
57
58 # Check if the 'injection_points' extension is available, as it may be
59 # possible that this script is run with installcheck, where the module
60 # would not be installed by default.
61 if (!$node->check_extension('injection_points'))
62 {
63 plan skip_all => 'Extension injection_points not installed';
64 }
65
66 # Create both physical and logical replication slots
67 $node->safe_psql(
68 'postgres', qq[
69 SELECT pg_create_physical_replication_slot(slot_name := 'physical_slot', immediately_reserve := true);
70 SELECT pg_create_logical_replication_slot('logical_slot', 'test_decoding');
71 ]);
72
73 my $log_offset = -s $node->logfile;
74
75 # Register an injection point on the node to forcibly cause a slot
76 # invalidation due to idle_timeout
77 $node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
78
79 $node->safe_psql('postgres',
80 "SELECT injection_points_attach('slot-timeout-inval', 'error');");
81
82 # Slot invalidation occurs during a checkpoint, so perform a checkpoint to
83 # invalidate the slots.
84 $node->safe_psql('postgres', "CHECKPOINT");
85
86 # Wait for slots to become inactive. Since nobody has acquired the slot yet,
87 # it can only be due to the idle timeout mechanism.
88 wait_for_slot_invalidation($node, 'physical_slot', $log_offset);
89 wait_for_slot_invalidation($node, 'logical_slot', $log_offset);
90
91 # Check that the invalidated slot cannot be acquired
92 my ($result, $stdout, $stderr);
93 ($result, $stdout, $stderr) = $node->psql(
94 'postgres', qq[
95 SELECT pg_replication_slot_advance('logical_slot', '0/1');
96 ]);
97 ok( $stderr =~ /can no longer access replication slot "logical_slot"/,
98 "detected error upon trying to acquire invalidated slot on node")
99 or die
100 "could not detect error upon trying to acquire invalidated slot \"logical_slot\" on node";
101
102 # Testcase end
103 # =============================================================================
104
105 done_testing();