postgresql/src/include
Peter Geoghegan 92fe23d93a 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
2025-04-04 12:27:04 -04:00
..
access Add nbtree skip scan optimization. 2025-04-04 12:27:04 -04:00
archive Update copyright for 2025 2025-01-01 11:21:55 -05:00
backup Update copyright for 2025 2025-01-01 11:21:55 -05:00
bootstrap pg_noreturn to replace pg_attribute_noreturn() 2025-03-13 12:37:26 +01:00
catalog Add nbtree skip scan optimization. 2025-04-04 12:27:04 -04:00
commands Add an additional hook for EXPLAIN option validation. 2025-03-20 13:47:55 -04:00
common Update Unicode data to Unicode 16.0.0 2025-04-03 12:00:09 +02:00
datatype Avoid using timezone Asia/Manila in regression tests. 2025-01-20 15:47:53 -05:00
executor Change SQL-language functions to use the plan cache. 2025-04-02 14:06:02 -04:00
fe_utils pg_rewind: Add dbname to primary_conninfo when using --write-recovery-conf. 2025-03-12 16:56:04 -07:00
foreign Update copyright for 2025 2025-01-01 11:21:55 -05:00
jit Fix grammatical typos around possessive "its" 2025-01-29 14:39:14 +07:00
lib Use PRI?64 instead of "ll?" in format strings (continued). 2025-03-29 10:43:57 +01:00
libpq Make cancel request keys longer 2025-04-02 16:41:48 +03:00
mb pg_noreturn to replace pg_attribute_noreturn() 2025-03-13 12:37:26 +01:00
nodes Convert PathKey to use CompareType 2025-04-04 11:22:20 +02:00
optimizer Convert 'x IN (VALUES ...)' to 'x = ANY ...' then appropriate 2025-04-04 16:01:50 +03:00
parser Extend ALTER DEFAULT PRIVILEGES to define default privileges for large objects. 2025-04-04 19:02:17 +09:00
partitioning Fix incorrect #endif comment 2025-03-10 13:36:04 +13:00
pch Update copyright for 2025 2025-01-01 11:21:55 -05:00
port Use function attributes for SSE 4.2 even when targeting that extension 2025-04-01 12:01:58 +07:00
portability Update copyright for 2025 2025-01-01 11:21:55 -05:00
postmaster pg_noreturn to replace pg_attribute_noreturn() 2025-03-13 12:37:26 +01:00
regex Update copyright for 2025 2025-01-01 11:21:55 -05:00
replication Use standard die() signal handler in walreceiver 2025-04-04 12:38:32 +03:00
rewrite Eliminate code duplication in replace_rte_variables callbacks 2025-02-25 16:11:34 +09:00
snowball Update to latest Snowball sources. 2025-02-18 21:13:54 -05:00
statistics Stats: use schemaname/relname instead of regclass. 2025-03-25 11:16:06 -07:00
storage Make cancel request keys longer 2025-04-02 16:41:48 +03:00
tcop Improve error message when standby does accept connections. 2025-04-02 15:13:01 +09:00
tsearch Update copyright for 2025 2025-01-01 11:21:55 -05:00
utils Add nbtree skip scan optimization. 2025-04-04 12:27:04 -04:00
.gitignore Use <stdint.h> and <inttypes.h> for c.h integers. 2024-12-04 15:05:38 +13:00
c.h Use workaround of __builtin_setjmp only on MINGW on MSVCRT 2025-04-01 16:24:59 -04:00
fmgr.h Avoid mixing designated and non-designated field initializers. 2025-03-27 11:06:30 -04:00
funcapi.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
getopt_long.h IWYU widely useful pragmas 2025-01-15 18:57:53 +01:00
Makefile Use <stdint.h> and <inttypes.h> for c.h integers. 2024-12-04 15:05:38 +13:00
meson.build Update copyright for 2025 2025-01-01 11:21:55 -05:00
miscadmin.h Make cancel request keys longer 2025-04-02 16:41:48 +03:00
pg_config.h.in Add missing declarations to pg_config.h.in 2025-04-03 13:57:27 +02:00
pg_config_manual.h Avoid invalidating all RelationSyncCache entries on publication rename. 2025-03-13 09:16:33 +05:30
pg_getopt.h IWYU widely useful pragmas 2025-01-15 18:57:53 +01:00
pg_trace.h IWYU widely useful pragmas 2025-01-15 18:57:53 +01:00
pgstat.h pgstat: Allow checksum errors to be reported in critical sections 2025-03-30 16:12:04 -04:00
pgtar.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
pgtime.h Seek zone abbreviations in the IANA data before timezone_abbreviations. 2025-01-16 14:11:19 -05:00
port.h Add timingsafe_bcmp(), for constant-time memory comparison 2025-04-02 15:32:40 +03:00
postgres.h IWYU widely useful pragmas 2025-01-15 18:57:53 +01:00
postgres_ext.h libpq: Deprecate pg_int64. 2025-03-25 21:40:00 +13:00
postgres_fe.h IWYU widely useful pragmas 2025-01-15 18:57:53 +01:00
varatt.h Update copyright for 2025 2025-01-01 11:21:55 -05:00
windowapi.h Update copyright for 2025 2025-01-01 11:21:55 -05:00