Re: Unfortunate pushing down of expressions below sort

Chengpeng Yan <chengpeng_yan@outlook.com>

From: Chengpeng Yan <chengpeng_yan@Outlook.com>
To: Tom Lane <tgl@sss.pgh.pa.us>, Andres Freund <andres@anarazel.de>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>, David Rowley <dgrowleyml@gmail.com>
Date: 2026-02-11T14:17:01Z
Lists: pgsql-hackers

Attachments

Hi,

I've attached v2 of the patch.

Changes since v1:
- No planner logic changes.
- Fixed one regression-test fallout in contrib/postgres_fdw by updating
expected EXPLAIN output. With the planner change, the deparsed remote
SQL can omit a redundant constant there, so GROUP BY positional
references shift accordingly; query results are unchanged.

Behavior is unchanged from v1:
- Postpone additional non-sort target expressions only when doing so
does not require carrying any extra Vars/PlaceHolderVars through Sort.
- Existing volatile/SRF/expensive-expression behavior is unchanged.

Validation:
- make check-world passes.

Would especially appreciate feedback on whether the “no extra Vars/PHVs
through sort” rule is the right safety boundary.

Thanks!

--
Best regards,
Chengpeng Yan