Re: BUG #17088: FailedAssertion in prepagg.c

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrew Gierth <andrew@tao11.riddles.org.uk>
Cc: Michael Paquier <michael@paquier.xyz>, cyg0810@gmail.com, pgsql-bugs@lists.postgresql.org
Date: 2021-07-07T20:49:22Z
Lists: pgsql-bugs
Michael Paquier <michael@paquier.xyz> 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 = v1 AND v1 = - - ( SELECT GROUPING ( 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 cube(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



Commits

  1. Fix assorted missing logic for GroupingFunc nodes.