[PATCH] Table sync race with REFRESH PUBLICATION
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
Hi,
I think we're missing a recheck in ProcessSyncingTablesForApply().
We take the subscription lock before marking a table READY, but
continue to use the SYNCDONE state we cached earlier. A concurrent
REFRESH PUBLICATION can remove the table while we're waiting for
the lock. Once we get it, there's no pg_subscription_rel row left
to update, and apply errors out.
I asked an LLM to review this and it could reproduce the missing-row
error. With disable_on_error = true, that disables the whole
subscription. The attached fix reads the state and LSN again under
the lock. Just checking for a row wouldn't be enough: two refreshes
could remove and re-add the table, and using the old SYNCDONE state
would skip the new initial copy.
The TAP test just checks the A/B behaviour: the subscription gets
disabled without the fix and stays enabled with it. I tested
remove/re-add separately, but left it out of the TAP test because it
needs quite a bit more setup and there has been no field report of
this issue yet (that I know of). I added the test as a separate patch
for the same reason, in case we want to leave it out given how niche
the case is. If we want to add specific tests, I'm happy to send revised
patches with those.
Thoughts?
Regards,
Ayush
Attachments:
t253947_10002-Test-table-sync-after-a-concurrent-refresh.patchapplication/octet-stream; name=0002-Test-table-sync-after-a-concurrent-refresh.patchDownload+127-1
0001-Recheck-table-sync-state-after-refresh.patchapplication/octet-stream; name=0001-Recheck-table-sync-state-after-refresh.patchDownload+14-4
A couple of comments for 0001.
======
1.
+ if (GetSubscriptionRelState(MyLogicalRepWorker->subid,
+ rstate->relid, &statelsn) != SUBREL_STATE_SYNCDONE ||
+ current_lsn < statelsn)
+ continue;
1.
Would a local variable simplify the condition?
Also, the related SEQUENCE code [1]https://github.com/postgres/postgres/commit/dca73f7dd03ddfc89a88019659fc0161e0427a8c#diff-e188109d851e85cc74d6bb7543f8ddf2be5cc2764e976ed44a7c82ddcc5ad884R697 had
i) logging if COPYSEQ_NOT_SUBSCRIBED was detected. Should this do
something similar?
ii) a comment saying the error must be avoided. Should this do
something similar?
Something like:
current_relstate = GetSubscriptionRelState(...);
if (current_relstate != SUBREL_STATE_SYNCDONE || current_lsn < statelsn)
{
char * msg = (current_relstate == SUBREL_STATE_UNKNOWN) ?
"a concurrent refresh has removed relation oid %u of
subscription \"%s\"" :
"a concurrent refresh has changed relation oid %u of
subscription \"%s\"";
ereport(LOG, errmsg(msg, rstate->relid, MySubscription->name));
/*
* Skipping is the only sensible action. It must not be treated as an error
* because when disable_on_error is true, that would disable the entire
* subscription, including unrelated tables.
*/
continue;
}
~~~
2.
IIUC,
i) If the concurrent REFRESH removed the relation, then the new
relstate will be SUBREL_STATE_UNKNOWN.
ii) If there were multiple concurrent REFRESHes and the same relation
got re-added, then the state would be SUBREL_STATE_INIT.
Either way, the state is not SUBREL_STATE_SYNCDONE.
AFAIK, there is no way for a newly added same relation to get back to
SUBREL_STATE_SYNCDONE while we are still blocked on this lock. IOW,
was that extra LSN check (current_lsn < statelsn) really needed? Is
just checking the state enough?
Kind Regards,
Peter Smith.
Fujitsu Australia
Hi,
Thanks for the review!
On Mon, 28 Sept 2026 at 14:48, Peter Smith <smithpb2250@gmail.com> wrote:
A couple of comments for 0001.
======
1. + if (GetSubscriptionRelState(MyLogicalRepWorker->subid, + rstate->relid, &statelsn) != SUBREL_STATE_SYNCDONE || + current_lsn < statelsn) + continue;1.
Would a local variable simplify the condition?
Yes, done.
Also, the related SEQUENCE code [1] had
i) logging if COPYSEQ_NOT_SUBSCRIBED was detected. Should this do
something similar?
ii) a comment saying the error must be avoided. Should this do
something similar?
I added a DEBUG1 message. I didn't use LOG because, unlike the
sequence case, the copy has already finished by this point. We only
skip the READY update for a row that's gone (or now belongs to a new
sync), so there isn't really anything for the user to act on?
For the comment, I kept it to one line saying the table may have been
removed or re-added while we were waiting for the lock.
2.
IIUC,i) If the concurrent REFRESH removed the relation, then the new
relstate will be SUBREL_STATE_UNKNOWN.
ii) If there were multiple concurrent REFRESHes and the same relation
got re-added, then the state would be SUBREL_STATE_INIT.Either way, the state is not SUBREL_STATE_SYNCDONE.
AFAIK, there is no way for a newly added same relation to get back to
SUBREL_STATE_SYNCDONE while we are still blocked on this lock. IOW,
was that extra LSN check (current_lsn < statelsn) really needed? Is
just checking the state enough?
Yeah, I think you're right, got rid of it.
Attached is v2 with those changes. Thoughts?
Regards,
Ayush
Attachments:
t253947_3v2-0001-Recheck-table-sync-state-after-refresh.patchapplication/octet-stream; name=v2-0001-Recheck-table-sync-state-after-refresh.patchDownload+15-4
v2-0002-Test-table-sync-after-a-concurrent-refresh.patchapplication/octet-stream; name=v2-0002-Test-table-sync-after-a-concurrent-refresh.patchDownload+127-1
Attached is v2 with those changes. Thoughts?
Hi Ayush.
//////////
Patch v2-0001
//////////
Patch 0001 looks good to me, but more eyes than mine need to review it.
//////////
Patch v2-0002
//////////
I confirmed the test fails when patch 0001 is not applied and passes
when it is applied.
I assume the test code is AI-generated, because it looks way less
human-readable than a typical TAP test. If the test is ever intended
to be pushed, then it needs lots of comments to describe what it is
doing and expecting at each step.
FWIW, I've attached a copy of that test code augmented with some
AI-generated comments. It's still very difficult -- I don't claim to
understand everything it is doing -- but the comments might be helpful
to somebody. PSA.
======
Kind Regards,
Peter Smith.
Fujitsu Australia
Attachments:
Hi,
On Tue, 29 Sept 2026 at 07:30, Peter Smith <smithpb2250@gmail.com> wrote:
Attached is v2 with those changes. Thoughts?
Hi Ayush.
//////////
Patch v2-0001
//////////Patch 0001 looks good to me, but more eyes than mine need to review it.
Thanks for the review!
//////////
Patch v2-0002
//////////I confirmed the test fails when patch 0001 is not applied and passes
when it is applied.I assume the test code is AI-generated, because it looks way less
human-readable than a typical TAP test. If the test is ever intended
to be pushed, then it needs lots of comments to describe what it is
doing and expecting at each step.
I do not intend this to be pushed since as I stated upthread its
a niche case that came out of manual review rather than field report.
I'll post a simplified version in some time for the same, thanks
for looking into it.
Regards,
Ayush
Hi,
On Tue, 29 Sept 2026 at 10:14, Ayush Tiwari <ayushtiwari.slg01@gmail.com> wrote:
On Tue, 29 Sept 2026 at 07:30, Peter Smith <smithpb2250@gmail.com> wrote:
Attached is v2 with those changes. Thoughts?
Hi Ayush.
//////////
Patch v2-0001
//////////Patch 0001 looks good to me, but more eyes than mine need to review it.
Thanks for the review!
//////////
Patch v2-0002
//////////I confirmed the test fails when patch 0001 is not applied and passes
when it is applied.I assume the test code is AI-generated, because it looks way less
human-readable than a typical TAP test. If the test is ever intended
to be pushed, then it needs lots of comments to describe what it is
doing and expecting at each step.I do not intend this to be pushed since as I stated upthread its
a niche case that came out of manual review rather than field report.I'll post a simplified version in some time for the same, thanks
for looking into it.
Attached is v3, with simplified tests and 0001 remains the same.
Regards,
Ayush
Attachments:
t253947_6v3-0001-Recheck-table-sync-state-after-refresh.patchapplication/octet-stream; name=v3-0001-Recheck-table-sync-state-after-refresh.patchDownload+15-4
v3-0002-Test-table-sync-after-a-concurrent-refresh.patchapplication/octet-stream; name=v3-0002-Test-table-sync-after-a-concurrent-refresh.patchDownload+89-1
Hi Ayush,
Attached is v3, with simplified tests and 0001 remains the same.
v3 applies cleanly, the tests pass. 0001 looks correct to me. 0002
catches the bug reliably: reverting just the recheck block makes it
fail.
Looks good to me.
--
Best regards,
Aleksander Alekseev
Hi Ayush,
I discussed your latest v3 patch with my LLM. It agreed with the code,
but recommended more explanation about the logic/details.
e.g.
SUGGESTED COMMENT (before the current_relstate assignment)
/*
* Re-read the state under the lock. A concurrent REFRESH
* may have removed or re-added the table while we waited.
* Only the apply worker launches sync workers, so a
* re-added table can't have reached SYNCDONE yet. Skip
* rather than error, since that would disable the whole
* subscription under disable_on_error.
*/
======
Kind Regards,
Peter Smith.
Fujitsu Australia
Hi,
On Wed, 25 Nov 2026 at 11:34, Peter Smith <smithpb2250@gmail.com> wrote:
I discussed your latest v3 patch with my LLM. It agreed with the code,
but recommended more explanation about the logic/details.e.g.
SUGGESTED COMMENT (before the current_relstate assignment)
/*
* Re-read the state under the lock. A concurrent REFRESH
* may have removed or re-added the table while we waited.
* Only the apply worker launches sync workers, so a
* re-added table can't have reached SYNCDONE yet. Skip
* rather than error, since that would disable the whole
* subscription under disable_on_error.
*/
Thanks for re-reviewing the patch.
I've updated the comment after altering a bit. Attached v4.
Regards,
Ayush