pgsql: nbtree: Avoid allocating _bt_search stack.

From: Peter Geoghegan <pg(at)bowt(dot)ie>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: nbtree: Avoid allocating _bt_search stack.
Date: 2026-03-12 17:22:47
Message-ID: E1w0jkF-003dxO-0Y@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

nbtree: Avoid allocating _bt_search stack.

Avoid allocating memory for an nbtree descent stack during index scans.
We only require a descent stack during inserts, when it is used to
determine where to insert a new pivot tuple/downlink into the target
leaf page's parent page in the event of a page split. (Page deletion's
first phase also performs a _bt_search that requires a descent stack.)

This optimization improves performance by minimizing palloc churn. It
speeds up index scans that call _bt_search frequently/descend the index
many times, especially when the cost of scanning the index dominates
(e.g., with index-only skip scans). Testing has shown that the
underlying issue causes performance problems for an upcoming patch that
will replace btgettuple with a new btgetbatch interface to enable I/O
prefetching.

Author: Peter Geoghegan <pg(at)bowt(dot)ie>
Reviewed-By: Tomas Vondra <tomas(at)vondra(dot)me>
Discussion: https://postgr.es/m/CAH2-Wzmy7NMba9k8m_VZ-XNDZJEUQBU8TeLEeL960-rAKb-+tQ@mail.gmail.com

Branch
------
master

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

Modified Files
--------------
contrib/amcheck/verify_nbtree.c | 4 +---
src/backend/access/nbtree/nbtinsert.c | 19 ++++++++++++++++++-
src/backend/access/nbtree/nbtpage.c | 2 +-
src/backend/access/nbtree/nbtsearch.c | 35 ++++++++++++++++++-----------------
src/backend/access/nbtree/nbtutils.c | 16 ----------------
src/include/access/nbtree.h | 3 +--
6 files changed, 39 insertions(+), 40 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Geoghegan 2026-03-12 17:26:44 pgsql: Use simplehash for backend-private buffer pin refcounts.
Previous Message Robert Haas 2026-03-12 17:00:57 pgsql: Add pg_plan_advice contrib module.