-- Test the planner's ability to reorder the distinctClause Pathkeys to match
-- the input path's ordering
--
-CREATE TABLE distinct_tbl (x int, y int, z int);
-INSERT INTO distinct_tbl SELECT i%10, i%10, i%10 FROM generate_series(1, 1000) AS i;
-CREATE INDEX distinct_tbl_x_y_idx ON distinct_tbl (x, y);
-ANALYZE distinct_tbl;
+CREATE TABLE distinct_on_tbl (x int, y int, z int);
+INSERT INTO distinct_on_tbl SELECT i%10, i%10, i%10 FROM generate_series(1, 1000) AS i;
+CREATE INDEX distinct_on_tbl_x_y_idx ON distinct_on_tbl (x, y);
+ANALYZE distinct_on_tbl;
-- Produce results with sorting.
SET enable_hashagg TO OFF;
-- Ensure we avoid the need to re-sort by reordering the distinctClause
-- Pathkeys to match the ordering of the input path
EXPLAIN (COSTS OFF)
-SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl;
- QUERY PLAN
-------------------------------------------------------------------
+SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl;
+ QUERY PLAN
+------------------------------------------------------------------------
Unique
- -> Index Only Scan using distinct_tbl_x_y_idx on distinct_tbl
+ -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
(2 rows)
-SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl;
+SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl;
x | y
---+---
0 | 0
-- Ensure we leverage incremental-sort by reordering the distinctClause
-- Pathkeys to partially match the ordering of the input path
EXPLAIN (COSTS OFF)
-SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_tbl ORDER BY x) s;
- QUERY PLAN
-------------------------------------------------------------------------------
+SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_on_tbl ORDER BY x) s;
+ QUERY PLAN
+------------------------------------------------------------------------------------
Unique
-> Incremental Sort
Sort Key: s.x, s.y
Presorted Key: s.x
-> Subquery Scan on s
- -> Index Only Scan using distinct_tbl_x_y_idx on distinct_tbl
+ -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
(6 rows)
-SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_tbl ORDER BY x) s;
+SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_on_tbl ORDER BY x) s;
x | y
---+---
0 | 0
-- Ensure we reorder the distinctClause Pathkeys to match the ordering of the
-- input path even if there is ORDER BY clause
EXPLAIN (COSTS OFF)
-SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ORDER BY y;
- QUERY PLAN
-------------------------------------------------------------------------
+SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ORDER BY y;
+ QUERY PLAN
+------------------------------------------------------------------------------
Sort
Sort Key: y
-> Unique
- -> Index Only Scan using distinct_tbl_x_y_idx on distinct_tbl
+ -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
(4 rows)
-SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ORDER BY y;
+SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ORDER BY y;
x | y
---+---
0 | 0
-- Ensure the resulting pathkey list matches the initial distinctClause Pathkeys
EXPLAIN (COSTS OFF)
-SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_tbl order by x, z, y) s ORDER BY y, x, z;
- QUERY PLAN
-------------------------------------------------------------------------------------
+SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_on_tbl order by x, z, y) s ORDER BY y, x, z;
+ QUERY PLAN
+---------------------------------------------------------------------------------------------
Sort
Sort Key: s.y, s.x, s.z
-> Unique
Presorted Key: s.x
-> Subquery Scan on s
-> Sort
- Sort Key: distinct_tbl.x, distinct_tbl.z, distinct_tbl.y
- -> Seq Scan on distinct_tbl
+ Sort Key: distinct_on_tbl.x, distinct_on_tbl.z, distinct_on_tbl.y
+ -> Seq Scan on distinct_on_tbl
(10 rows)
-SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_tbl order by x, z, y) s ORDER BY y, x, z;
+SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_on_tbl order by x, z, y) s ORDER BY y, x, z;
x | y
---+---
0 | 0
(10 rows)
RESET enable_hashagg;
-DROP TABLE distinct_tbl;
+DROP TABLE distinct_on_tbl;
-- the input path's ordering
--
-CREATE TABLE distinct_tbl (x int, y int, z int);
-INSERT INTO distinct_tbl SELECT i%10, i%10, i%10 FROM generate_series(1, 1000) AS i;
-CREATE INDEX distinct_tbl_x_y_idx ON distinct_tbl (x, y);
-ANALYZE distinct_tbl;
+CREATE TABLE distinct_on_tbl (x int, y int, z int);
+INSERT INTO distinct_on_tbl SELECT i%10, i%10, i%10 FROM generate_series(1, 1000) AS i;
+CREATE INDEX distinct_on_tbl_x_y_idx ON distinct_on_tbl (x, y);
+ANALYZE distinct_on_tbl;
-- Produce results with sorting.
SET enable_hashagg TO OFF;
-- Ensure we avoid the need to re-sort by reordering the distinctClause
-- Pathkeys to match the ordering of the input path
EXPLAIN (COSTS OFF)
-SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl;
-SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl;
+SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl;
+SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl;
-- Ensure we leverage incremental-sort by reordering the distinctClause
-- Pathkeys to partially match the ordering of the input path
EXPLAIN (COSTS OFF)
-SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_tbl ORDER BY x) s;
-SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_tbl ORDER BY x) s;
+SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_on_tbl ORDER BY x) s;
+SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_on_tbl ORDER BY x) s;
-- Ensure we reorder the distinctClause Pathkeys to match the ordering of the
-- input path even if there is ORDER BY clause
EXPLAIN (COSTS OFF)
-SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ORDER BY y;
-SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ORDER BY y;
+SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ORDER BY y;
+SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ORDER BY y;
-- Ensure the resulting pathkey list matches the initial distinctClause Pathkeys
EXPLAIN (COSTS OFF)
-SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_tbl order by x, z, y) s ORDER BY y, x, z;
-SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_tbl order by x, z, y) s ORDER BY y, x, z;
+SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_on_tbl order by x, z, y) s ORDER BY y, x, z;
+SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_on_tbl order by x, z, y) s ORDER BY y, x, z;
RESET enable_hashagg;
-DROP TABLE distinct_tbl;
+DROP TABLE distinct_on_tbl;