Re: BUG #18935: The optimiser's choice of sort doubles the execution time.

Laurenz Albe <laurenz.albe@cybertec.at>

From: Laurenz Albe <laurenz.albe@cybertec.at>
To: xiaohongjun@stu.xidian.edu.cn, pgsql-bugs@lists.postgresql.org
Date: 2025-05-19T15:23:58Z
Lists: pgsql-bugs
On Mon, 2025-05-19 at 12:42 +0000, PG Bug reporting form wrote:
> database4=# explain analyze SELECT t0.c0 FROM t0 INNER JOIN t1* ON
> ((t1.c0)=(((t1.c0)-(((((t1.c0)*('(-795716537,-245904803]'::int4range)))-(range_merge(t1.c0,
> t0.c0))))))) GROUP BY t0.c0;
>
> [explicitly disabling sort leads to a faster execution plan]

I see no bug here.
The join condition is so complicated that the planner has a hard
time estimating the number of result rows for the join.
It guesses way to low, so the sort is expensive, and avoiding it is cheaper.

The optimizer cannot be perfect, particularly in the face of complicated
expressions like that.

Yours,
Laurenz Albe