Handle concurrent sequence refreshes.

'ALTER SUBSCRIPTION ... REFRESH SEQUENCES' can race with a running
sequence synchronization worker. If the worker has fetched a sequence's
value from the publisher but not yet marked it READY, a concurrent refresh
that resets the sequence to INIT can be overwritten by the worker's stale
value, silently losing the refresh request.

Handle this by stopping any running sequence sync worker before resetting
the sequences to INIT. This is race-free because AlterSubscription()
already holds AccessExclusiveLock on the subscription object. That lock
blocks a running worker's UpdateSubscriptionRelState(), which takes
AccessShareLock on the object, and also any worker the apply worker
re-launches, because a new worker takes AccessShareLock on the object in
InitializeLogRepWorker() before it reads pg_subscription_rel. Such a
worker cannot act on the sequence states until the refresh commits, by
which time they are reset to INIT and it will synchronize the latest
publisher values.

Reported-by: Noah Misch <[email protected]>
Author: Amit Kapila <[email protected]>
Reviewed-by: vignesh C <[email protected]>
Reviewed-by: Shveta Malik <[email protected]>
Reviewed-by: Hayato Kuroda <[email protected]>
Backpatch-through: 19
Discussion: https://postgr.es/m/[email protected]

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/a8c9d2be280443956cf2740017bf869ccdc562c3

Modified Files
--------------
src/backend/commands/subscriptioncmds.c | 67 ++++++++++++++++++++++++---------
1 file changed, 50 insertions(+), 17 deletions(-)

Reply via email to