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 1v2CWZ-001I0P-NM for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Sep 2025 17:46:27 +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 1v2CWX-004eBO-JS for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Sep 2025 17:46:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1v2CWX-004eBF-9s for pgsql-hackers@lists.postgresql.org; Fri, 26 Sep 2025 17:46:25 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1v2CWV-0004Hg-29 for pgsql-hackers@postgresql.org; Fri, 26 Sep 2025 17:46:25 +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 58QHkJbS4096524; Fri, 26 Sep 2025 13:46:19 -0400 From: Tom Lane To: David Christensen cc: jian he , Andrey Borodin , Peter Eisentraut , pgsql-hackers , "David G. Johnston" , Jelte Fennema-Nio Subject: Re: [PATCH] GROUP BY ALL In-reply-to: References: <4D2047B0-E8D8-472B-B7E8-61206B1E6AFA@yandex-team.ru> Comments: In-reply-to David Christensen message dated "Fri, 26 Sep 2025 12:00:01 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <4096522.1758908779.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 26 Sep 2025 13:46:19 -0400 Message-ID: <4096523.1758908779@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk David Christensen writes: > Version 3 of this patch, incorporating some of the feedback thus far: Some random comments: I don't love where you put the parsing code. Instead of exposing addTargetToGroupList, I think you should have given transformGroupClause the responsibility of expanding GROUP BY ALL. One reason for that is that GROUP BY processing depends on the results of transformSortClause. This means that in v3, the behavior of SELECT x FROM ... GROUP BY x ORDER BY x; will be subtly different from SELECT x FROM ... GROUP BY ALL ORDER BY x; which seems like a bug, or at least not desirable. (You might need a DESC or USING decoration in the ORDER BY to expose this clearly.) The parsing code itself is not great: + TargetEntry *n =3D (TargetEntry*)lfirst(l1); + if (!contain_aggs_of_level((Node *)n->expr, 0)) + qry->groupClause =3D addTargetToGroupList(pstate, n, qry->groupClause= , qry->targetList, 0); You should be skipping resjunk entries, and please use "tle" or some such name less generic than "n", and "0" is not the correct location to pass to addTargetToGroupList. Probably the location of the ALL keyword would be the ideal thing, but if we don't have that, the notation for "no location known" is -1 not 0. We could also consider using exprLocation(tle->expr), despite the comment on addTargetToGroupList that that's not the right thing. This might actually be better than pointing at the ALL anyway, since that would give no hint which targetentry caused the error. The test cases seem poorly designed, because it's very hard to be sure whether the code expanded the ALL as-expected. I think you could improve them by not executing the queries but just EXPLAINing them, so that the expanded group-by list is directly visible. regards, tom lane