From 04dd551f2571fcbbbdb543e8ab776543d632df2d Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
Date: Wed, 25 Nov 2026 22:43:56 +0900
Subject: [PATCH v5] 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 | 25 +++++++++++++++++++++
 contrib/test_decoding/sql/messages.sql      | 17 ++++++++++++++
 doc/src/sgml/logicaldecoding.sgml           | 13 +++++++----
 doc/src/sgml/protocol.sgml                  |  3 ++-
 src/backend/replication/logical/decode.c    | 14 +++++++++++-
 5 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/contrib/test_decoding/expected/messages.out b/contrib/test_decoding/expected/messages.out
index 84baf8af3ee..dc2ceda182b 100644
--- a/contrib/test_decoding/expected/messages.out
+++ b/contrib/test_decoding/expected/messages.out
@@ -77,6 +77,31 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'for
  COMMIT
 (13 rows)
 
+-- Test the reported LSN.  pg_logical_emit_message() returns the end LSN of
+-- the message's WAL record.
+-- 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..bbde91f016a 100644
--- a/contrib/test_decoding/sql/messages.sql
+++ b/contrib/test_decoding/sql/messages.sql
@@ -22,6 +22,23 @@ 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.  pg_logical_emit_message() returns the end LSN of
+-- the message's WAL record.
+-- 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..d76881bd167 100644
--- a/doc/src/sgml/logicaldecoding.sgml
+++ b/doc/src/sgml/logicaldecoding.sgml
@@ -1143,10 +1143,13 @@ 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 of
+      the message's WAL record for a transactional message, like the LSN of
+      the other changes in the transaction. For a non-transactional message
+      it is the end of the record.
+      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
@@ -1378,6 +1381,8 @@ typedef void (*LogicalDecodeStreamMessageCB) (struct LogicalDecodingContext *ctx
       if it was emitted in one. For the other stream callbacks, the XID of
       the subtransaction is available as
       <literal>change-&gt;txn-&gt;xid</literal>.
+      The <parameter>message_lsn</parameter> is the start of the message's
+      WAL record, as only transactional messages are passed to this callback.
      </para>
     </sect3>
 
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..031bb1001bd 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,
+	/*
+	 * Transactional messages use origptr as their LSN, like regular DML
+	 * changes.
+	 *
+	 * Non-transactional messages use endptr instead, since we assume that
+	 * each message is applied and confirmed as a unit of its own, like a
+	 * commit. Using origptr 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

