On Tue, Nov 24, 2026 at 6:48 AM torikoshia <[email protected]> wrote:
>
> 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.

Thank you for updating the patch!
v4-0001-Use-start-LSN-for-transactional-logical-decoding-.patch
basically looks good to me. I've attached a top-up patch for minor
things. Please feel free to merge them if you think it's okay.

The v4-0001-Correct-documentation-of-logical-decoding-message-v15-19.txt
looks good to me, so I'm going to push it, barring any objections.

Regards,

-- 
Masahiko Sawada
Amazon Web Services: https://aws.amazon.com
diff --git a/contrib/test_decoding/expected/messages.out b/contrib/test_decoding/expected/messages.out
index f8a57981afb..dc2ceda182b 100644
--- a/contrib/test_decoding/expected/messages.out
+++ b/contrib/test_decoding/expected/messages.out
@@ -77,7 +77,8 @@ SELECT data FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL, 'for
  COMMIT
 (13 rows)
 
--- Test the reported LSN.
+-- 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
diff --git a/contrib/test_decoding/sql/messages.sql b/contrib/test_decoding/sql/messages.sql
index cc898982b77..bbde91f016a 100644
--- a/contrib/test_decoding/sql/messages.sql
+++ b/contrib/test_decoding/sql/messages.sql
@@ -22,7 +22,8 @@ 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.
+-- 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
diff --git a/doc/src/sgml/logicaldecoding.sgml b/doc/src/sgml/logicaldecoding.sgml
index 5af2c22b6a5..d76881bd167 100644
--- a/doc/src/sgml/logicaldecoding.sgml
+++ b/doc/src/sgml/logicaldecoding.sgml
@@ -1143,9 +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>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>
+      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
@@ -1379,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/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index 76bb4fb58be..031bb1001bd 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -669,15 +669,15 @@ logicalmsg_decode(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
 		snapshot = SnapBuildGetOrBuildSnapshot(builder);
 
 	/*
-	 * For transactional messages, use origptr as the mesage's LSN, like
-	 * regular DML changes.
+	 * Transactional messages use origptr as their 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.
+	 * 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,

Reply via email to