Hi Tender,

> On Nov 25, 2026, at 14:09, Tender Wang <[email protected]> wrote:
> 
> 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
> <0001-Preserve-presorted-ORDER-BY-keys-when-reordering-DIS.patch>

Thanks for working on this. I reviewed v1 and it looks good to me. I
wonder if it's worth adding a negative test with an index on (x, y, w
DESC, z DESC) and a query with ORDER BY y, x, z DESC, w DESC. If the
plan uses that index, it should still sort before Unique to pick the
right row.

Also, please run pgindent.

--
Best regards,
Chengpeng Yan


Reply via email to