Re: BUG #19649: Qual pushdown into GROUP BY subqueries ignores non-equivalence-preserving references to grouping col
Re: BUG #19649: Qual pushdown into GROUP BY subqueries ignores non-equivalence-preserving references to grouping col
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
Re: BUG #19649: Qual pushdown into GROUP BY subqueries ignores non-equivalence-preserving references to grouping col
сб, 5 сент. 2026 г. в 12:52, Andrey Rachitskiy <pl0h0yp1@gmail.com>:
v3 in attachment.P.S. I'm still learning plans and could be mistaken, so please don't judge too harshly.
misread the plan when I wrote v3. For a GROUP BY subquery the wrapped
qual is not pushed below the grouping. subquery_push_qual attaches it to
HAVING, and find_having_conflicts then keeps it there through the same
walker that the clauses.c fix changes. So v2 already produced correct
results for GROUP BY, by construction rather than by accident.
```
if (subquery->hasAggs || subquery->groupClause ||
subquery->groupingSets || subquery->havingQual)
subquery->havingQual = make_and_qual(subquery->havingQual, qual);
else
subquery->jointree->quals = make_and_qual(...);
```
So a wrapped jsonb qual such as j::text = '1' stays in HAVING and is not
lowered to WHERE. That is why the GroupAggregate and HashAggregate cases
already gave correct results under v2, with the filter on the Agg node.
DISTINCT, window PARTITION BY and set operations are different. They have
no HAVING, so subquery_push_qual routes the pushed qual to WHERE. That is
why point 6 in qual_is_pushdown_safe lists those three and omits GROUP BY.
The clauses.c fix covers both boundaries because both share the walker.
So the groupClause check I added to qual_is_pushdown_safe in v3 is
redundant for correctness. It only changes the plan shape: the filter
ends up on the Subquery Scan instead of on the Agg node. Both are
correct.
I attach v4, it is v2 with fixed comment.
Sorry for the noise, I'll be more attentive and take my time.
Re: BUG #19649: Qual pushdown into GROUP BY subqueries ignores non-equivalence-preserving references to grouping col
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Andrey Rachitskiy writes: > Refuse to push a non-operand reference to a grouping column below the > grouping boundary unless the grouping equality is image equality > (BTEQUALIMAGE_PROC). That covers jsonb, numeric, float8 and similar > types, and subsumes the old nondeterministic-collation check for > wrapped references. The more I think about it, the less I like this solution approach. The trouble with it is that in exchange for making ill-posed queries (with no real-world use-cases) seem to have stable semantics, you are going to cause a disastrous performance regression for perfectly fine queries that do have real-world use. I can't see us taking that tradeoff. Especially when the apparently stable semantics are in fact no such thing, so that there isn't any useful gain from making the code act that way. What we really need here, perhaps, is a way to mark functions that can distinguish values seen as equal by their datatype's default equality operator. I'm not sure of a good way to do that. If we did have such a marking, frankly what I would do with it would be less like "don't push down past aggregation" and more like "throw an error that this query has undefined results". Otherwise we're just going to replace bug reports of this shape with bug reports about the query's result being unstable. regards, tom lane