| From: | Nikhil Sontakke <nikhil(at)planetscale(dot)com> |
|---|---|
| To: | PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org> |
| Cc: | Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>, Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com> |
| Subject: | Streamed transactions lose replication origin when first change is not a row change |
| Date: | 2026-12-02 11:14:34 |
| Message-ID: | CA+UBoq2i_NTi5ym5eyxvieTV7GO1u5VXWY28B5RDsy2yqmn=hA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
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().
The test in 100_bugs.pl streams transactions starting with a row change
(control), TRUNCATE, a small logical message and a logical message
larger than logical_decoding_work_mem, and checks that exactly one
Origin message naming the right origin is sent. Without the patch, the
TRUNCATE and message cases fail. The large-message case is the one that
needs the message change: a small message is followed by row changes
that set the origin before the first block.
Back branches:
- 16 to 18 have the same streaming rule, so the same fix works, but
the ReorderBufferQueueMessage() signature can't change there. I'd
add a separate function that takes the origin and keep the old one as
a wrapper. Alternatively, back-patch only the ReorderBufferQueueChange()
part, leaving the case where transactional messages alone fill
logical_decoding_work_mem before any row change.
- In 14 and 15 a transaction is picked for streaming by size alone, so
it can be streamed when it holds only command ID and invalidation
changes, for example after a lot of DDL and before any row change.
The origin isn't known at that point, so this fix isn't complete
there. Fixing it would need pgoutput to send Origin with the first
block that has a known origin, or the origin to be taken from every
decoded record of the transaction. Given that built-in replication
is not affected, perhaps 14 and 15 aren't worth it?
Thoughts?
---
Nikhil Sontakke
PlanetScale
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Fix-replication-origin-of-streamed-transactions.patch | application/octet-stream | 9.4 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Etsuro Fujita | 2026-12-02 11:16:13 | Re: Asynchronous MergeAppend |
| Previous Message | Chengpeng Yan | 2026-12-02 10:43:23 | Re: memory leak in bounded sorts |