Skip to content

Commit 4959611

Browse files
committed
ORCA: fall back to Postgres planner for KNN ORDER BY queries
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.
1 parent 857723b commit 4959611

File tree

19 files changed

+235
-123
lines changed

19 files changed

+235
-123
lines changed

contrib/btree_gist/expected/cash_optimizer.out

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,11 @@ SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;
7777
QUERY PLAN
7878
------------------------------------------------------------
7979
Limit
80-
-> Sort
81-
Sort Key: ((a <-> '$21,472.79'::money))
82-
-> Result
83-
-> Gather Motion 3:1 (slice1; segments: 3)
84-
-> Seq Scan on moneytmp
85-
Optimizer: GPORCA
80+
-> Gather Motion 3:1 (slice1; segments: 3)
81+
Merge Key: ((a <-> '$21,472.79'::money))
82+
-> Limit
83+
-> Index Only Scan using moneyidx on moneytmp
84+
Order By: (a <-> '$21,472.79'::money)
8685
(7 rows)
8786

8887
SELECT a, a <-> '21472.79' FROM moneytmp ORDER BY a <-> '21472.79' LIMIT 3;

contrib/btree_gist/expected/date_optimizer.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> '02-13-2001'::date))
82-
-> Sort
83-
Sort Key: ((a <-> '02-13-2001'::date))
84-
-> Seq Scan on datetmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using dateidx on datetmp
84+
Order By: (a <-> '02-13-2001'::date)
8685
(7 rows)
8786

8887
SELECT a, a <-> '2001-02-13' FROM datetmp ORDER BY a <-> '2001-02-13' LIMIT 3;

contrib/btree_gist/expected/float4_optimizer.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> '-179'::real))
82-
-> Sort
83-
Sort Key: ((a <-> '-179'::real))
84-
-> Seq Scan on float4tmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using float4idx on float4tmp
84+
Order By: (a <-> '-179'::real)
8685
(7 rows)
8786

8887
SELECT a, a <-> '-179.0' FROM float4tmp ORDER BY a <-> '-179.0' LIMIT 3;

contrib/btree_gist/expected/float8_optimizer.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> '-1890'::double precision))
82-
-> Sort
83-
Sort Key: ((a <-> '-1890'::double precision))
84-
-> Seq Scan on float8tmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using float8idx on float8tmp
84+
Order By: (a <-> '-1890'::double precision)
8685
(7 rows)
8786

8887
SELECT a, a <-> '-1890.0' FROM float8tmp ORDER BY a <-> '-1890.0' LIMIT 3;

contrib/btree_gist/expected/int2_optimizer.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> '237'::smallint))
82-
-> Sort
83-
Sort Key: ((a <-> '237'::smallint))
84-
-> Seq Scan on int2tmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using int2idx on int2tmp
84+
Order By: (a <-> '237'::smallint)
8685
(7 rows)
8786

8887
SELECT a, a <-> '237' FROM int2tmp ORDER BY a <-> '237' LIMIT 3;

contrib/btree_gist/expected/int4_optimizer.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> 237))
82-
-> Sort
83-
Sort Key: ((a <-> 237))
84-
-> Seq Scan on int4tmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using int4idx on int4tmp
84+
Order By: (a <-> 237)
8685
(7 rows)
8786

8887
SELECT a, a <-> '237' FROM int4tmp ORDER BY a <-> '237' LIMIT 3;

contrib/btree_gist/expected/int8_optimizer.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841'
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> '464571291354841'::bigint))
82-
-> Sort
83-
Sort Key: ((a <-> '464571291354841'::bigint))
84-
-> Seq Scan on int8tmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using int8idx on int8tmp
84+
Order By: (a <-> '464571291354841'::bigint)
8685
(7 rows)
8786

8887
SELECT a, a <-> '464571291354841' FROM int8tmp ORDER BY a <-> '464571291354841' LIMIT 3;

contrib/btree_gist/expected/interval_optimizer.out

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ SELECT count(*) FROM intervaltmp WHERE a > '199 days 21:21:23'::interval;
7474

7575
EXPLAIN (COSTS OFF)
7676
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
77-
QUERY PLAN
78-
-----------------------------------------------------------------------------------
77+
QUERY PLAN
78+
---------------------------------------------------------------------------------------
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> '@ 199 days 21 hours 21 mins 23 secs'::interval))
82-
-> Sort
83-
Sort Key: ((a <-> '@ 199 days 21 hours 21 mins 23 secs'::interval))
84-
-> Seq Scan on intervaltmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using intervalidx on intervaltmp
84+
Order By: (a <-> '@ 199 days 21 hours 21 mins 23 secs'::interval)
85+
Optimizer: Postgres query optimizer
8686
(7 rows)
8787

8888
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
@@ -96,15 +96,15 @@ SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21
9696
SET enable_indexonlyscan=off;
9797
EXPLAIN (COSTS OFF)
9898
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;
99-
QUERY PLAN
100-
-----------------------------------------------------------------------------------
99+
QUERY PLAN
100+
---------------------------------------------------------------------------------------
101101
Limit
102102
-> Gather Motion 3:1 (slice1; segments: 3)
103103
Merge Key: ((a <-> '@ 199 days 21 hours 21 mins 23 secs'::interval))
104-
-> Sort
105-
Sort Key: ((a <-> '@ 199 days 21 hours 21 mins 23 secs'::interval))
106-
-> Seq Scan on intervaltmp
107-
Optimizer: GPORCA
104+
-> Limit
105+
-> Index Scan using intervalidx on intervaltmp
106+
Order By: (a <-> '@ 199 days 21 hours 21 mins 23 secs'::interval)
107+
Optimizer: Postgres query optimizer
108108
(7 rows)
109109

110110
SELECT a, a <-> '199 days 21:21:23' FROM intervaltmp ORDER BY a <-> '199 days 21:21:23' LIMIT 3;

contrib/btree_gist/expected/time_optimizer.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> '10:57:11'::time without time zone))
82-
-> Sort
83-
Sort Key: ((a <-> '10:57:11'::time without time zone))
84-
-> Seq Scan on timetmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using timeidx on timetmp
84+
Order By: (a <-> '10:57:11'::time without time zone)
8685
(7 rows)
8786

8887
SELECT a, a <-> '10:57:11' FROM timetmp ORDER BY a <-> '10:57:11' LIMIT 3;

contrib/btree_gist/expected/timestamp_optimizer.out

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,9 @@ SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-
7979
Limit
8080
-> Gather Motion 3:1 (slice1; segments: 3)
8181
Merge Key: ((a <-> 'Tue Oct 26 08:55:08 2004'::timestamp without time zone))
82-
-> Sort
83-
Sort Key: ((a <-> 'Tue Oct 26 08:55:08 2004'::timestamp without time zone))
84-
-> Seq Scan on timestamptmp
85-
Optimizer: GPORCA
82+
-> Limit
83+
-> Index Only Scan using timestampidx on timestamptmp
84+
Order By: (a <-> 'Tue Oct 26 08:55:08 2004'::timestamp without time zone)
8685
(7 rows)
8786

8887
SELECT a, a <-> '2004-10-26 08:55:08' FROM timestamptmp ORDER BY a <-> '2004-10-26 08:55:08' LIMIT 3;

0 commit comments

Comments
 (0)