| From: | Tomas Vondra <tomas(at)vondra(dot)me> |
|---|---|
| To: | Chengpeng Yan <chengpeng_yan(at)outlook(dot)com> |
| Cc: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: extremely slow planner when DISTINCT clause holds more columns |
| Date: | 2026-12-06 11:48:53 |
| Message-ID: | dde86ce9-43f6-4614-b3cc-37339387d2af@vondra.me |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
On 12/6/26 05:38, Chengpeng Yan wrote:
> Hi,
>
>> On Dec 6, 2026, at 08:55, Tomas Vondra <tomas(at)vondra(dot)me> wrote:
>>
>> On 12/5/26 22:57, Tomas Vondra wrote:
>>>
>>> ...
>>>
>>> I haven't tried, but I think the key to improving this is to get rid of
>>> the linear search, likely by replacing it with a lookup in a hash table,
>>> or something like that.
>>>
>>> ISTM that if we extracted info about the EC for the "var" expression
>>> (I'm assuming there can be just one, but maybe we'd need to keep a
>>> list?), and keep a hash of those. And use that to quickly find if
>>> there's a matching expression, without the linear search.
>>>
>>> It'd need to be a bit more elaborate, because we only eliminate
>>> expressions from different relations, so we'd need to keep a list of
>>> rels for each EC, probably.
>>>
>>
>> I meant something like the attached patch. It's a dirty PoC patch, it
>> passes "make check" but it can be broken in some way. I've also not
>> checked if it doesn't hurt simple queries etc.
>>
>>
>> --
>> Tomas Vondra
>> <add_unique_group_var.patch>
>
>
> I agree with your analysis: the cost is the linear search in
> add_unique_group_var(), where each pair of grouping Vars from
> different relations calls exprs_known_equal(), which walks all of
> root->eq_classes. The O(N^2) pairs you mention each walk roughly N
> ECs, so it is about O(N^3) equal() calls.
>
Right. So it's actually even worse than my napkin estimate.
> Your PoC fixes this, but it restructures the function quite a bit.
> I wonder if a smaller change would be enough: look up the ECs of each
> grouping expression once, keep them in its GroupVarInfo, and then just
> check whether two Vars share an EC instead of calling
> exprs_known_equal(). The EC lookups then cost O(N * M), M being the
> number of members of multi-member ECs, and the pairwise loop is O(N^2)
> cheap comparisons.
>
> Diff attached. Does that look workable to you?
>
It seems like a good simpler / limited fix. It does not quite get rid of
the linear search, but it makes it much cheaper. It's probably a good
compromise for rather uncommon / extreme query.
regards
--
Tomas Vondra
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Zhijie Hou | 2026-12-06 12:47:21 | Re: Incremental backup inconsistency after vacuum truncation + COPY regrowth |
| Previous Message | Shlomi Noach | 2026-12-06 10:54:25 | Re: [PATCH] Fix timeline history after recovery stops on an ancestor |