Re: Per-table resync for logical replication subscriptions

From: Cagri Biroglu <cagri(dot)biroglu(at)adyen(dot)com>
To: shveta malik <shveta(dot)malik(at)gmail(dot)com>
Cc: Shlok Kyal <shlok(dot)kyal(dot)oss(at)gmail(dot)com>, "Hayato Kuroda (Fujitsu)" <kuroda(dot)hayato(at)fujitsu(dot)com>, Ajin Cherian <itsajin(at)gmail(dot)com>, "smithpb2250(at)gmail(dot)com" <smithpb2250(at)gmail(dot)com>, "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, shveta malik <shvetamalik(at)gmail(dot)com>
Subject: Re: Per-table resync for logical replication subscriptions
Date: 2026-12-01 08:37:55
Message-ID: CAA36msqBNag1yT_6Sywxgnm+=BBn1KvrdKrrGPRFJQgvkBELnQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Shveta,

it had not been discussed earlier in the thread. The attached v16
adds 0003 for it.

REFRESH TABLE now connects to the publisher before truncating, and
fails if a named table is missing there or is not published by any of
the subscription's publications. In that case the local rows and
sync states are left unchanged.

The check is per table: the OID is looked up by name as tablesync
does, then passed to pg_get_publication_tables() with the publication
names (fd7a25af11e). Older publishers get the tablesync fallback.
Membership is not changed, and a later change on the publisher is
still left to [1].

One consequence is that the command now always connects. A
subscription owned by a non-superuser therefore needs password
authentication for it, and 039 sets that up.

The new test, 041, covers unpublished, dropped and unlogged
replacement tables, an unreachable publisher, and the old partition
after a publish_via_partition_root change.

Best regards,
Cagri Biroglu

On Mon, Nov 23, 2026 at 10:26 AM shveta malik <shveta(dot)malik(at)gmail(dot)com>
wrote:

> On Sat, Nov 21, 2026 at 11:41 PM Cagri Biroglu <cagri(dot)biroglu(at)adyen(dot)com>
> wrote:
> >
> > Hi Shveta,
> >
> > Thanks, and thanks Peter for pointing you here. Having followed [1],
> > I agree it is a use case: with your reproducer on master today the
> > table is copied whole, and once the publication is back, REFRESH
> > TABLE re-copies just that table with its column list and row filter.
> > It would also be how to copy a skipped table, if [1] later moves to a
> > WARNING as Peter suggested.
>
> Agreed.
>
> > For the case you describe, I would keep REFRESH TABLE as it is and
> > rely on [1]:
> >
> > - With [1], the re-seeded table ends where [1] leaves any table
> whose
> > initial sync finds it unpublished: it stays in 'd', nothing is
> > copied, and "with the ERROR the next retry simply syncs it" once
> it
> > is published again, as you said in [1]. If it stays unpublished,
> > the way out, with disable_on_error too, is the one your v4-0002
> > documents. I tested both on v15 with your v4.
>
> The end state is the same, but the difference lies in what happens to
> the data beforehand. With [1] alone, nothing is lost: the sync fails
> and the local rows stay. REFRESH TABLE, however, truncates them first
> and then hits the same error, so it turns a harmless "not published"
> state into data loss.
>
> The same happens independently of [1] when the table is dropped on the
> publisher: REFRESH TABLE truncates it, and the sync then keeps failing
> with "table ... not found on publisher". The error itself is right,
> but by the time it is raised, the subscriber's data is already lost,
> before the user can do anything about it.
>
> >
> > - Removing it has the problems you gave in [1] against the worker
> > doing it: a table published again stays out until someone runs
> > REFRESH PUBLICATION, and a per-table removal is only a partial
> > refresh, e.g. after a publish_via_partition_root change it would
> > drop the partitions without adding the root.
>
> Agreed, a per-table removal is only a partial refresh. I am fine with
> not doing it in REFRESH TABLE.
>
> >
> > - A check with the publisher first cannot replace [1], since the
> > table can be unpublished right after it, and every REFRESH TABLE
> > would have to connect; for ready tables under origin = any it does
> > not today.
>
> Agreed, it can't replace [1], and it is not meant to. It only avoids
> discarding data on a request that is already invalid when it is made;
> [1] remains the safety net for later changes.
>
> The connection is needed anyway; the command exists to copy the table
> again from the pub. If the publisher can't be reached, the table can't
> be refilled either, so IMO, it is better to fail before truncating.
>
> > Done with fetch_relation_list(), it also has the
> > publisher open every published table, the cost fd7a25af11e took
> out
> > of tablesync: about 160 ms for a FOR ALL TABLES publication of 50k
> > tables here, and it waits behind an ACCESS EXCLUSIVE lock on any
> of
> > them.
>
>
> The cost need not be that of fetch_relation_list(). The check can use
> the same per-table lookups as tablesync; look up the table by name,
> then call pg_get_publication_tables() with the publication names and
> the table's OID (the form added by fd7a25af11e).
>
> The check could occur just before the truncate. It can't rule out a
> table being unpublished right after it or before the copy at ENABLE,
> but that is a concurrent change on the publisher, which [1] already
> handles with an error. The aim is narrower: REFRESH TABLE should not
> discard the local data when the table is already not published at the
> time it is run, which is the case a user is far more likely to hit.
> Thoughts?
>
> >
> > REFRESH TABLE is meant for a table that is still published and
> > replicating but has drifted, and when to reconcile membership stays
> > with the user, as with REFRESH SEQUENCES. So in the next version I
> > would add the note REFRESH SEQUENCES has, to run REFRESH PUBLICATION
> > first if the publication's set of tables has changed; a table it
> > removes is then refused by REFRESH TABLE without being truncated.
>
> The note helps a user who knows the set of tables has changed, not one
> who doesn't, e.g. when someone else manages the publisher. Such a
> user, seeing the sync fail, may well run REFRESH TABLE to fix it and
> lose the rows. The patch already refuses a table fed by another
> subscription "rather than silently losing data"; IMO the same
> reasoning applies here.
>
> [1]:
> https://www.postgresql.org/message-id/CAJpy0uAWSLH-SMeH0JoqvGACv8truSfXiwFp4PCkTJE93CGigA%40mail.gmail.com
>
> ~~
>
> Overall, my only concern is the TRUNCATE part: the command should not
> end up discarding the local data for an invalid request. Kindly let me
> know if this has already been discussed earlier in the thread, as I
> haven't had a chance to read it completely.
>
> thanks
> Shveta
>

Attachment Content-Type Size
v16-0002-Allow-ALTER-SUBSCRIPTION-.-REFRESH-TABLE-on-part.patch application/octet-stream 46.6 KB
v16-0001-Add-ALTER-SUBSCRIPTION-.-REFRESH-TABLE-to-resync.patch application/octet-stream 88.4 KB
v16-0003-Check-the-publisher-before-REFRESH-TABLE-truncat.patch application/octet-stream 23.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Florin Irion 2026-12-01 08:41:50 Re: pg_plan_advice: add NO_ scan and join method tags
Previous Message Amit Kapila 2026-12-01 08:28:29 Re: Preserve conflict log table during upgrade