Hi, On Sat, Nov14, 2026 at 07:35 AM Masahiko Sawada wrote:
Agreed. It's better for callbacks to get both top-level and subtransaction's ids. I've updated the patches (the 0002 patch unchanged).
> - ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId; > + ctx->write_xid = xid;I think it would be better to keep the original code for consistency, as all the other callbacks set ctx->write_xid to txn->xid (the top-level transaction's XID). IIUC, this was changed to make the test pass since test_decoding does not otherwise output the subxid, but we could instead use pgoutput to test that and keep passing the top-level XID in ctx->write_xid. Please see the attached top-up patch for this change.
BTW, since we are changing the callback signature anyway, another wild idea would be to pass ReorderBufferChange directly instead so that it is fully consistent with the other change callbacks. That said, it would require more changes in third-party output plugins, so I don't insist on it if you disagree — just sharing for reference.
-- Best Regards, Zhijie Hou
From 952f55c52d9706be6f34f9c2554f4bd748f845f2 Mon Sep 17 00:00:00 2001 From: Zhijie Hou <[email protected]> Date: Mon, 16 Nov 2026 00:41:24 +0800 Subject: [PATCH] Top-up for v8-0001: Fix streamed block XID in stream_message_cb_wrapper. Author: Zhijie Hou <[email protected]> --- contrib/test_decoding/expected/stream.out | 31 ++++++++++++++++++----- contrib/test_decoding/sql/stream.sql | 21 ++++++++++----- src/backend/replication/logical/logical.c | 2 +- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/contrib/test_decoding/expected/stream.out b/contrib/test_decoding/expected/stream.out index f34a07ba761..d589c7a2b3f 100644 --- a/contrib/test_decoding/expected/stream.out +++ b/contrib/test_decoding/expected/stream.out @@ -159,6 +159,13 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc -- Verify that a transactional message emitted in a streamed subtransaction -- is associated with the subtransaction's XID. +CREATE PUBLICATION stream_pub; +SELECT 'init' FROM pg_create_logical_replication_slot('regression_pgoutput_slot', 'pgoutput'); + ?column? +---------- + init +(1 row) + SET debug_logical_replication_streaming = immediate; BEGIN; SAVEPOINT sp; @@ -170,21 +177,33 @@ SELECT 'msg_subxact' FROM pg_logical_emit_message(true, 'test', 'msg'); ROLLBACK TO SAVEPOINT sp; COMMIT; +-- Check that the streamed MESSAGE ('M') carries the aborted subtransaction's +-- XID rather than the top-level XID: in pgoutput's wire format, 'M' stores +-- its 4-byte XID at bytes 2..5, while STREAM ABORT ('A') stores the top-level +-- XID at bytes 2..5 and the aborted subtransaction's XID at bytes 6..9. WITH changes AS ( - SELECT xid, data - FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, - 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1') + SELECT data + FROM pg_logical_slot_get_binary_changes('regression_pgoutput_slot', NULL, NULL, + 'proto_version', '2', 'publication_names', 'stream_pub', + 'messages', 'true', 'streaming', 'on') ) -SELECT m.xid = a.xid AS subxid_matches +SELECT substr(m.data, 2, 4) = substr(a.data, 6, 4) AS subxid_matches FROM changes m, changes a -WHERE m.data LIKE 'streaming message:%' - AND a.data = 'aborting streamed (sub)transaction'; +WHERE get_byte(m.data, 0) = ascii('M') + AND get_byte(a.data, 0) = ascii('A'); subxid_matches ---------------- t (1 row) RESET debug_logical_replication_streaming; +SELECT pg_drop_replication_slot('regression_pgoutput_slot'); + pg_drop_replication_slot +-------------------------- + +(1 row) + +DROP PUBLICATION stream_pub; DROP TABLE stream_test; SELECT pg_drop_replication_slot('regression_slot'); pg_drop_replication_slot diff --git a/contrib/test_decoding/sql/stream.sql b/contrib/test_decoding/sql/stream.sql index ec68efed421..6cef3c83b09 100644 --- a/contrib/test_decoding/sql/stream.sql +++ b/contrib/test_decoding/sql/stream.sql @@ -84,6 +84,8 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'inc -- Verify that a transactional message emitted in a streamed subtransaction -- is associated with the subtransaction's XID. +CREATE PUBLICATION stream_pub; +SELECT 'init' FROM pg_create_logical_replication_slot('regression_pgoutput_slot', 'pgoutput'); SET debug_logical_replication_streaming = immediate; BEGIN; SAVEPOINT sp; @@ -91,16 +93,23 @@ SELECT 'msg_subxact' FROM pg_logical_emit_message(true, 'test', 'msg'); ROLLBACK TO SAVEPOINT sp; COMMIT; +-- Check that the streamed MESSAGE ('M') carries the aborted subtransaction's +-- XID rather than the top-level XID: in pgoutput's wire format, 'M' stores +-- its 4-byte XID at bytes 2..5, while STREAM ABORT ('A') stores the top-level +-- XID at bytes 2..5 and the aborted subtransaction's XID at bytes 6..9. WITH changes AS ( - SELECT xid, data - FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, - 'include-xids', '0', 'skip-empty-xacts', '1', 'stream-changes', '1') + SELECT data + FROM pg_logical_slot_get_binary_changes('regression_pgoutput_slot', NULL, NULL, + 'proto_version', '2', 'publication_names', 'stream_pub', + 'messages', 'true', 'streaming', 'on') ) -SELECT m.xid = a.xid AS subxid_matches +SELECT substr(m.data, 2, 4) = substr(a.data, 6, 4) AS subxid_matches FROM changes m, changes a -WHERE m.data LIKE 'streaming message:%' - AND a.data = 'aborting streamed (sub)transaction'; +WHERE get_byte(m.data, 0) = ascii('M') + AND get_byte(a.data, 0) = ascii('A'); RESET debug_logical_replication_streaming; +SELECT pg_drop_replication_slot('regression_pgoutput_slot'); +DROP PUBLICATION stream_pub; DROP TABLE stream_test; SELECT pg_drop_replication_slot('regression_slot'); diff --git a/src/backend/replication/logical/logical.c b/src/backend/replication/logical/logical.c index 44a27d0b653..95760981b26 100644 --- a/src/backend/replication/logical/logical.c +++ b/src/backend/replication/logical/logical.c @@ -1674,7 +1674,7 @@ stream_message_cb_wrapper(ReorderBuffer *cache, ReorderBufferTXN *txn, /* set output state */ ctx->accept_writes = true; - ctx->write_xid = xid; + ctx->write_xid = txn != NULL ? txn->xid : InvalidTransactionId; ctx->write_location = message_lsn; ctx->end_xact = false; -- 2.54.0 (Apple Git-157)
