| From: | Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> |
|---|---|
| To: | Amit Kapila <amit(dot)kapila16(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 22:03:45 |
| Message-ID: | CAD21AoDTMUV-SWzs8kG7GnSvwxCXubf--KJzgmDe7Tm66W8wRw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On Fri, Dec 4, 2026 at 3:39 AM Amit Kapila <amit(dot)kapila16(at)gmail(dot)com> wrote:
>
> 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.
Good point.
But since I'm confused about what value we should set to
txn->origin_id for streamed transactions, I'm not even sure that the
current behavior of txn->origin_id is a problem for other output
plugins.
The txn->origin_id should be the origin id stored in the commit record
but it's not available when streaming a transaction, so we're using
the origin_id of the first change of each chunk instead. As a result,
while we keep passing the same ReorderBufferTXN throughout a streamed
transaction, txn->origin_id can vary at every stream_start call, and
again at stream_commit.
Even if we somehow ensure to set the consistent origin id across
stream chunks, we cannot eliminate the possibilities that we could
change the origin at stream_commit. Otherwise, it would not be
consistent even between streamed transactions and non-streamed
transactions, which is bad. That means txn->origin_id before
stream_commit is only a guess, and plugins cannot rely on it being
stable anyway. So I think it would need to accept the behavior that we
could set different txn->origin_id values for each chunk and
stream_commit.
So that brought me a question: is it a problem that we call
stream_start with txn->origin_id being InvalidReplOriginId while
calling other stream_start or stream_commit with a valid one? IIUC
that should be okay. It's possible in principle that none of the
changes in a streamed transaction has an origin but the commit record
does.
If so, I think what reorderbuffer does is fine, and the problem is
rather on the pgoutput side: it decides whether to send the Origin
message only from txn->origin_id at the first stream_start. That said,
I'm not sure what pgoutput should send here. The subscriber uses the
Origin message only for the message ordering check, and changes in the
same chunk could even have different origins. Nikhil, what does your
consumer expect from the Origin message of a streamed transaction?
Regards,
--
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Baji Shaik | 2026-12-04 21:41:41 | Re: pg_dump writes a subscription's default slot name, which can't be restored |