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

From: vignesh C <vignesh21(at)gmail(dot)com>
To: Nikhil Sontakke <nikhil(at)planetscale(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>
Subject: Re: Streamed transactions lose replication origin when first change is not a row change
Date: 2026-12-03 09:53:20
Message-ID: CALDaNm1id+fFOOrYnp0NM8vn=t0oWrmpOoYjJv_RW8ixRfnz7Q@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 2 Dec 2026 at 16:45, Nikhil Sontakke <nikhil(at)planetscale(dot)com> wrote:
>
> Hi,
>
> When a large transaction is streamed, pgoutput sends an Origin message
> with the first streamed block only. The origin it sends,
> txn->origin_id, is copied in ReorderBufferProcessTXN() from the first
> change of the block:
>
> if (streaming)
> {
> txn->origin_id = change->origin_id;
> rb->stream_start(rb, txn, change->lsn);
>
> Not every change carries an origin. Command ID, snapshot and
> invalidation changes never do, and ReorderBufferQueueMessage() does not
> set one for transactional logical messages. So if a streamed
> transaction starts with any of these, it is streamed with no origin at
> all. Common cases:
>
> - a transaction starting with TRUNCATE, which updates the catalogs: the
> XLOG_HEAP2_NEW_CID record comes before the catalog tuple change, and
> its command ID change has no origin;
> - a transaction starting with pg_logical_emit_message(true, ...).
>
> Transactions sent at commit are fine, since they take the origin from
> the commit record. This goes back to 14, when streaming was added.
>
> Reproduction (wal_level = logical):
>
> CREATE TABLE t (a int);
> CREATE PUBLICATION pub FOR TABLE t;
> SELECT pg_replication_origin_create('upstream');
> SELECT pg_create_logical_replication_slot('s', 'pgoutput');
>
> SELECT pg_replication_origin_session_setup('upstream');
> BEGIN;
> TRUNCATE t;
> INSERT INTO t SELECT generate_series(1, 5000);
> COMMIT;
> SELECT pg_replication_origin_session_reset();
>
> SET logical_decoding_work_mem = '64kB';
> SELECT chr(get_byte(data, 0)) AS type, count(*)
> FROM pg_logical_slot_get_binary_changes('s', NULL, NULL,
> 'proto_version', '2', 'publication_names', 'pub',
> 'streaming', 'on')
> WHERE chr(get_byte(data, 0)) IN ('O', 'S')
> GROUP BY 1;
>
> This returns 11 Stream Start messages and no Origin message. With
> INSERT instead of TRUNCATE as the first statement, one Origin message
> is sent.
>
> Impact: the built-in apply worker ignores the Origin message, and
> origin = none filtering is done per change at decode time, so built-in
> logical replication is not affected. Other consumers of the pgoutput
> protocol that rely on the Origin message, for example to avoid
> replicating a change back to where it came from, see such a
> transaction as local.
>
> The attached patch, for master, records the origin on the top-level
> transaction in ReorderBufferQueueChange(), from the first queued change
> that has one, and stops copying it from the first change of each
> block. Every streamable change comes from a WAL record that includes
> the origin, and since bf07ab492c4 a transaction is streamed only once
> it has a streamable change, so the origin is known before the first
> block is sent. The patch also sets the origin of transactional message
> changes; that needs a new origin argument to ReorderBufferQueueMessage().

Thanks for reporting the issue and the patch. I found a case where it
sends an origin that the old code did not.

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.

The attached test (198_stream_origin_aborted_subxact.pl) fails with the patch.

Regards,
Vignesh

Attachment Content-Type Size
198_stream_origin_aborted_subxact.pl text/x-perl-script 3.2 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2026-12-03 10:08:08 RE: Use WALReadFromBuffers in more places
Previous Message cca5507 2026-12-03 09:50:01 Remove useless memory context switch in ExecHashTableCreate()