From 2815028627952e9e5319f15ba6d82c2400846ac4 Mon Sep 17 00:00:00 2001 From: Ayush Tiwari Date: Wed, 25 Nov 2026 19:09:41 +0530 Subject: [PATCH v4 1/2] Recheck table sync state after refresh An apply worker can cache a table's SYNCDONE state and then wait for the subscription lock while ALTER SUBSCRIPTION ... REFRESH PUBLICATION removes that table. The worker then tries to mark the absent catalog row READY and errors out. With disable_on_error, that disables the entire subscription, including unrelated tables. Two refreshes can also remove and re-add the table before the apply worker gets the lock. Completing the old sync would then mark the new INIT row READY and skip its initial copy. Re-read the table state after taking the subscription lock, and skip the READY transition unless it is still SYNCDONE. This table-side issue predates the sequence-side fix in dca73f7dd03. Author: Ayush Tiwari Reviewed-by: Peter Smith Reviewed-by: Aleksander Alekseev Discussion: https://postgr.es/m/CAJTYsWWJfq=M5U9h7sJFcQ+LPwq76CzLTzQbkRQPTJJ47SGxWw@mail.gmail.com --- src/backend/replication/logical/tablesync.c | 23 ++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index 1e17d220cd8..af7de51b7da 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -431,9 +431,8 @@ ProcessSyncingTablesForApply(XLogRecPtr current_lsn) if (current_lsn >= rstate->lsn) { char originname[NAMEDATALEN]; - - rstate->state = SUBREL_STATE_READY; - rstate->lsn = current_lsn; + char current_relstate; + XLogRecPtr statelsn; /* * Remove the tablesync origin tracking if exists. @@ -453,6 +452,24 @@ ProcessSyncingTablesForApply(XLogRecPtr current_lsn) LockSharedObject(SubscriptionRelationId, MyLogicalRepWorker->subid, 0, AccessShareLock); + /* + * A concurrent REFRESH PUBLICATION may have removed or + * re-added the table since we cached SYNCDONE. Only this + * apply worker launches tablesync workers, so a re-added + * table cannot have reached SYNCDONE yet. + */ + current_relstate = GetSubscriptionRelState(MyLogicalRepWorker->subid, + rstate->relid, &statelsn); + if (current_relstate != SUBREL_STATE_SYNCDONE) + { + elog(DEBUG1, "skipping READY transition for relation %u of subscription \"%s\" as it is no longer SYNCDONE", + rstate->relid, MySubscription->name); + continue; + } + + rstate->state = SUBREL_STATE_READY; + rstate->lsn = current_lsn; + if (!rel) rel = table_open(SubscriptionRelRelationId, RowExclusiveLock); -- 2.34.1