ORDER BY ALL

Started by Rushabh Lathia6 months ago4 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

appliestests failedCI history

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t53578
psql -h localhost -U postgres

Built from patchset v1 (message #1), September 21, 2026 at 01:12 AM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t53578_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t53578_1 && git checkout t53578_1

Patchset v1 (message #1) is on t53578_1

Jump to latest
#1Rushabh Lathia
rushabh.lathia@gmail.com

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

Attachments:

t53578_1
0001-Add-ORDER-BY-ALL-with-ASC-DESC-NULLS-support.patchapplication/octet-stream; name=0001-Add-ORDER-BY-ALL-with-ASC-DESC-NULLS-support.patchDownload+182-9
0002-Add-documentation-and-tests-for-ORDER-BY-ALL.patchapplication/octet-stream; name=0002-Add-documentation-and-tests-for-ORDER-BY-ALL.patchDownload+1166-6
#2Kirill Reshke
reshkekirill@gmail.com
In reply to: Rushabh Lathia (#1)
Re: ORDER BY ALL

On Tue, 24 Mar 2026 at 11:27, Rushabh Lathia <rushabh.lathia@gmail.com> wrote:

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

Hi! What about SQL standard compatibility? ef38a4d97 was merged only
after the SQL committee accepted GROUP BY ALL, there was discussion a
few years before [0]/messages/by-id/CAAhFRxjyTO5BHn9y1oOSEp0TtpTDTTTb7HJBNhTG+i3-hXC0XQ@mail.gmail.com which ended up in nothing because of SQL
standard... So I wonder what is perspective of this thread

[0]: /messages/by-id/CAAhFRxjyTO5BHn9y1oOSEp0TtpTDTTTb7HJBNhTG+i3-hXC0XQ@mail.gmail.com

--
Best regards,
Kirill Reshke

#3Rushabh Lathia
rushabh.lathia@gmail.com
In reply to: Kirill Reshke (#2)
Re: ORDER BY ALL

On Tue, Mar 24, 2026 at 1:32 PM Kirill Reshke <reshkekirill@gmail.com>
wrote:

On Tue, 24 Mar 2026 at 11:27, Rushabh Lathia <rushabh.lathia@gmail.com>
wrote:

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

Hi! What about SQL standard compatibility? ef38a4d97 was merged only
after the SQL committee accepted GROUP BY ALL, there was discussion a
few years before [0] which ended up in nothing because of SQL
standard... So I wonder what is perspective of this thread

[0]
/messages/by-id/CAAhFRxjyTO5BHn9y1oOSEp0TtpTDTTTb7HJBNhTG+i3-hXC0XQ@mail.gmail.com

Thanks a lot for sharing this. I was not aware that ORDER BY ALL is not yet
part of SQL Standards (my bad).

--
Best regards,
Kirill Reshke

--
Rushabh Lathia

#4Peter Eisentraut
peter_e@gmx.net
In reply to: Kirill Reshke (#2)
Re: ORDER BY ALL

On 24.03.26 09:02, Kirill Reshke wrote:

Hi! What about SQL standard compatibility? ef38a4d97 was merged only
after the SQL committee accepted GROUP BY ALL, there was discussion a
few years before [0] which ended up in nothing because of SQL
standard... So I wonder what is perspective of this thread

The SQL working group decided not to standardize ORDER BY ALL. Which
means we could implement it without concern that it will be standardized
in a different way.