[PATCH] Report changes discarded for relations not in the subscription

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

[PATCH] Report changes discarded for relations not in the subscription

От:
Hüseyin Demir <huseyin.d3r@gmail.com>
Дата:
Вложения:
Hi,

I encountered an issue after a pg_upgrade of a subscriber instance.
pg_subscription_rel came back empty, and the apply worker then discarded
every incoming change while the replication origin and the publisher
slot's confirmed_flush_lsn advanced past the discarded rows. Both ends
durably recording the transactions as applied, with nothing in either log
to say otherwise.

a relation that is not SUBREL_STATE_READY
is skipped with no log message at any elevation, no conflict and no
statistics counter.  That is correct while a tablesync worker is still
copying the table, but it is indistinguishable from a relation with no
pg_subscription_rel row at all, where the discard is permanent.  The
latter also occurs in ordinary operation, whenever a table is added to a
publication and the subscriber has not refreshed.

Behaviour is unchanged and the patch only
adds debug logging, so that the discard is visible and the two cases can
be told apart.

  - DEBUG1 when the relation is not part of the subscription, separating
    "the subscription has no tables at all" from "this one relation is not
    in the subscription", each with a hint pointing at
    ALTER SUBSCRIPTION ... REFRESH PUBLICATION.

  - DEBUG2, with sync state and LSNs, when the relation is merely not
    synchronized yet.

log_min_messages = debug1 for a minute then names the relation and tells a
lost table list apart from a missing REFRESH -- two conditions with
different fixes that the server currently cannot distinguish.

Reporting is throttled per relation. Basically a sync state change is reported
immediately, otherwise at most one message per
wal_retrieve_retry_interval, which is already the retry throttle for this
subsystem in syncutils.c and launcher.c and has a minimum of 1 ms, so
unlike a wal_receiver_timeout of 0 it cannot disable the throttle.  The
throttling state lives in LogicalRepRelMapEntry, so this is HEAD-only.

A TAP test is included in 024_add_drop_pub.pl for the published-but-not-
refreshed case, asserting both the message and that it is emitted once
rather than once per row.

Demir.

Re: [PATCH] Report changes discarded for relations not in the subscription

От:
Hüseyin Demir <huseyin.d3r@gmail.com>
Дата:
Вложения:
Hi Hayato,

> > Dear Hüseyin,
> >
> > > I encountered an issue after a pg_upgrade of a subscriber instance.
> > > pg_subscription_rel came back empty, and the apply worker then discarded
> > > every incoming change while the replication origin and the publisher
> > > slot's confirmed_flush_lsn advanced past the discarded rows. Both ends
> > > durably recording the transactions as applied, with nothing in either log
> > > to say otherwise.
> >
> > To confirm, what was the datasabse version of old and new cluster? 9a17be1e2
> > allowed to migrate the content of pg_subscription_rel catalog[1]. It means it's
> > unexpected behavior and must be fixed if old one is PG17+.
> >
> > [1]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=9a17be1e2
> >
> > Best regards,
> > Hayato Kuroda
> > FUJITSU LIMITED

Created a v2 patch and let me summarize the problem.

If a table is added to a publication (ALTER PUBLICATION ... ADD TABLE)
and ALTER SUBSCRIPTION ... REFRESH PUBLICATION is not run on the
subscriber, the table has no pg_subscription_rel row.  The apply
worker drops every change for it in should_apply_changes_for_rel(),
with no log message, no conflict and no statistics counter.  The
publisher is told the changes were applied, so the origin and the slot
move past them.  Rows only reappear if the table is later added and
copied again.

Report the skip from should_apply_changes_for_rel():

- relation not in the subscription: LOG, with a hint to run ALTER
SUBSCRIPTION ... REFRESH PUBLICATION
- relation not yet synchronized: DEBUG2

Each relation is reported once per sync state for the lifetime of the
apply worker, tracked in LogicalRepRelMapEntry, since this gate is
reached once per change.  A restarted worker reports again.

Example:
LOG:  skipping changes for relation "public.tab_4" in subscription "tap_sub"
DETAIL:  The relation is not part of the subscription.
HINT:  Use ALTER SUBSCRIPTION ... REFRESH PUBLICATION to add it.
No behavior change beyond the new messages.  The patch includes TAP
tests for the apply worker (024_add_drop_pub.pl) and for a parallel
apply worker (015_stream.pl).  I intend this for master only.


Regards,
Demir.

Re: [PATCH] Report changes discarded for relations not in the subscription

От:
Peter Smith <smithpb2250@gmail.com>
Дата:
Some initial review comments for patch v2.

======
src/backend/replication/logical/worker.c

