pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrew Gierth <andrew@tao11.riddles.org.uk>
Cc: Michael Paquier <michael@paquier.xyz>
Cc: cyg0810@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #17088: FailedAssertion in prepagg.c
Date: Wed, 07 Jul 2021 16:49:22 -0400
Message-ID: <1908212.1625690962@sss.pgh.pa.us> (raw)
In-Reply-To: <YOWJQTovI//DNe8/@paquier.xyz>
References: <17088-e33882b387de7f5c@postgresql.org>
	<YOWJQTovI//DNe8/@paquier.xyz>

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





view thread (12+ messages)  latest in thread

Message-ID: <1908212.1625690962@sss.pgh.pa.us>
Permalink:  ../1908212.1625690962@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1908212.1625690962@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, andrew@tao11.riddles.org.uk, michael@paquier.xyz, cyg0810@gmail.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: BUG #17088: FailedAssertion in prepagg.c
  In-Reply-To: <1908212.1625690962@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox