diff options
author | Peter Geoghegan | 2025-04-04 16:27:04 +0000 |
---|---|---|
committer | Peter Geoghegan | 2025-04-04 16:27:04 +0000 |
commit | 92fe23d93aa3bbbc40fca669cabc4a4d7975e327 (patch) | |
tree | e79d024c849f0a0b89378ff8c16b6d6b2d0cc777 /src/include/utils | |
parent | 3ba2cdaa45416196fadc7163998cda7b4e07e7d7 (diff) |
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@bowt.ie>
Reviewed-By: Masahiro Ikeda <masahiro.ikeda@nttdata.com>
Reviewed-By: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Reviewed-By: Matthias van de Meent <boekewurm+postgres@gmail.com>
Reviewed-By: Tomas Vondra <tomas@vondra.me>
Reviewed-By: Aleksander Alekseev <aleksander@timescale.com>
Reviewed-By: Alena Rybakina <a.rybakina@postgrespro.ru>
Discussion: https://postgr.es/m/CAH2-Wzmn1YsLzOGgjAQZdn1STSG_y8qP__vggTaPAYXJP+G4bw@mail.gmail.com
Diffstat (limited to 'src/include/utils')
-rw-r--r-- | src/include/utils/skipsupport.h | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/include/utils/skipsupport.h b/src/include/utils/skipsupport.h new file mode 100644 index 00000000000..bc51847cf61 --- /dev/null +++ b/src/include/utils/skipsupport.h @@ -0,0 +1,98 @@ +/*------------------------------------------------------------------------- + * + * skipsupport.h + * Support routines for B-Tree skip scan. + * + * B-Tree operator classes for discrete types can optionally provide a support + * function for skipping. This is used during skip scans. + * + * A B-tree operator class that implements skip support provides B-tree index + * scans with a way of enumerating and iterating through every possible value + * from the domain of indexable values. This gives scans a way to determine + * the next value in line for a given skip array/scan key/skipped attribute. + * Scans request the next (or previous) value whenever they run out of tuples + * matching the skip array's current element value. The next (or previous) + * value can be used to relocate the scan; it is applied in combination with + * at least one additional lower-order non-skip key, taken from the query. + * + * Skip support is used by discrete type (e.g., integer and date) opclasses. + * Indexes with an attribute whose input opclass is of one of these types tend + * to store adjacent values in adjoining groups of index tuples. Each time a + * skip scan with skip support successfully guesses that the next value in the + * index (for a given skipped column) is indeed the value that skip support + * just incremented its skip array to, it will have saved the scan some work. + * The scan will have avoided an index probe that directly finds the next + * value that appears in the index. (When skip support guesses wrong, then it + * won't have saved any work, but it also won't have added any useless work. + * The failed attempt to locate exactly-matching index tuples acts just like + * an explicit probe would; it'll still find the index's true next value.) + * + * It usually isn't feasible to implement skip support for an opclass whose + * input type is continuous. The B-Tree code falls back on next-key sentinel + * values for any opclass that doesn't provide its own skip support function. + * This isn't really an implementation restriction; there is no benefit to + * providing skip support for an opclass where guessing that the next indexed + * value is the next possible indexable value never (or hardly ever) works out. + * + * + * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/utils/skipsupport.h + * + *------------------------------------------------------------------------- + */ +#ifndef SKIPSUPPORT_H +#define SKIPSUPPORT_H + +#include "utils/relcache.h" + +typedef struct SkipSupportData *SkipSupport; +typedef Datum (*SkipSupportIncDec) (Relation rel, + Datum existing, + bool *overflow); + +/* + * State/callbacks used by skip arrays to procedurally generate elements. + * + * A BTSKIPSUPPORT_PROC function must set each and every field when called + * (there are no optional fields). + */ +typedef struct SkipSupportData +{ + /* + * low_elem and high_elem must be set with the lowest and highest possible + * values from the domain of indexable values (assuming ascending order) + */ + Datum low_elem; /* lowest sorting/leftmost non-NULL value */ + Datum high_elem; /* highest sorting/rightmost non-NULL value */ + + /* + * Decrement/increment functions. + * + * Returns a decremented/incremented copy of caller's existing datum, + * allocated in caller's memory context (for pass-by-reference types). + * It's not okay for these functions to leak any memory. + * + * When the decrement function (or increment function) is called with a + * value that already matches low_elem (or high_elem), function must set + * the *overflow argument. The return value is treated as undefined by + * the B-Tree code; it shouldn't need to be (and won't be) pfree'd. + * + * The B-Tree code's "existing" datum argument is often just a straight + * copy of a value from an index tuple. Operator classes must accept + * every possible representational variation within the underlying type. + * On the other hand, opclasses are _not_ required to preserve information + * that doesn't affect how datums are sorted (e.g., skip support for a + * fixed precision numeric type needn't preserve datum display scale). + * Operator class decrement/increment functions will never be called with + * a NULL "existing" argument, either. + */ + SkipSupportIncDec decrement; + SkipSupportIncDec increment; +} SkipSupportData; + +extern SkipSupport PrepareSkipSupportFromOpclass(Oid opfamily, Oid opcintype, + bool reverse); + +#endif /* SKIPSUPPORT_H */ |