|
213 | 213 | ################################################## |
214 | 214 | # Test that the synchronized slot will be dropped if the corresponding remote |
215 | 215 | # slot on the primary server has been dropped. |
216 | | -# |
217 | | -# Note: Both slots need to be dropped for the next test to work |
218 | 216 | ################################################## |
219 | 217 |
|
220 | 218 | $primary->psql('postgres',"SELECT pg_drop_replication_slot('lsub2_slot');"); |
221 | | -$primary->psql('postgres',"SELECT pg_drop_replication_slot('lsub1_slot');"); |
222 | 219 |
|
223 | 220 | $standby1->safe_psql('postgres',"SELECT pg_sync_replication_slots();"); |
224 | 221 |
|
225 | 222 | is($standby1->safe_psql( |
226 | 223 | 'postgres', |
227 | | -q{SELECT count(*) = 0 FROM pg_replication_slots WHERE slot_nameIN ('lsub1_slot', 'lsub2_slot');} |
| 224 | +q{SELECT count(*) = 0 FROM pg_replication_slots WHERE slot_name= 'lsub2_slot';} |
228 | 225 | ), |
229 | 226 | "t", |
230 | 227 | 'synchronized slot has been dropped'); |
231 | 228 |
|
232 | | -################################################## |
233 | | -# Verify that slotsync skip statistics are correctly updated when the |
234 | | -# slotsync operation is skipped. |
235 | | -################################################## |
236 | | - |
237 | | -# Create a logical replication slot and create some DDL on the primary so |
238 | | -# that the slot lags behind the standby. |
239 | | -$primary->safe_psql( |
240 | | -'postgres',qq( |
241 | | -SELECT pg_create_logical_replication_slot('lsub1_slot', 'pgoutput', false, false, true); |
242 | | -CREATE TABLE wal_push(a int); |
243 | | -)); |
244 | | -$primary->wait_for_replay_catchup($standby1); |
245 | | - |
246 | | -my$log_offset =-s$standby1->logfile; |
247 | | - |
248 | | -# Enable slot sync worker. |
249 | | -$standby1->append_conf('postgresql.conf',qq(sync_replication_slots = on)); |
250 | | -$standby1->reload; |
251 | | - |
252 | | -# Confirm that the slot sync worker is able to start. |
253 | | -$standby1->wait_for_log(qr/slot sync worker started/,$log_offset); |
254 | | - |
255 | | -# Confirm that the slot sync is skipped due to the remote slot lagging behind |
256 | | -$standby1->wait_for_log( |
257 | | -qr/could not synchronize replication slot\"lsub1_slot\"/,$log_offset); |
258 | | - |
259 | | -# Confirm that the slotsync skip statistics is updated |
260 | | -$result =$standby1->safe_psql('postgres', |
261 | | -"SELECT slotsync_skip_count > 0 FROM pg_stat_replication_slots WHERE slot_name = 'lsub1_slot'" |
262 | | -); |
263 | | -is($result,'t',"check slot sync skip count increments"); |
264 | | - |
265 | | -# Clean the table |
266 | | -$primary->safe_psql( |
267 | | -'postgres',qq( |
268 | | - DROP TABLE wal_push; |
269 | | -)); |
270 | | -$primary->wait_for_replay_catchup($standby1); |
271 | | - |
272 | | -# Re-create the logical replication slot and sync it to standby for further tests |
273 | | -$primary->safe_psql( |
274 | | -'postgres',qq( |
275 | | -SELECT pg_drop_replication_slot('lsub1_slot'); |
276 | | -SELECT pg_create_logical_replication_slot('lsub1_slot', 'pgoutput', false, false, true); |
277 | | -)); |
278 | | -$standby1->wait_for_log( |
279 | | -qr/newly created replication slot\"lsub1_slot\" is sync-ready now/, |
280 | | -$log_offset); |
281 | | - |
282 | | -$standby1->append_conf('postgresql.conf',qq(sync_replication_slots = off)); |
283 | | -$standby1->reload; |
284 | | - |
285 | 229 | ################################################## |
286 | 230 | # Test that if the synchronized slot is invalidated while the remote slot is |
287 | 231 | # still valid, the slot will be dropped and re-created on the standby by |
|
337 | 281 | # the failover slots. |
338 | 282 | $primary->wait_for_replay_catchup($standby1); |
339 | 283 |
|
340 | | -$log_offset =-s$standby1->logfile; |
| 284 | +my$log_offset =-s$standby1->logfile; |
341 | 285 |
|
342 | 286 | # Synchronize the primary server slots to the standby. |
343 | 287 | $standby1->safe_psql('postgres',"SELECT pg_sync_replication_slots();"); |
|
1043 | 987 |
|
1044 | 988 | is($result,'1',"data can be consumed using snap_test_slot"); |
1045 | 989 |
|
| 990 | +################################################## |
| 991 | +# Remove any unnecessary replication slots and clear pending transactions on the |
| 992 | +# primary server to ensure a clean environment. |
| 993 | +################################################## |
| 994 | + |
| 995 | +$primary->psql( |
| 996 | +'postgres',qq( |
| 997 | +SELECT pg_drop_replication_slot('sb1_slot'); |
| 998 | +SELECT pg_drop_replication_slot('lsub1_slot'); |
| 999 | +SELECT pg_drop_replication_slot('snap_test_slot'); |
| 1000 | +)); |
| 1001 | + |
| 1002 | +$subscriber2->safe_psql('postgres','DROP SUBSCRIPTION regress_mysub2;'); |
| 1003 | + |
| 1004 | +# Verify that all slots have been removed except the one necessary for standby2, |
| 1005 | +# which is needed for further testing. |
| 1006 | +is($primary->safe_psql( |
| 1007 | +'postgres', |
| 1008 | +q{SELECT count(*) = 0 FROM pg_replication_slots WHERE slot_name != 'sb2_slot';} |
| 1009 | +), |
| 1010 | +"t", |
| 1011 | +'all replication slots have been dropped except the physical slot used by standby2' |
| 1012 | +); |
| 1013 | + |
| 1014 | +# Commit the pending prepared transaction |
| 1015 | +$primary->safe_psql('postgres',"COMMIT PREPARED 'test_twophase_slotsync';"); |
| 1016 | +$primary->wait_for_replay_catchup($standby2); |
| 1017 | + |
| 1018 | +################################################## |
| 1019 | +# Verify that slotsync skip statistics are correctly updated when the |
| 1020 | +# slotsync operation is skipped. |
| 1021 | +################################################## |
| 1022 | + |
| 1023 | +# Create a logical replication slot and create some DDL on the primary so |
| 1024 | +# that the slot lags behind the standby. |
| 1025 | +$primary->safe_psql( |
| 1026 | +'postgres',qq( |
| 1027 | +SELECT pg_create_logical_replication_slot('lsub1_slot', 'pgoutput', false, false, true); |
| 1028 | +CREATE TABLE wal_push(a int); |
| 1029 | +)); |
| 1030 | +$primary->wait_for_replay_catchup($standby2); |
| 1031 | + |
| 1032 | +$log_offset =-s$standby2->logfile; |
| 1033 | + |
| 1034 | +# Enable slot sync worker |
| 1035 | +$standby2->append_conf( |
| 1036 | +'postgresql.conf',qq( |
| 1037 | +hot_standby_feedback = on |
| 1038 | +primary_conninfo = '$connstr_1 dbname=postgres' |
| 1039 | +log_min_messages = 'debug2' |
| 1040 | +sync_replication_slots = on |
| 1041 | +)); |
| 1042 | + |
| 1043 | +$standby2->reload; |
| 1044 | + |
| 1045 | +# Confirm that the slot sync worker is able to start. |
| 1046 | +$standby2->wait_for_log(qr/slot sync worker started/,$log_offset); |
| 1047 | + |
| 1048 | +# Confirm that the slot sync is skipped due to the remote slot lagging behind |
| 1049 | +$standby2->wait_for_log( |
| 1050 | +qr/could not synchronize replication slot\"lsub1_slot\"/,$log_offset); |
| 1051 | + |
| 1052 | +# Confirm that the slotsync skip statistics is updated |
| 1053 | +$result =$standby2->safe_psql('postgres', |
| 1054 | +"SELECT slotsync_skip_count > 0 FROM pg_stat_replication_slots WHERE slot_name = 'lsub1_slot'" |
| 1055 | +); |
| 1056 | +is($result,'t',"check slot sync skip count increments"); |
| 1057 | + |
1046 | 1058 | done_testing(); |