From e1190c0d599faa50db835cff14a91654ccc6fe71 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 08/30] Don't join starjoin dimensions without their fact

Within a join search subproblem containing the fact of a starjoin
cluster, reject joinrels containing a cluster dimension but not the
fact.  The dimensions have join clauses only to the fact, so such
joinrels are cartesian products, built only by clauseless joins or by
the last-ditch pass of join_search_one_level().  They are not needed, as
the dimensions can always be joined to the joinrels with the fact, in
the canonical order.  Subproblems without the fact are not restricted,
as joining the dimensions without the fact may be the only option there.

This prepares for ignoring pseudoconstant quals when building the
clusters.  Such quals make all rels joinable, so the standard join
search would otherwise consider all subsets of the dimensions without
the fact.

Update STARJOINS.md, and the comments in starjoin_reproducers.
---
 STARJOINS.md                                  | 46 ++++++++++-------
 src/backend/optimizer/path/joinrels.c         | 49 +++++++++++--------
 .../regress/expected/starjoin_reproducers.out |  8 +--
 src/test/regress/sql/starjoin_reproducers.sql |  8 +--
 4 files changed, 67 insertions(+), 44 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index 322782b56b4..b4d29602783 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -92,16 +92,15 @@ d), instead of 2^d.
 
 ### Joinrels without the fact
 
-Joins without the fact are not restricted, even in subproblems including
-the fact.  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.  Such joins with
-dimensions on both sides are rare, but possible: if the other rels have
-join clauses usable only at a later level, two such joinrels (each with
-a different dimension) may get joined.  The canonical order is not
-enforced for them, for the same reason as in subproblems without the
-fact (see below).
+In a subproblem containing the fact of a cluster, joinrels with a
+dimension of the cluster but without the fact are cartesian products, as
+the dimensions have join clauses only to the fact (and no join order
+restrictions).  `join_search_one_level` builds them when the other rel
+has no join clauses, or in the last-ditch pass, when no other join is
+possible at that level.  They may get joined further, and so the search
+might consider many subsets of the dimensions without the fact.  So such
+joinrels are rejected.  They are not needed, as the dimensions can
+always be joined to the joinrels with the fact, in the canonical order.
 
 ### Join search subproblems
 
@@ -120,9 +119,12 @@ joins of dimensions to the fact not changing the cardinality of the
 fact.  Without the fact, the dimensions can only be joined by cartesian
 products (they have join clauses only to the fact), which do change the
 cardinality, so the order matters, and enforcing it might eliminate the
-cheapest plan.  The product of the dimensions is then joined to the fact
-in an upper subproblem, where the dimensions are not initial rels, and
-so are not checked.
+cheapest plan.  Nor can the search forbid joining the dimensions without
+the fact in such subproblems, as that may be the only way to join them
+(e.g. "d1 CROSS JOIN d2" with `join_collapse_limit` set to 1).  The
+product of the dimensions is then joined to the fact in an upper
+subproblem, where the dimensions are not initial rels, and so are not
+checked.
 
 `starjoin_order_invalid` walks the initial rels of the current
 subproblem on each call, to find the base rels of the subproblem, and
@@ -133,8 +135,11 @@ whether it contains the fact of each cluster.
 The dimensions are interchangeable, as they have join clauses only to
 the fact, and no join order restrictions.  So for each pruned joinrel
 with the fact, the search builds the joinrel with the dimensions
-replaced by the first ones in the canonical order.  The pruning
-therefore can't make the join search fail.
+replaced by the first ones in the canonical order.  A pruned joinrel
+without the fact is a cartesian product of the dimensions and other
+rels, and the search builds it without the dimensions, which can then be
+joined to the joinrels with the fact.  The pruning therefore can't make
+the join search fail.
 
 ### GEQO
 
@@ -283,8 +288,15 @@ 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.
-- The search might forbid additional joins, e.g. joinrels containing a
-  dimension of a cluster but not its fact.
+- A pseudoconstant qual (e.g. "now() > '2000-01-01'", or an uncorrelated
+  EXISTS) gets the relids of the whole join domain, so it's in the
+  joininfo list of every rel, and every dimension appears to have join
+  clauses to rels other than the fact.  So a single such qual disables
+  all starjoin clusters in the query, even if it's in the ON clause of a
+  single dimension's join.  Ignoring pseudoconstant quals when building
+  the clusters would be enough to fix this.  The quals would still make
+  the dimensions joinable to each other without the fact, but such
+  joinrels are restricted (see `starjoin_order_invalid`).
 
 ## Regression tests
 
diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c
index 7abfb88aef2..b40333d6943 100644
--- a/src/backend/optimizer/path/joinrels.c
+++ b/src/backend/optimizer/path/joinrels.c
@@ -2180,7 +2180,8 @@ get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
  * For every star cluster in root->starjoin_clusters, we enforce the canonical
  * 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.
