pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: xinwen@stu.scu.edu.cn
Cc: pgsql-bugs@lists.postgresql.org, Andres Freund <andres@anarazel.de>
Subject: Re: BUG #17835: Two assertions failed in nodeAgg.c and execExprInterp.c with the same SQL
Date: Mon, 13 Mar 2023 11:26:14 -0400
Message-ID: <1772189.1678721174@sss.pgh.pa.us> (raw)
In-Reply-To: <17835-4f29f3098b2d0ba4@postgresql.org>
References: <17835-4f29f3098b2d0ba4@postgresql.org>

PG Bug reporting form <noreply@postgresql.org> writes:
> When executing the following query with CTE:

> WITH table1 ( column25 ) AS ( SELECT 1 ) SELECT FROM ( SELECT column25
> column12 FROM table1 ) AS alias0 GROUP BY column12 HAVING AVG ( ( SELECT 1
> FROM table1 JOIN ( SELECT AVG ( column25 ORDER BY CASE 1 WHEN column12 THEN
> ( SELECT AVG ( column12 ) FROM table1 ) END ) column14 FROM table1 ) AS
> alias3 ON alias3 . column14 = 1 ) ) = 1 ;

> I get a failed assertion with the following stacktrace:

Simplifying a bit, we get

WITH table1 ( col1 ) AS ( SELECT 1 )
SELECT AVG((
  SELECT AVG ( a1.col1 ORDER BY ( SELECT AVG ( a2.col2 ) FROM table1 ) )
  FROM table1 a1
))
FROM table1 AS a2(col2);

PG v10 says
	ERROR:  aggregate function calls cannot be nested
which seems correct: per spec, the innermost AVG actually should belong
to the outer query level that sources a2.col2.  Later versions fail to
detect that the query is nonsensical and end up with nonsensical
executor state instead.  I bisected this to

69c3936a1499b772a749ae629fc59b2d72722332 is the first bad commit
commit 69c3936a1499b772a749ae629fc59b2d72722332
Author: Andres Freund <andres@anarazel.de>
Date:   Tue Jan 9 13:25:38 2018 -0800

    Expression evaluation based aggregate transition invocation.

So that commit broke something about the nested-aggregate detection
logic.  It's not completely gone: if we simplify this to

WITH table1 ( col1 ) AS ( SELECT 1 )
SELECT AVG((
  SELECT AVG ( ( SELECT AVG ( a2.col2 ) FROM table1 ) )
  FROM table1 a1
))
FROM table1 AS a2(col2);

we still get

ERROR:  aggregate function calls cannot be nested
LINE 3:   SELECT AVG ( ( SELECT AVG ( a2.col2 ) FROM table1 ) )
                                ^

v10 detected this in ExecInitAgg, while later versions are trying to
do it in the parser, but evidently there's some gap there ...

			regards, tom lane





view thread (3+ messages)  latest in thread

Message-ID: <1772189.1678721174@sss.pgh.pa.us>
Permalink:  ../1772189.1678721174@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1772189.1678721174@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, xinwen@stu.scu.edu.cn, andres@anarazel.de
  Subject: Re: BUG #17835: Two assertions failed in nodeAgg.c and execExprInterp.c with the same SQL
  In-Reply-To: <1772189.1678721174@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