Re: Streamed transactions lose replication origin when first change is not a row change

From: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
To: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Cc: vignesh C <vignesh21(at)gmail(dot)com>, Nikhil Sontakke <nikhil(at)planetscale(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Streamed transactions lose replication origin when first change is not a row change
Date: 2026-12-04 11:39:26
Message-ID: CAA4eK1KWSJu+-aXkcDBc6XKXiU+FZZ+CNMye08nfhG1rne-0ZQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, Dec 4, 2026 at 1:13 AM Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> wrote:
>
> On Thu, Dec 3, 2026 at 1:53 AM vignesh C <vignesh21(at)gmail(dot)com> wrote:
> >
> > The origin is recorded on the top-level transaction in
> > ReorderBufferQueueChange() when a change with an origin is queued, and
> > it is never removed. If that change belongs to a savepoint that is
> > rolled back before the transaction is streamed, the changes are
> > discarded but the origin stays. The first streamed block then carries
> > an Origin message, although none of the changes in it has an origin.
> > Example (with the session origin set):
> > SELECT pg_replication_origin_session_setup('origin_a');
> > BEGIN;
> > SAVEPOINT s;
> > INSERT INTO t VALUES (1); -- has the origin
> > ROLLBACK TO s;
> > SELECT pg_replication_origin_session_reset(); -- origin is reset
> > INSERT INTO t SELECT generate_series(1, 5000); -- streamed
> > COMMIT;
> >
> > The transaction is local, and it is decoded with no Origin message
> > when it is not streamed. Streamed with logical_decoding_work_mem =
> > 64kB, it is sent with the origin origin_a.
> >
> > Clearing the origin whenever a subtransaction aborts would not be
> > correct, since a surviving change outside the savepoint may carry the
> > same origin. The attached test has a second case for that. One way to
> > fix it would be to keep the origin on the transaction that queued the
> > change and merge it into the top-level transaction only when a
> > subtransaction is assigned or committed.
>
> An alternative way to fix it would be to change pgoutput so that it
> remembers whether it has sent an origin message for a streamed
> transaction, and sends it right before the first DML change it sends
> for the transaction if that change has an origin. The origin message
> would not necessarily be right after the stream start, but it would
> still be before any DML messages of the transaction as the protocol
> docs say.
>

Won't in that case plugins other than pgoutput also need similar
handling? One more point to consider is that with the proposed
approach by Nikhil/Vignesh, we will be consistent with the
non-streaming case where the same origin will be used for the entire
transaction.

--
With Regards,
Amit Kapila.

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Guillaume Lelarge 2026-12-04 12:00:31 Re: Proposal: per-table row counts in pg_dump/pg_restore, with restore-time verification
Previous Message Shuntian Jiao 2026-12-04 11:19:07 Re: reltuples kept decreasing with each autovacuum run Re: BUG #18773