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.96) (envelope-from ) id 1wCdKg-002Awe-1e for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Apr 2026 12:57:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wCdKd-00Bsk0-1h for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Apr 2026 12:57:32 +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.96) (envelope-from ) id 1wCdKd-00Bsjr-0M for pgsql-hackers@lists.postgresql.org; Tue, 14 Apr 2026 12:57:32 +0000 Received: from mail-yw1-x1129.google.com ([2607:f8b0:4864:20::1129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wCdKb-00000000ySY-3vHC for pgsql-hackers@postgresql.org; Tue, 14 Apr 2026 12:57:31 +0000 Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-79a2ee65171so68061487b3.2 for ; Tue, 14 Apr 2026 05:57:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1776171449; cv=none; d=google.com; s=arc-20240605; b=Kq4HkXf3ivlEYP6zoO7a4MeMMwknv03iuuG7ozPL6UHQDqOHMfPuQESartVPQxVlh4 83yba6bWiMdU3xqPrsGyZxSiSX8B3/2p9ySBrEaY3pVBvmDTeL8ZPZpL18Yas1fvFFQr Dn3Iur0VrcIe2Iaomu9YNH7NQHeR2a1qiTlVXG28obDKPaZRpA12nM31y/yYiBYvKqZK KtKF5y/k7+MuZ098Z3fYuL7IKNik5PKADDnOD0bzHC0cWuK+YkE3z05Q/ZF0GxMcnCiq S+n6VpVzXR1+McUpUUimN/KTIL0lvTOWXoaCKI/tkgebkyzezFsvKap/Co0BVCmhIqa6 DuZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:dkim-signature; bh=6Im0kDXhsiOVrFskCl7D4jlTM3EmpPjc15r9qwtFmSc=; fh=3N2M4ppY8NEqfSfRtL5sE65WlSGmvAhGaAurMqmTDjw=; b=Hzs8zeOYGtAMmm4hRMQi1HEs8JE2Y8h2kKZAYcEdbbLAWd6dUnw423fHhHSZC41vO/ 2LqOMzFVZe1/rUm3k2AldkBRKB0wa986z+sUxSvpGgQsEP3KpoqQOxQEN8cHKVlACqGW /S+PKUPLWLwZsxa9mtstd/8gUiy/eskwTvOHutxUWgB+Xiq3Cs2CK9j3XxXgI3ygkRC5 kEHzNlclbIi4F+BXsyK/Myy9ajLGIV7WWrC92G1R7JPNSO6s0r+aMOOZuFwBEVO3zAh5 qkpGwcjePc+h+KSKHQ9gpMQY6kMtUNtBN31i9gM0zyllLB4boViVfKHEGeS9lYzcbRz7 wJSQ==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776171449; x=1776776249; darn=postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=6Im0kDXhsiOVrFskCl7D4jlTM3EmpPjc15r9qwtFmSc=; b=sOGQTger74SjTwgSFHd5JqVM8GH3M2gNXWdTP9rVI9nCAXA/dACC1JFymGiWdJaRZE 8MqJfwwFuHVxGEZORnT+Xjwa+EAexcCj8Q3C+1nd330cqGv0Ox7pVkA+sjNdqn7kdpBL B0UWYFWn/sWe9KjJCNUj9+1OPmQLVywmiHmJN1t6ispcShv9gq7qLU6I+vrN+s3Fhu8o xHzJ3gI8NY84AuPZhOBmKyRCWUEer1on5qUNcl3pmV6Cu+SPZOLIgDLry6JFEnTO+0Za EsYY2sGX24XjG4M9L3p7Q4stLx5YYMsMsCVefbPw5MBHtyxf+ykwmvEwKFX+hkqN11Bp fh6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776171449; x=1776776249; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=6Im0kDXhsiOVrFskCl7D4jlTM3EmpPjc15r9qwtFmSc=; b=qJX4MUOMOnfVDk718/6UgyqB8KaOJRqqApIjKOXAf1Du5vR3ANB1uLciR6Lv5igwCX lWVLtfzdSqQ1n70WctfsWgc5b36NER50WD3XlE7h2abuOmW6s2geblWe/RCcRubpQF/u wuWiilUtkSt+0HujUjGdBXjGRAz1gcuEPKaqOgaOaFwLxiteousUneIkhG5KZQxZ0f85 KcIj+/+WkOpqTMGKhLI3SSY5Or6UYPC9yb1sgNsOPrE4Csl8oOyMLTwQp+MTfZ1As3ZC gNdd7yfFooA/iMl2iwqxeQYb1nISMShSj/zCglwj69xXISpmCXUJ4tRFs1eL8nqPyIyh weSQ== X-Forwarded-Encrypted: i=1; AFNElJ8tBC4RIG4ZgQRDswA6ZVO/F63uMOzOAiDWie/HmAE6XzaOfsYPh69EMWSaS4D8C0zX4wKZf54yqozLL8Jl@postgresql.org X-Gm-Message-State: AOJu0Yx9aKlWxxy8YcD64fXMdntO0ynlksGdJil/Txm4HrmHv3BYIWai 9V58HZTCa0DpRiEtVZYVnDVFUno6WLABoQZgdWKMsaYQFX4urOmqlmTU5LzmfhlEkDFA24eCwR3 mHwgKSC6e0/sfemyfkUW9Sxxo8iUFA0M= X-Gm-Gg: AeBDiesTvxpvDuEl8zgNS3kL1mt0HZhGd19JXyO9e/sljP0tdiuML82PXTaLYEjqHdw OGibtkhEno8TQrrQCNFMABNVI36hNgKuonpJUbmRrsIXqc+gdQNAnbQCGP3xV9J6LYxrK/pj75D 0V5y6DT93l9u67w59ha00CFcZoLuwIFF1SExX2Fv+Qujd4sPcGjJGC8v+5df4XquKDc2XrsV8GT bu9duUeuFTSMCqdZGg5cbdBL2/57neVAGDiCcaOi6ehLus3X9r3qTqt3YBFm2WCA4GjCOMlLnkh zE+EO3BwWJI2pQI7biAdJCnvlLhq X-Received: by 2002:a05:690e:4801:b0:650:311d:55cf with SMTP id 956f58d0204a3-65198a6e155mr11544937d50.15.1776171449419; Tue, 14 Apr 2026 05:57:29 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a05:7010:2243:b0:4dc:621d:e723 with HTTP; Tue, 14 Apr 2026 05:57:27 -0700 (PDT) In-Reply-To: <49d5cbd1-7e47-4740-bc81-e574150f44cb@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> <4174779.1758917912@sss.pgh.pa.us> <87b40ca1-f935-4d71-9edb-9d9f1053cb45@eisentraut.org> <49d5cbd1-7e47-4740-bc81-e574150f44cb@eisentraut.org> From: "David G. Johnston" Date: Tue, 14 Apr 2026 05:57:27 -0700 X-Gm-Features: AQROBzABL6nlkZst3MevXmmLVf8sekX93v9O_-AOZaOfgawt-Thpoq89Dp-SCnY Message-ID: Subject: Re: [PATCH] GROUP BY ALL To: Peter Eisentraut Cc: Tom Lane , pgsql-hackers , David Christensen , Jelte Fennema-Nio Content-Type: multipart/alternative; boundary="0000000000004d8c8d064f6b2438" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000004d8c8d064f6b2438 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tuesday, April 14, 2026, Peter Eisentraut wrote: > > > I don't see any mention of using GROUP BY with window functions in our > relevant documentation, for example > > https://www.postgresql.org/docs/devel/sql-expressions.html# > SYNTAX-WINDOW-FUNCTIONS > https://www.postgresql.org/docs/devel/functions-window.html The select reference page covers this. But the window clause could get better treatment, it=E2=80=99s buried in step 5. https://www.postgresql.org/docs/current/sql-select.html > Commit ef38a4d9756 added a regression test > > EXPLAIN (COSTS OFF) SELECT a, COUNT(a) OVER (PARTITION BY a) FROM t1 GROU= P > BY ALL; > > but the test table contains no data, so I don't know if this kind of quer= y > produces interesting information. > Each non-null value of =E2=80=9Ca=E2=80=9D would have an output of 1, while= a null valued =E2=80=9Ca=E2=80=9D would have an output of 0. =E2=80=9CA=E2=80=9D is grou= ped since all expressions involving =E2=80=9Ca=E2=80=9D are non-aggregated. The equivalent rewrite i= s: Select a, count(a_expr) over =E2=80=A6 from ( =E2=80=94 step 5 select a, a as a_expr from tbl group by all. =E2=80=94 step 4 ); For purposes of group by all one would erase/ignore the actual window wrapper while leaving the expressions it operates over in place. This extends from =E2=80=9Cwindow expressions are processed after group by/havin= g=E2=80=9D documented in SELECT. IOW, group by all resolves during processing step 4 with intermediate results for the expressions within the window functions, then step 5 removes the intermediate expressions that don=E2=80=99t appear = in the final output while adding in the results of processing the window functions= . David J. --0000000000004d8c8d064f6b2438 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tuesday, April 14, 2026, Peter Eisentraut <peter@eisentraut.org> wrote:

I don't see any mention of using GROUP BY with window functions in our = relevant documentation, for example

https://www.postgresql.org/docs/= devel/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS
https://www.postgresql.org/docs/devel/functions-window.= html

The select reference page covers t= his.=C2=A0 But the window clause could get better treatment, it=E2=80=99s b= uried in step 5.



Commit ef38a4d9756 added a regression test

EXPLAIN (COSTS OFF) SELECT a, COUNT(a) OVER (PARTITION BY a) FROM t1 GROUP = BY ALL;

but the test table contains no data, so I don't know if this kind of qu= ery produces interesting information.

Each non-null value of =E2=80=9Ca=E2=80=9D= would have an output of 1, while a null valued =E2=80=9Ca=E2=80=9D would h= ave an output of 0. =C2=A0=E2=80=9CA=E2=80=9D is grouped since all expressi= ons involving =E2=80=9Ca=E2=80=9D are non-aggregated.=C2=A0 The equivalent = rewrite is:

Select a, count(a_expr) over =E2=80=A6= from ( =E2=80=94 step 5
=C2=A0 =C2=A0 select a, a as a_expr from= tbl group by all. =E2=80=94 step 4
);

F= or purposes of group by all one would erase/ignore the actual window wrappe= r while leaving the expressions it operates over in place.=C2=A0 This exten= ds from =E2=80=9Cwindow expressions are processed after group by/having=E2= =80=9D documented in SELECT.=C2=A0 IOW, group by all resolves during proces= sing step 4 with intermediate results for the expressions within the window= functions, then step 5 removes the intermediate expressions that don=E2=80= =99t appear in the final output while adding in the results of processing t= he window functions.

David J.

--0000000000004d8c8d064f6b2438--