Re: Fix HAVING-to-WHERE pushdown with mismatched operator families

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Thom Brown <thom(at)linux(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix HAVING-to-WHERE pushdown with mismatched operator families
Date: 2026-05-28 09:11:25
Message-ID: CAMbWs49dHFVVwx9MjYmvUrZu0L4vZTbGVyhj3T2SsWgS1Bzhzg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, May 27, 2026 at 8:04 AM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> Thanks for raising these points. For partition pruning,
> match_clause_to_partition_key() already checks both collation and
> opfamily compatibility, so I don't think it has similar issues. I'm
> not sure what is meant by "window function pushdowns", but your
> question prompted me to look around, and I did notice that pushing
> restriction clauses down into a subquery suffers from a similar
> problem, specifically, when the subquery has DISTINCT, DISTINCT ON, or
> a window PARTITION BY clause.

I think all these issues belong to the same class of bug: the planner
moves a qual clause across a grouping layer, and the result is wrong
when the qual's equivalence relation disagrees with the grouping's,
either an opfamily mismatch or a nondeterministic-collation mismatch.
This includes HAVING-to-WHERE pushdown, as well as qual pushdown into
a subquery past its DISTINCT, DISTINCT ON, window PARTITION BY, or
set-operation grouping layer.

v2 attached tries to fix the full bug class through a shared walker
expression_has_grouping_conflict that detects either kind of conflict
in an expression tree. The walker takes a callback that maps each
Var to the grouping equality operator for its column (or InvalidOid
for non-grouping Vars). See the commit message for details.

- Richard

Attachment Content-Type Size
v2-0001-Fix-qual-pushdown-past-grouping-with-mismatched-e.patch application/octet-stream 62.2 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-05-28 09:26:55 Re: tablecmds: clarify recurse vs recusing
Previous Message Chao Li 2026-05-28 09:09:13 Fix race during concurrent logical decoding activation