pgsql: Avoid BufferGetLSNAtomic() calls during nbtree scans.

From: Peter Geoghegan <pg(at)bowt(dot)ie>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Avoid BufferGetLSNAtomic() calls during nbtree scans.
Date: 2025-06-06 14:21:00
Message-ID: [email protected]
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Avoid BufferGetLSNAtomic() calls during nbtree scans.

Delay calling BufferGetLSNAtomic() until we finish reading a page that
actually contains items that btgettuple will return to the executor.
This reduces the number of calls during plain index scans (we'll only
call BufferGetLSNAtomic() when _bt_readpage returns true), and totally
eliminates calls during index-only scans, bitmap index scans, and plain
index scans of an unlogged relation.

Currently, when checksums (or wal_log_hints) are enabled, acquiring a
page's LSN in BufferGetLSNAtomic() involves locking the buffer header
(which involves the use of spinlocks). Testing has shown that enabling
page-level checksums causes large regressions with certain workloads,
especially on larger multi-socket systems.

The regression isn't tied to any Postgres 18 commit. However, Postgres
18 commit 04bec894 made initdb use checksums by default, so it seems
prudent to address the problem now.

Author: Peter Geoghegan <pg(at)bowt(dot)ie>
Reviewed-By: Tomas Vondra <tomas(at)vondra(dot)me>
Discussion: https://postgr.es/m/[email protected]
Discussion: https://postgr.es/m/CAH2-Wzk-Dg5XWs_jDuiHt4_7ryrSY+n=vxmHY51EVqPDFsKXmg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/e6eed40e44419e3268d01fe0d2daec08a7df68f7

Modified Files
--------------
src/backend/access/nbtree/nbtree.c | 30 +++++++++++++++
src/backend/access/nbtree/nbtsearch.c | 70 ++++++++++++++++++-----------------
src/backend/access/nbtree/nbtutils.c | 68 ++++++++++++++++------------------
src/include/access/nbtree.h | 5 ++-
4 files changed, 102 insertions(+), 71 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Nathan Bossart 2025-06-06 16:41:41 pgsql: Fixed signed/unsigned mismatch in test_dsm_registry.
Previous Message Robert Haas 2025-06-06 13:04:24 pgsql: pg_prewarm: Allow autoprewarm to use more than 1GB to dump block