Subject: Re: Unfortunate pushing down of expressions below sort

From: Tom Lane <[email-masked]>
Date: Thu, 05 Feb 2026 19:13:09 -0500

Andres Freund <[email-masked]> writes:
> We push down expressions below a sort node, even though they could be
> evaluated above.

Yeah.  That's a hangover from an ancient decision that sort/limit
would always be applied at the top of the plan tree.  I'm too
lazy to check the details right now, but I think we already relaxed
that in some cases (or maybe it was about evaluating stuff
before/after GROUP BY?).

> That can very substantially increase the space needed for the
> sort.

Could decrease it too, eg if what you are outputting is
md5(some-wide-column).  Not sure we are smart enough to tell
which way is better.

			regards, tom lane

---

From: Tom Lane <[email-masked]>
Date: Thu, 05 Feb 2026 20:02:31 -0500

> that in some cases (or maybe it was about evaluating stuff

Ah, right: see make_sort_input_target() and its very extensive
comment in planner.c.  I wonder why that didn't trigger in
your example.

			regards, tom lane