From 2d274c48ecd8b262df260b91f111bbdbfd9f9eb7 Mon Sep 17 00:00:00 2001 From: Vignesh C Date: Fri, 10 Jul 2026 18:02:05 +0530 Subject: [PATCH v1 1/2] Skip refreshing sequences that are already being synchronized 'ALTER SUBSCRIPTION ... REFRESH SEQUENCES' resets the synchronization state of each subscribed sequence to 'INIT', allowing the sequence sync worker to synchronize its value from the publisher. However, if a sequence is already in the 'INIT' state, it indicates that a previous 'REFRESH SEQUENCES' has not yet been processed by the sequence sync worker. Resetting it again can race with the worker if it has already fetched the publisher's sequence value but has not yet applied it to the subscriber. In that case, the worker may later overwrite the subscriber sequence with the stale value it previously fetched and mark the sequence as 'READY', causing the subsequent refresh request to be lost. Avoid this race by skipping sequences that are already in the 'INIT' state. Emit a warning informing the user that the sequence is already being synchronized and suggesting that 'ALTER SUBSCRIPTION ... REFRESH SEQUENCES' be rerun after the current synchronization completes. --- src/backend/commands/subscriptioncmds.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/backend/commands/subscriptioncmds.c b/src/backend/commands/subscriptioncmds.c index 4292e7fb8f4..6569cebb142 100644 --- a/src/backend/commands/subscriptioncmds.c +++ b/src/backend/commands/subscriptioncmds.c @@ -1389,6 +1389,26 @@ AlterSubscription_refresh_seq(Subscription *sub) { Oid relid = subrel->relid; + /* + * If the sequence is already marked INIT, a previous REFRESH + * SEQUENCES has not yet been picked up by the sequencesync + * worker. Resetting it again here would race with that worker + * applying the values it already fetched from the publisher, + * possibly leaving the sequence marked READY with stale data. + * Skip it and let the user know instead. + */ + if (subrel->state == SUBREL_STATE_INIT) + { + ereport(WARNING, + errmsg("sequence \"%s.%s\" of subscription \"%s\" is already being synchronized, skipping", + get_namespace_name(get_rel_namespace(relid)), + get_rel_name(relid), + sub->name), + errhint("Rerun %s after the current synchronization completes.", + "ALTER SUBSCRIPTION ... REFRESH SEQUENCES")); + continue; + } + UpdateSubscriptionRelState(sub->oid, relid, SUBREL_STATE_INIT, InvalidXLogRecPtr, false); ereport(DEBUG1, -- 2.50.1 (Apple Git-155)