ORCA: fall back to planner for ORDER BY with ordering operators (amcanorderbyop)#1652
Closed
ORCA: fall back to planner for ORDER BY with ordering operators (amcanorderbyop)#1652
Conversation
…norderbyop) ORCA does not support amcanorderbyop (KNN ordered index scans). Queries like `ORDER BY col <-> 'value' LIMIT N` on GiST indexes cannot produce ordered index scans in ORCA, resulting in inefficient Seq Scan + Sort plans instead of KNN-GiST Index Scan. Previously, these queries would accidentally get correct plans because column-level COLLATE "C" caused a blanket fallback to the PostgreSQL planner, which does support amcanorderbyop. After commit 3f4ce85 added COLLATE "C" support to ORCA, these queries lost their fallback path. Add has_orderby_ordering_op() in walkers.c to detect when a query's ORDER BY clause contains an operator registered as AMOP_ORDER in pg_amop (e.g., <-> for trigram/point distance). When detected, ORCA falls back to the PostgreSQL planner which can generate KNN ordered index scans. The check is precise: only ORDER BY with ordering operators triggers fallback. Other queries on the same tables (WHERE with LIKE/%%, equality filters, etc.) continue to use ORCA normally.
Only fall back to the PostgreSQL planner when ALL ordering-operator expressions in ORDER BY have at least one direct Var (column reference) argument. Expressions like "circle(p,1) <-> point(0,0)" wrap the column in a function call, which can cause "lossy distance functions are not supported in index-only scans" errors in the planner. Leave such queries for ORCA to handle via Seq Scan + Sort.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ORCA does not support
amcanorderbyop(KNN ordered index scans). Queries likeORDER BY col <-> 'value' LIMIT Non GiST indexes cannot produce ordered index scans in ORCA, resulting in inefficient Seq Scan + Sort plans instead of KNN-GiST Index Scan.Previously, these queries accidentally got correct plans because column-level
COLLATE "C"caused a blanket fallback to the PostgreSQL planner, which does supportamcanorderbyop. After commit 3f4ce85 addedCOLLATE "C"support to ORCA, these queries lost their fallback path and regressed to slow plans.Changes
Add
has_orderby_ordering_op()inwalkers.c: Detects when a query'sORDER BYclause contains an operator registered asAMOP_ORDERinpg_amop(e.g.,<->for distance). When detected, ORCA falls back to the PostgreSQL planner which can generate KNN ordered index scans.Refine fallback to skip lossy distance functions: Only fall back when ALL ordering-operator expressions have at least one direct
Var(column reference) argument. Expressions likecircle(p,1) <-> point(0,0)wrap the column in a function call, which causes "lossy distance functions are not supported in index-only scans" errors in the planner. Such queries are left for ORCA to handle via Seq Scan + Sort.Update expected test outputs: btree_gist, pg_trgm, create_index, and gist test expected files updated to reflect the improved plans (Index Only Scan instead of Seq Scan + Sort).
Key design decisions
ORDER BYwith ordering operators triggers fallback. Other queries on the same tables (e.g.,WHEREwithLIKE/%, equality filters) continue to use ORCA normally.Var-argument check avoids a known planner limitation with lossy distance functions wrapped in expressions.Test plan
make installcheck-worldpasses