Re: Enable partitionwise join for partition keys wrapped by RelabelType - Mailing list pgsql-hackers

From jian he
Subject Re: Enable partitionwise join for partition keys wrapped by RelabelType
Date
Msg-id CACJufxGqEf8K_kH6JLRY6gXWVh9p6BikWSsGrwmzY6oPrSEvQQ@mail.gmail.com
Whole thread Raw
In response to Enable partitionwise join for partition keys wrapped by RelabelType  ("Matheus Alcantara" <matheusssilv97@gmail.com>)
List pgsql-hackers
On Mon, Dec 15, 2025 at 10:46 PM Matheus Alcantara
<matheusssilv97@gmail.com> wrote:
>
> Hi,
>
> The function exprs_known_equal() is used by the planner to determine if
> two expressions are semantically equivalent, often by checking if they
> belong to the same Equivalence Class (EC).
>

hi.

src/include/nodes/primnodes.h CollateExpr comments:
/*----------
 * CollateExpr - COLLATE
 *
 * The planner replaces CollateExpr with RelabelType during expression
 * preprocessing, so execution never sees a CollateExpr.
 *----------
 */

examine_variable handling RelabelType (transformed from CollateExpr) is wrong, i
think. Roughly speaking it will reduce "t2.c collate case_insensitive" to
"t2.c". see [1].

If examine_variable does not strip the RelabelType(CollateExpr) node, then
estimate_num_groups->add_unique_group_var may need to deal with RelabelType.

The estimate_num_groups function should account for collation settings.  "GROUP
BY a" and "GROUP BY a COLLATE case_insensitive" may result in different row
estimates and should be handled distinctly. Therefore exprs_known_equal within
add_unique_group_var must ensure collation is compared before assuming equality.

In this context, while strippping RelabelType, we should ensure
exprCollation(RelabelType->arg) is the same as the RelabelType->resultcollid.

However, it does not affect partitionwise join, because commit [2] already fixed
the collation issue. so we don't have to worry about
have_partkey_equi_join->exprs_known_equal code path for the RelabelType node.

so i change exprs_known_equal to:

+            /* Remove any relabel decorations if collation match */
+            if (IsA(expr, RelabelType))
+            {
+                RelabelType *relabel = castNode(RelabelType, expr);
+                Expr       *rexpr = (Expr *) relabel;
+
+                while (rexpr && IsA(rexpr, RelabelType))
+                    rexpr = ((RelabelType *) rexpr)->arg;
+
+                if (exprCollation((Node *) rexpr) == relabel->resultcollid)
+                    expr = rexpr;
+            }

[1] https://postgr.es/m/CACJufxGLCiyhM+P0gxesg2x--PTrMY3PszqSqOq_H4QS_oq3Jg@mail.gmail.com
[2] https://git.postgresql.org/cgit/postgresql.git/commit/?id=075acdd93388c080c0fb0aca5723144ad7a56dac


--
jian
https://www.enterprisedb.com

Attachment

pgsql-hackers by date:

Previous
From: Andreas Karlsson
Date:
Subject: Re: Add missing JIT inline pass for llvm>=17
Next
From: Henson Choi
Date:
Subject: Re: Row pattern recognition