In 5543677ec9 we introduced an optimization that uses Limit instead of
Unique to implement DISTINCT when all the DISTINCT pathkeys have been
marked as redundant.  I happened to notice that this optimization was
not applied to partial DISTINCT, which I think should be.  This can
improve plans in some cases, such as

-- on master
explain (costs off) select distinct four from tenk1 where four = 4;
                  QUERY PLAN
----------------------------------------------
 Limit
   ->  Gather
         Workers Planned: 4
         ->  Unique
               ->  Parallel Seq Scan on tenk1
                     Filter: (four = 4)
(6 rows)

-- patched
explain (costs off) select distinct four from tenk1 where four = 4;
                  QUERY PLAN
----------------------------------------------
 Limit
   ->  Gather
         Workers Planned: 4
         ->  Limit
               ->  Parallel Seq Scan on tenk1
                     Filter: (four = 4)
(6 rows)

Such queries might not be that common, but it's very cheap to apply this
optimization.

Attached is a patch for that.

Thanks
Richard

Attachment: v1-0001-Apply-the-LIMIT-1-optimization-to-partial-DISTINCT.patch
Description: Binary data

Reply via email to