Re: Fix HAVING-to-WHERE pushdown with nondeterministic collations
Richard Guo <guofenglinux@gmail.com>
From: Richard Guo <guofenglinux@gmail.com>
To: Pg Hackers <pgsql-hackers@lists.postgresql.org>
Date: 2026-04-22T06:36:23Z
Lists: pgsql-hackers
Attachments
- v2-0001-Fix-HAVING-to-WHERE-pushdown-with-nondeterministi.patch (application/octet-stream) patch v2-0001
On Tue, Mar 31, 2026 at 12:41 PM Richard Guo <guofenglinux@gmail.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
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
Fix HAVING-to-WHERE pushdown for simple-CASE form
- 1132af22cf7d 18.4 landed
- ba82de48e62c 19 (unreleased) landed
-
Fix HAVING-to-WHERE pushdown with nondeterministic collations
- e8fd5e579223 18.4 landed
- f76686ce7f77 19 (unreleased) landed