Re: BUG #18751: Sub-optimal UNION ALL plan

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Kirill Reshke <reshkekirill(at)gmail(dot)com>
Cc: pingw33n(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #18751: Sub-optimal UNION ALL plan
Date: 2024-12-23 15:18:56
Message-ID: 1347038.1734967136@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Kirill Reshke <reshkekirill(at)gmail(dot)com> writes:
> I reproduced this on REL_16_STABLE, HEAD & REL_13_STABLE, so this is
> not really a bug, just a missing optimization?

Yeah. I believe what is happening is that the addition of the WHERE
clause forces the second sub-SELECT to be planned as an independent
query. And that level of planning has no idea that it might be
useful to produce a result ordered by "t", so it doesn't generate
a sub-plan that can do that. Then the best that the outer level
can do is sort after-the-fact.

You could work around this by writing the second sub-SELECT like

(select * from t2 where ... order by t)

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2024-12-23 15:35:36 Re: Commit 5a2fed911a broke parallel query
Previous Message Kirill Reshke 2024-12-23 14:24:40 Re: BUG #18751: Sub-optimal UNION ALL plan