On Tue, Nov 10, 2026 at 9:19 AM Masahiko Sawada <[email protected]>
wrote:
Thanks for your review!
Regarding the patch:
- ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
+ /*
+ * Non-transactional messages are processed as separate transactions
on
+ * the receiver, so use endptr as the confirmed flush position for
them.
+ */
+ ReorderBufferQueueMessage(ctx->reorder, xid, snapshot,
+ message->transactional ? buf->origptr : buf->endptr,
message->transactional,
I think this needs more comments: what we assume on the consumer side
(a non-transactional message is applied and confirmed as a unit of its
own, like a commit), and what happens if we used origptr for a
non-transactional message instead; a consumer that reports the
message's LSN as its flush position can get the message decoded again
after a restart, since decoding resumes from the confirmed position
inclusive. It's the same reason why the commit message carries both
commit_lsn and end_lsn.
Agreed.
Also, logicaldecoding.sgml says "The lsn has WAL location of the
message" for message_cb, which needs the same update, in the
back-branch patches too.
Agreed.
Also, since the parameter is actually named `message_lsn` rather than
`lsn`, the attached patch changes this as well.
On 2026-11-12 07:15, João Rito wrote:
Thanks for your review!
One more small idea: the existing tests pass both before and after the
patch, so none of them covers this change. Maybe a small test in
contrib/test_decoding could check that a transactional message and the
next change get different LSNs.
It might be redundant, but the attached patch adds tests to check the
LSNs for both transactional and non-transactional messages.
What do you think?
--
Thanks,
--
Atsushi Torikoshi
Seconded from NTT DATA CORPORATION to SRA OSS K.K.
From 17562131057288d6012293ca31e4cc785425738b Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <[email protected]>
Date: Tue, 24 Nov 2026 23:25:03 +0900
Subject: [PATCH v4] Use start LSN for transactional logical decoding messages
Logical decoding currently reports the end LSN of a logical message
record for both transactional and non-transactional messages. This
differs from regular transactional changes, for which the reported LSN
identifies the start of the WAL record.
This difference can matter to logical decoding consumers that persist
the LSN of decoded changes and use it to determine where to resume
decoding after an unexpected interruption.
For transactional logical messages, this patch uses the start LSN, as
is done for other transactional changes.
For non-transactional logical messages, it continues to use the end
LSN. Such messages are processed independently of a surrounding
transaction, so the end LSN is used as their confirmed flush position.
---
contrib/test_decoding/expected/messages.out | 24 +++++++++++++++++++++
contrib/test_decoding/sql/messages.sql | 16 ++++++++++++++
doc/src/sgml/logicaldecoding.sgml | 9 ++++----
doc/src/sgml/protocol.sgml | 3 ++-
src/backend/replication/logical/decode.c | 14 +++++++++++-
5 files changed, 60 insertions(+), 6 deletions(-)
diff --git a/contrib/test_decoding/expected/messages.out b/contrib/test_decoding/expected/messages.out
index 84baf8af3ee..f8a57981afb 100644
--- a/contrib/test_decoding/expected/messages.out
+++ b/contrib/test_decoding/expected/messages.out
@@ -77,6 +77,30 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'for
COMMIT
(13 rows)
+-- Test the reported LSN.
+-- For transactional messages, decoding reports the start LSN,
+-- so it must be less than the returned LSN.
+SELECT pg_logical_emit_message(true, 'test', 'lsn_test') AS message_end_lsn \gset
+SELECT lsn < :'message_end_lsn'::pg_lsn AS lsn_before_end
+FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL,
+ 'force-binary', '0', 'skip-empty-xacts', '1', 'include-xids', '0')
+WHERE data = 'message: transactional: 1 prefix: test, sz: 8 content:lsn_test';
+ lsn_before_end
+----------------
+ t
+(1 row)
+
+-- Non-transactional messages use the end LSN, matching the return value.
+SELECT pg_logical_emit_message(false, 'test', 'lsn_test', true) AS message_end_lsn \gset
+SELECT lsn = :'message_end_lsn'::pg_lsn AS lsn_matches_end
+FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL,
+ 'force-binary', '0', 'skip-empty-xacts', '1', 'include-xids', '0')
+WHERE data = 'message: transactional: 0 prefix: test, sz: 8 content:lsn_test';
+ lsn_matches_end
+-----------------
+ t
+(1 row)
+
-- test db filtering
\set prevdb :DBNAME
\c template1
diff --git a/contrib/test_decoding/sql/messages.sql b/contrib/test_decoding/sql/messages.sql
index 1f3dcb63ee7..cc898982b77 100644
--- a/contrib/test_decoding/sql/messages.sql
+++ b/contrib/test_decoding/sql/messages.sql
@@ -22,6 +22,22 @@ SELECT 'ignorethis' FROM pg_logical_emit_message(true, 'test', 'czechtastic');
SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'force-binary', '0', 'skip-empty-xacts', '1', 'include-xids', '0');
+-- Test the reported LSN.
+-- For transactional messages, decoding reports the start LSN,
+-- so it must be less than the returned LSN.
+SELECT pg_logical_emit_message(true, 'test', 'lsn_test') AS message_end_lsn \gset
+SELECT lsn < :'message_end_lsn'::pg_lsn AS lsn_before_end
+FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL,
+ 'force-binary', '0', 'skip-empty-xacts', '1', 'include-xids', '0')
+WHERE data = 'message: transactional: 1 prefix: test, sz: 8 content:lsn_test';
+
+-- Non-transactional messages use the end LSN, matching the return value.
+SELECT pg_logical_emit_message(false, 'test', 'lsn_test', true) AS message_end_lsn \gset
+SELECT lsn = :'message_end_lsn'::pg_lsn AS lsn_matches_end
+FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL,
+ 'force-binary', '0', 'skip-empty-xacts', '1', 'include-xids', '0')
+WHERE data = 'message: transactional: 0 prefix: test, sz: 8 content:lsn_test';
+
-- test db filtering
\set prevdb :DBNAME
\c template1
diff --git a/doc/src/sgml/logicaldecoding.sgml b/doc/src/sgml/logicaldecoding.sgml
index f21e1115a94..5af2c22b6a5 100644
--- a/doc/src/sgml/logicaldecoding.sgml
+++ b/doc/src/sgml/logicaldecoding.sgml
@@ -1143,10 +1143,11 @@ typedef void (*LogicalDecodeMessageCB) (struct LogicalDecodingContext *ctx,
the XID of the transaction that emitted the message; for a
transactional message emitted in a subtransaction, it is the
subtransaction's XID while <parameter>txn</parameter> is the top-level
- transaction. The <parameter>lsn</parameter> has WAL
- location of the message. The <parameter>transactional</parameter> says
- if the message was sent as transactional or not. Similar to the change
- callback, in case of decoding a prepared (but yet uncommitted)
+ transaction. The <parameter>message_lsn</parameter> is the
+ start LSN of the message for transactional messages, or its end LSN for
+ non-transactional messages. The <parameter>transactional</parameter>
+ says if the message was sent as transactional or not. Similar to the
+ change callback, in case of decoding a prepared (but yet uncommitted)
transaction or decoding of an uncommitted transaction, this message
callback might also error out due to simultaneous rollback of
this very same transaction. In that case, the logical decoding of this
diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml
index 50ce1cbdb32..f785575ebba 100644
--- a/doc/src/sgml/protocol.sgml
+++ b/doc/src/sgml/protocol.sgml
@@ -6687,7 +6687,8 @@ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
<term>Int64 (XLogRecPtr)</term>
<listitem>
<para>
- The LSN of the logical decoding message.
+ The start LSN of the logical decoding message for transactional
+ messages, or its end LSN for non-transactional messages.
</para>
</listitem>
</varlistentry>
diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index f44f94d9f07..76bb4fb58be 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -668,7 +668,19 @@ logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
if (!message->transactional)
snapshot = SnapBuildGetOrBuildSnapshot(builder);
- ReorderBufferQueueMessage(ctx->reorder, xid, snapshot, buf->endptr,
+ /*
+ * For transactional messages, use origptr as the mesage's LSN, like
+ * regular DML changes.
+ *
+ * For non-transactional messages, use endptr as the message's LSN, since
+ * we assume that each message is applied and confirmed as a unit of its
+ * own, like a commit. Using origptr instead would leave the replication
+ * slot's confirmed_flush pointing at the message record itself. Since
+ * decoding resumes from the confirmed position inclusive, the message
+ * could be decoded and sent again after a restart.
+ */
+ ReorderBufferQueueMessage(ctx->reorder, xid, snapshot,
+ message->transactional ? buf->origptr : buf->endptr,
message->transactional,
message->message, /* first part of message is
* prefix */
--
2.48.1
From 17b836a4aa926eb0a68cdc6edc356595a60d3424 Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <[email protected]>
Date: Tue, 24 Nov 2026 09:20:52 +0900
Subject: [PATCH v4] Correct documentation of logical decoding message LSN
The documentation describes the LSN of a logical decoding message as
the WAL location of the message, implying the start LSN of its WAL
record. In fact, both the message callback and the logical replication
protocol report the end LSN. Correct both descriptions to state this
explicitly.
This difference may matter to logical decoding consumers, for example,
those that persist the LSN of decoded changes and use it to determine
where to resume after an unexpected interruption. Unlike DML changes,
a logical message is reported with the end LSN of its WAL record, so
such consumers may need special handling for logical messages when
determining the resume position.
---
doc/src/sgml/logicaldecoding.sgml | 8 ++++----
doc/src/sgml/protocol.sgml | 2 +-
2 files changed, 5 insertions(+), 5 deletions(-)
diff --git a/doc/src/sgml/logicaldecoding.sgml b/doc/src/sgml/logicaldecoding.sgml
index 6dc49108997..8c9efc9fe95 100644
--- a/doc/src/sgml/logicaldecoding.sgml
+++ b/doc/src/sgml/logicaldecoding.sgml
@@ -1138,10 +1138,10 @@ typedef void (*LogicalDecodeMessageCB) (struct LogicalDecodingContext *ctx,
the transaction, like the time stamp at which it has been committed and
its XID. Note however that it can be NULL when the message is
non-transactional and the XID was not assigned yet in the transaction
- which logged the message. The <parameter>lsn</parameter> has WAL
- location of the message. The <parameter>transactional</parameter> says
- if the message was sent as transactional or not. Similar to the change
- callback, in case of decoding a prepared (but yet uncommitted)
+ which logged the message. The <parameter>message_lsn</parameter> is
+ the end LSN of the message. The <parameter>transactional</parameter>
+ says if the message was sent as transactional or not. Similar to the
+ change callback, in case of decoding a prepared (but yet uncommitted)
transaction or decoding of an uncommitted transaction, this message
callback might also error out due to simultaneous rollback of
this very same transaction. In that case, the logical decoding of this
diff --git a/doc/src/sgml/protocol.sgml b/doc/src/sgml/protocol.sgml
index 50ce1cbdb32..a67e7c2eb20 100644
--- a/doc/src/sgml/protocol.sgml
+++ b/doc/src/sgml/protocol.sgml
@@ -6687,7 +6687,7 @@ psql "dbname=postgres replication=database" -c "IDENTIFY_SYSTEM;"
<term>Int64 (XLogRecPtr)</term>
<listitem>
<para>
- The LSN of the logical decoding message.
+ The end LSN of the logical decoding message.
</para>
</listitem>
</varlistentry>
--
2.48.1