pgsql: Add infrastructure for efficient LSN waiting

From: Alexander Korotkov <akorotkov(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Add infrastructure for efficient LSN waiting
Date: 2025-11-05 09:44:46
Message-ID: E1vGa4M-0058Gz-2g@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Add infrastructure for efficient LSN waiting

Implement a new facility that allows processes to wait for WAL to reach
specific LSNs, both on primary (waiting for flush) and standby (waiting
for replay) servers.

The implementation uses shared memory with per-backend information
organized into pairing heaps, allowing O(1) access to the minimum
waited LSN. This enables fast-path checks: after replaying or flushing
WAL, the startup process or WAL writer can quickly determine if any
waiters need to be awakened.

Key components:
- New xlogwait.c/h module with WaitForLSNReplay() and WaitForLSNFlush()
- Separate pairing heaps for replay and flush waiters
- WaitLSN lightweight lock for coordinating shared state
- Wait events WAIT_FOR_WAL_REPLAY and WAIT_FOR_WAL_FLUSH for monitoring

This infrastructure can be used by features that need to wait for WAL
operations to complete.

Discussion: https://www.postgresql.org/message-id/flat/CAPpHfdsjtZLVzxjGT8rJHCYbM0D5dwkO+BBjcirozJ6nYbOW8Q(at)mail(dot)gmail(dot)com
Discussion: https://www.postgresql.org/message-id/flat/CABPTF7UNft368x-RgOXkfj475OwEbp%2BVVO-wEXz7StgjD_%3D6sw%40mail.gmail.com
Author: Kartyshov Ivan <i(dot)kartyshov(at)postgrespro(dot)ru>
Author: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Author: Xuneng Zhou <xunengzhou(at)gmail(dot)com>
Reviewed-by: Michael Paquier <michael(at)paquier(dot)xyz>
Reviewed-by: Peter Eisentraut <peter(dot)eisentraut(at)enterprisedb(dot)com>
Reviewed-by: Dilip Kumar <dilipbalaut(at)gmail(dot)com>
Reviewed-by: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Reviewed-by: Alexander Lakhin <exclusion(at)gmail(dot)com>
Reviewed-by: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
Reviewed-by: Euler Taveira <euler(at)eulerto(dot)com>
Reviewed-by: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
Reviewed-by: Kyotaro Horiguchi <horikyota(dot)ntt(at)gmail(dot)com>
Reviewed-by: Xuneng Zhou <xunengzhou(at)gmail(dot)com>

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3b4e53a075ea5671b075f8fd873241179f8e64af

Modified Files
--------------
src/backend/access/transam/Makefile | 3 +-
src/backend/access/transam/meson.build | 1 +
src/backend/access/transam/xlogwait.c | 409 ++++++++++++++++++++++++
src/backend/storage/ipc/ipci.c | 3 +
src/backend/utils/activity/wait_event_names.txt | 3 +
src/include/access/xlogwait.h | 98 ++++++
src/include/storage/lwlocklist.h | 1 +
src/tools/pgindent/typedefs.list | 4 +
8 files changed, 521 insertions(+), 1 deletion(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Eisentraut 2025-11-05 15:38:52 pgsql: doc: Add section for temporal tables
Previous Message Richard Guo 2025-11-05 09:16:25 pgsql: Fix assertion failure in generate_orderedappend_paths()