Skip to content

ORCA: fall back to Postgres planner for KNN ORDER BY queries#1653

Open
yjhjstz wants to merge 1 commit intoapache:mainfrom
yjhjstz:fix_orca_gist
Open

ORCA: fall back to Postgres planner for KNN ORDER BY queries#1653
yjhjstz wants to merge 1 commit intoapache:mainfrom
yjhjstz:fix_orca_gist

Conversation

@yjhjstz
Copy link
Copy Markdown
Member

@yjhjstz yjhjstz commented Apr 1, 2026

ORCA is unaware of amcanorderbyop, so it plans "ORDER BY col <-> val"
queries with a full Seq Scan + Sort instead of a native KNN ordered
index scan. Detect this pattern by checking whether any ORDER BY
target is an operator with amoppurpose = AMOP_ORDER in pg_amop and at
least one direct Var argument, then raise ExmiQuery2DXLUnsupportedFeature
to hand the query off to the Postgres planner, which generates an
efficient Index Only Scan with native KNN ordering.

Queries where the ordering operator's arguments are entirely computed
expressions (e.g. circle(col,1) <-> point(0,0)) are excluded from the
fallback to avoid lossy-distance errors in index-only scans.

Fixes #ISSUE_Number

What does this PR do?

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context

CI Skip Instructions


* (not wrapped in a function or other expression).
*/
static bool
has_plain_var_arg(OpExpr *op)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check requires a direct Var node. A RelabelType wrapping a Var (common with type coercions) would not match:

-- If the column is int2 but the operator expects int4,
-- the Var gets wrapped in RelabelType
ORDER BY int2_col <-> 42

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed by strip_implicit_coercions

@reshke
Copy link
Copy Markdown
Contributor

reshke commented Apr 1, 2026

will try to take a look soon. Changes looks sane

  ORCA is unaware of amcanorderbyop, so it plans "ORDER BY col <-> val"
  queries with a full Seq Scan + Sort instead of a native KNN ordered
  index scan.  Detect this pattern by checking whether any ORDER BY
  target is an operator with amoppurpose = AMOP_ORDER in pg_amop and at
  least one direct Var argument, then raise ExmiQuery2DXLUnsupportedFeature
  to hand the query off to the Postgres planner, which generates an
  efficient Index Only Scan with native KNN ordering.

  Queries where the ordering operator's arguments are entirely computed
  expressions (e.g. circle(col,1) <-> point(0,0)) are excluded from the
  fallback to avoid lossy-distance errors in index-only scans.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants