postgresql/src/include/utils
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 Generate automatically code and documentation related to wait events 2023-07-05 10:53:11 +09:00
acl.h Reintroduce MAINTAIN privilege and pg_maintain predefined role. 2024-03-13 14:49:26 -05:00
aclchk_internal.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
array.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
arrayaccess.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
ascii.h Move is_valid_ascii() to ascii.h. 2024-01-29 12:08:57 -06:00
attoptcache.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
backend_progress.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
backend_status.h Revert "Add notBefore and notAfter to SSL cert info display" 2024-03-22 22:58:41 +01:00
builtins.h Adjust populate_record_field() to handle errors softly 2024-01-24 15:04:33 +09:00
bytea.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
cash.h Convert *GetDatum() and DatumGet*() macros to inline functions 2022-09-27 20:50:21 +02:00
catcache.h Use a hash table for catcache.c's CatCList objects. 2024-03-22 17:13:53 -04:00
combocid.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
conffiles.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
date.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
datetime.h Support TZ and OF format codes in to_timestamp(). 2024-01-25 17:47:08 -05:00
datum.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
dsa.h Allow specifying initial and maximum segment sizes for DSA. 2024-03-27 11:43:29 +09:00
dynahash.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
elog.h Remove obsolete check in SIGTERM handler for the startup process. 2024-02-14 17:09:31 -06:00
evtcache.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
expandeddatum.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
expandedrecord.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
float.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
fmgrtab.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
formatting.h Add SQL/JSON query functions 2024-03-21 17:07:03 +09:00
freepage.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
geo_decls.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
guc.h Add allow_alter_system GUC. 2024-03-29 08:45:11 -04:00
guc_hooks.h Introduce a new GUC 'standby_slot_names'. 2024-03-08 08:10:45 +05:30
guc_tables.h Rename COMPAT_OPTIONS_CLIENT to COMPAT_OPTIONS_OTHER. 2024-03-27 10:45:28 -04:00
help_config.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
hsearch.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
index_selfuncs.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
inet.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
injection_point.h Add backend support for injection points 2024-01-22 10:15:50 +09:00
inval.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
json.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
jsonb.h Add SQL/JSON query functions 2024-03-21 17:07:03 +09:00
jsonfuncs.h Add SQL/JSON query functions 2024-03-21 17:07:03 +09:00
jsonpath.h Add basic JSON_TABLE() functionality 2024-04-04 20:20:15 +09:00
logtape.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
lsyscache.h Allow specifying an access method for partitioned tables 2024-03-25 16:30:36 +01:00
memdebug.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
memutils.h Avoid recursion in MemoryContext functions 2024-03-08 13:18:30 +02:00
memutils_internal.h Adjust memory allocation functions to allow sibling calls 2024-02-27 16:39:42 +13:00
memutils_memorychunk.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
meson.build Update copyright for 2024 2024-01-03 20:49:05 -05:00
multirangetypes.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
numeric.h Add functions to generate random numbers in a specified range. 2024-03-27 10:12:39 +00:00
palloc.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
partcache.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_crc.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_locale.h Address more review comments on commit 2d819a08a1. 2024-03-18 11:58:13 -07:00
pg_lsn.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pg_rusage.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
pgstat_internal.h Rename SLRU elements in view pg_stat_slru 2024-02-28 09:39:52 +01:00
pidfile.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
plancache.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
portal.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
ps_status.h Speedup and increase usability of set proc title functions 2023-02-20 16:18:27 +13:00
queryenvironment.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
rangetypes.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
regproc.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
rel.h Allow table AM to store complex data structures in rd_amcache 2024-03-21 23:00:34 +02:00
relcache.h Give SMgrRelation pointers a well-defined lifetime. 2024-01-31 12:31:02 +02:00
relfilenumbermap.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
relmapper.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
relptr.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
reltrigger.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
resowner.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
resowner_private.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
rls.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
ruleutils.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
sampling.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
selfuncs.h Enhance nbtree ScalarArrayOp execution. 2024-04-06 11:47:10 -04:00
sharedtuplestore.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
snapmgr.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
snapshot.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
sortsupport.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
spccache.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
syscache.h Generate syscache info from catalog files 2024-01-23 07:31:06 +01:00
timeout.h Introduce transaction_timeout 2024-02-15 23:56:12 +02:00
timestamp.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
tuplesort.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
tuplestore.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
typcache.h Add planner support functions for range operators <@ and @>. 2024-01-20 13:57:54 -05:00
tzparser.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
usercontext.h Perform logical replication actions as the table owner. 2023-04-04 11:25:23 -04:00
uuid.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
varbit.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
varlena.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
wait_event.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
xid8.h Update copyright for 2024 2024-01-03 20:49:05 -05:00
xml.h Update copyright for 2024 2024-01-03 20:49:05 -05:00