pgsql: Fix qual pushdown past grouping through simple CASE

From: Richard Guo <rguo(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Fix qual pushdown past grouping through simple CASE
Date: 2026-09-02 06:35:03
Message-ID: E1x1eYo-000000038vr-0Jiu@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix qual pushdown past grouping through simple CASE

Commit 44fb59fc6 taught the grouping-conflict walker to treat the arg
of a simple CASE as a direct operand of each WHEN comparison, but it
only checked the collation, on the assumption that the WHEN operator
is always the type-default "=" and thus matches the grouping eqop.
That assumption fails once the arg is relabeled to another type: the
WHEN then compares under that type's "=", which need not agree with
the grouping equality. For instance, with a DISTINCT over a citext
column, a qual such as "CASE t::text WHEN 'A' THEN ..." was pushed
below the Unique, although the equivalent "t::text = 'A'" is correctly
kept above it.

Instead of special-casing the arg, have the walker bind a Var arg
while walking the WHEN conditions and resolve each CaseTestExpr to it,
so that the arg is checked exactly as each WHEN uses it: with the
opfamily and collation checks of a direct operand when the WHEN is a
comparison, and as a non-operand reference otherwise. A non-Var arg
is walked once as a non-operand, as before. The CaseTestExpr in an
ArrayCoerceExpr's elemexpr and a JsonConstructorExpr's coercion stand
for something else and are left alone.

Reported-by: Tender Wang <tndrwang(at)gmail(dot)com>
Author: Richard Guo <guofenglinux(at)gmail(dot)com>
Reviewed-by: Tender Wang <tndrwang(at)gmail(dot)com>
Reviewed-by: Ewan Young <kdbase(dot)hack(at)gmail(dot)com>
Discussion: https://postgr.es/m/CAHewXNkvGTOgijRLjmudpg=wz0d-J8ChY2o7ksh3Be+Q_Bxwog@mail.gmail.com
Backpatch-through: 18

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/264ddc9a678c41fca3435f26d7c581fd2b034cf8

Modified Files
--------------
src/backend/optimizer/util/clauses.c | 127 +++++++++++++++++--------
src/test/regress/expected/collate.icu.utf8.out | 35 +++++++
src/test/regress/expected/subselect.out | 27 ++++++
src/test/regress/sql/collate.icu.utf8.sql | 12 +++
src/test/regress/sql/subselect.sql | 11 +++
5 files changed, 171 insertions(+), 41 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2026-09-02 07:23:10 pgsql: Add more test coverage for pg_clear_attribute_stats()
Previous Message Fujii Masao 2026-09-02 05:37:28 pgsql: Fix checkpointer restartpoint assertion failure