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

Attachment: v4-0001-log-unapplied-changes-for-non-ready-relations.patch
Description: Binary data

Reply via email to