From 7aba0ec3be9faaa33fbf6f5a9502a0d3f5e0e6fe Mon Sep 17 00:00:00 2001 From: Nikhil Sontakke Date: Wed, 2 Dec 2026 15:43:28 +0530 Subject: [PATCH] Fix replication origin of streamed transactions. When a large transaction is streamed, the reorder buffer took the transaction's replication origin from the first change of each streamed block. Not every change carries an origin: command ID, snapshot and invalidation changes never do, and transactional logical messages were queued without one. So a streamed transaction whose first change was one of these, for example a transaction starting with TRUNCATE or another statement that updates the catalogs, or with pg_logical_emit_message(), was streamed with no origin. pgoutput sends the Origin message only with the first streamed block, so downstream consumers never received it. Transactions sent at commit were not affected, since they take the origin from the commit record. Fix by recording the origin on the top-level transaction from the first queued change that has one, and by setting the origin of transactional logical message changes. Every streamable change comes from a WAL record that includes the origin, and since commit bf07ab492c4 a transaction is streamed only once it has a streamable change, so the origin is known before the first block is sent. The built-in logical replication apply worker ignores the Origin message, and origin-based filtering is done per change, so neither was affected; other consumers of the pgoutput protocol could see a replicated transaction as local. --- src/backend/replication/logical/decode.c | 1 + .../replication/logical/reorderbuffer.c | 29 ++++++- src/include/replication/reorderbuffer.h | 1 + src/test/subscription/t/100_bugs.pl | 80 +++++++++++++++++++ 4 files changed, 110 insertions(+), 1 deletion(-) diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c index 031bb1001bd..ad286664fce 100644 --- a/src/backend/replication/logical/decode.c +++ b/src/backend/replication/logical/decode.c @@ -681,6 +681,7 @@ logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf) */ ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, message->transactional ? buf->origptr : buf->endptr, + origin_id, message->transactional, message->message, /* first part of message is * prefix */ diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c index 2917d971f17..a76dfb88c0c 100644 --- a/src/backend/replication/logical/reorderbuffer.c +++ b/src/backend/replication/logical/reorderbuffer.c @@ -101,6 +101,7 @@ #include "miscadmin.h" #include "pgstat.h" #include "replication/logical.h" +#include "replication/origin.h" #include "replication/reorderbuffer.h" #include "replication/slot.h" #include "replication/snapbuild.h" /* just for SnapBuildSnapDecRefcount */ @@ -849,6 +850,28 @@ ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, toptxn->txn_flags |= RBTXN_HAS_STREAMABLE_CHANGE; } + /* + * Remember the transaction's replication origin, for streaming. It is + * taken from the first change that has one, not from whichever change + * happens to be first when streaming starts: internal changes, such as + * the command ID change queued ahead of a catalog update, never have an + * origin. Every streamable change comes from a WAL record that includes + * the origin, and a transaction is streamed only once it has a streamable + * change, so the origin is known before the first block is sent. + * + * This is a best effort: a session can change its origin in the middle of + * a transaction. The origin from the commit, prepare, commit prepared or + * rollback prepared record is authoritative, and replaces this one before + * the transaction is replayed or finished. + */ + if (change->origin_id != InvalidReplOriginId) + { + ReorderBufferTXN *toptxn = rbtxn_get_toptxn(txn); + + if (toptxn->origin_id == InvalidReplOriginId) + toptxn->origin_id = change->origin_id; + } + change->lsn = lsn; change->txn = txn; @@ -871,10 +894,14 @@ ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, XLogRecPtr lsn, /* * A transactional message is queued to be processed upon commit and a * non-transactional message gets processed immediately. + * + * origin_id is the replication origin of the message's record. It is used + * only for transactional messages, which keep it with the queued change. */ void ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid, Snapshot snap, XLogRecPtr lsn, + ReplOriginId origin_id, bool transactional, const char *prefix, Size message_size, const char *message) { @@ -896,6 +923,7 @@ ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid, change = ReorderBufferAllocChange(rb); change->action = REORDER_BUFFER_CHANGE_MESSAGE; + change->origin_id = origin_id; change->data.msg.prefix = pstrdup(prefix); change->data.msg.message_size = message_size; change->data.msg.message = palloc(message_size); @@ -2286,7 +2314,6 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn, { if (streaming) { - txn->origin_id = change->origin_id; rb->stream_start(rb, txn, change->lsn); stream_started = true; } diff --git a/src/include/replication/reorderbuffer.h b/src/include/replication/reorderbuffer.h index 612a7a81f40..84666cd1aa5 100644 --- a/src/include/replication/reorderbuffer.h +++ b/src/include/replication/reorderbuffer.h @@ -727,6 +727,7 @@ extern void ReorderBufferQueueChange(ReorderBuffer *rb, TransactionId xid, bool toast_insert); extern void ReorderBufferQueueMessage(ReorderBuffer *rb, TransactionId xid, Snapshot snap, XLogRecPtr lsn, + ReplOriginId origin_id, bool transactional, const char *prefix, Size message_size, const char *message); extern void ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid, diff --git a/src/test/subscription/t/100_bugs.pl b/src/test/subscription/t/100_bugs.pl index a39d6b0d100..f424ff397ad 100644 --- a/src/test/subscription/t/100_bugs.pl +++ b/src/test/subscription/t/100_bugs.pl @@ -673,6 +673,86 @@ $node_publisher->safe_psql('postgres', "DROP TABLE tab_upsert"); $node_publisher->stop('fast'); +# A streamed transaction lost its replication origin when its first decoded +# change was not a row change: the origin was copied from the first change of +# the streamed block, and neither the command ID change queued for a catalog +# update nor a transactional logical message carried one. pgoutput sends the +# Origin message only with the first streamed block, so it was never sent. +# The large message case streams a block holding only that message, so it +# also checks that message changes carry their origin. + +$node_publisher->rotate_logfile(); +$node_publisher->start(); + +$node_publisher->safe_psql( + 'postgres', qq( + CREATE TABLE tab_stream_origin (a int); + CREATE PUBLICATION pub_stream_origin FOR TABLE tab_stream_origin; + SELECT pg_replication_origin_create('regress_stream_origin'); +)); + +my %stream_origin_first = ( + 'a row change' => 'INSERT INTO tab_stream_origin VALUES (0);', + 'a transactional logical message' => + "SELECT pg_logical_emit_message(true, 'prefix', 'payload');", + 'a large transactional logical message' => + "SELECT pg_logical_emit_message(true, 'prefix', repeat('x', 100000));", + 'a catalog update' => 'TRUNCATE tab_stream_origin;'); + +foreach my $first (sort keys %stream_origin_first) +{ + $node_publisher->safe_psql('postgres', + "SELECT pg_create_logical_replication_slot('stream_origin_slot', 'pgoutput')" + ); + $node_publisher->safe_psql( + 'postgres', qq( + SELECT pg_replication_origin_session_setup('regress_stream_origin'); + BEGIN; + $stream_origin_first{$first} + INSERT INTO tab_stream_origin SELECT generate_series(1, 5000); + COMMIT; + SELECT pg_replication_origin_session_reset(); +)); + + # Count the Origin and Stream Start messages, and get the origin name + # from the Origin message (after the type byte and the commit LSN). The + # transaction must be streamed in more than one block, and carry exactly + # one Origin message. + my $result = $node_publisher->safe_psql( + 'postgres', qq( + SET logical_decoding_work_mem = '64kB'; + CREATE TEMP TABLE stream_origin_msgs AS + SELECT chr(get_byte(data, 0)) AS type, data + FROM pg_logical_slot_get_binary_changes( + 'stream_origin_slot', NULL, NULL, + 'proto_version', '2', + 'publication_names', 'pub_stream_origin', + 'messages', 'true', + 'streaming', 'on'); + SELECT count(*) FILTER (WHERE type = 'O'), + count(*) FILTER (WHERE type = 'S') > 1, + string_agg(convert_from(substr(data, 10, length(data) - 10), + 'UTF8'), ',') + FILTER (WHERE type = 'O') + FROM stream_origin_msgs; +)); + is($result, '1|t|regress_stream_origin', + "streamed transaction starting with $first sends its origin"); + + $node_publisher->safe_psql('postgres', + "SELECT pg_drop_replication_slot('stream_origin_slot')"); +} + +# Clean up +$node_publisher->safe_psql( + 'postgres', qq( + SELECT pg_replication_origin_drop('regress_stream_origin'); + DROP PUBLICATION pub_stream_origin; + DROP TABLE tab_stream_origin; +)); + +$node_publisher->stop('fast'); + # Start clusters for tests with injection points. Rotate log files for making # debugging easier. $node_publisher->rotate_logfile(); -- 2.54.0 (Apple Git-157)