| From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
|---|---|
| To: | Tomas Vondra <tomas(at)vondra(dot)me> |
| Cc: | 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 07:21:58 |
| Message-ID: | CAFj8pRCC3tOgmkGvPnR9TY8AVMWR2M6iquQ5yxxk0dax1jsQow@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
so 5. 12. 2026 v 22:57 odesílatel Tomas Vondra <tomas(at)vondra(dot)me> napsal:
>
>
> On 12/5/26 22:20, Pavel Stehule wrote:
> > Hi
> >
> > po 30. 11. 2026 v 15:14 odesílatel Pavel Stehule
> > <pavel(dot)stehule(at)gmail(dot)com <mailto:pavel(dot)stehule(at)gmail(dot)com>> napsal:
> >
> > Hi
> >
> > po 30. 11. 2026 v 14:34 odesílatel Pavel Stehule
> > <pavel(dot)stehule(at)gmail(dot)com <mailto:pavel(dot)stehule(at)gmail(dot)com>> napsal:
> >
> > Hi
> >
> > i rechecked this test on Postgres compiled without asserts, and
> > the overhead of DISTINCT is not too strong, but still it is
> visible
> >
> > cca 3ms x 0.5 ms. There is dependency on the number of columns -
> > the real slow query has 220 columns in the DISTINCT clause and
> > planning is about 50 ms.
> >
> > I tested pg13 - master and the overhead is visible on all
> versions
> >
> >
> > with more columns the overhead is more visible (the test is attached)
> >
> > the profile
> >
> > + 100,00% 0,00% postgres postgres [.] _start
> > ▒
> > + 100,00% 0,00% postgres libc.so.6 [.]
> > __libc_start_main@@GLIBC_2.34
> > ▒
> > + 100,00% 0,00% postgres libc.so.6 [.] call_init
> > (inlined)
> ◆
> > + 100,00% 0,00% postgres libc.so.6 [.]
> > __libc_start_call_main
> > ▒
> > + 100,00% 0,00% postgres postgres [.] main
> > ▒
> > + 100,00% 0,00% postgres postgres [.]
> > PostmasterMain
> > ▒
> > + 100,00% 0,00% postgres postgres [.] ServerLoop
> > ▒
> > + 100,00% 0,00% postgres postgres [.]
> > BackendStartup
> > ▒
> > + 100,00% 0,00% postgres postgres [.]
> > postmaster_child_launch
> > ▒
> > + 100,00% 0,00% postgres postgres [.]
> > BackendInitialize
> > ▒
> > + 100,00% 0,00% postgres postgres [.] PostgresMain
> > ▒
> > + 99,99% 0,00% postgres postgres [.]
> > exec_simple_query
> > ▒
> > + 99,87% 0,00% postgres postgres [.] PortalRun
> > ▒
> > + 99,87% 0,00% postgres postgres [.]
> > FillPortalStore
> > ▒
> > + 99,87% 0,00% postgres postgres [.]
> > PortalRunUtility
> > ▒
> > + 99,87% 0,00% postgres postgres [.]
> > ProcessUtility
> > ▒
> > + 99,87% 0,00% postgres postgres [.]
> > standard_ProcessUtility
> > ▒
> > + 99,87% 0,00% postgres postgres [.] ExplainQuery
> > ▒
> > + 99,86% 0,00% postgres postgres [.]
> > ExplainOneQuery
> > ▒
> > + 99,86% 0,00% postgres postgres [.]
> > standard_ExplainOneQuery
> > ▒
> > + 99,76% 0,00% postgres postgres [.] pg_plan_query
> > ▒
> > + 99,76% 0,00% postgres postgres [.] planner
> > ▒
> > + 99,76% 0,00% postgres postgres [.]
> > standard_planner
> > ▒
> > + 99,00% 0,00% postgres postgres [.]
> > subquery_planner
> > ▒
> > + 98,99% 0,00% postgres postgres [.]
> > grouping_planner
> > ▒
> > + 98,40% 0,00% postgres postgres [.]
> > create_distinct_paths
> > ▒
> > + 98,40% 0,00% postgres postgres [.]
> > create_final_distinct_paths
> > ▒
> > + 98,37% 0,00% postgres postgres [.]
> > estimate_num_groups
> > ▒
> > + 98,34% 0,06% postgres postgres [.]
> > add_unique_group_var
> > ▒
> > + 97,67% 6,65% postgres postgres [.]
> > exprs_known_equal
> > ▒
> > + 92,33% 89,05% postgres postgres [.] equal
> > ▒
> > + 2,03% 1,99% postgres postgres [.] _equalVar
> > ▒
> > + 1,32% 0,47% postgres postgres [.]
> > check_stack_depth
> > ▒
> > + 0,84% 0,69% postgres postgres [.]
> > stack_is_too_deep
> >
> > Regards
> >
> > Pavel
> >
> >
> > following patch reduce planning time from cca 50ms to cca 12ms - for 180
> > columns - it is still slow, but less 4 times
> >
> > pavel(at)nemesis:~/src/postgresql$ git diff
> > diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/
> > optimizer/path/equivclass.c
> > index 248386a6298..e76cf713098 100644
> > --- a/src/backend/optimizer/path/equivclass.c
> > +++ b/src/backend/optimizer/path/equivclass.c
> > @@ -2707,6 +2707,9 @@ exprs_known_equal(PlannerInfo *root, Node *item1,
> > Node *item2, Oid opfamily)
> > !list_member_oid(ec->ec_opfamilies, opfamily))
> > continue;
> >
> > + if (list_length(ec->ec_members) < 2)
> > + continue;
> > +
> This seems like a very narrow improvement, it stops working as soon as
> you get larger equivalence classes. Which is one of the things that
> makes this more expensive and we'd want to optimize.
>
> After a little bit of profiling, the issue is pretty obviously in
> add_unique_group_var:
>
> --89.34%--estimate_num_groups
> |
> --89.12%--add_unique_group_var
> |
> --88.43%--exprs_known_equal
> |
> |--50.24%--equal
> | |
> | |--13.54%--check_stack_depth
> | | |
> | | ...
> | |
> | --10.40%--_equalVar
> |
> --3.46%--__memset_avx2_unaligned_erms
>
> The problem is that we're doing a linear search on the 'varinfos' list,
> whenever we're adding a new grouping Var. And with the queries you
> shared the lists get pretty long, and it's O(N^2) complexity.
>
> The improvement you shared helps a little bit, because it makes the
> exprs_known_equal a little bit cheaper, but the quadratic complexity is
> still there and wins.
>
> 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.
>
sure, my patch is not a solution - it is just taking low level fruit.
Nothing more.
It is only two lines patch - and maybe it can be backported. Still it does
reduction from 50-60 ms to 12ms
Regards
Pavel
>
>
> regards
>
> --
> Tomas Vondra
>
>
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Manu | 2026-12-06 07:40:49 | Re: Proposal: "query_work_mem" GUC, to distribute working memory to the query's individual operators |
| Previous Message | Pavel Stehule | 2026-12-06 07:18:48 | Re: extremely slow planner when DISTINCT clause holds more columns |