Avoid BufferGetLSNAtomic() calls during nbtree scans.
authorPeter Geoghegan <pg@bowt.ie>
Fri, 6 Jun 2025 14:19:44 +0000 (10:19 -0400)
committerPeter Geoghegan <pg@bowt.ie>
Fri, 6 Jun 2025 14:19:44 +0000 (10:19 -0400)
commite6eed40e44419e3268d01fe0d2daec08a7df68f7
tree6895e05e740c6c192b1102d60af5ac12dd987084
parent016e407f4ba10b230f5094c9ba36a1df3d34fb22
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@bowt.ie>
Reviewed-By: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/941f0190-e3c6-4622-9ac7-c04e936e5fdb@vondra.me
Discussion: https://postgr.es/m/CAH2-Wzk-Dg5XWs_jDuiHt4_7ryrSY+n=vxmHY51EVqPDFsKXmg@mail.gmail.com
src/backend/access/nbtree/nbtree.c
src/backend/access/nbtree/nbtsearch.c
src/backend/access/nbtree/nbtutils.c
src/include/access/nbtree.h