]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Reject concurrent sequence refreshes.
authorAmit Kapila <akapila@postgresql.org>
Wed, 15 Jul 2026 10:17:35 +0000 (15:47 +0530)
committerAmit Kapila <akapila@postgresql.org>
Wed, 15 Jul 2026 10:17:35 +0000 (15:47 +0530)
'ALTER SUBSCRIPTION ... REFRESH SEQUENCES' can race with an already
running sequence synchronization worker. If a second refresh request
resets the synchronization state while the worker has already fetched
sequence values from the publisher but has not yet applied them to the
subscriber, the worker can overwrite the subscriber with stale values
and mark the synchronization as complete.

Avoid this race by rejecting 'ALTER SUBSCRIPTION ... REFRESH SEQUENCES'
when a sequence synchronization worker is already running for the
subscription. The command reports an error asking the user to rerun it
after the current synchronization completes.

Also add a wait for the re-added 'regress_s4' sequence to finish
synchronizing in 036_sequences.pl, so the subsequent test does not race
against its sequencesync worker.

Reported-by: Noah Misch <noah@leadboat.com>
Author: vignesh C <vignesh21@gmail.com>
Reviewed-by: Shveta Malik <shveta.malik@gmail.com>
Reviewed-by: Hayato Kuroda <kuroda.hayato@fujitsu.com>
Reviewed-by: Amit Kapila <amit.kapila16@gmail.com>
Backpatch-through: 19, where it was introduced
Discussion: https://postgr.es/m/20260710045217.f0.noahmisch@microsoft.com

src/backend/commands/subscriptioncmds.c
src/test/subscription/t/036_sequences.pl

index ee06a726f420d3f14d1df853e14fece6b0022f18..94b163cccc97d698de4e56a2480beb73be21151b 100644 (file)
@@ -1304,6 +1304,33 @@ AlterSubscription_refresh_seq(Subscription *sub)
        WalReceiverConn *wrconn;
        bool            must_use_password;
 
+       /*
+        * Disallow a concurrent REFRESH SEQUENCES while a sequence sync worker
+        * for this subscription is still running. This avoids a race where the
+        * publisher's sequence advances after the current worker has fetched its
+        * value but before it marks the sequence READY. A user may then issue
+        * another REFRESH SEQUENCES to synchronize the updated value. Since the
+        * affected sequences are already in the INIT state, the running worker
+        * has no indication that a new synchronization has been requested. It
+        * would then apply the stale value it already fetched and mark the
+        * sequence READY, causing the new synchronization request to be lost and
+        * preventing the updated publisher values from being synchronized.
+        */
+       LWLockAcquire(LogicalRepWorkerLock, LW_SHARED);
+       if (logicalrep_worker_find(WORKERTYPE_SEQUENCESYNC, sub->oid, InvalidOid,
+                                                          true))
+       {
+               LWLockRelease(LogicalRepWorkerLock);
+               ereport(ERROR,
+                               errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+               /* translator: %s is an SQL ALTER command */
+                               errmsg("cannot execute %s while a sequence synchronization worker is running",
+                                          "ALTER SUBSCRIPTION ... REFRESH SEQUENCES"),
+                               errhint("Try again after the current synchronization completes."));
+       }
+
+       LWLockRelease(LogicalRepWorkerLock);
+
        /* Load the library providing us libpq calls. */
        load_file("libpqwalreceiver", false);
 
index 2a0819aaf015e7485440c290096871a0b18bbc5e..8b02b24a7e90cd23d3fdfc1571b084bebd96fe6b 100644 (file)
@@ -232,6 +232,10 @@ $node_publisher->safe_psql(
        CREATE SEQUENCE regress_s4 START 10 INCREMENT 2;
 ));
 
+# Wait for the missing sequence added to be synced
+$node_subscriber->poll_query_until('postgres', $synced_query)
+  or die "Timed out while waiting for subscriber to synchronize data";
+
 ##########
 # Ensure that insufficient privileges on the publisher for a sequence
 # are reported correctly as a permission issue, not as a missing sequence.