Re: extremely slow planner when DISTINCT clause holds more columns

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-11-30 14:14:56
Message-ID: CAFj8pRAE0O+YXcSe3M1Sq5wYv=k-tniAOebjPm-9DyjNZt=NdQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

>
> Regards
>
> Pavel
>

Attachment Content-Type Size
slow-plan-distinct.sql application/sql 8.7 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Baji Shaik 2026-11-30 14:32:20 Re: Three more loops that let a function drop a not-yet-processed index/partition
Previous Message Matheus Alcantara 2026-11-30 14:09:49 Performance improvements on rescan of Append/MergeAppend with run-time partition pruning