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 1k8x2a-00012O-6D for pgsql-bugs@arkaria.postgresql.org; Fri, 21 Aug 2020 02:44:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k8x2X-0005Fr-Hx for pgsql-bugs@arkaria.postgresql.org; Fri, 21 Aug 2020 02:44:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k8x2X-0005Fk-Bg for pgsql-bugs@lists.postgresql.org; Fri, 21 Aug 2020 02:44:25 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k8x2P-00054t-MB for pgsql-bugs@lists.postgresql.org; Fri, 21 Aug 2020 02:44:24 +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 07L2iCdO1765057; Thu, 20 Aug 2020 22:44:13 -0400 From: Tom Lane To: Andy Fan cc: Andrew Gierth , David Rowley , pavelsivash@gmail.com, PostgreSQL mailing lists Subject: Re: BUG #16585: Wrong filtering on a COALESCE field after using GROUPING SETS In-reply-to: References: <16585-9d8c340d23ade8c1@postgresql.org> <87mu2qixx5.fsf@news-spur.riddles.org.uk> <1750212.1597958727@sss.pgh.pa.us> <87tuwxrnzm.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andy Fan message dated "Fri, 21 Aug 2020 10:08:42 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1765055.1597977852.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 20 Aug 2020 22:44:12 -0400 Message-ID: <1765056.1597977852@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andy Fan writes: > On Fri, Aug 21, 2020 at 5:51 AM Andrew Gierth > wrote: >> Unless I'm missing something, it should be safe to reference output >> columns that are not mentioned in any grouping set, > I think such columns usually are aggregation expr, If we want to push d= own > a qual which reference to an aggregation expr, we have to push down > to having cause, However I am not sure such pushing down really helps. Well, they can either be aggregates, or functions of the grouping columns. You're right that there's not much we can do (today) with restrictions on aggregate outputs, but there can be value in pushing down restrictions on the other sort. As an example, consider the regression database's tenk1 table, and for argument's sake add regression=3D# create index on tenk1 (abs(hundred)); CREATE INDEX Then we can get regression=3D# explain select * from (select hundred, ten, abs(hundred) a,= count(*) c from tenk1 group by 1,2) ss where a =3D 42; QUERY PLAN = = --------------------------------------------------------------------------= ---------- HashAggregate (cost=3D225.98..227.18 rows=3D96 width=3D20) Group Key: tenk1.hundred, tenk1.ten -> Bitmap Heap Scan on tenk1 (cost=3D5.06..225.23 rows=3D100 width=3D= 8) Recheck Cond: (abs(hundred) =3D 42) -> Bitmap Index Scan on tenk1_abs_idx (cost=3D0.00..5.04 rows=3D= 100 width=3D0) Index Cond: (abs(hundred) =3D 42) (6 rows) which is a lot cheaper than the pure seqscan you get with no pushed-down condition. One thing that I find curious is that if I alter this example to use grouping sets, say regression=3D# explain select * from (select hundred, ten, abs(hundred) a,= count(*) c from tenk1 group by grouping sets (1,2)) ss where a =3D 42; QUERY PLAN = ----------------------------------------------------------------- HashAggregate (cost=3D495.00..546.65 rows=3D2 width=3D20) Hash Key: tenk1.hundred Hash Key: tenk1.ten Filter: (abs(tenk1.hundred) =3D 42) -> Seq Scan on tenk1 (cost=3D0.00..445.00 rows=3D10000 width=3D8) (5 rows) i.e. it's not seeing the abs() condition as pushable below the aggregation. I'm not quite sure if that's a necessary restriction or a missed optimization. regards, tom lane