Review
Enable partitionwise join for partition keys wrapped by RelabelType
- Proposal version
- 9
- CommitFest entry
- 6315
- Base commit
- dc168a83874fbf5cee4afb76a623e1349a539724
Review: Enable partitionwise join for partition keys wrapped by RelabelType (v9)
Reviewed the v9 series (four patches) applied on master dc168a83874fbf5cee4afb76a623e1349a539724. It applied cleanly; nothing from this thread is on origin/master (git log --grep on the thread Message-ID, plain and URL-encoded, and on the subject, is empty). v9 is a test-only fix over v8 (a COLLATE "C" qualifier so a citext test's Append child order is stable across platforms); patches 1-3 are unchanged from v8.
The CF title names the original inner-join change. The series also fixes partition pruning and enables partitionwise outer joins and full partitionwise aggregation for the same class of keys.
Summary
When a partition key involves a binary-compatible cast, for example
PARTITION BY LIST ((c::text))
on a varchar column, the stored key is a RelabelType. Several planner and pruning routines already strip RelabelType from the clause or grouping side of an equal() comparison, but not from the key itself (or, in one path, from only one of the two sides). The mismatch means the key never compares equal to a bare Var or to a stripped operand, so pruning, partitionwise join, and full partitionwise aggregation are silently skipped.
0001 strips RelabelType from the partition key in match_clause_to_partition_key(). Collation is still taken from the PartitionScheme and checked with PartCollMatchesExprColl(), not from the stripped expression.
0002 is the original change: exprs_known_equal() strips RelabelType from the two inputs and from each EC member, but only layers that do not change the exposed collation. The guard is necessary. canonicalize_ec_expression() wraps EC members in a collation-changing RelabelType so they expose the class collation; dropping that wrapper would let have_partkey_equi_join() treat keys as equal when they are equal only in a non-deterministic EC collation, and the partitions would not be join-closed. The function is also used for grouping ndistinct estimates, which is how an earlier unconditional strip was shown to be wrong.
0003 covers outer joins. Those quals do not form an equivalence class, so have_partkey_equi_join() can only match the clause through match_expr_to_partition_keys(), which already stripped the operand but not rel->partexprs or rel->nullable_partexprs.
0004 does the same in group_by_has_partkey() so a key like ((c::text)) can choose PARTITIONWISE_AGGREGATE_FULL. Stripping removes the implicit "same type, therefore same equality" guarantee, so the patch also requires the grouping clause's equality operator to belong to the partitioning operator family, matching what have_partkey_equi_join() already does. Without that check, a type that is binary-coercible to text but compares case-insensitively (citext, or a similar handmade type) can split 'A' and 'a' across partitions while GROUP BY merges them, and a full per-partition aggregate returns the wrong groups.
No catalog, on-disk, WAL, protocol, or SQL-syntax change. The user-visible effect is more pruning and more partitionwise plans for RelabelType-wrapped keys; results must stay the same. The GUC defaults are unchanged. No documentation is needed.
Testing
Build: meson, cassert + debug + injection points + TAP. ICU is present. No compiler warnings; git diff --check is clean.
- Suites setup, regress, isolation, and citext: 6/6 pass, including the new tests in partition_prune, partition_join, partition_aggregate, collate.icu.utf8, and postgres_fdw. Several of those tests execute the query, not only EXPLAIN, and include the two important negative cases (non-deterministic collation must not become a partitionwise join; citext/handmade case-insensitive types must stay at partial aggregation).
- Release builds, base vs patched, plus the cassert build: pruning for LIST, RANGE, and HASH expression keys (equality, IN, IS NULL) returns identical rows and now visits only the matching partition(s). Inner, left, right, and full joins on ((c::text)), and a three-way left join that exercises nullable_partexprs, all match the unpatched results. A plain text partition key is unchanged (already partitionwise). The collation safety query returns 9, not 5, and is not planned as a partitionwise join. GROUP BY citext on a text-partitioned table returns one group of 2000 with or without the patch, and stays a partial aggregate. The grouping ndistinct estimate for a text/domain-over-text join is still 144 (actual 12), the same as master; the earlier unconditional strip had reduced it to 12. No assertion failures.
Performance
Release builds, base and patched on the same master, 800k-row tables, eight LIST partitions on ((c::text)), no parallelism, jit off, data cached. Median of 9 alternating runs. Results were identical in every case.
query base patched change WHERE c = '0003' 30.4 ms 9.5 ms -68.9% (base scans all 8 parts; patched scans 1) inner join on c::text, id 231.1 ms 229.6 ms -0.6% (already partitionwise on master: both sides keep the wrapper) left join on c::text, id 335.0 ms 253.2 ms -24.4% (patched becomes partitionwise) GROUP BY c 63.4 ms 63.1 ms -0.5% (patched uses full per-partition aggregates, no finalize; on this workload the finalize was already cheap)
The pruning win is the large, predictable one. The inner-join path for expression keys was already partitionwise, which matches the v3 regression the series fixed by stripping both sides of exprs_known_equal(). Enabling partitionwise left joins is a clear win here. Full partitionwise aggregation is the right plan shape but is not faster than partial aggregation when each partition already holds a single group.
One costing caveat already discussed in the thread is unchanged and not introduced here: a partitionwise Append does not carry run-time pruning (joinrel->baserestrictinfo is empty), and run-time pruning is not costed, so the planner may prefer a partitionwise plan over one that would have pruned at execution. That already happens for plain text keys; this series just makes the same choice available for RelabelType-wrapped keys.
Code review
-
The split is right: pruning, the EC path, the outer-join path, and aggregation are separately testable. 0003 could be folded into 0002, as already noted, but both are small and the separation is defensible.
-
Three RelabelType strippers exist: a collation-preserving one in equivclass.c, an unconditional one in relnode.c, and an inline loop in partprune.c and group_by_has_partkey(). The difference is intentional (the first must not drop a collation-changing wrapper; the others rely on a separate collation or opfamily check). A single helper with an explicit "preserve collation" flag would still be nicer, and would make a missed future call site harder.
-
The opfamily check in 0004 is necessary and well tested. The grouping side still strips only one RelabelType layer, which matches the existing comment that eval_const_expressions() leaves at most one; the partition-key side now loops. Fine.
-
I did not find remaining holes in the same family for RIGHT, FULL, SEMI, or ANTI joins: 0003's change sits in match_expr_to_partition_keys(), which those paths already use, and right/full joins in testing became partitionwise with unchanged results.
Assessment
A well-factored series that closes the same RelabelType asymmetry in pruning, inner partitionwise join, outer partitionwise join, and full partitionwise aggregation. The two sharp edges (collation- changing EC wrappers, and binary-compatible types whose equality is coarser than the partition opfamily) were found, explained, and tested, including result checks. No correctness problem found, and the performance wins for pruning and for newly partitionwise outer joins are clear. Residual nits are the duplicated strip helpers and the pre-existing run-time-pruning costing limitation. v9 looks ready to commit.