Re: Fix HAVING-to-WHERE pushdown with nondeterministic collations

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix HAVING-to-WHERE pushdown with nondeterministic collations
Date: 2026-04-22 06:36:23
Message-ID: CAMbWs49eJmHYHuDOrxh3M-fpmBEyQ6CFVrobj-HKV-hVaBR2oQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Mar 31, 2026 at 12:41 PM Richard Guo <guofenglinux(at)gmail(dot)com> wrote:
> The attached draft patch fixes this for HEAD by leveraging GROUP Vars
> (Vars referencing RTE_GROUP) to detect collation conflicts on a
> per-clause basis, so only unsafe clauses are kept in HAVING while safe
> ones are still pushed. Please see the commit message for more
> details.

I noticed a bug in this patch. The pull_var_clause call in
having_collation_conflict_walker needs to recurse through Aggrefs,
since they can still be present in havingQual at this point and we
need to look through them to reach any GROUP Vars in their direct
arguments. v2 attached fixes this.

> For versions prior to v18, we do not have GROUP Vars. I wonder if we
> can take a conservative approach: skipping the HAVING-to-WHERE
> pushdown optimization entirely if any GROUP BY expression uses a
> nondeterministic collation.

I'm afraid this approach would regress performance for queries that
currently benefit from the optimization. But a proper pre-v18 fix
would require a different approach from the v18+ one, since GROUP Vars
don't exist on earlier branches. Given the absence of field reports,
I don't think the risk of carrying a different fix on stable branches
is justified. So I'm inclined to back-patch this fix to v18 only.

Any thoughts?

- Richard

Attachment Content-Type Size
v2-0001-Fix-HAVING-to-WHERE-pushdown-with-nondeterministi.patch application/octet-stream 20.6 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message David Geier 2026-04-22 06:45:01 Re: Wrong results with equality search using trigram index and non-deterministic collation
Previous Message Yuchen Li 2026-04-22 06:34:46 Re: Cleanup shadows variable warnings, round 1