pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexander Korotkov <akorotkov@postgresql.org>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Don't make WAIT FOR LSN depend on WAL written after a page bound
Date: Wed, 18 Nov 2026 14:23:27 +0000
Message-ID: <E1xTgZK-00000000FQL-44Ro@gemulon.postgresql.org> (raw)

Don't make WAIT FOR LSN depend on WAL written after a page boundary

When the last WAL record ends exactly at a page boundary,
pg_current_wal_insert_lsn() reports the position after the next page's
header, 24 bytes past the boundary, or 40 bytes at a segment start.  The
flush, write, and replay positions stay at the boundary until more WAL
is written.  So WAIT FOR LSN on the value recommended in the
documentation could hang until some unrelated record, such as
bgwriter's RUNNING_XACTS, or until its timeout expired.  This was first
seen as an unexplained delay in 049_wait_for_lsn.pl and then hit about
once in 2000 waits by an application doing read-your-writes.

No WAL record can end inside a page header, so a target pointing into
or just past the header denotes the same point in the WAL as the start
of the page.  Map such targets to the page start in WAIT itself, so that
existing callers of pg_current_wal_insert_lsn() don't need a workaround.
Do this in ExecWaitStmt() rather than in WaitForLSN(), whose internal
callers may want to wait for any WAL past a given record end.

Similar off-by-header problem was fixed for gistGetFakeLSN() in b1f14c9672 by
introducing GetXLogInsertEndRecPtr().

Reported-by: Jobin Augustine <jobin.augustine@percona.com>
Author: Alexander Korotkov <aekorotkov@gmail.com>
Co-authored-by: Xuneng Zhou <xunengzhou@gmail.com>
Discussion: https://postgr.es/m/CABPTF7WZ1yuYz8V=xsbghg8e7qaAm5MpyNw6BthWcbN7+P6biw@mail.gmail.com
Backpatch-through: 19

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/24723e7cf397fc0f79b0b14412831db36882a29a

Modified Files
--------------
src/backend/commands/wait.c             | 37 ++++++++++++++-
src/test/recovery/t/049_wait_for_lsn.pl | 81 +++++++++++++++++++++++++++++++++
2 files changed, 116 insertions(+), 2 deletions(-)



view thread (2+ messages)  latest in thread

Message-ID: <E1xTgZK-00000000FQL-44Ro@gemulon.postgresql.org>
Permalink:  ../E1xTgZK-00000000FQL-44Ro@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1xTgZK-00000000FQL-44Ro@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-committers@postgresql.org
  Cc: akorotkov@postgresql.org
  Subject: Re: pgsql: Don't make WAIT FOR LSN depend on WAL written after a page bound
  In-Reply-To: <E1xTgZK-00000000FQL-44Ro@gemulon.postgresql.org>
  References: <E1xTgZK-00000000FQL-44Ro@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox