Re: Apply the "LIMIT 1" optimization to partial DISTINCT

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Richard Guo <guofenglinux(at)gmail(dot)com>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Apply the "LIMIT 1" optimization to partial DISTINCT
Date: 2024-01-26 08:14:01
Message-ID: CAApHDvqU7_Wryoi0v_PeCPhtgKUzgttrRb2DAzBf+R1PpMzMTw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, 26 Jan 2024 at 20:42, Richard Guo <guofenglinux(at)gmail(dot)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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Junwang Zhao 2024-01-26 08:18:14 Re: Make COPY format extendable: Extract COPY TO format implementations
Previous Message Pavel Stehule 2024-01-26 08:13:43 Re: proposal: psql: show current user in prompt