From 0c894367a66c4836eb2cda48823faed85289fe24 Mon Sep 17 00:00:00 2001 From: shihao zhong Date: Fri, 16 Oct 2026 22:41:34 -0400 Subject: [PATCH] Strip relabeling from the partition key when matching pruning clauses The clause side is already stripped of RelabelType, but the key was not. Since the previous commit relabels a rebuilt COALESCE, CASE or NULLIF, a key over an inlined SQL function no longer matched and was not pruned. --- src/backend/partitioning/partprune.c | 4 ++++ src/test/regress/expected/partition_prune.out | 17 +++++++++++++++++ src/test/regress/sql/partition_prune.sql | 12 ++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/backend/partitioning/partprune.c b/src/backend/partitioning/partprune.c index f128b99c36a..5a7be1afde1 100644 --- a/src/backend/partitioning/partprune.c +++ b/src/backend/partitioning/partprune.c @@ -1177,6 +1177,10 @@ gen_partprune_steps_internal(GeneratePruningStepsContext *context, PartClauseInfo *pc = NULL; List *clause_steps = NIL; + /* The clause side is stripped of relabeling, so do it here too */ + while (IsA(partkey, RelabelType)) + partkey = ((RelabelType *) partkey)->arg; + switch (match_clause_to_partition_key(context, clause, partkey, i, &clause_is_not_null, diff --git a/src/test/regress/expected/partition_prune.out b/src/test/regress/expected/partition_prune.out index 3506ecec477..df8fc9e0478 100644 --- a/src/test/regress/expected/partition_prune.out +++ b/src/test/regress/expected/partition_prune.out @@ -5145,3 +5145,20 @@ select * from (select a, b from phv_boolpart) t (2 rows) drop table phv_boolpart; +-- Partition key whose typmod changes when an SQL function is inlined +create function relabel_part_f(numeric) returns numeric + language sql immutable as 'select $1'; +create table relabel_part (a numeric(10), b numeric(10)) + partition by list ((coalesce(relabel_part_f(a), b))); +create table relabel_part1 partition of relabel_part for values in (1); +create table relabel_part2 partition of relabel_part for values in (2); +explain (costs off) +select * from relabel_part where coalesce(relabel_part_f(a), b) = 1; + QUERY PLAN +------------------------------------------------------ + Seq Scan on relabel_part1 relabel_part + Filter: ((COALESCE(a, b))::numeric = '1'::numeric) +(2 rows) + +drop table relabel_part; +drop function relabel_part_f(numeric); diff --git a/src/test/regress/sql/partition_prune.sql b/src/test/regress/sql/partition_prune.sql index d6e096dc565..964cb1a70a9 100644 --- a/src/test/regress/sql/partition_prune.sql +++ b/src/test/regress/sql/partition_prune.sql @@ -1611,3 +1611,15 @@ select * from (select a, b from phv_boolpart) t group by grouping sets (a, b); drop table phv_boolpart; + +-- Partition key whose typmod changes when an SQL function is inlined +create function relabel_part_f(numeric) returns numeric + language sql immutable as 'select $1'; +create table relabel_part (a numeric(10), b numeric(10)) + partition by list ((coalesce(relabel_part_f(a), b))); +create table relabel_part1 partition of relabel_part for values in (1); +create table relabel_part2 partition of relabel_part for values in (2); +explain (costs off) +select * from relabel_part where coalesce(relabel_part_f(a), b) = 1; +drop table relabel_part; +drop function relabel_part_f(numeric);