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 1m1EU5-0004ji-R7 for pgsql-bugs@arkaria.postgresql.org; Wed, 07 Jul 2021 20:49:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m1EU4-0006tY-Q7 for pgsql-bugs@arkaria.postgresql.org; Wed, 07 Jul 2021 20:49:28 +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 1m1EU4-0006tI-Jg for pgsql-bugs@lists.postgresql.org; Wed, 07 Jul 2021 20:49:28 +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 1m1EU1-0006eJ-Ue for pgsql-bugs@lists.postgresql.org; Wed, 07 Jul 2021 20:49:27 +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 167KnMJD1908213; Wed, 7 Jul 2021 16:49:23 -0400 From: Tom Lane To: Andrew Gierth cc: Michael Paquier , cyg0810@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17088: FailedAssertion in prepagg.c In-reply-to: References: <17088-e33882b387de7f5c@postgresql.org> Comments: In-reply-to Michael Paquier message dated "Wed, 07 Jul 2021 20:00:17 +0900" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1908211.1625690962.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 07 Jul 2021 16:49:22 -0400 Message-ID: <1908212.1625690962@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Michael Paquier writes: >> CREATE TEMP TABLE v0 ( v1 INT PRIMARY KEY ) ON COMMIT DELETE ROWS ; = >> SELECT FROM ( VALUES ( ( SELECT - - 84 FROM v0 LIMIT - -1 ) ) ) v1 ( v1= ) >> GROUP BY ROLLUP ( v1 , v1 ) , ROLLUP ( ROW ( ) , ROW ( - - - - -128 , >> 6099928.000000 ) , v1 ) ORDER BY v1 =3D v1 AND v1 =3D - - ( SELECT GROU= PING ( v1 >> ) GROUP BY v1 ) ASC FETCH FIRST ROWS WITH TIES > Reproduced here, thanks for the test case. As far as I can see, this > is not limited to 14. Yeah, this looks like it probably dates back to the addition of GroupingFunc. The test case can be simplified a good deal: SELECT (SELECT GROUPING(v1)) FROM (VALUES ((SELECT 1))) v(v1) GROUP BY cub= e(v1); server closed the connection unexpectedly I also found a probably-related variant: SELECT (SELECT GROUPING(v1)) FROM (VALUES ((SELECT 1))) v(v1) GROUP BY v1;= = ERROR: plan should not reference subplan's variable These cases don't fail if the GROUPING call isn't inside a sub-select. The proximate cause of the assertion failure is that preprocess_aggrefs isn't expecting to find a SubLink, which is reasonable since we should have removed them already. However, what it's actually seeing is {TARGETENTRY = :expr = {SUBPLAN = ... :args ( {GROUPINGFUNC = :args ( {PLACEHOLDERVAR = :phexpr = {SUBLINK = ... } ... If we don't put GROUPING(v1) inside a sub-SELECT, it looks like {GROUPINGFUNC = :args ( {PLACEHOLDERVAR = :phexpr = {PARAM = :paramkind 1 = :paramid 0 = :paramtype 23 = :paramtypmod -1 = :paramcollid 0 = :location -1 } :phrels (b 2) :phid 1 = :phlevelsup 0 } ) :refs (i 1) :cols <> = :agglevelsup 0 = :location 15 } which seems a whole lot saner. So I surmise that somebody is missing doing something relevant to the "args" list of a SubPlan. An alternative theory is that we should never have done anything at all to the argument tree of a GroupingFunc. Since it's not supposed to be evaluated, treating it as a target for expression preprocessing might be a mistake altogether. I wonder why its arguments aren't stored as sortgroupref indexes or the like. regards, tom lane