Re: BUG #19649: Qual pushdown into GROUP BY subqueries ignores non-equivalence-preserving references to grouping col

From: Andrey Rachitskiy <pl0h0yp1(at)gmail(dot)com>
To: Andrei Lepikhov <lepihov(at)gmail(dot)com>
Cc: 303677365(at)qq(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org, Tender Wang <tndrwang(at)gmail(dot)com>
Subject: Re: BUG #19649: Qual pushdown into GROUP BY subqueries ignores non-equivalence-preserving references to grouping col
Date: 2026-09-05 07:52:39
Message-ID: CAB8bMivO2qD9obEMdPxDpQ+Zanq_e_fHA2c1tKe=YeRxjnbXWA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

пт, 4 сент. 2026 г. в 21:10, Andrey Rachitskiy <pl0h0yp1(at)gmail(dot)com>:

>
> пт, 4 сент. 2026 г. в 20:12, Andrei Lepikhov <lepihov(at)gmail(dot)com>:
>
>> On 03/09/2026 23:17, Andrey Rachitskiy wrote:
>> > This blocks wrapper-based finer equivalence at pushdown boundaries while
>> > preserving existing behavior for truly direct operands.
>> I don't like this fix. It causes regressions where we haven't had it
>> before.
>> Let's see:
>>
>> CREATE TABLE r(i int, s text, ts timestamptz);
>> EXPLAIN (COSTS OFF)
>> SELECT * FROM (SELECT i, count(*) c FROM r GROUP BY i) s WHERE i::text =
>> '5';
>>
>> Before:
>>
>> GroupAggregate
>> Group Key: r.i
>> -> Sort
>> Sort Key: r.i
>> -> Seq Scan on r
>> Filter: ((i)::text = '5'::text)
>>
>> With your fix:
>>
>> HashAggregate
>> Group Key: r.i
>> Filter: ((r.i)::text = '5'::text)
>> -> Seq Scan on r
>>
>> I think, filter should be pushed down to the scan.
>>
>> --
>> regards, Andrei Lepikhov,
>> pgEdge
>>
>
> Dear Andrei,
>
> Thanks for the review.
>
> v2 with a correction in the attachment.
> I also removed the duplicate logic.
>
>
In v2, I did not account for the GROUP BY case.
44fb59fc605 checks it later in
find_having_conflicts, after the qual is pushed into HAVING.
ReplaceVarsFromTargetList copies a GROUP Var from the tlist, so the
walker can keep a wrapped jsonb qual on the Agg node.
Without the wrapper check the qual moves to WHERE. The reporter's
j::text filters then turn count(*) from 2 into 1.
This update also looks at groupClause in qual_is_pushdown_safe, so the
qual is not pushed. The filter stays on Subquery Scan, as for
DISTINCT.

v3 in attachment.

P.S. I'm still learning plans and could be mistaken, so please don't judge
too harshly.

Attachment Content-Type Size
v3-0001-Fix-qual-pushdown-for-wrapped-grouping-comparisons.patch text/x-patch 21.1 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Richard Guo 2026-09-04 23:09:42 Re: BUG #19653: "variable not found in subplan target list" during planning with parallel parameterized nested loop,