+ * the starjoin clusters.  Joinrels containing a cluster dimension but not
+ * the fact are not allowed at all.
  *
  * Clusters are computed for the whole query, but the join search may be split
  * into multiple subproblems (join_collapse_limit, from_collapse_limit, FULL
@@ -2193,12 +2194,14 @@ get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
  * all.  Clusters with the fact outside the current subproblem are ignored.
  *
  * 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.
+ * other orderings are pruned.  Other rels may be joined at any point.  The
+ * dimensions are interchangeable (they have join clauses only to the fact,
+ * and no join order restrictions), so for each pruned joinrel with the fact
+ * the search builds the joinrel with the dimensions replaced by the first
+ * ones in the canonical order.  A pruned joinrel without the fact is a
+ * cartesian product of the dimensions and other rels, and the search builds
+ * it without the dimensions, which can then be joined to the joinrels with
+ * the fact.  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
@@ -2206,9 +2209,6 @@ get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
  * 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.
  */
 static bool
 starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
@@ -2252,9 +2252,12 @@ starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 		 * cardinality of the fact.  Without the fact, the dimensions can only
 		 * be joined by cartesian products (they have join clauses only to the
 		 * fact), which do change the cardinality, so the order matters and
-		 * enforcing it might eliminate the cheapest plan.  The joinrel is
-		 * joined to the fact in an upper subproblem, where the dimensions are
-		 * not initial rels, and so are not checked.
+		 * enforcing it might eliminate the cheapest plan.  Nor can we forbid
+		 * joining the dimensions without the fact, as that may be the only
+		 * way to join them (e.g. "d1 CROSS JOIN d2" with join_collapse_limit
+		 * set to 1).  The joinrel is joined to the fact in an upper
+		 * subproblem, where the dimensions are not initial rels, and so are
+		 * not checked.
 		 *
 		 * Snowflake joins are not an exception: a dimension with its own
 		 * dimensions is not a member of this cluster, but it may be the fact
@@ -2267,18 +2270,22 @@ starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 		cluster_dims = bms_intersect(c->dim_relids, baserel_relids);
 
 		/*
-		 * Joins without the fact are not restricted either, for the same
-		 * 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.
+		 * With the fact in this subproblem, joinrels with a dimension but
+		 * without the fact are not needed, as the dimensions can be joined to
+		 * the joinrels with the fact (in the canonical order).  The
+		 * dimensions have join clauses only to the fact (and no join order
+		 * restrictions), so such joinrels are cartesian products, built only
+		 * when the other rel has no join clauses, or by the last-ditch pass
+		 * of join_search_one_level.  But they may get joined further, and so
+		 * the search might consider many subsets of the dimensions without
+		 * the fact.
 		 */
 		if (!bms_is_member(c->fact_relid, join_relids))
 		{
+			violates = bms_overlap(cluster_dims, join_relids);
 			bms_free(cluster_dims);
+			if (violates)
+				break;
 			continue;
 		}
 
diff --git a/src/test/regress/expected/starjoin_reproducers.out b/src/test/regress/expected/starjoin_reproducers.out
index 9fd7a429649..4968c290776 100644
--- a/src/test/regress/expected/starjoin_reproducers.out
+++ b/src/test/regress/expected/starjoin_reproducers.out
@@ -10,8 +10,9 @@
 -- subproblem can build, otherwise the planning fails with "ERROR:  failed to
 -- build any N-way joins".
 --
--- In query J, the last-ditch pass joins dimensions to rels without the fact,
--- and two such rels then get joined.
+-- In query J, the last-ditch pass would join dimensions to rels without the
+-- fact (and two such rels would then get joined), but such joinrels are not
+-- allowed, and the join search must not fail without them.
 --
 -- For each query, show the plan with the GUC enabled, and then check that
 -- the query returns the same result with the GUC enabled and disabled.
@@ -562,7 +563,8 @@ RESET join_collapse_limit;
 -- star (the fact has a join clause only to w, outside the subquery) and two
 -- outer joins of three rels each, linked by a non-strict clause between their
 -- nullable sides.  Nothing can be joined at level 4, so the last-ditch pass
--- joins the outer joins to dimensions, and the clause then joins those rels.
+-- joins the outer joins to other rels, but not to the dimensions, as such
+-- joinrels would not include the fact.
 SET from_collapse_limit = 9;
 EXPLAIN (COSTS OFF)
 SELECT count(*)
diff --git a/src/test/regress/sql/starjoin_reproducers.sql b/src/test/regress/sql/starjoin_reproducers.sql
index a833c10fcbb..7f53084cbfb 100644
--- a/src/test/regress/sql/starjoin_reproducers.sql
+++ b/src/test/regress/sql/starjoin_reproducers.sql
@@ -10,8 +10,9 @@
 -- subproblem can build, otherwise the planning fails with "ERROR:  failed to
 -- build any N-way joins".
 --
--- In query J, the last-ditch pass joins dimensions to rels without the fact,
--- and two such rels then get joined.
+-- In query J, the last-ditch pass would join dimensions to rels without the
+-- fact (and two such rels would then get joined), but such joinrels are not
+-- allowed, and the join search must not fail without them.
 --
 -- For each query, show the plan with the GUC enabled, and then check that
 -- the query returns the same result with the GUC enabled and disabled.
@@ -277,7 +278,8 @@ RESET join_collapse_limit;
 -- star (the fact has a join clause only to w, outside the subquery) and two
 -- outer joins of three rels each, linked by a non-strict clause between their
 -- nullable sides.  Nothing can be joined at level 4, so the last-ditch pass
--- joins the outer joins to dimensions, and the clause then joins those rels.
+-- joins the outer joins to other rels, but not to the dimensions, as such
+-- joinrels would not include the fact.
 SET from_collapse_limit = 9;
 EXPLAIN (COSTS OFF)
 SELECT count(*)
-- 
2.56.1

