From c8b86ffee37b55e7059fddf83b5f8a3091631f6a Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas@vondra.me>
Date: Wed, 25 Nov 2026 13:47:05 +0100
Subject: [PATCH v7 03/30] Make starjoin pruning canonical

starjoin_order_invalid() skipped joins with cluster dimensions on only
one side, so the join search still built joins of the fact with any
dimension, not just the first one.  Check all joins with the fact, so
the fact gets joined to the dimensions only in the canonical order.  The
dimensions are interchangeable, so this can't make the join search fail.

This applies to GEQO too, which now joins the dimensions in the
canonical order, whatever their order in the tour.

Update STARJOINS.md, removing the open issue about the join search
building 19 joinrels for a star with 10 dimensions, instead of the 10 in
the canonical order.  The starjoin_other check showing that issue now
passes.
---
 STARJOINS.md                                 | 40 +++++++---------
 src/backend/optimizer/path/joinrels.c        | 50 +++++++++-----------
 src/test/regress/expected/starjoin_other.out | 16 +++----
 src/test/regress/sql/starjoin_other.sql      | 14 +++---
 4 files changed, 53 insertions(+), 67 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index 9af5dd47069..244d9ce34e7 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -75,17 +75,18 @@ fact of another cluster.
 The canonical order of a cluster joins the dimensions in relid order,
 which is usually the order in which they appear in the query.
 
-`starjoin_order_invalid` restricts the joins where both inputs contain
-dimensions of a cluster, and the result contains the fact.  The
-dimensions in such a joinrel have to be a prefix of the canonical order,
-so there must be no dimension after the first missing one.  It's enough
-to check these joins, as otherwise all the dimensions come from one
-input, which was checked when it was built.  So joinrels with the fact
-and a single dimension are not restricted, and both "(f JOIN d1) JOIN
-d2" and "(f JOIN d2) JOIN d1" are considered.  For each set of the other
-rels, the search builds a joinrel with the fact and each of the d
-dimensions, and then the longer prefixes of the canonical order, so
-about 2d joinrels with the fact instead of 2^d.
+`starjoin_order_invalid` enforces the canonical decomposition of any
+joinrel containing the fact: the cluster dimensions in the joinrel must
+form a prefix of the canonical order.  This applies even if only one
+side of the join has dimensions, so that the fact gets joined to the
+dimensions in the canonical order, and not e.g. to the second dimension
+first.  A missing dimension is fine on its own, as the joinrel may not
+include all the dimensions yet.  It's a violation only if the joinrel
+includes a dimension after it, i.e. if the first missing dimension
+precedes the last dimension in the joinrel.  Other rels may be joined at
+any point.  For each set of the other rels, the search then builds only
+d+1 joinrels with the fact (with the first k dimensions, for k from 0 to
+d), instead of 2^d.
 
 ### Joinrels without the fact
 
@@ -136,11 +137,12 @@ therefore can't make the join search fail.
 ### GEQO
 
 GEQO is subject to the same restriction, as it builds the joinrels using
-`make_join_rel` too.  It joins a dimension only to the clump with the
-fact (the only desirable join), and a dimension that can't be joined yet
-remains a separate clump.  GEQO tries to merge it again whenever the
-clump with the fact grows, and at the end it force-joins the remaining
-clumps, so it still finds a plan.
+`make_join_rel` too.  So it joins the dimensions in the canonical order,
+whatever their order in the tour.  It joins a dimension only to the
+clump with the fact (the only desirable join), and a dimension that
+can't be joined yet remains a separate clump.  GEQO tries to merge it
+again whenever the clump with the fact grows, and at the end it
+force-joins the remaining clumps, so it still finds a plan.
 
 ## Conditions for a dimension
 
@@ -237,12 +239,6 @@ the plans, the planning cost, or the regression tests.
   (see starjoin_eager, and the eager_first_partial_agg_on check in
   starjoin_other).  A possible direction, not tried yet, is an explicit
   canonical order putting the dimensions with grouping columns last.
-- Joins where one side has no cluster dimensions are not checked, so the
-  join search still builds the joinrels of the fact with each single
-  dimension, and e.g. "(f JOIN d2) JOIN d1" passes the check too.  For a
-  star with 10 dimensions, the search builds 19 joinrels instead of the
-  10 in the canonical order, and uses more memory than planning the
-  canonical order (the flat_search_canonical check in starjoin_other).
 - A dimension in any relid set of a `SpecialJoinInfo` is rejected,
   including the syntactic ones (`syn_lefthand` and `syn_righthand`).  So
   a LEFT JOIN or a semijoin (EXISTS) placed after the star joins
diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c
index b72413c43a9..7abfb88aef2 100644
--- a/src/backend/optimizer/path/joinrels.c
+++ b/src/backend/optimizer/path/joinrels.c
@@ -2178,9 +2178,9 @@ get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
  *		non-canonical ordering of a starjoin cluster.
  *
  * For every star cluster in root->starjoin_clusters, we enforce the canonical
- * decomposition of any joinrel containing the fact and at least one cluster
- * dimension - the dimensions must form a prefix of the order determined when
- * collecting the starjoin clusters.
+ * decomposition of any joinrel containing the fact - the cluster dimensions
+ * in the joinrel must form a prefix of the order determined when collecting
+ * the starjoin clusters.
  *
  * Clusters are computed for the whole query, but the join search may be split
  * into multiple subproblems (join_collapse_limit, from_collapse_limit, FULL
@@ -2192,15 +2192,20 @@ get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
  * like any other relation.  Otherwise there might be no valid join order at
  * all.  Clusters with the fact outside the current subproblem are ignored.
  *
- * This rule preserves exactly one decomposition per joinrel relid set, so
- * each canonical joinrel is still built; redundant orderings are pruned.
+ * So the fact gets joined to the dimensions in the canonical order, and the
+ * other orderings are pruned.  Other rels may be joined at any point, and
+ * joins without the fact are not restricted.  The dimensions are
+ * interchangeable (they have join clauses only to the fact, and no join order
+ * restrictions), so for each pruned joinrel the search builds the joinrel
+ * with the dimensions replaced by the first ones in the canonical order.  So
+ * the pruning can't make the join search fail.
  *
  * GEQO is subject to the same check.  It joins a dimension only to the clump
  * with the fact (the only desirable join), and a dimension that can't be
  * joined yet remains a separate clump.  But whenever the clump with the fact
- * grows, GEQO tries to merge it with the other clumps again, and at the end
- * it force-joins the remaining clumps (joins without the fact are not
- * restricted), so it still finds a plan.
+ * grows, GEQO tries to merge it with the other clumps again, so the
+ * dimensions get joined in the canonical order, whatever their order in the
+ * tour.
  *
  * We might forbid additional joins, for example we might forbid joinrels
  * containing a cluster dimension without the fact.
@@ -2261,27 +2266,14 @@ starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 		/* dimensions joined individually in this subproblem */
 		cluster_dims = bms_intersect(c->dim_relids, baserel_relids);
 
-		/*
-		 * If all dimensions are on one side of the join, we've already
-		 * checked the canonical order earlier, when building it. No need to
-		 * redo the check again.
-		 */
-		if (!bms_overlap(rel1->relids, cluster_dims) ||
-			!bms_overlap(rel2->relids, cluster_dims))
-		{
-			bms_free(cluster_dims);
-			continue;
-		}
-
 		/*
 		 * Joins without the fact are not restricted either, for the same
-		 * reason as above.  Such joins with dimensions on both sides are
-		 * rare, but possible.  The dimensions have join clauses only to the
-		 * fact (and no join order restrictions), so join_search_one_level
-		 * joins them to rels without the fact only by cartesian products,
-		 * e.g. in the last-ditch pass, when no other join is possible at that
-		 * level.  But the other rel may have join clauses usable only at a
-		 * later level, and so two such joinrels (each with a different
+		 * reason as above.  The dimensions have join clauses only to the fact
+		 * (and no join order restrictions), so join_search_one_level joins
+		 * them to rels without the fact only by cartesian products, e.g. in
+		 * the last-ditch pass, when no other join is possible at that level.
+		 * But the other rel may have join clauses usable only at a later
+		 * level, and so even two such joinrels (each with a different
 		 * dimension) may get joined.
 		 */
 		if (!bms_is_member(c->fact_relid, join_relids))
@@ -2293,7 +2285,9 @@ starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 		/*
 		 * The dimensions in the join have to be a prefix of the cluster
 		 * dimensions (in relid order), i.e. there must be no dimension after
-		 * the first gap.
+		 * the first gap.  This applies even if only one side of the join has
+		 * dimensions, so that the fact gets joined to the dimensions in this
+		 * order, and not e.g. to the second dimension first.
 		 *
 		 * XXX Using the relid order is fine for now, but if we choose to
 		 * allow snowflake joins, we'll need an explicit (ordered) list.
diff --git a/src/test/regress/expected/starjoin_other.out b/src/test/regress/expected/starjoin_other.out
index f6fe849430b..b5d9823ea75 100644
--- a/src/test/regress/expected/starjoin_other.out
+++ b/src/test/regress/expected/starjoin_other.out
@@ -222,12 +222,10 @@ RESET work_mem;
 --
 -- Canonical pruning
 --
--- Joins where one side has no cluster dimensions are not checked, so the
--- join search still builds the 2-way joins of the fact with each dimension,
--- and (fact, d2) + d1 passes the check too.  For the star with 10 dimensions
--- the search builds 19 join rels instead of the 10 in the canonical order.
--- Compare to planning the canonical order directly, forced by
--- join_collapse_limit = 1 (the plans are the same).
+-- Joins of the fact are checked even if only one side has dimensions, so
+-- for the star with 10 dimensions the join search builds only the 10 join
+-- rels in the canonical order.  Compare to planning the canonical order
+-- directly, forced by join_collapse_limit = 1 (the plans are the same).
 --
 SELECT $$
 SELECT count(*) FROM starjoin_other_f AS f
@@ -253,11 +251,11 @@ SELECT starjoin_other_plan(:'q_star', true) = :'plan_canonical' AS flat_plan_can
  t
 (1 row)
 
--- XXX the search uses more memory than planning the canonical order
+-- the search uses about as much memory as planning the canonical order
 SELECT starjoin_other_memory(:'q_star', true) <= :memory_canonical * 1.1 AS flat_search_canonical;
  flat_search_canonical 
 -----------------------
- f
+ t
 (1 row)
 
 RESET join_collapse_limit;
@@ -358,7 +356,7 @@ RESET join_collapse_limit;
 --
 -- If the query has a cluster, the canonical order is checked for each join in
 -- all subproblems, including a large separate subproblem (8 rels joinable in
--- any order).  With only two dimensions there's nothing to prune in the star,
+-- any order).  With only two dimensions there's little to prune in the star,
 -- so the planning should not use more memory with the GUC enabled (e.g. if
 -- the check leaked memory).
 --
diff --git a/src/test/regress/sql/starjoin_other.sql b/src/test/regress/sql/starjoin_other.sql
index 2eed24ab491..94280fa9313 100644
--- a/src/test/regress/sql/starjoin_other.sql
+++ b/src/test/regress/sql/starjoin_other.sql
@@ -239,12 +239,10 @@ RESET work_mem;
 --
 -- Canonical pruning
 --
--- Joins where one side has no cluster dimensions are not checked, so the
--- join search still builds the 2-way joins of the fact with each dimension,
--- and (fact, d2) + d1 passes the check too.  For the star with 10 dimensions
--- the search builds 19 join rels instead of the 10 in the canonical order.
--- Compare to planning the canonical order directly, forced by
--- join_collapse_limit = 1 (the plans are the same).
+-- Joins of the fact are checked even if only one side has dimensions, so
+-- for the star with 10 dimensions the join search builds only the 10 join
+-- rels in the canonical order.  Compare to planning the canonical order
+-- directly, forced by join_collapse_limit = 1 (the plans are the same).
 --
 SELECT $$
 SELECT count(*) FROM starjoin_other_f AS f
@@ -267,7 +265,7 @@ SELECT starjoin_other_plan(:'q_star', true) AS plan_canonical,
 -- a single subproblem with 11 rels (below geqo_threshold)
 SET join_collapse_limit = 20;
 SELECT starjoin_other_plan(:'q_star', true) = :'plan_canonical' AS flat_plan_canonical;
--- XXX the search uses more memory than planning the canonical order
+-- the search uses about as much memory as planning the canonical order
 SELECT starjoin_other_memory(:'q_star', true) <= :memory_canonical * 1.1 AS flat_search_canonical;
 RESET join_collapse_limit;
 
@@ -349,7 +347,7 @@ RESET join_collapse_limit;
 --
 -- If the query has a cluster, the canonical order is checked for each join in
 -- all subproblems, including a large separate subproblem (8 rels joinable in
--- any order).  With only two dimensions there's nothing to prune in the star,
+-- any order).  With only two dimensions there's little to prune in the star,
 -- so the planning should not use more memory with the GUC enabled (e.g. if
 -- the check leaked memory).
 --
-- 
2.56.1

