[PATCH] Table sync race with REFRESH PUBLICATION

Поиск
Список
Период
Сортировка
Искать

[PATCH] Table sync race with REFRESH PUBLICATION

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Вложения:
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

Re: [PATCH] Table sync race with REFRESH PUBLICATION

От:
Peter Smith <smithpb2250@gmail.com>
Дата:
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] 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?

======
[1] https://github.com/postgres/postgres/commit/dca73f7dd03ddfc89a88019659fc0161e0427a8c#diff-e188109d851e85cc74d6bb7543f8ddf2be5cc2764e976ed44a7c82ddcc5ad884R697

Kind Regards,
Peter Smith.
Fujitsu Australia


Re: [PATCH] Table sync race with REFRESH PUBLICATION

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Вложения:
Hi,

Thanks for the review!

On Mon, 28 Sept 2026 at 14:48, Peter Smith  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

Re: [PATCH] Table sync race with REFRESH PUBLICATION

От:
Peter Smith <smithpb2250@gmail.com>
Дата:
Вложения:
> 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

Re: [PATCH] Table sync race with REFRESH PUBLICATION

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Hi,

On Tue, 29 Sept 2026 at 07:30, Peter Smith  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


Re: [PATCH] Table sync race with REFRESH PUBLICATION

От:
Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Дата:
Вложения:
Hi,

On Tue, 29 Sept 2026 at 10:14, Ayush Tiwari  wrote:

> On Tue, 29 Sept 2026 at 07:30, Peter Smith  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

Re: [PATCH] Table sync race with REFRESH PUBLICATION

От:
Aleksander Alekseev <aleksander@tigerdata.com>
Дата:
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


FAQ