From 71aff04c70d5ecfdb844d2a9fa524dc0a04d7f6b Mon Sep 17 00:00:00 2001 From: Richard Guo Date: Thu, 19 Nov 2026 14:31:07 +0900 Subject: [PATCH v6 6/8] Detect unique inner sides of joins for child relations When a join is performed partitionwise, each child join is planned like any other join, including the check whether its inner side is unique. But that check knew nothing about child relations, so a child join was never marked as having a unique inner side, not even for a plain join of two partitioned tables on their primary keys. For an inner side that is a single child relation, the existing proofs work from a relation's own unique indexes and restriction clauses, which a child relation has just like any other. So rel_supports_distinctness and rel_is_distinct_for now accept appendrel children as well as base relations. For an inner side that is a child join, no UniqueKeys for child relations are needed. The rows of a child join are a subset of the rows of its parent join, and its clauses refer to the same EquivalenceClasses as the parent's clauses, with the child's expressions as child members. So a child join is distinct if its parent is, and we just ask about the parent. That misses what could be proven from unique indexes that only individual partitions have. --- src/backend/optimizer/README | 13 ++- src/backend/optimizer/path/uniquekeys.c | 15 ++- src/backend/optimizer/plan/analyzejoins.c | 6 +- src/test/regress/expected/uniquekeys.out | 124 +++++++++++++++++++++- src/test/regress/sql/uniquekeys.sql | 42 +++++++- 5 files changed, 192 insertions(+), 8 deletions(-) diff --git a/src/backend/optimizer/README b/src/backend/optimizer/README index 2d90f230eca..b8144bbba44 100644 --- a/src/backend/optimizer/README +++ b/src/backend/optimizer/README @@ -1048,8 +1048,8 @@ so that a query that never needs them does not pay for them. UniqueKeys are used to detect unique inner sides of joins when the inner side is itself a join, and to skip DISTINCT and GROUP BY steps whose input is already distinct. The following describes each of these in turn, and -then how outer joins and semijoins fit in. For now only plain tables get -UniqueKeys. +then how outer joins, semijoins and appendrel children fit in. For now +only plain tables get UniqueKeys. We do not keep UniqueKeys for join relations. A join relation would typically have many of them, most of which would never be used. And since @@ -1183,6 +1183,15 @@ join relation that includes the right-hand side of a semijoin but only part of its left-hand side, which can only be formed by making the right-hand side unique first. +Appendrel children have no UniqueKeys. A child relation that is by itself +the inner side of a join is checked through its own unique indexes, like +any plain relation. For a child join we rely on its parent: the rows of a +child join are a subset of its parent's rows, and the ECs that a question +about it refers to are those of its parent, with the child's expressions as +child members. So a child join is distinct if its parent is, and we just +ask about the parent. That misses, for child joins, what could be proven +from indexes that only individual partitions have. + Order of processing for EquivalenceClasses and PathKeys ------------------------------------------------------- diff --git a/src/backend/optimizer/path/uniquekeys.c b/src/backend/optimizer/path/uniquekeys.c index fc76dc6f959..bfdb42747c0 100644 --- a/src/backend/optimizer/path/uniquekeys.c +++ b/src/backend/optimizer/path/uniquekeys.c @@ -7,7 +7,8 @@ * relation agree on. Plain relations get them from their unique indexes. * Join relations have no UniqueKeys of their own: whether a join relation is * distinct is worked out when needed, from the UniqueKeys of the base - * relations it consists of. + * relations it consists of. Appendrel children have none either; see + * joinrel_is_distinct_for(). * * There are two kinds of question. Mergejoinable join clauses are assumed * to be strict, so when matching them we need not care about rows that have @@ -88,6 +89,10 @@ joinrel_supports_distinctness(PlannerInfo *root, RelOptInfo *joinrel) Assert(IS_JOIN_REL(joinrel)); + /* For a child join, we look at its parent; see joinrel_is_distinct_for */ + if (IS_OTHER_REL(joinrel)) + joinrel = joinrel->top_parent; + /* A grouped join rel's rows are not combinations of base rel rows */ if (IS_GROUPED_REL(joinrel)) return false; @@ -127,6 +132,14 @@ joinrel_is_distinct_for(PlannerInfo *root, RelOptInfo *joinrel, if (!joinrel_supports_distinctness(root, joinrel)) return false; + /* + * A child join's rows are a subset of its parent's rows, and its clauses + * use the same ECs as its parent's. So it is distinct if its parent is, + * and we need no UniqueKeys for child relations. + */ + if (IS_OTHER_REL(joinrel)) + joinrel = joinrel->top_parent; + foreach_node(RestrictInfo, rinfo, clause_list) { /* caller identified the side belonging to this relation for us */ diff --git a/src/backend/optimizer/plan/analyzejoins.c b/src/backend/optimizer/plan/analyzejoins.c index 2eda06ec7ce..70e9416ef8b 100644 --- a/src/backend/optimizer/plan/analyzejoins.c +++ b/src/backend/optimizer/plan/analyzejoins.c @@ -605,8 +605,8 @@ rel_supports_distinctness(PlannerInfo *root, RelOptInfo *rel) /* For a join relation, the proof has to come from UniqueKeys */ if (IS_JOIN_REL(rel)) return joinrel_supports_distinctness(root, rel); - /* Otherwise we only know about baserels ... */ - if (rel->reloptkind != RELOPT_BASEREL) + /* Otherwise we only know about baserels and their appendrel children */ + if (!IS_SIMPLE_REL(rel)) return false; if (rel->rtekind == RTE_RELATION) { @@ -680,7 +680,7 @@ rel_is_distinct_for(PlannerInfo *root, RelOptInfo *rel, List *clause_list, * rel_supports_distinctness first, but it doesn't seem worth taking any * risk for. */ - if (rel->reloptkind != RELOPT_BASEREL) + if (!IS_SIMPLE_REL(rel)) return false; if (rel->rtekind == RTE_RELATION) { diff --git a/src/test/regress/expected/uniquekeys.out b/src/test/regress/expected/uniquekeys.out index a2b45c7add6..e52e23954e5 100644 --- a/src/test/regress/expected/uniquekeys.out +++ b/src/test/regress/expected/uniquekeys.out @@ -886,4 +886,126 @@ select distinct x, sum(x) over (partition by y) from uk_a; -> Seq Scan on uk_a (7 rows) -drop table uk_a, uk_b, uk_c, uk_d, uk_n; +-- +-- Test child relations under partitionwise join. +-- +create table uk_p (id int primary key, x int, y int) partition by range (id); +create table uk_p1 partition of uk_p for values from (0) to (100); +create table uk_p2 partition of uk_p for values from (100) to (200); +create table uk_q (id int primary key, x int, y int) partition by range (id); +create table uk_q1 partition of uk_q for values from (0) to (100); +create table uk_q2 partition of uk_q for values from (100) to (200); +create table uk_r (id int, x int, y int) partition by range (id); +create table uk_r1 partition of uk_r for values from (0) to (100); +create table uk_r2 partition of uk_r for values from (100) to (200); +-- Disable hash and merge joins, so that the nullable side of a left join is +-- the inner side. +set enable_partitionwise_join = on; +set max_parallel_workers_per_gather = 0; +set enable_hashjoin = off; +set enable_mergejoin = off; +-- a child relation is unique through its own index +explain (verbose, costs off) +select * from uk_p p join uk_q q on p.id = q.id; + QUERY PLAN +------------------------------------------------------------- + Append + -> Nested Loop + Output: p_1.id, p_1.x, p_1.y, q_1.id, q_1.x, q_1.y + Inner Unique: true + -> Seq Scan on public.uk_p1 p_1 + Output: p_1.id, p_1.x, p_1.y + -> Index Scan using uk_q1_pkey on public.uk_q1 q_1 + Output: q_1.id, q_1.x, q_1.y + Index Cond: (q_1.id = p_1.id) + -> Nested Loop + Output: p_2.id, p_2.x, p_2.y, q_2.id, q_2.x, q_2.y + Inner Unique: true + -> Seq Scan on public.uk_p2 p_2 + Output: p_2.id, p_2.x, p_2.y + -> Index Scan using uk_q2_pkey on public.uk_q2 q_2 + Output: q_2.id, q_2.x, q_2.y + Index Cond: (q_2.id = p_2.id) +(17 rows) + +-- a child join is unique if its parent is +explain (verbose, costs off) +select * from uk_p p left join + (uk_q q join uk_p p2 on q.id = p2.id) on p.id = q.id; + QUERY PLAN +------------------------------------------------------------------------------------- + Append + -> Nested Loop Left Join + Output: p_1.id, p_1.x, p_1.y, q_1.id, q_1.x, q_1.y, p2_1.id, p2_1.x, p2_1.y + Inner Unique: true + -> Seq Scan on public.uk_p1 p_1 + Output: p_1.id, p_1.x, p_1.y + -> Nested Loop + Output: q_1.id, q_1.x, q_1.y, p2_1.id, p2_1.x, p2_1.y + Inner Unique: true + -> Index Scan using uk_q1_pkey on public.uk_q1 q_1 + Output: q_1.id, q_1.x, q_1.y + Index Cond: (q_1.id = p_1.id) + -> Index Scan using uk_p1_pkey on public.uk_p1 p2_1 + Output: p2_1.id, p2_1.x, p2_1.y + Index Cond: (p2_1.id = q_1.id) + -> Nested Loop Left Join + Output: p_2.id, p_2.x, p_2.y, q_2.id, q_2.x, q_2.y, p2_2.id, p2_2.x, p2_2.y + Inner Unique: true + -> Seq Scan on public.uk_p2 p_2 + Output: p_2.id, p_2.x, p_2.y + -> Nested Loop + Output: q_2.id, q_2.x, q_2.y, p2_2.id, p2_2.x, p2_2.y + Inner Unique: true + -> Index Scan using uk_q2_pkey on public.uk_q2 q_2 + Output: q_2.id, q_2.x, q_2.y + Index Cond: (q_2.id = p_2.id) + -> Index Scan using uk_p2_pkey on public.uk_p2 p2_2 + Output: p2_2.id, p2_2.x, p2_2.y + Index Cond: (p2_2.id = q_2.id) +(29 rows) + +-- but not if its parent is not +explain (verbose, costs off) +select * from uk_p p left join + (uk_q q join uk_r r on q.id = r.id) on p.id = q.id; + QUERY PLAN +---------------------------------------------------------------------------------- + Append + -> Nested Loop Left Join + Output: p_1.id, p_1.x, p_1.y, q_1.id, q_1.x, q_1.y, r_1.id, r_1.x, r_1.y + Join Filter: (p_1.id = q_1.id) + -> Seq Scan on public.uk_p1 p_1 + Output: p_1.id, p_1.x, p_1.y + -> Materialize + Output: q_1.id, q_1.x, q_1.y, r_1.id, r_1.x, r_1.y + -> Nested Loop + Output: q_1.id, q_1.x, q_1.y, r_1.id, r_1.x, r_1.y + Inner Unique: true + -> Seq Scan on public.uk_r1 r_1 + Output: r_1.id, r_1.x, r_1.y + -> Index Scan using uk_q1_pkey on public.uk_q1 q_1 + Output: q_1.id, q_1.x, q_1.y + Index Cond: (q_1.id = r_1.id) + -> Nested Loop Left Join + Output: p_2.id, p_2.x, p_2.y, q_2.id, q_2.x, q_2.y, r_2.id, r_2.x, r_2.y + Join Filter: (p_2.id = q_2.id) + -> Seq Scan on public.uk_p2 p_2 + Output: p_2.id, p_2.x, p_2.y + -> Materialize + Output: q_2.id, q_2.x, q_2.y, r_2.id, r_2.x, r_2.y + -> Nested Loop + Output: q_2.id, q_2.x, q_2.y, r_2.id, r_2.x, r_2.y + Inner Unique: true + -> Seq Scan on public.uk_r2 r_2 + Output: r_2.id, r_2.x, r_2.y + -> Index Scan using uk_q2_pkey on public.uk_q2 q_2 + Output: q_2.id, q_2.x, q_2.y + Index Cond: (q_2.id = r_2.id) +(31 rows) + +reset enable_partitionwise_join; +reset max_parallel_workers_per_gather; +reset enable_hashjoin; +reset enable_mergejoin; +drop table uk_a, uk_b, uk_c, uk_d, uk_n, uk_p, uk_q, uk_r; diff --git a/src/test/regress/sql/uniquekeys.sql b/src/test/regress/sql/uniquekeys.sql index 7465e8ff424..24764f565c3 100644 --- a/src/test/regress/sql/uniquekeys.sql +++ b/src/test/regress/sql/uniquekeys.sql @@ -259,4 +259,44 @@ select distinct id, sum(x) over (partition by y) from uk_a; explain (costs off) select distinct x, sum(x) over (partition by y) from uk_a; -drop table uk_a, uk_b, uk_c, uk_d, uk_n; +-- +-- Test child relations under partitionwise join. +-- + +create table uk_p (id int primary key, x int, y int) partition by range (id); +create table uk_p1 partition of uk_p for values from (0) to (100); +create table uk_p2 partition of uk_p for values from (100) to (200); +create table uk_q (id int primary key, x int, y int) partition by range (id); +create table uk_q1 partition of uk_q for values from (0) to (100); +create table uk_q2 partition of uk_q for values from (100) to (200); +create table uk_r (id int, x int, y int) partition by range (id); +create table uk_r1 partition of uk_r for values from (0) to (100); +create table uk_r2 partition of uk_r for values from (100) to (200); + +-- Disable hash and merge joins, so that the nullable side of a left join is +-- the inner side. +set enable_partitionwise_join = on; +set max_parallel_workers_per_gather = 0; +set enable_hashjoin = off; +set enable_mergejoin = off; + +-- a child relation is unique through its own index +explain (verbose, costs off) +select * from uk_p p join uk_q q on p.id = q.id; + +-- a child join is unique if its parent is +explain (verbose, costs off) +select * from uk_p p left join + (uk_q q join uk_p p2 on q.id = p2.id) on p.id = q.id; + +-- but not if its parent is not +explain (verbose, costs off) +select * from uk_p p left join + (uk_q q join uk_r r on q.id = r.id) on p.id = q.id; + +reset enable_partitionwise_join; +reset max_parallel_workers_per_gather; +reset enable_hashjoin; +reset enable_mergejoin; + +drop table uk_a, uk_b, uk_c, uk_d, uk_n, uk_p, uk_q, uk_r; -- 2.53.0