Re: Fix GROUP BY ALL handling of ORDER BY operator semantics

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Daniel Gustafsson <daniel(at)yesql(dot)se>
Cc: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Fix GROUP BY ALL handling of ORDER BY operator semantics
Date: 2026-07-14 16:08:20
Message-ID: 3338663.1784045300@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Daniel Gustafsson <daniel(at)yesql(dot)se> writes:
> Ack. Another case I am a bit confused by is:

> postgres=# create table t (a integer);
> CREATE TABLE
> postgres=# select 1 as x from t group by ();
> x
> ---
> 1
> (1 row)

> postgres=# select 1 as x from t group by all;
> x
> ---
> (0 rows)

> Shouldn't those two queries yield the same result, or am I missing something
> obvious?

I might be undercaffeinated still, but I think those are both correct.
"GROUP BY ()" has similar effects to use of an aggregate or HAVING
clause: it forces the table scan's results to be combined into a
single grouped row. But you get a grouped row even if the table is
empty. The second case is equivalent to "select 1 as x from t group
by x", and this is different because it will produce a grouped row
only if the table isn't empty. (The fact that the grouping expression
is a constant doesn't change the rule.) Not one of SQL's more
consistent behaviors perhaps, but I believe it's all per spec.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Sami Imseih 2026-07-14 16:31:33 Re: [Proposal] Adding callback support for custom statistics kinds
Previous Message Tom Lane 2026-07-14 15:56:49 Re: document the dangers of granting TRIGGER or REFERENCES