Re: BUG #17088: FailedAssertion in prepagg.c

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
Cc: Michael Paquier <michael(at)paquier(dot)xyz>, cyg0810(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17088: FailedAssertion in prepagg.c
Date: 2021-07-07 20:49:22
Message-ID: 1908212.1625690962@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Michael Paquier <michael(at)paquier(dot)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

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andrew Gierth 2021-07-07 21:18:01 Re: BUG #17088: FailedAssertion in prepagg.c
Previous Message Moti 2021-07-07 18:02:17 problem in app installation