Hi Shihao, Good catch. Thanks for looking into this.
On Wed, Nov 11, 2026 at 7:36 AM shihao zhong <[email protected]> wrote: > Hi hackers, > > The following query fails an assertion in get_matching_part_pairs(): > > create table pl (a int, b int) partition by list (a); > create table pl_1 partition of pl for values in (1, 2, 3); > create table pl_2 partition of pl for values in (4, 5, 6); > create table pl_d partition of pl default; > create table pl2 (a int, b int) partition by list (a); > create table pl2_1 partition of pl2 for values in (1, 2, 3); > create table pl2_2 partition of pl2 for values in (4, 5, 6); > set enable_partitionwise_join = on; > explain (costs off) > select 1 from pl r1 full join pl2 r2 on r1.a = r2.a > where r2.a in (select a from pl s where s.a between 1 and 3) > and r1.a > 0; > > With merged bounds, the partitions of the join rel come from the first > pair of inputs. Bound merging ignores pruned partitions, so another join > order can pair the partitions differently. Its child rels then don't > match the existing child joins. Release builds are fine, the results > match those with partitionwise join off. > > The attached patch skips the partitionwise join for such a pair of > inputs. It applies to 17 and up. > > - Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids)); + if (bms_num_members(child_relids1) != bms_num_members(rel1->relids)) + return false; This assertion's intended invariant was discussed in [1]: different pairs of joining relations should produce the same set of child joins. Later, Fujita-san explained how to obtain matching partitions from already-built child joins in [2]. That explanation gives the reasoning behind the assertion: each partition can match at most one partition on the other side, and matching partitions have matching/overlapping bounds. In this test, the equal-bounds path and the bound-merging path treat pruning differently. partition_bounds_equal() does not consider pruned partitions specially. If it returns true, compute_partition_bounds() retains the original bounds and try_partitionwise_join() pairs partitions at corresponding positions, skipping a child join when an input partition was pruned. In contrast, merge_list_bounds() skips the pruned partition's list values during matching. A value belonging to that partition can then appear unmatched and be paired with the default partition on that side. However, pruning an explicit partition does not change the default partition's constraint: the default partition still excludes the explicit partition's values. Thus the bound-merging path can construct a child join between partitions that cannot contain matching rows. Another pair of joining relations does not construct the corresponding child join, and its all_partrels lacks a partition relid required by the assertion. I think we should investigate fixing this inconsistency in partition matching rather than only abandoning the alternative partitionwise join. The matching algorithm should retain the exclusions imposed by pruned partitions when deciding whether an otherwise unmatched partition can match a default partition. merge_range_bounds() also skips pruned partitions through get_range_partition(), so it would be useful to check whether an analogous range-partitioned test exposes the same issue. We already have tests involving default partitions and partition pruning, but in those two-table tests the restrictions prune the default partition. They do not cover this three-table case, where an explicit partition is pruned while the default partition survives and different pairs of joining relations are considered. [1] https://www.postgresql.org/message-id/CAFjFpRdRA9rHiTiFAHu2b4%2Bv%3D6t%2Bo3Z-53QR2bWhtfrSjb7UOw%40mail.gmail.com [2] https://www.postgresql.org/message-id/CAPmGK16dJcMJhVQ0pq3KGA_L%2BOuMTOcynMu-7jMsK06uZkCnqw%40mail.gmail.com -- Best Wishes, Ashutosh Bapat
