| From: | Tender Wang <tndrwang(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Preserve presorted ORDER BY keys when reordering DISTINCT ON keys |
| Date: | 2026-11-25 06:09:25 |
| Message-ID: | CAHewXN=yaR83id87FqCVc04972LHb0oxtH4pDUPPfneyFO+ZoA@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi,
Commit a8ccf4e93a7e taught the planner to reorder DISTINCT keys to
match an input path's ordering.
Since the order of the DISTINCT keys does not affect which rows belong
to the same group, the planner
can use an existing ordering directly or use incremental sort when
only part of the ordering is available.
I found a DISTINCT ON case where the index already provides the full
ordering needed below Unique,
but the planner only recognizes the DISTINCT keys as presorted:
CREATE TABLE distinct_on_demo (x int, y int, z int);
INSERT INTO distinct_on_demo
SELECT i % 10, i % 10, i % 10
FROM generate_series(1, 1000) AS g(i);
INSERT INTO distinct_on_demo VALUES (0, 0, 21), (0, 0, 22);
CREATE INDEX distinct_on_demo_xyz_idx
ON distinct_on_demo (x, y, z DESC);
ANALYZE distinct_on_demo;
SET enable_seqscan = off;
EXPLAIN (COSTS OFF)
SELECT DISTINCT ON (y, x) x, y, z
FROM distinct_on_demo
ORDER BY y, x, z DESC;
QUERY PLAN
--------------------------------------------------------------------------------------
Sort
Sort Key: y, x, z DESC
-> Unique
-> Incremental Sort
Sort Key: x, y, z DESC
Presorted Key: x, y
-> Index Only Scan using distinct_on_demo_xyz_idx on
distinct_on_demo
(7 rows)
The required ordering is (y, x, z DESC), and the index provides (x, y, z DESC).
After reordering the DISTINCT keys to (x, y), the index already puts
all rows in each group together and orders them by z DESC.
For example, Unique should keep (0, 0, 22) for the (0, 0) group.
However, truncate_useless_pathkeys() only counts (x, y) as useful for
DISTINCT, so it drops z DESC from the index path's advertised
ordering.
The planner can then add an incremental sort below Unique to sort each
(x, y) group by z DESC, even though the index already provides that
ordering.
The attached patch makes truncate_useless_pathkeys() and
get_useful_pathkeys_for_distinct() use the same prefix-matching rules.
Once all DISTINCT keys have been matched in any order, the planner can
also reuse the remaining ORDER BY keys that match in their original
order.
In this example, that lets Unique consume the index ordering directly:
QUERY PLAN
--------------------------------------------------------------------------------
Sort
Sort Key: y, x, z DESC
-> Unique
-> Index Only Scan using distinct_on_demo_xyz_idx on distinct_on_demo
(4 rows)
The remaining sort is above Unique and applies the requested output
order to the deduplicated rows.
--
Tender Wang
| Attachment | Content-Type | Size |
|---|---|---|
| 0001-Preserve-presorted-ORDER-BY-keys-when-reordering-DIS.patch | text/plain | 10.6 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | shveta malik | 2026-11-25 06:18:45 | Re: Proposal: Conflict log history table for Logical Replication |
| Previous Message | Jiho Lee | 2026-11-25 06:07:17 | Re: PGC_S_ARGV vs pg_file_settings and pg_settings |