postgresql/src/backend/utils/adt
Peter Geoghegan 5bf748b86b Enhance nbtree ScalarArrayOp execution.
Commit 9e8da0f7 taught nbtree to handle ScalarArrayOpExpr quals
natively.  This works by pushing down the full context (the array keys)
to the nbtree index AM, enabling it to execute multiple primitive index
scans that the planner treats as one continuous index scan/index path.
This earlier enhancement enabled nbtree ScalarArrayOp index-only scans.
It also allowed scans with ScalarArrayOp quals to return ordered results
(with some notable restrictions, described further down).

Take this general approach a lot further: teach nbtree SAOP index scans
to decide how to execute ScalarArrayOp scans (when and where to start
the next primitive index scan) based on physical index characteristics.
This can be far more efficient.  All SAOP scans will now reliably avoid
duplicative leaf page accesses (just like any other nbtree index scan).
SAOP scans whose array keys are naturally clustered together now require
far fewer index descents, since we'll reliably avoid starting a new
primitive scan just to get to a later offset from the same leaf page.

The scan's arrays now advance using binary searches for the array
element that best matches the next tuple's attribute value.  Required
scan key arrays (i.e. arrays from scan keys that can terminate the scan)
ratchet forward in lockstep with the index scan.  Non-required arrays
(i.e. arrays from scan keys that can only exclude non-matching tuples)
"advance" without the process ever rolling over to a higher-order array.

