pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Geoghegan <pg@bowt.ie>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Optimize nbtree backward scan boundary cases.
Date: Fri, 08 Dec 2023 19:05:40 +0000
Message-ID: <E1rBgAN-009F6R-It@gemulon.postgresql.org> (raw)

Optimize nbtree backward scan boundary cases.

Teach _bt_binsrch (and related helper routines like _bt_search and
_bt_compare) about the initial positioning requirements of backward
scans.  Routines like _bt_binsrch already know all about "nextkey"
searches, so it seems natural to teach them about "goback"/backward
searches, too.  These concepts are closely related, and are much easier
to understand when discussed together.

Now that certain implementation details are hidden from _bt_first, it's
straightforward to add a new optimization: backward scans using the <
strategy now avoid extra leaf page accesses in certain "boundary cases".
Consider the following example, which uses the tenk1 table (and its
tenk1_hundred index) from the standard regression tests:

SELECT * FROM tenk1 WHERE hundred < 12 ORDER BY hundred DESC LIMIT 1;

Before this commit, nbtree would scan two leaf pages, even though it was
only really necessary to scan one leaf page.  We'll now descend straight
to the leaf page containing a (12, -inf) high key instead.  The scan
will locate matching non-pivot tuples with "hundred" values starting
from the value 11.  The scan won't waste a page access on the right
sibling leaf page, which cannot possibly contain any matching tuples.

You can think of the optimization added by this commit as disabling an
optimization (the _bt_compare "!pivotsearch" behavior that was added to
Postgres 12 in commit dd299df8) for a small subset of cases where it was
always counterproductive.

Equivalently, you can think of the new optimization as extending the
"pivotsearch" behavior that page deletion by VACUUM has long required
(since the aforementioned Postgres 12 commit went in) to other, similar
cases.  Obviously, this isn't strictly necessary for these new cases
(unlike VACUUM, _bt_first is prepared to move the scan to the left once
on the leaf level), but the underlying principle is the same.

Author: Peter Geoghegan <pg@bowt.ie>
Reviewed-By: Matthias van de Meent <boekewurm+postgres@gmail.com>
Discussion: https://postgr.es/m/CAH2-Wz=XPzM8HzaLPq278Vms420mVSHfgs9wi5tjFKHcapZCEw@mail.gmail.com

Branch
------
master

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

Modified Files
--------------
contrib/amcheck/verify_nbtree.c           |  16 +--
src/backend/access/nbtree/nbtpage.c       |  16 ++-
src/backend/access/nbtree/nbtsearch.c     | 232 +++++++++++++++---------------
src/backend/access/nbtree/nbtutils.c      |  12 +-
src/include/access/nbtree.h               |  11 +-
src/test/regress/expected/btree_index.out |  47 ++++++
src/test/regress/sql/btree_index.sql      |  25 ++++
7 files changed, 210 insertions(+), 149 deletions(-)



Message-ID: <E1rBgAN-009F6R-It@gemulon.postgresql.org>
Permalink:  ../E1rBgAN-009F6R-It@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1rBgAN-009F6R-It@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: pg@bowt.ie, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Optimize nbtree backward scan boundary cases.
  In-Reply-To: <E1rBgAN-009F6R-It@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox