Debian Codegraph @

src/test/recovery/t/046_checkpoint_logical_slot.pl

raw
1 # Copyright (c) 2025, PostgreSQL Global Development Group
2 #
3 # This test verifies the case when the logical slot is advanced during
4 # checkpoint. The test checks that the logical slot's restart_lsn still refers
5 # to an existed WAL segment after immediate restart.
6 #
7 use strict;
8 use warnings FATAL => 'all';
9
10 use PostgreSQL::Test::Cluster;
11 use PostgreSQL::Test::Utils;
12
13 use Test::More;
14
15 if ($ENV{enable_injection_points} ne 'yes')
16 {
17 plan skip_all => 'Injection points not supported by this build';
18 }
19
20 my ($node, $result);
21
22 $node = PostgreSQL::Test::Cluster->new('mike');
23 $node->init(allows_streaming => 'logical');
24 $node->start;
25
26 # Check if the extension injection_points is available, as it may be
27 # possible that this script is run with installcheck, where the module
28 # would not be installed by default.
29 if (!$node->check_extension('injection_points'))
30 {
31 plan skip_all => 'Extension injection_points not installed';
32 }
33
34 $node->safe_psql('postgres', q(CREATE EXTENSION injection_points));
35
36 # Create the two slots we'll need.
37 $node->safe_psql('postgres',
38 q{select pg_create_logical_replication_slot('slot_logical', 'test_decoding')}
39 );
40 $node->safe_psql('postgres',
41 q{select pg_create_physical_replication_slot('slot_physical', true)});
42
43 # Advance both slots to the current position just to have everything "valid".
44 $node->safe_psql('postgres',
45 q{select count(*) from pg_logical_slot_get_changes('slot_logical', null, null)}
46 );
47 $node->safe_psql('postgres',
48 q{select pg_replication_slot_advance('slot_physical', pg_current_wal_lsn())}
49 );
50
51 # Run checkpoint to flush current state to disk and set a baseline.
52 $node->safe_psql('postgres', q{checkpoint});
53
54 # Generate some transactions to get RUNNING_XACTS.
55 my $xacts = $node->background_psql('postgres');
56 $xacts->query_until(
57 qr/run_xacts/,
58 q(\echo run_xacts
59 SELECT 1 \watch 0.1
60 \q
61 ));
62
63 $node->advance_wal(20);
64
65 # Run another checkpoint to set a new restore LSN.
66 $node->safe_psql('postgres', q{checkpoint});
67
68 $node->advance_wal(20);
69
70 # Run another checkpoint, this time in the background, and make it wait
71 # on the injection point) so that the checkpoint stops right before
72 # removing old WAL segments.
73 note('starting checkpoint');
74
75 my $checkpoint = $node->background_psql('postgres');
76 $checkpoint->query_safe(
77 q(select injection_points_attach('checkpoint-before-old-wal-removal','wait'))
78 );
79 $checkpoint->query_until(
80 qr/starting_checkpoint/,
81 q(\echo starting_checkpoint
82 checkpoint;
83 \q
84 ));
85
86 # Wait until the checkpoint stops right before removing WAL segments.
87 note('waiting for injection_point');
88 $node->wait_for_event('checkpointer', 'checkpoint-before-old-wal-removal');
89 note('injection_point is reached');
90
91 # Try to advance the logical slot, but make it stop when it moves to the next
92 # WAL segment (this has to happen in the background, too).
93 my $logical = $node->background_psql('postgres');
94 $logical->query_safe(
95 q{select injection_points_attach('logical-replication-slot-advance-segment','wait');}
96 );
97 $logical->query_until(
98 qr/get_changes/,
99 q(
100 \echo get_changes
101 select count(*) from pg_logical_slot_get_changes('slot_logical', null, null) \watch 1
102 \q
103 ));
104
105 # Wait until the slot's restart_lsn points to the next WAL segment.
106 note('waiting for injection_point');
107 $node->wait_for_event('client backend',
108 'logical-replication-slot-advance-segment');
109 note('injection_point is reached');
110
111 # OK, we're in the right situation: time to advance the physical slot, which
112 # recalculates the required LSN, and then unblock the checkpoint, which
113 # removes the WAL still needed by the logical slot.
114 $node->safe_psql('postgres',
115 q{select pg_replication_slot_advance('slot_physical', pg_current_wal_lsn())}
116 );
117
118 # Generate a long WAL record, spawning at least two pages for the follow-up
119 # post-recovery check.
120 $node->safe_psql('postgres',
121 q{select pg_logical_emit_message(false, '', repeat('123456789', 1000))});
122
123 # Continue the checkpoint and wait for its completion.
124 my $log_offset = -s $node->logfile;
125 $node->safe_psql('postgres',
126 q{select injection_points_wakeup('checkpoint-before-old-wal-removal')});
127 $node->wait_for_log(qr/checkpoint complete/, $log_offset);
128
129 # Abruptly stop the server.
130 $node->stop('immediate');
131
132 $node->start;
133
134 eval {
135 $node->safe_psql('postgres',
136 q{select count(*) from pg_logical_slot_get_changes('slot_logical', null, null);}
137 );
138 };
139 is($@, '', "Logical slot still valid");
140
141 # If we send \q with $<psql_session>->quit the command can be sent to the
142 # session already closed. So \q is in initial script, here we only finish
143 # IPC::Run
144 $xacts->{run}->finish;
145 $checkpoint->{run}->finish;
146 $logical->{run}->finish;
147
148 # Verify that the synchronized slots won't be invalidated immediately after
149 # synchronization in the presence of a concurrent checkpoint.
150 my $primary = $node;
151
152 $primary->append_conf('postgresql.conf', "autovacuum = off");
153 $primary->reload;
154
155 my $backup_name = 'backup';
156
157 $primary->backup($backup_name);
158
159 # Create a standby
160 my $standby = PostgreSQL::Test::Cluster->new('standby');
161 $standby->init_from_backup(
162 $primary, $backup_name,
163 has_streaming => 1);
164
165 my $connstr_1 = $primary->connstr;
166 $standby->append_conf(
167 'postgresql.conf', qq(
168 hot_standby_feedback = on
169 primary_slot_name = 'phys_slot'
170 primary_conninfo = '$connstr_1 dbname=postgres'
171 ));
172
173 $primary->safe_psql('postgres',
174 q{SELECT pg_create_logical_replication_slot('failover_slot', 'test_decoding', false, false, true);
175 SELECT pg_create_physical_replication_slot('phys_slot');}
176 );
177
178 $standby->start;
179
180 # Generate some activity and switch WAL file on the primary
181 $primary->advance_wal(1);
182 $primary->safe_psql('postgres', "CHECKPOINT");
183 $primary->wait_for_replay_catchup($standby);
184
185 # checkpoint on the standby and make it wait on the injection point so that the
186 # checkpoint stops right before invalidating replication slots.
187 note('starting checkpoint');
188
189 $checkpoint = $standby->background_psql('postgres');
190 $checkpoint->query_safe(
191 q(select injection_points_attach('restartpoint-before-slot-invalidation','wait'))
192 );
193 $checkpoint->query_until(
194 qr/starting_checkpoint/,
195 q(\echo starting_checkpoint
196 checkpoint;
197 ));
198
199 # Wait until the checkpoint stops right before invalidating slots
200 note('waiting for injection_point');
201 $standby->wait_for_event('checkpointer', 'restartpoint-before-slot-invalidation');
202 note('injection_point is reached');
203
204 # Enable slot sync worker to synchronize the failover slot to the standby
205 $standby->append_conf('postgresql.conf', qq(sync_replication_slots = on));
206 $standby->reload;
207
208 # Wait for the slot to be synced
209 $standby->poll_query_until(
210 'postgres',
211 "SELECT COUNT(*) > 0 FROM pg_replication_slots WHERE slot_name = 'failover_slot'");
212
213 # Release the checkpointer
214 $standby->safe_psql('postgres',
215 q{select injection_points_wakeup('restartpoint-before-slot-invalidation');
216 select injection_points_detach('restartpoint-before-slot-invalidation')});
217
218 $checkpoint->quit;
219
220 # Confirm that the slot is not invalidated
221 is( $standby->safe_psql(
222 'postgres',
223 q{SELECT invalidation_reason IS NULL AND synced FROM pg_replication_slots WHERE slot_name = 'failover_slot';}
224 ),
225 "t",
226 'logical slot is not invalidated');
227
228 done_testing();