Improve WAIT FOR read-your-writes consistency doc

Started by Sami Imseih3 months ago10 messageshackers
Jump to latest
#1Sami Imseih
samimseih.pg@gmail.com

The WAIT FOR docs say that "The standby_replay 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."

I think that needs one more caveat.

WAIT FOR success does not guarantee read-your-writes if the supplied LSN
is before the transaction's COMMIT record.

Maybe the last sentence should be qualified with:

" .... the primary for writes, provided that the target LSN was
captured after COMMIT on the primary."

This may be obvious to users of this feature, but perhaps worth being
more explicit about it.

Thoughts?

--
Sami Imseih
Amazon Web Services (AWS)

#2Xuneng Zhou
xunengzhou@gmail.com
In reply to: Sami Imseih (#1)
Re: Improve WAIT FOR read-your-writes consistency doc

Hi Sami,

On Wed, Sep 9, 2026 at 5:42 AM Sami Imseih <samimseih.pg@gmail.com> wrote:

The WAIT FOR docs say that "The standby_replay 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."

I think that needs one more caveat.

WAIT FOR success does not guarantee read-your-writes if the supplied LSN
is before the transaction's COMMIT record.

Maybe the last sentence should be qualified with:

" .... the primary for writes, provided that the target LSN was
captured after COMMIT on the primary."

This may be obvious to users of this feature, but perhaps worth being
more explicit about it.

Thoughts?

Thanks for raising this! I agree this may need to be harnessed. If we
change this line, we might also want to modify the
read-your-writes-consistency chapter.
<sect2 id="read-your-writes-consistency">
<title>Read-Your-Writes Consistency</title>

I am also wondering whether a example like this is helpful, though it is
somehow duplicated with the existing one:
postgres=# BEGIN;
BEGIN
postgres=*# UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama';
UPDATE 100
postgres=*# COMMIT;
COMMIT
postgres=# SELECT pg_current_wal_insert_lsn();

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#3Sami Imseih
samimseih.pg@gmail.com
In reply to: Xuneng Zhou (#2)
Re: Improve WAIT FOR read-your-writes consistency doc

Thanks for the feedback.

Thanks for raising this! I agree this may need to be harnessed. If we change this line, we might also want to modify the read-your-writes-consistency chapter.
<sect2 id="read-your-writes-consistency">
<title>Read-Your-Writes Consistency</title>

Yes, that needs to mention the COMMIT LSN explicitly.

I am also wondering whether a example like this is helpful, though it is somehow duplicated with the existing one:
postgres=# BEGIN;
BEGIN
postgres=*# UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama';
UPDATE 100
postgres=*# COMMIT;
COMMIT
postgres=# SELECT pg_current_wal_insert_lsn();

Rather than change the example, just updated the explanation of the
example with the proper commit wording.

WDYT of the attached?

--
Sami

Attachments:

t253720_3
v1-0001-Clarify-WAIT-FOR-read-your-writes-documentation.patchapplication/octet-stream; name=v1-0001-Clarify-WAIT-FOR-read-your-writes-documentation.patchDownload+21-12
#4Xuneng Zhou
xunengzhou@gmail.com
In reply to: Sami Imseih (#3)
Re: Improve WAIT FOR read-your-writes consistency doc

On Wed, Sep 9, 2026 at 9:57 PM Sami Imseih <samimseih.pg@gmail.com> wrote:

Thanks for the feedback.

Thanks for raising this! I agree this may need to be harnessed. If we change this line, we might also want to modify the read-your-writes-consistency chapter.
<sect2 id="read-your-writes-consistency">
<title>Read-Your-Writes Consistency</title>

Yes, that needs to mention the COMMIT LSN explicitly.

I am also wondering whether a example like this is helpful, though it is somehow duplicated with the existing one:
postgres=# BEGIN;
BEGIN
postgres=*# UPDATE movie SET genre = 'Dramatic' WHERE genre = 'Drama';
UPDATE 100
postgres=*# COMMIT;
COMMIT
postgres=# SELECT pg_current_wal_insert_lsn();

Rather than change the example, just updated the explanation of the
example with the proper commit wording.

WDYT of the attached?

Just a trivial comment:

+    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.

We might need to specify the word 'end' clearly to avoid
misunderstanding the LSN position as the beginning of the commit
record.

at or after the *end* of the write transaction’s COMMIT record.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.

#5Sami Imseih
samimseih.pg@gmail.com
In reply to: Xuneng Zhou (#4)
Re: Improve WAIT FOR read-your-writes consistency doc

Thanks!

WDYT of the attached?

Just a trivial comment:

+    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.

We might need to specify the word 'end' clearly to avoid
misunderstanding the LSN position as the beginning of the commit
record.

at or after the *end* of the write transaction’s COMMIT record.

I rather the comment be more high-level, and we just need to convey
that the function should be called after the commit.

See the attached.

--
Sami Imseih
Amazon Web Services (AWS)

Attachments:

t253720_5
v2-0001-Clarify-WAIT-FOR-read-your-writes-documentation.patchapplication/octet-stream; name=v2-0001-Clarify-WAIT-FOR-read-your-writes-documentation.patchDownload+19-12
#6Sami Imseih
samimseih.pg@gmail.com
In reply to: Sami Imseih (#5)
Re: Improve WAIT FOR read-your-writes consistency doc

Hi,

Attached is a rebase.

--
Sami Imseih
Amazon Web Services (AWS)

Attachments:

t253720_6
v3-0001-Clarify-WAIT-FOR-read-your-writes-documentation.patchapplication/octet-stream; name=v3-0001-Clarify-WAIT-FOR-read-your-writes-documentation.patchDownload+20-13
#7Nathan Bossart
nathandbossart@gmail.com
In reply to: Sami Imseih (#6)
Re: Improve WAIT FOR read-your-writes consistency doc

[RMT hat]

Please note that this one is now listed as an open item for v19.

--
nathan

#8Alexander Korotkov
aekorotkov@gmail.com
In reply to: Sami Imseih (#6)
Re: Improve WAIT FOR read-your-writes consistency doc

On Tue, Sep 15, 2026 at 4:27 PM Sami Imseih <samimseih.pg@gmail.com> wrote:

Attached is a rebase.

I've made this following changes to the patch.
1) Change <command>WAIT FOR</command> back to <command>WAIT</command>
(see a23ab4862cfe).
2) In the example, app gets lsn at or after *the end* of the relevant
write transaction's COMMIT record. If we say lsn at the COMMIT record
then it might be interpreted as at he beginning of that record (and
that is wrong).

I'm going to push this if no objections.

------
Regards,
Alexander Korotkov
Supabase

Attachments:

t253720_8
v4-0001-doc-Say-which-LSN-gives-read-your-writes-with-WAI.patchapplication/octet-stream; name=v4-0001-doc-Say-which-LSN-gives-read-your-writes-with-WAI.patchDownload+19-12
#9Sami Imseih
samimseih.pg@gmail.com
In reply to: Alexander Korotkov (#8)
Re: Improve WAIT FOR read-your-writes consistency doc

On Wed, Sep 16, 2026 at 5:30 PM Alexander Korotkov <aekorotkov@gmail.com>
wrote:

On Tue, Sep 15, 2026 at 4:27 PM Sami Imseih <samimseih.pg@gmail.com>
wrote:

Attached is a rebase.

I've made this following changes to the patch.
1) Change <command>WAIT FOR</command> back to <command>WAIT</command>
(see a23ab4862cfe).
2) In the example, app gets lsn at or after *the end* of the relevant
write transaction's COMMIT record. If we say lsn at the COMMIT record
then it might be interpreted as at he beginning of that record (and
that is wrong).

I'm going to push this if no objections.

LGTM

Sami

Show quoted text
#10Alexander Korotkov
aekorotkov@gmail.com
In reply to: Sami Imseih (#9)
Re: Improve WAIT FOR read-your-writes consistency doc

On Thu, Sep 17, 2026 at 1:42 AM Sami Imseih <samimseih.pg@gmail.com> wrote:

On Wed, Sep 16, 2026 at 5:30 PM Alexander Korotkov <aekorotkov@gmail.com> wrote:

On Tue, Sep 15, 2026 at 4:27 PM Sami Imseih <samimseih.pg@gmail.com> wrote:

Attached is a rebase.

I've made this following changes to the patch.
1) Change <command>WAIT FOR</command> back to <command>WAIT</command>
(see a23ab4862cfe).
2) In the example, app gets lsn at or after *the end* of the relevant
write transaction's COMMIT record. If we say lsn at the COMMIT record
then it might be interpreted as at he beginning of that record (and
that is wrong).

I'm going to push this if no objections.

LGTM

Thank you, pushed.

------
Regards,
Alexander Korotkov
Supabase