Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pBIJZ-0004Kp-V1 for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Dec 2022 16:33:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pBIJY-0008VH-Ih for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Dec 2022 16:33:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pBIJY-0008V8-8A for pgsql-hackers@lists.postgresql.org; Fri, 30 Dec 2022 16:33:00 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pBIJT-0007ca-Qb for pgsql-hackers@lists.postgresql.org; Fri, 30 Dec 2022 16:32:59 +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 2BUGWrYv748464; Fri, 30 Dec 2022 11:32:53 -0500 From: Tom Lane To: Richard Guo cc: pgsql-hackers@lists.postgresql.org Subject: Re: Removing redundant grouping columns In-reply-to: References: <185315.1672179489@sss.pgh.pa.us> Comments: In-reply-to Richard Guo message dated "Fri, 30 Dec 2022 16:05:55 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <748462.1672417973.1@sss.pgh.pa.us> Date: Fri, 30 Dec 2022 11:32:53 -0500 Message-ID: <748463.1672417973@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Richard Guo writes: > On Wed, Dec 28, 2022 at 6:18 AM Tom Lane wrote: >> This patch is aimed at being smarter about cases where we have >> redundant GROUP BY entries, for example >> SELECT ... WHERE a.x = b.y GROUP BY a.x, b.y; > While we are here, I wonder if we can do the same trick for > distinctClause, to cope with cases like > select distinct a.x, b.y from a, b where a.x = b.y; We do that already, no? regression=# create table foo (x int, y int); CREATE TABLE regression=# explain select distinct * from foo where x = 1; QUERY PLAN ----------------------------------------------------------------- Unique (cost=38.44..38.50 rows=11 width=8) -> Sort (cost=38.44..38.47 rows=11 width=8) Sort Key: y -> Seq Scan on foo (cost=0.00..38.25 rows=11 width=8) Filter: (x = 1) (5 rows) regression=# explain select distinct * from foo where x = y; QUERY PLAN ----------------------------------------------------------------- Unique (cost=38.44..38.50 rows=11 width=8) -> Sort (cost=38.44..38.47 rows=11 width=8) Sort Key: x -> Seq Scan on foo (cost=0.00..38.25 rows=11 width=8) Filter: (x = y) (5 rows) But if you do regression=# explain select * from foo where x = y group by x, y; QUERY PLAN ----------------------------------------------------------------- Group (cost=38.44..38.52 rows=11 width=8) Group Key: x, y -> Sort (cost=38.44..38.47 rows=11 width=8) Sort Key: x -> Seq Scan on foo (cost=0.00..38.25 rows=11 width=8) Filter: (x = y) (6 rows) then you can see that the Sort step knows it need only consider one column even though the Group step considers both. regards, tom lane