| From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
|---|---|
| To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: extremely slow planner when DISTINCT clause holds more columns |
| Date: | 2026-12-05 21:20:13 |
| Message-ID: | CAFj8pRD+r_awp2WbGrKYv2xjWsbs+AnyJA5Or-Ltchp3vFxQqw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi
po 30. 11. 2026 v 15:14 odesílatel Pavel Stehule <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>
> 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;
+
/* Ignore children here */
foreach(lc2, ec->ec_members)
{
Regards
Pavel
>
>
>>
>> Regards
>>
>> Pavel
>>
>
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Manu | 2026-12-05 21:51:15 | Re: [PATCH] Reduce pg_class scans in GRANT/REVOKE ON ALL TABLES IN SCHEMA |
| Previous Message | Corey Huinker | 2026-12-05 20:34:52 | Re: Add sortsupport to money_ops, pg_lsn_ops and xid8_ops |