From 32f5a2eafb8223df67f4f2f762e2fb8b710a3877 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 04/30] Allow starjoin dimensions in the syntactic LHS of
 special joins

A dimension was rejected if it was in any relid set of any
SpecialJoinInfo, including the syntactic ones.  So a LEFT JOIN or a
semijoin written after the star joins disabled the optimization, even if
it referenced only the fact, because the dimensions were in its
syntactic LHS.

Only the minimal sets restrict the join order, so check just those.  A
dimension in the syntactic RHS of a special join is always in a minimal
set of some special join, so it's still rejected.

Update STARJOINS.md, removing the open issue.  The starjoin_other checks
showing that issue now pass.
---
 STARJOINS.md                                 | 14 +++++++-------
 src/backend/optimizer/plan/initsplan.c       | 16 ++++++++++------
 src/include/nodes/pathnodes.h                |  6 +++---
 src/test/regress/expected/starjoin_other.out | 20 ++++++++++----------
 src/test/regress/sql/starjoin_other.sql      | 16 ++++++++--------
 5 files changed, 38 insertions(+), 34 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index 244d9ce34e7..9d8aea0fe4e 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -174,7 +174,13 @@ The fact has to be a base relation too.
    So the dimension can be joined only to the fact (or to joinrels
    including it), which makes the dimensions interchangeable.
 
-5. The dimension does not participate in any `SpecialJoinInfo`.
+5. The dimension is not in the minimal LHS or RHS of any
+   `SpecialJoinInfo` (it may be in the syntactic LHS, e.g. of a LEFT
+   JOIN written after it).
+
+   Only the minimal sets restrict the join order.  A dimension in the
+   syntactic RHS of a special join is always in a minimal set of some
+   special join, so it's still rejected.
 
 6. The dimension has no lateral references in or out.
 
@@ -239,12 +245,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.
-- 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
-  disables the optimization, even if it references only the fact, while
-  writing the LEFT JOIN first works (the left_join_last_pruned and
-  exists_pruned checks in starjoin_other).
 - The conditions check the join clauses of a dimension to other rels,
   but not additional join clauses to the fact (not matching the foreign
   key).  Such a clause makes the join selective, so the dimension should
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index 07776c13e02..f782d0bc166 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -4130,7 +4130,8 @@ check_memoizable(RestrictInfo *restrictinfo)
  *	   the fact (checked via have_relevant_joinclause, which also covers
  *	   equivalence-class joins).
  *
- *	5. The dimension does not participate in any SpecialJoinInfo.
+ *	5. The dimension is not in the minimal LHS or RHS of any SpecialJoinInfo
+ *	   (it may be in the syntactic LHS, e.g. of a LEFT JOIN written after it).
  *
  *	6. The dimension has no lateral references in or out.
  *
