Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e4ptL-0002iu-65 for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Oct 2017 15:04:19 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e4ptK-0005eG-JI for pgsql-bugs@arkaria.postgresql.org; Wed, 18 Oct 2017 15:04:18 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e4ptK-0005du-09 for pgsql-bugs@postgresql.org; Wed, 18 Oct 2017 15:04:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e4ptG-0003H7-Sx for pgsql-bugs@postgresql.org; Wed, 18 Oct 2017 15:04:16 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v9IF41nD026651; Wed, 18 Oct 2017 11:04:01 -0400 From: Tom Lane To: Andrew Gierth cc: Heikki Linnakangas , Pg Bugs Subject: Re: Improper const-evaluation of HAVING with grouping sets and subquery pullup In-reply-to: <87tvyyjg2s.fsf@news-spur.riddles.org.uk> References: <7dbdcf5c-b5a6-ef89-4958-da212fe10176@iki.fi> <1197.1507820787@sss.pgh.pa.us> <87efq3lq93.fsf@news-spur.riddles.org.uk> <87a80rli2w.fsf@news-spur.riddles.org.uk> <32319.1508163874@sss.pgh.pa.us> <8760bfku2u.fsf@news-spur.riddles.org.uk> <2431.1508166918@sss.pgh.pa.us> <871sm3ktl7.fsf@news-spur.riddles.org.uk> <87tvyyjg2s.fsf@news-spur.riddles.org.uk> Comments: In-reply-to Andrew Gierth message dated "Tue, 17 Oct 2017 10:16:58 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <26649.1508339040.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 18 Oct 2017 11:04:00 -0400 Message-ID: <26650.1508339040@sss.pgh.pa.us> List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org Andrew Gierth writes: > "Heikki" =3D=3D Heikki Linnakangas writes: > Heikki> Why are there no "summary" rows with NULLs, despite the ROLLUP? > Here's why it happens: after substituting for the aliases, you have > GROUP BY g, rollup(g) > which is equivalent to > GROUP BY GROUPING SETS ((g,g), (g)) > which is equivalent to > GROUP BY GROUPING SETS ((g), (g)) I don't think I buy this explanation, because the plan tree doesn't show any indication that we're actually folding (g,g) to (g): regression=3D# EXPLAIN SELECT g as newalias1, g as newalias3 FROM generate_series(1,3) g GROUP BY newalias1, ROLLUP(newalias3); QUERY PLAN=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ---------------------------------------------------------------------------= ----- HashAggregate (cost=3D15.00..21.50 rows=3D400 width=3D8) Hash Key: g, g Hash Key: g -> Function Scan on generate_series g (cost=3D0.00..10.00 rows=3D1000 = width=3D8) (4 rows) regression=3D# EXPLAIN SELECT g as newalias1, g+0 as newalias3 FROM generate_series(1,3) g GROUP BY newalias1, ROLLUP(newalias3); QUERY PLAN=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 ---------------------------------------------------------------------------= ----- HashAggregate (cost=3D17.50..25.00 rows=3D400 width=3D8) Hash Key: g, (g + 0) Hash Key: g -> Function Scan on generate_series g (cost=3D0.00..12.50 rows=3D1000 = width=3D8) (4 rows) If these behave differently, why does the plan structure look the same? I think that Heikki's expectation is the correct one, and the reason the output looks the way it does is that setrefs.c is dropping the ball somehow and confusing the two "g" references. The finished plan has two identical Var references in the Agg node's output tlist: :targetlist ( {TARGETENTRY=20 :expr=20 {VAR=20 :varno 65001=20 :varattno 1=20 :vartype 23=20 :vartypmod -1=20 :varcollid 0=20 :varlevelsup 0=20 :varnoold 1=20 :varoattno 1=20 :location 7 } :resno 1=20 :resname newalias1=20 :ressortgroupref 1=20 :resorigtbl 0=20 :resorigcol 0=20 :resjunk false } {TARGETENTRY=20 :expr=20 {VAR=20 :varno 65001=20 :varattno 1=20 :vartype 23=20 :vartypmod -1=20 :varcollid 0=20 :varlevelsup 0=20 :varnoold 1=20 :varoattno 1=20 :location 23 } :resno 2=20 :resname newalias3=20 :ressortgroupref 2=20 :resorigtbl 0=20 :resorigcol 0=20 :resjunk false } ) The two "g" values are correctly distinguished in the FunctionScan's tlist, however: :targetlist ( {TARGETENTRY=20 :expr=20 {VAR=20 :varno 1=20 :varattno 1=20 :vartype 23=20 :vartypmod -1=20 :varcollid 0=20 :varlevelsup 0=20 :varnoold 1=20 :varoattno 1=20 :location 7 } :resno 1=20 :resname <>=20 :ressortgroupref 1=20 :resorigtbl 0=20 :resorigcol 0=20 :resjunk false } {TARGETENTRY=20 :expr=20 {VAR=20 :varno 1=20 :varattno 1=20 :vartype 23=20 :vartypmod -1=20 :varcollid 0=20 :varlevelsup 0=20 :varnoold 1=20 :varoattno 1=20 :location 23 } :resno 2=20 :resname <>=20 :ressortgroupref 2=20 :resorigtbl 0=20 :resorigcol 0=20 :resjunk false } ) We should have used ressortgroupref matching to prevent this, but without having checked the code right now, I think that we might only apply that logic to non-Var tlist entries. If the Agg output tlist had mentioned column 2 not column 1 of the child node, I bet we'd get the right answer. Digression: this seems like another example in which the "same" Var can represent two different values. I've had a bee in my bonnet for awhile that we need to stop doing that, but I'm not entirely sure what to do instead. In the case of Vars that might go to null because of an outer join, we could perhaps fix things by not smashing join alias Vars to their referents (at least, not till much much later in the planner than we do now). That doesn't seem to apply here though. Perhaps the GROUP BY operation ought to be understood as providing its own set of output Vars? That would mean creating an RTE to represent GROUP BY, but maybe that's not awful. In the nearer term, maybe we can get a back-patchable fix by being more careful about ressortgroupref matching. regards, tom lane --=20 Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs