@@ -125,23 +125,23 @@ SELECT DISTINCT ON (four) four,hundred
125
125
-- Test the planner's ability to reorder the distinctClause Pathkeys to match
126
126
-- the input path's ordering
127
127
--
128
- CREATE TABLE distinct_tbl (x int, y int, z int);
129
- INSERT INTO distinct_tbl SELECT i%10, i%10, i%10 FROM generate_series(1, 1000) AS i;
130
- CREATE INDEX distinct_tbl_x_y_idx ON distinct_tbl (x, y);
131
- ANALYZE distinct_tbl ;
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
132
-- Produce results with sorting.
133
133
SET enable_hashagg TO OFF;
134
134
-- Ensure we avoid the need to re-sort by reordering the distinctClause
135
135
-- Pathkeys to match the ordering of the input path
136
136
EXPLAIN (COSTS OFF)
137
- SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ;
138
- QUERY PLAN
139
- ------------------------------------------------------------------
137
+ SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ;
138
+ QUERY PLAN
139
+ ------------------------------------------------------------------------
140
140
Unique
141
- -> Index Only Scan using distinct_tbl_x_y_idx on distinct_tbl
141
+ -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
142
142
(2 rows)
143
143
144
- SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ;
144
+ SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ;
145
145
x | y
146
146
---+---
147
147
0 | 0
@@ -159,18 +159,18 @@ SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl;
159
159
-- Ensure we leverage incremental-sort by reordering the distinctClause
160
160
-- Pathkeys to partially match the ordering of the input path
161
161
EXPLAIN (COSTS OFF)
162
- SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_tbl ORDER BY x) s;
163
- QUERY PLAN
164
- ------------------------------------------------------------------------------
162
+ SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_on_tbl ORDER BY x) s;
163
+ QUERY PLAN
164
+ ------------------------------------------------------------------------------------
165
165
Unique
166
166
-> Incremental Sort
167
167
Sort Key: s.x, s.y
168
168
Presorted Key: s.x
169
169
-> Subquery Scan on s
170
- -> Index Only Scan using distinct_tbl_x_y_idx on distinct_tbl
170
+ -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
171
171
(6 rows)
172
172
173
- SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_tbl ORDER BY x) s;
173
+ SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_on_tbl ORDER BY x) s;
174
174
x | y
175
175
---+---
176
176
0 | 0
@@ -188,16 +188,16 @@ SELECT DISTINCT ON (y, x) x, y FROM (SELECT * FROM distinct_tbl ORDER BY x) s;
188
188
-- Ensure we reorder the distinctClause Pathkeys to match the ordering of the
189
189
-- input path even if there is ORDER BY clause
190
190
EXPLAIN (COSTS OFF)
191
- SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ORDER BY y;
192
- QUERY PLAN
193
- ------------------------------------------------------------------------
191
+ SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ORDER BY y;
192
+ QUERY PLAN
193
+ ------------------------------------------------------------------------------
194
194
Sort
195
195
Sort Key: y
196
196
-> Unique
197
- -> Index Only Scan using distinct_tbl_x_y_idx on distinct_tbl
197
+ -> Index Only Scan using distinct_on_tbl_x_y_idx on distinct_on_tbl
198
198
(4 rows)
199
199
200
- SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ORDER BY y;
200
+ SELECT DISTINCT ON (y, x) x, y FROM distinct_on_tbl ORDER BY y;
201
201
x | y
202
202
---+---
203
203
0 | 0
@@ -214,9 +214,9 @@ SELECT DISTINCT ON (y, x) x, y FROM distinct_tbl ORDER BY y;
214
214
215
215
-- Ensure the resulting pathkey list matches the initial distinctClause Pathkeys
216
216
EXPLAIN (COSTS OFF)
217
- SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_tbl order by x, z, y) s ORDER BY y, x, z;
218
- QUERY PLAN
219
- ------------------------------------------------------------------------------------
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;
218
+ QUERY PLAN
219
+ ---------------------------------------------------------------------------------------------
220
220
Sort
221
221
Sort Key: s.y, s.x, s.z
222
222
-> Unique
@@ -225,11 +225,11 @@ SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_tbl order by x, z, y
225
225
Presorted Key: s.x
226
226
-> Subquery Scan on s
227
227
-> Sort
228
- Sort Key: distinct_tbl .x, distinct_tbl .z, distinct_tbl .y
229
- -> Seq Scan on distinct_tbl
228
+ Sort Key: distinct_on_tbl .x, distinct_on_tbl .z, distinct_on_tbl .y
229
+ -> Seq Scan on distinct_on_tbl
230
230
(10 rows)
231
231
232
- SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_tbl order by x, z, y) s ORDER BY y, x, z;
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;
233
233
x | y
234
234
---+---
235
235
0 | 0
@@ -245,4 +245,4 @@ SELECT DISTINCT ON (y, x) x, y FROM (select * from distinct_tbl order by x, z, y
245
245
(10 rows)
246
246
247
247
RESET enable_hashagg;
248
- DROP TABLE distinct_tbl ;
248
+ DROP TABLE distinct_on_tbl ;
0 commit comments