From 17562131057288d6012293ca31e4cc785425738b Mon Sep 17 00:00:00 2001
From: Atsushi Torikoshi <torikoshia@oss.nttdata.com>
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

