Re: BUG #17152: ERROR: AddressSanitizer: SEGV on unknown address

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: 253540651(at)qq(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #17152: ERROR: AddressSanitizer: SEGV on unknown address
Date: 2021-08-18 18:01:46
Message-ID: 1495661.1629309706@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

I wrote:
> So somehow the fact that outer references are involved is misleading that
> error check. I've not traced it further than that.

Actually, it's simpler than that. The patch that added FILTER
taught check_agg_arguments to check the FILTER argument like this:

(void) expression_tree_walker((Node *) filter,
check_agg_arguments_walker,
(void *) &context);

which was a blind copy-and-paste of what it does for the "args"
list. However, that coding for "args" includes an undocumented
optimization: "args" is a List, and check_agg_arguments_walker
doesn't care about Lists, so it's okay to recurse directly to
expression_tree_walker without invoking the walker on the top-level
List node. This is completely NOT okay for the "filter" argument,
which could be directly a Var or Aggref. Hence, the check completely
misses a top-level Var or Aggref in FILTER.

It would be enough to convert this one call to

(void) check_agg_arguments_walker((Node *) filter, &context);

but in the attached I just converted all three of check_agg_arguments'
invocations to look that way. The few nanoseconds shaved by the
other coding don't justify the risk of more copy-and-paste bugs.

An interesting nearby issue is that check_agglevels_and_constraints is
not really handling this correctly either. It supposes that once it's
identified the agglevelsup for an Aggref node, it can look that many
levels up in the parse stack to see whether it's inside a relevant
FILTER expression. This is wrong, because we've not yet determined
the semantic level of the surrounding aggregate if any, so we've
certainly not set p_expr_kind in the relevant outer pstate level.
That explains why you get

regression=# CREATE TEMP TABLE v0 ( v2 int );
CREATE TABLE
regression=#
SELECT
MODE ( ) WITHIN GROUP ( ORDER BY v2 )
FILTER ( WHERE MODE ( ) WITHIN GROUP ( ORDER BY v2 > 0 ) )
FROM v0;
ERROR: aggregate functions are not allowed in FILTER
LINE 3: FILTER ( WHERE MODE ( ) WITHIN GROUP ( ORDER BY v2 > 0 )...
^

while (after this patch) the outer-level case gives

regression=# SELECT (
SELECT
MODE ( ) WITHIN GROUP ( ORDER BY v2 )
FILTER ( WHERE MODE ( ) WITHIN GROUP ( ORDER BY v2 > 0 ) )
)
FROM v0;
ERROR: aggregate function calls cannot be nested
LINE 4: FILTER ( WHERE MODE ( ) WITHIN GROUP ( ORDER BY v2 > 0 )...
^

check_agglevels_and_constraints doesn't complain at the lower
Aggref, so it's left to the upper one to whine.

I'm not too concerned about this cosmetic difference, but I wonder
if there are any cases where check_agglevels_and_constraints will
throw an error when it shouldn't. Right offhand I can't see any,
but I may just lack imagination today.

Regardless of that, we certainly need the attached patch (and,
probably, some more regression tests).

regards, tom lane

Attachment Content-Type Size
fix-detection-of-nested-aggs-in-FILTER.patch text/x-diff 1.2 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Daniel Gustafsson 2021-08-18 18:53:00 Re: BUG #17148: About --no-strict-names option and --quiet option of pg_amcheck command
Previous Message Tom Lane 2021-08-18 15:55:58 Re: BUG #17151: A SEGV in optimizer