From: | Peter Geoghegan <pg(at)bowt(dot)ie> |
---|---|
To: | pgsql-committers(at)lists(dot)postgresql(dot)org |
Subject: | pgsql: Add nbtree skip scan optimization. |
Date: | 2025-04-04 16:28:54 |
Message-ID: | E1u0juY-002ezW-2G@gemulon.postgresql.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-committers |
Add nbtree skip scan optimization.
Teach nbtree multi-column index scans to opportunistically skip over
irrelevant sections of the index given a query with no "=" conditions on
one or more prefix index columns. When nbtree is passed input scan keys
derived from a predicate "WHERE b = 5", new nbtree preprocessing steps
output "WHERE a = ANY(<every possible 'a' value>) AND b = 5" scan keys.
That is, preprocessing generates a "skip array" (and an output scan key)
for the omitted prefix column "a", which makes it safe to mark the scan
key on "b" as required to continue the scan. The scan is therefore able
to repeatedly reposition itself by applying both the "a" and "b" keys.
A skip array has "elements" that are generated procedurally and on
demand, but otherwise works just like a regular ScalarArrayOp array.
Preprocessing can freely add a skip array before or after any input
ScalarArrayOp arrays. Index scans with a skip array decide when and
where to reposition the scan using the same approach as any other scan
with array keys. This design builds on the design for array advancement
and primitive scan scheduling added to Postgres 17 by commit 5bf748b8.
Testing has shown that skip scans of an index with a low cardinality
skipped prefix column can be multiple orders of magnitude faster than an
equivalent full index scan (or sequential scan). In general, the
cardinality of the scan's skipped column(s) limits the number of leaf
pages that can be skipped over.
The core B-Tree operator classes on most discrete types generate their
array elements with the help of their own custom skip support routine.
This infrastructure gives nbtree a way to generate the next required
array element by incrementing (or decrementing) the current array value.
It can reduce the number of index descents in cases where the next
possible indexable value frequently turns out to be the next value
stored in the index. Opclasses that lack a skip support routine fall
back on having nbtree "increment" (or "decrement") a skip array's
current element by setting the NEXT (or PRIOR) scan key flag, without
directly changing the scan key's sk_argument. These sentinel values
behave just like any other value from an array -- though they can never
locate equal index tuples (they can only locate the next group of index
tuples containing the next set of non-sentinel values that the scan's
arrays need to advance to).
A skip array's range is constrained by "contradictory" inequality keys.
For example, a skip array on "x" will only generate the values 1 and 2
given a qual such as "WHERE x BETWEEN 1 AND 2 AND y = 66". Such a skip
array qual usually has near-identical performance characteristics to a
comparable SAOP qual "WHERE x = ANY('{1, 2}') AND y = 66". However,
improved performance isn't guaranteed. Much depends on physical index
characteristics.
B-Tree preprocessing is optimistic about skipping working out: it
applies static, generic rules when determining where to generate skip
arrays, which assumes that the runtime overhead of maintaining skip
arrays will pay for itself -- or lead to only a modest performance loss.
As things stand, these assumptions are much too optimistic: skip array
maintenance will lead to unacceptable regressions with unsympathetic
queries (queries whose scan can't skip over many irrelevant leaf pages).
An upcoming commit will address the problems in this area by enhancing
_bt_readpage's approach to saving cycles on scan key evaluation, making
it work in a way that directly considers the needs of = array keys
(particularly = skip array keys).
Author: Peter Geoghegan <pg(at)bowt(dot)ie>
Reviewed-By: Masahiro Ikeda <masahiro(dot)ikeda(at)nttdata(dot)com>
Reviewed-By: Heikki Linnakangas <heikki(dot)linnakangas(at)iki(dot)fi>
Reviewed-By: Matthias van de Meent <boekewurm+postgres(at)gmail(dot)com>
Reviewed-By: Tomas Vondra <tomas(at)vondra(dot)me>
Reviewed-By: Aleksander Alekseev <aleksander(at)timescale(dot)com>
Reviewed-By: Alena Rybakina <a(dot)rybakina(at)postgrespro(dot)ru>
Discussion: https://postgr.es/m/CAH2-Wzmn1YsLzOGgjAQZdn1STSG_y8qP__vggTaPAYXJP+G4bw@mail.gmail.com
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/92fe23d93aa3bbbc40fca669cabc4a4d7975e327
Modified Files
--------------
doc/src/sgml/btree.sgml | 31 +-
doc/src/sgml/indexam.sgml | 3 +-
doc/src/sgml/indices.sgml | 68 ++-
doc/src/sgml/monitoring.sgml | 5 +-
doc/src/sgml/perform.sgml | 32 ++
doc/src/sgml/xindex.sgml | 16 +-
src/backend/access/index/indexam.c | 3 +-
src/backend/access/nbtree/nbtcompare.c | 273 ++++++++++
src/backend/access/nbtree/nbtpreprocesskeys.c | 631 ++++++++++++++++++---
src/backend/access/nbtree/nbtree.c | 196 ++++++-
src/backend/access/nbtree/nbtsearch.c | 130 ++++-
src/backend/access/nbtree/nbtutils.c | 756 +++++++++++++++++++++++---
src/backend/access/nbtree/nbtvalidate.c | 4 +
src/backend/commands/opclasscmds.c | 25 +
src/backend/utils/adt/Makefile | 1 +
src/backend/utils/adt/date.c | 46 ++
src/backend/utils/adt/meson.build | 1 +
src/backend/utils/adt/selfuncs.c | 491 +++++++++++++----
src/backend/utils/adt/skipsupport.c | 61 +++
src/backend/utils/adt/timestamp.c | 48 ++
src/backend/utils/adt/uuid.c | 70 +++
src/include/access/amapi.h | 3 +-
src/include/access/nbtree.h | 34 +-
src/include/catalog/catversion.h | 2 +-
src/include/catalog/pg_amproc.dat | 22 +
src/include/catalog/pg_proc.dat | 27 +
src/include/utils/skipsupport.h | 98 ++++
src/test/regress/expected/alter_generic.out | 10 +-
src/test/regress/expected/btree_index.out | 41 ++
src/test/regress/expected/create_index.out | 183 ++++++-
src/test/regress/expected/psql.out | 3 +-
src/test/regress/sql/alter_generic.sql | 5 +-
src/test/regress/sql/btree_index.sql | 21 +
src/test/regress/sql/create_index.sql | 63 ++-
src/tools/pgindent/typedefs.list | 2 +
35 files changed, 3024 insertions(+), 381 deletions(-)
From | Date | Subject | |
---|---|---|---|
Next Message | Jeff Davis | 2025-04-04 17:29:00 | pgsql: Oversight in commit b81ffa13e3. |
Previous Message | Alexander Korotkov | 2025-04-04 16:03:21 | Re: pgsql: Convert 'x IN (VALUES ...)' to 'x = ANY ...' then appropriate |