Add an xid argument to the logical decoding message callbacks. Previously, when streaming an in-progress transaction, a transactional logical decoding message emitted in a subtransaction was passed to the stream_message callback with the top-level transaction only, and the callback had no way to know which subtransaction had emitted it. The change callbacks get that through ReorderBufferChange, but a message has none. pgoutput therefore sent the message with the top-level XID, and when the subtransaction was later rolled back and STREAM ABORT arrived for it, the subscriber could not discard the message with it. Oversight in commit 7259736a6e5.
To fix this, we add the XID of the (sub)transaction that emitted the message as a new argument of both the message and stream_message callbacks, and have pgoutput send it. Output plugins can now see which subtransaction a message belongs to, as they already can for the other change types, while still getting the top-level transaction as before. Both callbacks get the argument so that a plugin can keep implementing them with one function, as pgoutput does. We considered fixing this in a backpatchable way, such as passing the subtransaction's ReorderBufferTXN to the callback instead of the top-level one, but that silently changes the meaning of an existing argument, and an output plugin that keeps its state on the top-level transaction would break. Adding an argument breaks the ABI, but it is the cleaner interface, so we chose it and don't backpatch. The bug exists in the back branches too, but there have been no reports, and the built-in logical replication doesn't use logical messages there. Author: Zhijie Hou <[email protected]> Co-authored-by: Masahiko Sawada <[email protected]> Reviewed-by: Amit Kapila <[email protected]> Discussion: https://postgr.es/m/[email protected] Branch ------ master Details ------- https://git.postgresql.org/pg/commitdiff/7bb8cc295b538df21d854e55e12a32dbbd8609b1 Modified Files -------------- contrib/test_decoding/expected/stream.out | 47 +++++++++++++++++++++++++ contrib/test_decoding/sql/stream.sql | 29 +++++++++++++++ contrib/test_decoding/test_decoding.c | 22 ++++++------ doc/src/sgml/logicaldecoding.sgml | 14 +++++++- src/backend/replication/logical/logical.c | 12 +++---- src/backend/replication/logical/reorderbuffer.c | 11 +++--- src/backend/replication/pgoutput/pgoutput.c | 17 ++++----- src/include/replication/output_plugin.h | 10 ++++-- src/include/replication/reorderbuffer.h | 2 ++ 9 files changed, 132 insertions(+), 32 deletions(-)
