The query is valid, the plan is bad. ORDER BY can't change the outcome
so it is wasteful. The Sort reads the whole input first.
The Planner already drops the ORDER BY for EXISTS in
simplify_EXISTS_query.
This does the same for scalar subqueries.
Best regards,
Iliia
On Sun, Oct 18, 2026 at 11:19 PM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> Iliia Khaprov <i(dot)khaprov(at)gmail(dot)com> writes:
> > The patch drops the ORDER BY of EXPR, MULTIEXPR and ROWCOMPARE
> > sublinks in make_subplan, as simplify_EXISTS_query does for EXISTS.
>
> Why is this worth spending development effort on? "One person
> wasted a lot of time on a badly-written query" isn't a good
> enough answer.
>
> regards, tom lane