Re: Apply the "LIMIT 1" optimization to partial DISTINCT - Mailing list pgsql-hackers

From David Rowley
Subject Re: Apply the "LIMIT 1" optimization to partial DISTINCT
Date
Msg-id CAApHDvqU7_Wryoi0v_PeCPhtgKUzgttrRb2DAzBf+R1PpMzMTw@mail.gmail.com
Whole thread Raw
In response to Apply the "LIMIT 1" optimization to partial DISTINCT  (Richard Guo <guofenglinux@gmail.com>)
Responses Re: Apply the "LIMIT 1" optimization to partial DISTINCT
List pgsql-hackers
On Fri, 26 Jan 2024 at 20:42, Richard Guo <guofenglinux@gmail.com> wrote:
>
> 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.

It seems very likely that the parallel plan would only be chosen if
the planner estimated there'd just be 1 row before the distinct.
Otherwise, the non-partial path's LIMIT would come out so cheap that
it would be unlikely that the parallel plan would be picked.

I think your test case only chooses the parallel plan because you're
doing FROM tenk1 WHERE four=4.  And that column only contains values
0..3.

However, having said that. Parallel plans are often picked when there
is some highly selective qual as parallel_tuple_cost has to be applied
to fewer tuples for such plans, so probably this is worth doing.

David



pgsql-hackers by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: proposal: psql: show current user in prompt
Next
From: Junwang Zhao
Date:
Subject: Re: Make COPY format extendable: Extract COPY TO format implementations