Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v2Etz-001cuH-30 for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Sep 2025 20:18:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1v2Etx-005dLh-2h for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Sep 2025 20:18:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v2Etw-005dLZ-PG for pgsql-hackers@lists.postgresql.org; Fri, 26 Sep 2025 20:18:45 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v2Etv-000HaM-0O for pgsql-hackers@postgresql.org; Fri, 26 Sep 2025 20:18:45 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 58QKIWXe4174780; Fri, 26 Sep 2025 16:18:32 -0400 From: Tom Lane To: Peter Eisentraut cc: pgsql-hackers , "David G. Johnston" , David Christensen , Jelte Fennema-Nio Subject: Re: [PATCH] GROUP BY ALL In-reply-to: <3d01eb20-d587-4dec-9261-0c92240ce546@eisentraut.org> References: <931747.1721687375@sss.pgh.pa.us> <634aca95-6db5-4beb-b18d-67e65582817f@eisentraut.org> <4054709.1758895915@sss.pgh.pa.us> <3d01eb20-d587-4dec-9261-0c92240ce546@eisentraut.org> Comments: In-reply-to Peter Eisentraut message dated "Fri, 26 Sep 2025 17:54:18 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4174778.1758917912.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 26 Sep 2025 16:18:32 -0400 Message-ID: <4174779.1758917912@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Peter Eisentraut writes: > On 26.09.25 16:11, Tom Lane wrote: >> I thought I understood this definition, up till your last >> comment. What's invalid about that expanded query? > This was a sloppy example. Here is a better one: > create table t1 (a int, b int, c int); > select a, sum(b)+c from t1 group by all; > This is equivalent to > select a, sum(b)+c from t1 group by a; > which would be rejected as > ERROR: column "t1.c" must appear in the GROUP BY clause or be used > in an aggregate function Got it, mostly. There is an edge case, though: what if there are no candidate grouping items? I see these test cases in David's patch: +-- oops all aggregates +EXPLAIN (COSTS OFF) SELECT COUNT(a), SUM(b) FROM t1 GROUP BY ALL; + QUERY PLAN = +---------------------- + Aggregate + -> Seq Scan on t1 +(2 rows) + +-- empty column list +EXPLAIN (COSTS OFF) SELECT FROM t1 GROUP BY ALL; + QUERY PLAN = +---------------- + Seq Scan on t1 +(1 row) That is, in such cases the patch behaves as if there were no GROUP BY clause at all, which seems kinda dubious. Should this be an error, and if not what's it supposed to do? The second case is outside the SQL spec so I'm not expecting guidance on that, but surely the committee thought about the first case. Also, what about window functions in the tlist? If you do regression=3D# explain select sum(q1) over(partition by q2) from int8_tbl = group by 1; you get = ERROR: window functions are not allowed in GROUP BY LINE 1: explain select sum(q1) over(partition by q2) from int8_tbl g... ^ but that's not what is happening with regression=3D# explain select sum(q1) over(partition by q2) from int8_tbl = group by all; ERROR: column "int8_tbl.q2" must appear in the GROUP BY clause or be used= in an aggregate function LINE 1: explain select sum(q1) over(partition by q2) from int8_tbl g... ^ (I didn't stop to figure out why this isn't giving the same error, but maybe it's an order-of-checks thing.) In any case: should this give "window functions are not allowed in GROUP BY", or should the window-function-containing tlist item be silently skipped by GROUP BY ALL? Trying to make it work is surely not the right answer. regards, tom lane