Naturally, only required SAOP scan keys trigger skipping over leaf pages
(non-required arrays cannot safely end or start primitive index scans).
Consequently, even index scans of a composite index with a high-order
inequality scan key (which we'll mark required) and a low-order SAOP
scan key (which we won't mark required) now avoid repeating leaf page
accesses -- that benefit isn't limited to simpler equality-only cases.
In general, all nbtree index scans now output tuples as if they were one
continuous index scan -- even scans that mix a high-order inequality
with lower-order SAOP equalities reliably output tuples in index order.
This allows us to remove a couple of special cases that were applied
when building index paths with SAOP clauses during planning.

Bugfix commit 807a40c5 taught the planner to avoid generating unsafe
path keys: path keys on a multicolumn index path, with a SAOP clause on
any attribute beyond the first/most significant attribute.  These cases
are now all safe, so we go back to generating path keys without regard
for the presence of SAOP clauses (just like with any other clause type).
Affected queries can now exploit scan output order in all the usual ways
(e.g., certain "ORDER BY ... LIMIT n" queries can now terminate early).

Also undo changes from follow-up bugfix commit a4523c5a, which taught
the planner to produce alternative index paths, with path keys, but
without low-order SAOP index quals (filter quals were used instead).
We'll no longer generate these alternative paths, since they can no
longer offer any meaningful advantages over standard index qual paths.
Affected queries thereby avoid all of the disadvantages that come from
using filter quals within index scan nodes.  They can avoid extra heap
page accesses from using filter quals to exclude non-matching tuples
(index quals will never have that problem).  They can also skip over
irrelevant sections of the index in more cases (though only when nbtree
determines that starting another primitive scan actually makes sense).

There is a theoretical risk that removing restrictions on SAOP index
paths from the planner will break compatibility with amcanorder-based
index AMs maintained as extensions.  Such an index AM could have the
same limitations around ordered SAOP scans as nbtree had up until now.
Adding a pro forma incompatibility item about the issue to the Postgres
17 release notes seems like a good idea.

Author: Peter Geoghegan <pg@bowt.ie>
Author: Matthias van de Meent <boekewurm+postgres@gmail.com>
Reviewed-By: Heikki Linnakangas <hlinnaka@iki.fi>
Reviewed-By: Matthias van de Meent <boekewurm+postgres@gmail.com>
Reviewed-By: Tomas Vondra <tomas.vondra@enterprisedb.com>
Discussion: https://postgr.es/m/CAH2-Wz=ksvN_sjcnD1+Bt-WtifRA5ok48aDYnq3pkKhxgMQpcw@mail.gmail.com
2024-04-06 11:47:10 -04:00
..
.gitignore Build all Flex files standalone 2022-09-04 12:09:01 +07:00
Makefile Add functions to generate random numbers in a specified range. 2024-03-27 10:12:39 +00:00
acl.c Optimize roles_is_member_of() with a Bloom filter. 2024-03-26 14:43:37 -05:00
amutils.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
array_expanded.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
array_selfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
array_typanalyze.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
array_userfuncs.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
arrayfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
arraysubs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
arrayutils.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
ascii.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
bool.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
cash.c Convert a few more datatype input functions to report errors softly. 2022-12-14 18:03:11 -05:00
char.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
cryptohashfuncs.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
date.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
datetime.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
datum.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
dbsize.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
domains.c Adjust populate_record_field() to handle errors softly 2024-01-24 15:04:33 +09:00
encode.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
enum.c Allow more cases to pass the unsafe-use-of-new-enum-value restriction. 2024-03-24 14:30:29 -04:00
expandeddatum.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
expandedrecord.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
float.c Add functions to generate random numbers in a specified range. 2024-03-27 10:12:39 +00:00
format_type.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
formatting.c Add unicode_strtitle() for Unicode Default Case Conversion. 2024-03-29 17:35:07 -07:00
genfile.c Remove the adminpack contrib extension 2024-03-04 12:39:22 +01:00
geo_ops.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
geo_selfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
geo_spgist.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
hbafuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
inet_cidr_ntop.c Avoid conflicts with library versions of inet_net_ntop() and friends. 2019-08-18 19:27:23 -04:00
inet_net_pton.c Avoid conflicts with library versions of inet_net_ntop() and friends. 2019-08-18 19:27:23 -04:00
int.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
int8.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
json.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
jsonb.c Add SQL/JSON query functions 2024-03-21 17:07:03 +09:00
jsonb_gin.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
jsonb_op.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
jsonb_util.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
jsonbsubs.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
jsonfuncs.c Add SQL/JSON query functions 2024-03-21 17:07:03 +09:00
jsonpath.c Add SQL/JSON query functions 2024-03-21 17:07:03 +09:00
jsonpath_exec.c Add basic JSON_TABLE() functionality 2024-04-04 20:20:15 +09:00
jsonpath_gram.y Implement various jsonpath methods 2024-01-25 10:15:43 -05:00
jsonpath_internal.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
jsonpath_scan.l Implement various jsonpath methods 2024-01-25 10:15:43 -05:00
levenshtein.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
like.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
like_match.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
like_support.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
lockfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
mac.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
mac8.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
mcxtfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
meson.build Add functions to generate random numbers in a specified range. 2024-03-27 10:12:39 +00:00
misc.c Add pg_basetype() function to extract a domain's base type. 2024-03-30 13:57:19 -04:00
multirangetypes.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
multirangetypes_selfuncs.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
name.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
network.c Work around spurious compiler warning in inet operators 2023-03-16 14:48:45 -07:00
network_gist.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
network_selfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
network_spgist.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
numeric.c Add functions to generate random numbers in a specified range. 2024-03-27 10:12:39 +00:00
numutils.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
oid.c Use new overflow-safe integer comparison functions. 2024-02-16 14:05:36 -06:00
oracle_compat.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
orderedsetaggs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
partitionfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_locale.c Use version for builtin collations. 2024-03-29 10:53:26 -07:00
pg_lsn.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
pg_upgrade_support.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
pgstatfuncs.c Revert "Add notBefore and notAfter to SSL cert info display" 2024-03-22 22:58:41 +01:00
pseudorandomfuncs.c Add functions to generate random numbers in a specified range. 2024-03-27 10:12:39 +00:00
pseudotypes.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
quote.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
rangetypes.c Add planner support functions for range operators <@ and @>. 2024-01-20 13:57:54 -05:00
rangetypes_gist.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
rangetypes_selfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
rangetypes_spgist.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
rangetypes_typanalyze.c Make the order of the header file includes consistent 2024-03-13 15:07:00 +01:00
regexp.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
regproc.c Add to_regtypemod function to extract typemod from a string type name. 2024-03-20 17:11:28 -04:00
ri_triggers.c Add temporal FOREIGN KEY contraints 2024-03-24 07:37:13 +01:00
rowtypes.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
ruleutils.c Add basic JSON_TABLE() functionality 2024-04-04 20:20:15 +09:00
selfuncs.c Enhance nbtree ScalarArrayOp execution. 2024-04-06 11:47:10 -04:00
tid.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
timestamp.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
trigfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
tsginidx.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
tsgistidx.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
tsquery.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
tsquery_cleanup.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
tsquery_gist.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
tsquery_op.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
tsquery_rewrite.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
tsquery_util.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
tsrank.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
tsvector.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
tsvector_op.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
tsvector_parser.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
uuid.c Add some UUID support functions 2024-03-19 09:32:04 +01:00
varbit.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
varchar.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
varlena.c C comment: mention no doc for negative start of substring(text) 2024-03-26 12:27:41 -04:00
version.c Update copyright for 2024 2024-01-03 20:49:05 -05:00
windowfuncs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
xid.c Use new overflow-safe integer comparison functions. 2024-02-16 14:05:36 -06:00
xid8funcs.c Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
xml.c Add destroyStringInfo function for cleaning up StringInfos 2024-03-16 23:18:28 +01:00