Re: [PATCH] Remove useless ORDER BY from scalar subqueries

From: Iliia Khaprov <i(dot)khaprov(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org, pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PATCH] Remove useless ORDER BY from scalar subqueries
Date: 2026-10-18 21:34:31
Message-ID: CAFO+O_qv-hTpLqb=wvVfCw=M86a0iT=3oX53C8e0T+ht0MSNdQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Previous Message Tom Lane 2026-10-18 21:19:25 Re: [PATCH] Remove useless ORDER BY from scalar subqueries