report_unapplied_change:

1.
+/*
+ * Report that changes for this relation are not being applied.
+ *
+ * A relation that is not ready is skipped silently on every change.  That is
+ * correct while a tablesync worker is still copying it, but it is
+ * indistinguishable from a relation missing from pg_subscription_rel
+ * altogether -- one whose changes are discarded, and acknowledged to the
+ * publisher as applied, for as long as the subscription runs.  Report the
+ * latter at LOG and the former, which is transient and expected, at DEBUG2.
+ *
+ * The gate this serves is reached once per change, so report each sync state
+ * of a relation only once.  The record is kept in the relation map entry, so
+ * it lasts as long as the worker does.
+ */

Much of this seemed like background and justification for the patch,
instead of describing what the function does. IMO, saying "latter" and
"former" is difficult to understand without reading it several times.
Most of this paragraph might be better moved to comments in the
function body, not in the function header.

~~~

2.
+ if (rel->reported && rel->state == rel->reportedstate)
+ return;

Where are `reported` and `reportedstate` initialised to non-garbage?

~~~

3.
+ ereport(LOG,
+ errmsg("skipping changes for relation \"%s.%s\" in subscription \"%s\"",
+   rel->remoterel.nspname, rel->remoterel.relname,
+   MySubscription->name),
+ errdetail("The relation is not part of the subscription."),
+ errhint("Use ALTER SUBSCRIPTION ... REFRESH PUBLICATION to add it."));

The errmsg and errdetail seem conflicting, by firstly saying "in
subscription \"%s\", and then saying "The relation is not part of the
subscription."

SUGGESTION
errmsg("skipping changes for relation \"%s.%s\"" ...)
errdetail("The published relation is not included in subscription \"%s\"." ...)
errhint("Consider using ALTER SUBSCRIPTION ... REFRESH PUBLICATION")

My LLM said "to add it" in the errhint might be a bit strong -- e.g.
there can be legitimate reasons for the unapplied change. E.g there
might have been a RERESH PUBLICATION moments before that removed the
row, so these in-flight changes really should be skipped.

======
src/include/replication/logicalrelation.h

4.
+ /*
+ * Sync state.
+ *
+ * reported and reportedstate limit report_unapplied_change() to one
+ * report per sync state: whether a report was made, and for which state.
+ * They are kept next to state so that they occupy padding that follows it
+ * anyway.
+ */
  char state;
+ bool reported;
+ char reportedstate;
  XLogRecPtr statelsn;
 } LogicalRepRelMapEntry;

4A
Why do we need the commentary about padding?

~

4B.
IIUC, you could achieve the same with a single member "char
last_unapplied_state" with an initial value of '?'.

#define SUBREL_STATE_INIT_LAST_UNAPPLIED '?'

======
Kind Regards,
Peter Smith.
Fujitsu Australia


Re: [PATCH] Report changes discarded for relations not in the subscription

От:
Hüseyin Demir <huseyin.d3r@gmail.com>
Дата:
Вложения:
> Some initial review comments for patch v2.
>
> ======
> src/backend/replication/logical/worker.c
>
> report_unapplied_change:
>
> 1.
> +/*
> + * Report that changes for this relation are not being applied.
> + *
> + * A relation that is not ready is skipped silently on every change.  That is
> + * correct while a tablesync worker is still copying it, but it is
> + * indistinguishable from a relation missing from pg_subscription_rel
> + * altogether -- one whose changes are discarded, and acknowledged to the
> + * publisher as applied, for as long as the subscription runs.  Report the
> + * latter at LOG and the former, which is transient and expected, at DEBUG2.
> + *
> + * The gate this serves is reached once per change, so report each sync state
> + * of a relation only once.  The record is kept in the relation map entry, so
> + * it lasts as long as the worker does.
> + */
>
> Much of this seemed like background and justification for the patch,
> instead of describing what the function does. IMO, saying "latter" and
> "former" is difficult to understand without reading it several times.
> Most of this paragraph might be better moved to comments in the
> function body, not in the function header.
>

Makes sense. Splitted the comments

> 2.
> + if (rel->reported && rel->state == rel->reportedstate)
> + return;
>
> Where are `reported` and `reportedstate` initialised to non-garbage?

While checking this my LLM also noticed that the entry is re-zeroed
whenever the publisher resends a RELATION message, for example after
DDL. So the report is once per sync state each time the entry is
built, not once for the lifetime of the worker as v2 claimed. I
corrected the comments and the commit message.

> 3.
> + ereport(LOG,
> + errmsg("skipping changes for relation \"%s.%s\" in subscription \"%s\"",
> +   rel->remoterel.nspname, rel->remoterel.relname,
> +   MySubscription->name),
> + errdetail("The relation is not part of the subscription."),
> + errhint("Use ALTER SUBSCRIPTION ... REFRESH PUBLICATION to add it."));
>
> The errmsg and errdetail seem conflicting, by firstly saying "in
> subscription \"%s\", and then saying "The relation is not part of the
> subscription."
>
> SUGGESTION
> errmsg("skipping changes for relation \"%s.%s\"" ...)
> errdetail("The published relation is not included in subscription \"%s\"." ...)
> errhint("Consider using ALTER SUBSCRIPTION ... REFRESH PUBLICATION")
>
> My LLM said "to add it" in the errhint might be a bit strong -- e.g.
> there can be legitimate reasons for the unapplied change. E.g there
> might have been a RERESH PUBLICATION moments before that removed the
> row, so these in-flight changes really should be skipped.
>

Appreciated, added this enhancement to v3 patch as well.

> src/include/replication/logicalrelation.h
>
> 4.
> + /*
> + * Sync state.
> + *
> + * reported and reportedstate limit report_unapplied_change() to one
> + * report per sync state: whether a report was made, and for which state.
> + * They are kept next to state so that they occupy padding that follows it
> + * anyway.
> + */
>   char state;
> + bool reported;
> + char reportedstate;
>   XLogRecPtr statelsn;
>  } LogicalRepRelMapEntry;
>
> 4A
> Why do we need the commentary about padding?
>
> ~
>
> 4B.
> IIUC, you could achieve the same with a single member "char
> last_unapplied_state" with an initial value of '?'.
>
> #define SUBREL_STATE_INIT_LAST_UNAPPLIED '?'

Yes, that works and is simpler. v3 replaces reported and reportedstate
with a single last_unapplied_state, and report_unapplied_change() now
just checks rel->state == rel->last_unapplied_state.

The relation map entries are zeroed with memset() when they are built,
and a zero here is SUBREL_STATE_UNKNOWN. That is exactly the state
that gets the LOG message, so a zeroed field would wrongly suppress
it. v3 therefore sets the sentinel explicitly after the memset() in
both logicalrep_relmap_update() and logicalrep_partition_open(), and
the field's comment says so. I used your
SUBREL_STATE_INIT_LAST_UNAPPLIED name and defined it in
logicalrelation.h, next to the struct that uses it.

Greetings,
Demir.

Re: [PATCH] Report changes discarded for relations not in the subscription

От:
Peter Smith <smithpb2250@gmail.com>
Дата:
Some review comments for patch v3.

======
src/backend/replication/logical/worker.c

1.
+ /*
+ * This is reached once per change, so remember the last state reported in
+ * the relation map entry and stay quiet until the state changes.
+ */
+ if (rel->state == rel->last_unapplied_state)
+ return;

Comment seems overly verbose. How about just something like :

/* Quick return if no state change. */

======
src/test/subscription/t/024_add_drop_pub.pl

2.
I'm not sure why 2 test tables (`tab_4` and `tab_5`) are needed. Isn't
one enough?

~

3.
It's a bit overkill to insert 100+ rows to test this. Maybe do less
work to make tests more efficient.

======

On Mon, Nov 23, 2026 at 2:08 AM Hüseyin Demir  wrote:
>
...
> >
> > 4B.
> > IIUC, you could achieve the same with a single member "char
> > last_unapplied_state" with an initial value of '?'.
> >
> > #define SUBREL_STATE_INIT_LAST_UNAPPLIED '?'
>
> Yes, that works and is simpler. v3 replaces reported and reportedstate
> with a single last_unapplied_state, and report_unapplied_change() now
> just checks rel->state == rel->last_unapplied_state.
>
> The relation map entries are zeroed with memset() when they are built,
> and a zero here is SUBREL_STATE_UNKNOWN. That is exactly the state
> that gets the LOG message, so a zeroed field would wrongly suppress
> it. v3 therefore sets the sentinel explicitly after the memset() in
> both logicalrep_relmap_update() and logicalrep_partition_open(), and
> the field's comment says so. I used your
> SUBREL_STATE_INIT_LAST_UNAPPLIED name and defined it in
> logicalrelation.h, next to the struct that uses it.
>

OK, that's like what I meant. Although I am not sure if
SUBREL_STATE_INIT_LAST_UNAPPLIED would be better defined adjacent to
all the other non-stored SUBREL_XXX.

Meanwhile, I won't review this patch further until more people review
it and agree that this state-based logging is the best strategy.

======
Kind Regards,
Peter Smith.
Fujitsu Australia


FAQ