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

From: Richard Guo <guofenglinux(at)gmail(dot)com>
To: SATYANARAYANA NARLAPURAM <satyanarlapuram(at)gmail(dot)com>
Cc: wenhui qiu <qiuwenhuifx(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Fix HAVING-to-WHERE pushdown with nondeterministic collations
Date: 2026-05-08 08:07:07
Message-ID: CAMbWs4-m+mWYuokm16zqeNFTFAGFna4wjBvgQTRy3Dvr1sMsDQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, May 6, 2026 at 9:58 AM SATYANARAYANA NARLAPURAM
<satyanarlapuram(at)gmail(dot)com> wrote:
> It appears HAVING-to-WHERE pushdown is still wrong with CASE and nondeterministic
> collations. The shorthand CASE expression bypasses the new collation-conflict detector,
> so the HAVING clause gets pushed to WHERE, filtering rows before
> grouping and silently changing aggregate results.

Right. For simple CASE (CaseExpr with a non-NULL arg), parse analysis
builds each WHEN as OpExpr(CaseTestExpr op val), where CaseTestExpr is
a placeholder for the arg, while the actual arg sits at cexpr->arg,
outside the OpExpr that carries the comparison's inputcollid. A GROUP
Var at cexpr->arg is therefore visited without the WHEN's inputcollid
on the stack. So the conflict fails to be detected, and the HAVING
clause is incorrectly pushed to WHERE.

> My understanding is shallow here, attached a draft patch that adds a CaseExpr branch to
> having_collation_conflict_walker() mirroring the existing RowCompareExpr
> special case. Patch includes the tests. Please take a look.

This patch is on the right track. I didn't like how the stack was
restored after walking cexpr->arg; list_truncate fits better there.
The comments also needed some tightening. I've made those
adjustments, pushed, and back-patched.

- Richard

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-05-08 08:14:40 Fix wrong error message from pg_get_tablespace_ddl()
Previous Message Daniel Gustafsson 2026-05-08 07:32:16 Re: Fix typo 586/686 in atomics/arch-x86.h