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 <[email protected]>
Author: Alexander Korotkov <[email protected]>
Co-authored-by: Xuneng Zhou <[email protected]>
Discussion: 
https://postgr.es/m/CABPTF7WZ1yuYz8V=xsbghg8e7qaam5mpynw6bthwcbn7+p6...@mail.gmail.com
Backpatch-through: 19

Branch
------
REL_19_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/19aa5713be2b32109c006b3bce9b0881aef3a723

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(-)

Reply via email to