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

Started by Hüseyin Demir3 months ago11 messageshackers

Thread notes

1 note#feature#minor#enhancement
hackorum-bot#feature#minor#enhancement
hackorum-bot
2 days ago

@global #feature #minor #enhancement

#feature#minor#enhancement
Jump to latest
#1Hü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.

Attachments:

t253705_1
v1-0001-report-unapplied-changes-for-non-ready-relations.patchapplication/octet-stream; name=v1-0001-report-unapplied-changes-for-non-ready-relations.patchDownload+212-6
#2Hayato Kuroda (Fujitsu)
kuroda.hayato@fujitsu.com
In reply to: Hüseyin Demir (#1)
RE: [PATCH] Report changes discarded for relations not in the subscription

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]https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=9a17be1e2. 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

#3Hüseyin Demir
huseyin.d3r@gmail.com
In reply to: Hayato Kuroda (Fujitsu) (#2)
Re: [PATCH] Report changes discarded for relations not in the subscription

Hi Hayato,

The source version is 14 and the target is 18.

Hayato Kuroda (Fujitsu) <kuroda.hayato@fujitsu.com>, 9 Eyl 2026 Çar,
05:57 tarihinde şunu yazdı:

Show quoted text

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

#4Hüseyin Demir
huseyin.d3r@gmail.com
In reply to: Hüseyin Demir (#3)
Re: [PATCH] Report changes discarded for relations not in the subscription

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.

Attachments:

t253705_4
v2-0001-log-unapplied-changes-for-non-ready-relations.patchapplication/octet-stream; name=v2-0001-log-unapplied-changes-for-non-ready-relations.patchDownload+178-6
#5Peter Smith
smithpb2250@gmail.com
In reply to: Hüseyin Demir (#4)
Re: [PATCH] Report changes discarded for relations not in the subscription

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

#6Hüseyin Demir
huseyin.d3r@gmail.com
In reply to: Peter Smith (#5)
Re: [PATCH] Report changes discarded for relations not in the subscription

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.

Attachments:

t253705_6
v3-0001-log-unapplied-changes-for-non-ready-relations.patchapplication/octet-stream; name=v3-0001-log-unapplied-changes-for-non-ready-relations.patchDownload+187-5
#7Peter Smith
smithpb2250@gmail.com
In reply to: Hüseyin Demir (#6)
Re: [PATCH] Report changes discarded for relations not in the subscription

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 <huseyin.d3r@gmail.com> 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

#8shveta malik
shveta.malik@gmail.com
In reply to: Hüseyin Demir (#4)
Re: [PATCH] Report changes discarded for relations not in the subscription

On Fri, Oct 30, 2026 at 1:07 PM Hüseyin Demir <huseyin.d3r@gmail.com> wrote:

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():

+1 on the proposal. The problem seems real to me.
I think the most common real-world trigger is probably FOR ALL TABLES
and FOR TABLES IN SCHEMA publications. With those, every new table on
the publisher is published automatically, and the subscriber discards
its rows until someone refreshes. A once-per-relation LOG seems like a
cheap and reasonable fix to me. I will review the patch soon.

Show quoted text

- 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.

#9shveta malik
shveta.malik@gmail.com
In reply to: Hüseyin Demir (#4)
Re: [PATCH] Report changes discarded for relations not in the subscription

On Fri, Oct 30, 2026 at 1:07 PM Hüseyin Demir <huseyin.d3r@gmail.com> wrote:

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.

I observed that this is not true. It actually depends on the lifetime
of LogicalRepRelMapEntry, not the apply worker. In
logicalrep_relmap_update(), the entry gets cleared, which resets
'last_unapplied_state' as well. logicalrep_relmap_update() is called
on each RELATION message from the publisher. This means that whenever
the missed table is altered on the publisher (e.g. create index, alter
table), the subscriber will log the message again on the subsequent
INSERT into that table. For DDL this is fine, as it is infrequent, but
for streamed transactions, it logs once for every streamed
transaction, as each streamed transaction sends the RELATION message
again (see the comment atop RelationSyncEntry in pgoutput.c). Is this
acceptable? See the test below:

On the publisher, set logical_decoding_work_mem = '64kB'. Create a
table tab1 (create table tab1 (a int);) on both nodes and add it to
the publication, but do not refresh the subscription. Then run on the
publisher:

INSERT INTO tab1 SELECT g FROM generate_series(1, 5000) g;
INSERT INTO tab1 SELECT g FROM generate_series(1, 5000) g;
INSERT INTO tab1 SELECT g FROM generate_series(1, 5000) g;

The subscriber logs "skipping changes for relation .." three times,
once per transaction.

thanks
Shveta

#10Hüseyin Demir
huseyin.d3r@gmail.com
In reply to: shveta malik (#9)
Re: [PATCH] Report changes discarded for relations not in the subscription

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.

I observed that this is not true. It actually depends on the lifetime
of LogicalRepRelMapEntry, not the apply worker. In
logicalrep_relmap_update(), the entry gets cleared, which resets
'last_unapplied_state' as well. logicalrep_relmap_update() is called
on each RELATION message from the publisher. This means that whenever
the missed table is altered on the publisher (e.g. create index, alter
table), the subscriber will log the message again on the subsequent
INSERT into that table. For DDL this is fine, as it is infrequent, but
for streamed transactions, it logs once for every streamed
transaction, as each streamed transaction sends the RELATION message
again (see the comment atop RelationSyncEntry in pgoutput.c). Is this
acceptable? See the test below:

On the publisher, set logical_decoding_work_mem = '64kB'. Create a
table tab1 (create table tab1 (a int);) on both nodes and add it to
the publication, but do not refresh the subscription. Then run on the
publisher:

INSERT INTO tab1 SELECT g FROM generate_series(1, 5000) g;
INSERT INTO tab1 SELECT g FROM generate_series(1, 5000) g;
INSERT INTO tab1 SELECT g FROM generate_series(1, 5000) g;

The subscriber logs "skipping changes for relation .." three times,
once per transaction.

thanks
Shveta

Hi,

Ah you're right I didn't consider the streaming transactions on
publisher and your suggestion makes sense. I think I managed to
implement this.

- logicalrep_relmap_update() keeps last_unapplied_state when it
rebuilds an existing entry, so a recent RELATION message (streamed
transaction or DDL) no longer causes a second report. With v4 your
test gives one LOG line in both streaming modes.
- last_unapplied_state is reset to SUBREL_STATE_INIT_LAST_UNAPPLIED
once changes for the relation are applied again. Without this,
carrying the state over would miss a real event. For example, a table
that is skipped, then refreshed and synchronized (with no changes
arriving during the sync), then removed from the subscription again
would stay silent the second time, because the remembered state would
still be unknown.

You said a repeat after DDL is fine since DDL is infrequent. v4 does
not repeat after DDL either, since the sync state has not changed.

Regards,
Demir.

Attachments:

t253705_10
v4-0001-log-unapplied-changes-for-non-ready-relations.patchapplication/octet-stream; name=v4-0001-log-unapplied-changes-for-non-ready-relations.patchDownload+228-6
#11Hüseyin Demir
huseyin.d3r@gmail.com
In reply to: Peter Smith (#7)
Re: [PATCH] Report changes discarded for relations not in the subscription

Hi,

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

Yes. v4 uses only tab_4.

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

v4 inserts single rows. I also dropped the restart (DISABLE/ENABLE)
case. The 024 test now checks the first report, no repeat after DDL,
applying after REFRESH, and a second report after the table is removed
from the subscription again.

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.

Moved to pg_subscription_rel.h, after SUBREL_STATE_CATCHUP.

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

Understood. Shveta found that v3 logged once per streamed transaction,
because the state was lost each time the relation map entry was rebuilt.
v4 fixes that, so the behaviour is now what I described earlier: once per
sync state, and again after changes have been applied in between

Regards,
Demir.

Attachments:

t253705_11
v4-0001-log-unapplied-changes-for-non-ready-relations.patchapplication/octet-stream; name=v4-0001-log-unapplied-changes-for-non-ready-relations.patchDownload+228-6