4 SELECT DISTINCT ON (string4) string4, two, ten
6 ORDER BY string4 using <, two using >, ten using <;
15 -- this will fail due to conflict of ordering requirements
16 SELECT DISTINCT ON (string4, ten) string4, two, ten
18 ORDER BY string4 using <, two using <, ten using <;
19 ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions
20 LINE 1: SELECT DISTINCT ON (string4, ten) string4, two, ten
22 SELECT DISTINCT ON (string4, ten) string4, ten, two
24 ORDER BY string4 using <, ten using >, two using <;
69 -- bug #5049: early 8.4.x chokes on volatile DISTINCT ON clauses
70 select distinct on (1) floor(random()) as r, f1 from int4_tbl order by 1,2;
77 -- Test the planner's ability to use a LIMIT 1 instead of a Unique node when
78 -- all of the distinct_pathkeys have been marked as redundant
80 -- Ensure we also get a LIMIT plan with DISTINCT ON
82 SELECT DISTINCT ON (four) four,two
83 FROM tenk1 WHERE four = 0 ORDER BY 1;
85 ----------------------------
91 -- and check the result of the above query is correct
92 SELECT DISTINCT ON (four) four,two
93 FROM tenk1 WHERE four = 0 ORDER BY 1;
99 -- Ensure a Sort -> Limit is used when the ORDER BY contains additional cols
101 SELECT DISTINCT ON (four) four,two
102 FROM tenk1 WHERE four = 0 ORDER BY 1,2;
104 ----------------------------------
112 -- Same again but use a column that is indexed so that we get an index scan
115 SELECT DISTINCT ON (four) four,hundred
116 FROM tenk1 WHERE four = 0 ORDER BY 1,2;
118 -----------------------------------------------
120 -> Index Scan using tenk1_hundred on tenk1
125 -- Test the planner's ability to reorder the distinctClause Pathkeys to match
126 -- the input path's ordering
128 CREATE TABLE distinct_on_tbl (x int, y int, z int);
129 INSERT INTO distinct_on_tbl SELECT i%10, i%10, i%10 FROM generate_series(1, 1000) AS i;
130 CREATE INDEX distinct_on_tbl_x_y_idx ON distinct_on_tbl (x, y);
131 ANALYZE distinct_on_tbl;
132 -- Produce results with sorting.
133 SET enable_hashagg TO OFF;
134 -- Ensure we avoid the need to re-sort by reordering the distinctClause
135 -- Pathkeys to match the ordering of the input path
137 SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl;
139 ------------------------------------------------------------------------
141 -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
144 SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl;
159 -- Ensure we leverage incremental-sort by reordering the distinctClause
160 -- Pathkeys to partially match the ordering of the input path
162 SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_on_tbl ORDER BY x) s;
164 ------------------------------------------------------------------------------------
169 -> Subquery Scan on s
170 -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
173 SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_on_tbl ORDER BY x) s;
188 -- Ensure we reorder the distinctClause Pathkeys to match the ordering of the
189 -- input path even if there is ORDER BY clause
191 SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ORDER BY y;
193 ------------------------------------------------------------------------------
197 -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
200 SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ORDER BY y;
215 -- Ensure the resulting pathkey list matches the initial distinctClause Pathkeys
217 SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_on_tbl order by x, z, y) s ORDER BY y, x, z;
219 ---------------------------------------------------------------------------------------------
221 Sort Key: s.y, s.x, s.z
224 Sort Key: s.x, s.y, s.z
226 -> Subquery Scan on s
228 Sort Key: distinct_on_tbl.x, distinct_on_tbl.z, distinct_on_tbl.y
229 -> Seq Scan on distinct_on_tbl
232 SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_on_tbl order by x, z, y) s ORDER BY y, x, z;
247 RESET enable_hashagg;
248 DROP TABLE distinct_on_tbl;