Re: Optimizing nbtree ScalarArrayOp execution, allowing multi-column ordered scans, skip scan
Matthias van de Meent <boekewurm+postgres@gmail.com>
Commits
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Move nbtree preprocessing into new .c file.
- 597b1ffbf123 18.0 landed
-
Fix nbtree lookahead overflow bug.
- 09a8407dbfd8 18.0 landed
- 6749d4aabe74 17.0 landed
-
Remove unneeded nbtree array preprocessing assert.
- 480bc6e3ed3a 17.0 landed
-
Don't try to fix eliminated nbtree array scan keys.
- f22e17f76cf5 17.0 landed
-
Remove redundant nbtree preprocessing assertions.
- 3b08133cd13c 17.0 landed
-
Avoid extra lookups with nbtree array inequalities.
- 473411fc5115 17.0 landed
-
Enhance nbtree ScalarArrayOp execution.
- 5bf748b86bc6 17.0 landed
-
Improvements and fixes for e0b1ee17dc
- 7e6fb5da41d8 17.0 cited
-
Skip checking of scan keys required for directional scan in B-tree
- e0b1ee17dc3a 17.0 cited
-
Fix btmarkpos/btrestrpos array key wraparound bug.
- 714780dcddf0 17.0 cited
-
Add nbtree high key "continuescan" optimization.
- 29b64d1de7c7 12.0 cited
-
Consider secondary factors during nbtree splits.
- fab250243387 12.0 cited
-
Make heap TID a tiebreaker nbtree index column.
- dd299df8189b 12.0 cited
-
Fix planning of btree index scans using ScalarArrayOpExpr quals.
- 807a40c551dd 9.3.0 cited
-
Fix btree stop-at-nulls logic properly.
- 882368e854b6 9.2.0 cited
-
Teach btree to handle ScalarArrayOpExpr quals natively.
- 9e8da0f75731 9.2.0 cited
Attachments
On Sat, 16 Mar 2024 at 01:12, Peter Geoghegan <pg@bowt.ie> wrote: > > On Fri, Mar 8, 2024 at 9:00 AM Matthias van de Meent > <boekewurm+postgres@gmail.com> wrote: > > I've attached v14, where 0001 is v13, 0002 is a patch with small > > changes + some large comment suggestions, and 0003 which contains > > sorted merge join code for _bt_merge_arrays. > > This is part of my next revision, v15, which I've attached (along with > a test case that you might find useful, explained below). > > v15 makes the difference between the non-required scan key trigger and > required scan key trigger cases clearer within _bt_advance_array_keys. OK, here's a small additional review, with a suggestion for additional changes to _bt_preprocess: > @@ -1117,6 +3160,46 @@ _bt_compare_scankey_args(IndexScanDesc scan, ScanKey op, > /* > * The opfamily we need to worry about is identified by the index column. > */ > Assert(leftarg->sk_attno == rightarg->sk_attno); > > + /* > + * If either leftarg or rightarg are equality-type array scankeys, we need > + * specialized handling (since by now we know that IS NULL wasn't used) > + */ > [...] > + } > + > opcintype = rel->rd_opcintype[leftarg->sk_attno - 1]; Here, you insert your code between the comment about which opfamily to choose and the code assigning the opfamily. I think this needs some cleanup. > + * Don't call _bt_preprocess_array_keys_final in this fast path > + * (we'll miss out on the single value array transformation, but > + * that's not nearly as important when there's only one scan key) Why is it OK to ignore it? Or, why don't we apply it here? --- Attached 2 patches for further optimization of the _bt_preprocess_keys path (on top of your v15), according to the following idea: Right now, we do "expensive" processing with xform merging for all keys when we have more than 1 keys in the scan. However, we only do per-attribute merging of these keys, so if there is only one key for any attribute, the many cycles spent in that loop are mostly wasted. By checking for single-scankey attributes, we can short-track many multi-column index scans because they usually have only a single scan key per attribute. The first implements that idea, the second reduces the scope of various variables so as to improve compiler optimizability. I'll try to work a bit more on merging the _bt_preprocess steps into a single main iterator, but this is about as far as I got with clean patches. Merging the steps for array preprocessing with per-key processing and post-processing is proving a bit more complex than I'd anticipated, so I don't think I'll be able to finish that before the feature freeze, especially with other things that keep distracting me. Matthias van de Meent