@@ -4292,8 +4293,13 @@ generate_starjoin_clusters(PlannerInfo *root)
 				continue;
 
 			/*
-			 * Should not be involved in any SpecialJoinInfo (for now the
-			 * starjoin is done only for inner joins).
+			 * Should not be constrained by any SpecialJoinInfo (for now the
+			 * starjoin is done only for inner joins).  Only the minimal sets
+			 * restrict the join order, so a dimension may be in the syntactic
+			 * LHS (e.g. of a LEFT JOIN written after the star joins).  A
+			 * dimension in the syntactic RHS of an outer join or semijoin is
+			 * always in one of the minimal sets of the lowest such join, or
+			 * of a join below it.
 			 *
 			 * XXX We could relax this, and allow left joins, as long as the
 			 * target has a unique constraint.
@@ -4303,9 +4309,7 @@ generate_starjoin_clusters(PlannerInfo *root)
 				SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(lc3);
 
 				if (bms_is_member(d, sjinfo->min_lefthand) ||
-					bms_is_member(d, sjinfo->min_righthand) ||
-					bms_is_member(d, sjinfo->syn_lefthand) ||
-					bms_is_member(d, sjinfo->syn_righthand))
+					bms_is_member(d, sjinfo->min_righthand))
 				{
 					ok = false;
 					break;
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
index 99aeb253c54..01af5467c79 100644
--- a/src/include/nodes/pathnodes.h
+++ b/src/include/nodes/pathnodes.h
@@ -1520,9 +1520,9 @@ typedef struct ForeignKeyOptInfo
  * each dimension must be joined to the fact via an equality condition that
  * matches a foreign key on the fact referencing the dimension. The columns
  * on the fact must all be NOT NULL, and the dimension must have no other
- * join clauses, no local restriction quals, no participation in special
- * joins or lateral references, etc. (see generate_starjoin_clusters for
- * the full list of preconditions).
+ * join clauses, no local restriction quals, no join order restrictions from
+ * special joins, no lateral references, etc. (see generate_starjoin_clusters
+ * for the full list of preconditions).
  *
  * When all of these conditions hold, every join ordering that has the fact
  * as the driver produces the same row counts.  The costs may still differ
diff --git a/src/test/regress/expected/starjoin_other.out b/src/test/regress/expected/starjoin_other.out
index b5d9823ea75..27b4eb7c935 100644
--- a/src/test/regress/expected/starjoin_other.out
+++ b/src/test/regress/expected/starjoin_other.out
@@ -262,12 +262,12 @@ RESET join_collapse_limit;
 --
 -- Outer joins and semijoins
 --
--- Dimensions must not be part of any SpecialJoinInfo, but the check considers
--- the syntactic relids (syn_lefthand/syn_righthand), not only the minimal
--- ones.  So a LEFT JOIN or a semijoin (EXISTS) placed after the star joins
--- disables the optimization, even if it references only the fact.  Writing
--- the LEFT JOIN first works.  Each query has 8 rels (a single subproblem), and
--- with the optimization the join search needs much less memory.
+-- Dimensions must not be constrained by any SpecialJoinInfo, but only the
+-- minimal relids (min_lefthand/min_righthand) matter.  So a LEFT JOIN or a
+-- semijoin (EXISTS) referencing only the fact does not disable the
+-- optimization, even if written after the star joins, with the dimensions in
+-- its syntactic LHS.  Each query has 8 rels (a single subproblem), and with
+-- the optimization the join search needs much less memory.
 --
 SELECT $$
 SELECT count(a.v) FROM starjoin_other_f AS f
@@ -306,20 +306,20 @@ SELECT starjoin_other_memory(:'q_left_join_first', true)
  t
 (1 row)
 
--- XXX no pruning with the LEFT JOIN at the end
+-- pruning with the LEFT JOIN at the end
 SELECT starjoin_other_memory(:'q_left_join_last', true)
        < starjoin_other_memory(:'q_left_join_last', false) * 0.5 AS left_join_last_pruned;
  left_join_last_pruned 
 -----------------------
- f
+ t
 (1 row)
 
--- XXX no pruning with a semijoin
+-- pruning with a semijoin
 SELECT starjoin_other_memory(:'q_exists', true)
        < starjoin_other_memory(:'q_exists', false) * 0.5 AS exists_pruned;
  exists_pruned 
 ---------------
- f
+ t
 (1 row)
 
 --
diff --git a/src/test/regress/sql/starjoin_other.sql b/src/test/regress/sql/starjoin_other.sql
index 94280fa9313..41875fec76a 100644
--- a/src/test/regress/sql/starjoin_other.sql
+++ b/src/test/regress/sql/starjoin_other.sql
@@ -272,12 +272,12 @@ RESET join_collapse_limit;
 --
 -- Outer joins and semijoins
 --
--- Dimensions must not be part of any SpecialJoinInfo, but the check considers
--- the syntactic relids (syn_lefthand/syn_righthand), not only the minimal
--- ones.  So a LEFT JOIN or a semijoin (EXISTS) placed after the star joins
--- disables the optimization, even if it references only the fact.  Writing
--- the LEFT JOIN first works.  Each query has 8 rels (a single subproblem), and
--- with the optimization the join search needs much less memory.
+-- Dimensions must not be constrained by any SpecialJoinInfo, but only the
+-- minimal relids (min_lefthand/min_righthand) matter.  So a LEFT JOIN or a
+-- semijoin (EXISTS) referencing only the fact does not disable the
+-- optimization, even if written after the star joins, with the dimensions in
+-- its syntactic LHS.  Each query has 8 rels (a single subproblem), and with
+-- the optimization the join search needs much less memory.
 --
 SELECT $$
 SELECT count(a.v) FROM starjoin_other_f AS f
@@ -314,10 +314,10 @@ $$ AS q_exists \gset
 
 SELECT starjoin_other_memory(:'q_left_join_first', true)
        < starjoin_other_memory(:'q_left_join_first', false) * 0.5 AS left_join_first_pruned;
--- XXX no pruning with the LEFT JOIN at the end
+-- pruning with the LEFT JOIN at the end
 SELECT starjoin_other_memory(:'q_left_join_last', true)
        < starjoin_other_memory(:'q_left_join_last', false) * 0.5 AS left_join_last_pruned;
--- XXX no pruning with a semijoin
+-- pruning with a semijoin
 SELECT starjoin_other_memory(:'q_exists', true)
        < starjoin_other_memory(:'q_exists', false) * 0.5 AS exists_pruned;
 
-- 
2.56.1

