ORDER BY ALL

From: Rushabh Lathia <rushabh(dot)lathia(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: ORDER BY ALL
Date: 2026-03-24 06:27:09
Message-ID: CAGPqQf2_b6FSsGm2TwtUu0pq5ruQpSYqTo4Y8jYMV4DP2E17Ng@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

Please find the attached patch, to implement the ORDER BY ALL clause.
Commit ef38a4d97, implemented GROUP BY ALL clause, and this
feature follows the same pattern.

ORDER BY ALL is a form of ORDER BY that automatically adds all
non-junk columns from the SELECT target list to the ORDER BY clause.

This implementation supports:
- ORDER BY ALL (default ascending order)
- ORDER BY ALL ASC
- ORDER BY ALL DESC
- ORDER BY ALL NULLS FIRST/LAST
- ORDER BY ALL ASC/DESC NULLS FIRST/LAST

The syntax works by creating a marker SortBy node with a NULL
node pointer that carries the sort direction and nulls ordering.
During query transformation, this marker is detected and expanded
to order by all non-junk columns in the target list with the
specified direction.

Implementation details:
- gram.y: Added ORDER BY ALL grammar with optional ASC/DESC and NULLS
in both main sort_clause and PLpgSQL_Expr rules
- parse_clause.c: Implemented ORDER BY ALL expansion logic that iterates
over target list columns
- analyze.c: Updated to pass orderByAll flag through transformation
- parsenodes.h: Added orderByAll boolean to SelectStmt and Query
- ruleutils.c: Added deparsing support for ORDER BY ALL that preserves
sort direction and NULLS ordering in view definitions, including proper
handling of implicit vs explicit ordering

Please take a look at the attached patch and let me know your thoughts.

Thanks,
Rushabh Lathia
www.EnterpriseDB.com

Attachment Content-Type Size
0001-Add-ORDER-BY-ALL-with-ASC-DESC-NULLS-support.patch application/octet-stream 12.6 KB
0002-Add-documentation-and-tests-for-ORDER-BY-ALL.patch application/octet-stream 36.3 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Fujii Masao 2026-03-24 06:29:55 Re: [PATCH] Fix unexpected loss of DEFERRABLE property after toggling NOT ENFORCED / ENFORCED
Previous Message Chao Li 2026-03-24 06:12:18 Re: bugfix - fix broken output in expanded aligned format, when data are too short