From a91433161b14e2efa802d5edff9e7a082aed3e10 Mon Sep 17 00:00:00 2001 From: Vignesh C Date: Thu, 16 Jul 2026 07:47:08 +0530 Subject: [PATCH v2] Avoid redundant REFRESH SEQUENCES in 036_sequences The TAP test issued ALTER SUBSCRIPTION ... REFRESH SEQUENCES after verifying the "insufficient privileges" error. However, since the failed sequence has not yet reached the READY state, the apply worker automatically restarts the sequence synchronization worker. As a result, the explicit REFRESH SEQUENCES can race with the restarted worker and fail with: ERROR: cannot execute ALTER SUBSCRIPTION ... REFRESH SEQUENCES while a sequence synchronization worker is running Remove the unnecessary REFRESH SEQUENCES command. Additionally, synced_query only checked pg_subscription_rel.srsubstate for 'r', but a sequence synchronization worker sets that state before it actually exits (e.g. it may still be reporting warnings/errors and doing cleanup). Since the worker remains registered in shared memory during that window, any subsequent ALTER SUBSCRIPTION ... REFRESH SEQUENCES/PUBLICATION issued right after polling synced_query can still hit the same "sequence synchronization worker is running" error. Strengthen synced_query to also wait until pg_stat_subscription no longer reports a running 'sequence synchronization' worker. --- src/test/subscription/t/036_sequences.pl | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/test/subscription/t/036_sequences.pl b/src/test/subscription/t/036_sequences.pl index b3b3b20f82b..d6da5bbc9a9 100644 --- a/src/test/subscription/t/036_sequences.pl +++ b/src/test/subscription/t/036_sequences.pl @@ -57,9 +57,16 @@ $node_subscriber->safe_psql('postgres', "CREATE SUBSCRIPTION regress_seq_sub CONNECTION '$publisher_connstr' PUBLICATION regress_seq_pub" ); -# Wait for initial sync to finish +# Wait for initial sync to finish. Besides checking that all sequences have +# reached the 'r' (ready) state, also make sure that no sequence +# synchronization worker is still running. A worker can remain registered for +# a short time after marking its sequences ready (e.g. while reporting +# errors/warnings before exiting), and issuing ALTER SUBSCRIPTION ... REFRESH +# SEQUENCES/PUBLICATION while it is still running would fail with "cannot +# execute ... while a sequence synchronization worker is running". my $synced_query = - "SELECT count(1) = 0 FROM pg_subscription_rel WHERE srsubstate NOT IN ('r');"; + "SELECT (SELECT count(1) FROM pg_subscription_rel WHERE srsubstate NOT IN ('r')) = 0 " + . "AND (SELECT count(1) FROM pg_stat_subscription WHERE worker_type = 'sequence synchronization') = 0;"; $node_subscriber->poll_query_until('postgres', $synced_query) or die "Timed out while waiting for subscriber to synchronize data"; @@ -328,9 +335,8 @@ $node_publisher->safe_psql('postgres', qq(DROP SEQUENCE regress_s2;)); $log_offset = -s $node_subscriber->logfile; -$node_subscriber->safe_psql('postgres', - "ALTER SUBSCRIPTION regress_seq_sub REFRESH SEQUENCES"); - +# No need to issue REFRESH SEQUENCES. The sequence synchronization worker is +# restarted automatically because regress_s2 is not yet in the READY state. $node_subscriber->wait_for_log( qr/WARNING: ( [A-Z0-9]+:)? missing sequence on publisher \("public.regress_s2"\)/, $log_offset); -- 2.50.1 (Apple Git-155)