postgresql/src/backend
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
..
access Enhance nbtree ScalarArrayOp execution. 2024-04-06 11:47:10 -04:00
archive Add built-in ERROR handling for archive callbacks. 2024-04-02 22:28:11 -05:00
backup Align blocks in incremental backups to BLCKSZ 2024-04-05 16:30:01 +02:00
bootstrap Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
catalog CREATE INDEX: do not update stats during binary upgrade. 2024-04-03 16:12:45 -07:00
commands Remove obsolete comment in CopyReadLineText(). 2024-04-06 11:16:27 -04:00
executor Enhance nbtree ScalarArrayOp execution. 2024-04-06 11:47:10 -04:00
foreign Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
jit Add SQL/JSON query functions 2024-03-21 17:07:03 +09:00
lib Use the pairing heap instead of a flat array for LSN replay waiters 2024-04-03 18:15:41 +03:00
libpq Revert "Add notBefore and notAfter to SSL cert info display" 2024-03-22 22:58:41 +01:00
main Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
nodes Add basic JSON_TABLE() functionality 2024-04-04 20:20:15 +09:00
optimizer Enhance nbtree ScalarArrayOp execution. 2024-04-06 11:47:10 -04:00
parser Suppress "variable may be used uninitialized" warning. 2024-04-05 10:58:30 -04:00
partitioning Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
po Update copyright for 2024 2024-01-03 20:49:05 -05:00
port Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
postmaster Add built-in ERROR handling for archive callbacks. 2024-04-02 22:28:11 -05:00
regex Support C.UTF-8 locale in the new builtin collation provider. 2024-03-19 15:24:41 -07:00
replication Allow synced slots to have their inactive_since. 2024-04-05 09:48:49 +05:30
rewrite Add RETURNING support to MERGE. 2024-03-17 13:58:59 +00:00
snowball Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
statistics Make stxstattarget nullable 2024-03-17 12:26:26 +01:00
storage Increase default vacuum_buffer_usage_limit to 2MB. 2024-04-06 23:12:03 +13:00
tcop Invent SERIALIZE option for EXPLAIN. 2024-04-03 17:41:57 -04:00
tsearch Remove unused #include's from backend .c files 2024-03-04 12:02:20 +01:00
utils Enhance nbtree ScalarArrayOp execution. 2024-04-06 11:47:10 -04:00
.gitignore
Makefile Rework lwlocknames.txt to become lwlocklist.h 2024-03-20 11:55:20 +01:00
common.mk Blind attempt to fix LLVM dependency in the backend 2022-09-15 10:53:48 +07:00
meson.build Remove AIX support 2024-02-28 15:17:23 +04:00
nls.mk Remove distprep 2023-11-06 15:18:04 +01:00