From 13efac74d82a57435d998811f1e1a5c37251cc91 Mon Sep 17 00:00:00 2001
From: Sami Imseih <samimseih.pg@gmail.com>
Date: Wed, 9 Sep 2026 13:48:04 +0000
Subject: [PATCH v1 1/1] Clarify WAIT FOR read-your-writes documentation

To achieve read-your-writes consistency with WAIT FOR, the target LSN
must be captured after the transaction's COMMIT record. The existing
documentation did not make that explicit, and could be read as allowing
an LSN captured after a statement but before COMMIT.

Document the COMMIT-LSN requirement explicitly in both the WAIT FOR
reference page and the read-your-writes consistency section.
---
 doc/src/sgml/high-availability.sgml | 13 ++++++++-----
 doc/src/sgml/ref/wait_for.sgml      | 19 +++++++++++++------
 2 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/doc/src/sgml/high-availability.sgml b/doc/src/sgml/high-availability.sgml
index d58b16977cd..7e4fe3d3fc9 100644
--- a/doc/src/sgml/high-availability.sgml
+++ b/doc/src/sgml/high-availability.sgml
@@ -1409,9 +1409,10 @@ synchronous_standby_names = 'ANY 2 (s1, s2, s3)'
    </para>
 
    <para>
-    This is achieved by the following steps.  After performing write
-    operations, the application retrieves the current WAL location using a
-    function call like this.
+    This is achieved by the following steps.  After committing the write
+    operations on the primary, the application retrieves a WAL location
+    that is at or after the transaction's <literal>COMMIT</literal> record,
+    using a function call like this.
 
 <programlisting>
 postgres=# SELECT pg_current_wal_insert_lsn();
@@ -1438,8 +1439,10 @@ postgres=# WAIT FOR LSN '0/306EE20';
 (1 row)
 </programlisting>
     Once the command returns a status of success, it guarantees that all
-    changes up to the provided <acronym>LSN</acronym> have been applied,
-    ensuring that subsequent read queries will reflect the latest updates.
+    changes up to the provided <acronym>LSN</acronym> have been applied.
+    Therefore, if that <acronym>LSN</acronym> is at or after the write
+    transaction's <literal>COMMIT</literal> record on the primary,
+    subsequent read queries will reflect those updates.
    </para>
   </sect2>
 
diff --git a/doc/src/sgml/ref/wait_for.sgml b/doc/src/sgml/ref/wait_for.sgml
index 04ca9400426..e5b54d47afc 100644
--- a/doc/src/sgml/ref/wait_for.sgml
+++ b/doc/src/sgml/ref/wait_for.sgml
@@ -231,7 +231,9 @@ WAIT FOR LSN '<replaceable class="parameter">lsn</replaceable>'
    <parameter>mode</parameter>. The <literal>standby_replay</literal> mode
    waits for the LSN to be replayed (applied to the database), which is
    useful to achieve read-your-writes consistency while using an async
-   replica for reads and the primary for writes. The
+   replica for reads and the primary for writes, provided that the target
+   <acronym>LSN</acronym> is at or after the relevant write transaction's
+   <literal>COMMIT</literal> record on the primary. The
    <literal>standby_flush</literal> mode waits for the WAL to be flushed
    to durable storage on the replica, or to have already been replayed
    from WAL present on the standby. The <literal>standby_write</literal> mode
@@ -289,9 +291,13 @@ WAIT FOR LSN '<replaceable class="parameter">lsn</replaceable>'
   <para>
    You can use <command>WAIT FOR</command> command to wait for
    the <type>pg_lsn</type> value.  For example, an application could update
-   the <literal>movie</literal> table and get the <acronym>lsn</acronym> after
-   changes just made.  This example uses <function>pg_current_wal_insert_lsn</function>
-   on primary server to get the <acronym>lsn</acronym> given that
+   the <literal>movie</literal> table and get an <acronym>lsn</acronym> that
+   is at or after the relevant write transaction's <literal>COMMIT</literal>
+   record.  In the default autocommit mode shown here, the
+   <command>UPDATE</command> commits before the subsequent
+   <command>SELECT</command>.  This example uses
+   <function>pg_current_wal_insert_lsn</function> on primary server to get
+   the <acronym>lsn</acronym> given that
    <varname>synchronous_commit</varname> could be set to
    <literal>off</literal>.
 
@@ -306,8 +312,9 @@ postgres=# SELECT pg_current_wal_insert_lsn();
 </programlisting>
 
    Then an application could run <command>WAIT FOR</command>
-   with the <parameter>lsn</parameter> obtained from primary.  After that the
-   changes made on primary should be guaranteed to be visible on replica.
+   with the <parameter>lsn</parameter> obtained from primary by that
+   post-commit <command>SELECT</command>.  After that the changes made on
+   primary should be guaranteed to be visible on replica.
 
 <programlisting>
 postgres=# WAIT FOR LSN '0/0306EE20';
-- 
2.50.1

