From 05f4868745b068fb7ed9aaa53974f7ea272d27de 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 13/30] GEQO: Don't enforce the canonical starjoin order in
 GEQO

GEQO evaluates a fixed number of join orders, each joining all the
initial rels, so pruning the non-canonical orders does not make it any
cheaper.  It only adds the overhead of the checks, and makes tours with
the dimensions in different orders build the same plan.

Stars with many dimensions are planned by the standard join search now,
as the dimensions count as fewer rels toward geqo_threshold.  So enforce
the canonical order, and the restrictions on joinrels with dimensions
but without the fact, only in the standard join search.

Update STARJOINS.md, the documentation of enable_starjoin_ordering, and
the starjoin_geqo checks.  Remove the starjoin_other checks that GEQO
can join a selective dimension first, as they would pass even if the
dimension stayed in the cluster.
---
 STARJOINS.md                                 | 23 +++++++++----------
 doc/src/sgml/config.sgml                     |  3 ++-
 src/backend/optimizer/path/joinrels.c        | 17 +++++++++-----
 src/test/regress/expected/starjoin_geqo.out  | 15 ++++++------
 src/test/regress/expected/starjoin_other.out | 24 --------------------
 src/test/regress/sql/starjoin_geqo.sql       | 13 ++++++-----
 src/test/regress/sql/starjoin_other.sql      | 10 --------
 7 files changed, 39 insertions(+), 66 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index a1cfca87b96..84f68139c0e 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -225,13 +225,15 @@ the join search fail.
 
 ### GEQO
 
-GEQO is subject to the same restriction, as it builds the joinrels using
-`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.
+Only the standard join search is restricted, not GEQO.  GEQO evaluates a
+fixed number of join orders (tours), each joining all the initial rels,
+so the restriction would not make it any cheaper.  It would only add the
+overhead of the checks, and make tours with the dimensions in different
+orders build the same plan.  For a query joining 5 facts with 10
+dimensions each, GEQO planning took 210ms when enforcing the canonical
+order and the restrictions on joinrels without the fact, and 151ms
+without them.  `starjoin_order_invalid` recognizes the standard join
+search by the `join_rel_level` field of `PlannerInfo` being set.
 
 The standard join search joins the dimensions of a cluster to the fact
 only in the canonical order, so it considers only d+1 of the 2^d subsets
@@ -409,9 +411,6 @@ the plans, the planning cost, or the regression tests.
   the multiplied rows.  The clauseless_first_cost_higher check in
   starjoin_other shows this, for a star with two scans of a 2-row table
   without join clauses.
-- GEQO evaluates a fixed number of join orders, so enforcing the
-  canonical order in GEQO only adds overhead, and the GUC changes the
-  GEQO plans (the geqo_plan_unaffected check in starjoin_geqo).
 - `join_search_hook` gets the number of initial rels without the
   discount, so an extension implementing its own join search has to call
   `starjoin_geqo_levels` to make the same choice between the standard
@@ -449,8 +448,8 @@ the plans, the planning cost, or the regression tests.
   the GUC enabled and disabled.  The cases include extra join clauses
   between the fact and a dimension, OR clauses, dimensions of very
   different widths, outer joins and semijoins, stars split into
-  subproblems, GEQO, eager aggregation, and rels without join clauses.
-  Each check is a single boolean, so that the output does not depend on
+  subproblems, eager aggregation, and rels without join clauses.  Each
+  check is a single boolean, so that the output does not depend on
   platform-specific costs or memory usage, and some checks show the
   known limitations.
 - starjoin_sample checks that the GUC does not change the plans starting
diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 6f2864fc36b..837f3acceeb 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -6330,7 +6330,8 @@ ANY <replaceable class="parameter">num_sync</replaceable> ( <replaceable class="
         same number of rows, and the planner considers only one of them.
         This can significantly reduce planning time for queries joining many
         dimension tables, but may occasionally miss a cheaper join order.
-        When comparing the number of <literal>FROM</literal> items to
+        The genetic query optimizer does not use this optimization, but when
+        comparing the number of <literal>FROM</literal> items to
         <xref linkend="guc-geqo-threshold"/>, <replaceable>N</replaceable>
         such dimension tables of a fact table, appearing as separate items in
         the same <literal>FROM</literal> list as the fact table, count as
diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c
index 53c0163af97..55029d1bbcb 100644
--- a/src/backend/optimizer/path/joinrels.c
+++ b/src/backend/optimizer/path/joinrels.c
@@ -2276,12 +2276,13 @@ starjoin_setup_subproblem(PlannerInfo *root)
  * 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
- * joined yet remains a separate clump.  But whenever the clump with the fact
- * 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.
+ * Only the standard join search is pruned, not GEQO.  GEQO evaluates a fixed
+ * number of join orders (tours), each joining all the initial rels, so the
+ * pruning would not make it any cheaper.  It would only add the overhead of
+ * the checks, and make tours with the dimensions in different orders build
+ * the same plan.  Instead, the dimensions count as fewer rels toward
+ * geqo_threshold (see starjoin_geqo_levels), so that stars get planned by
+ * the standard join search.
  */
 static bool
 starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
@@ -2294,6 +2295,10 @@ starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 	if (root->starjoin_clusters == NIL)
 		return false;
 
+	/* not in the standard join search (e.g. GEQO), see above */
+	if (root->join_rel_level == NULL)
+		return false;
+
 	/* relids of the result */
 	join_relids = bms_union(rel1->relids, rel2->relids);
 
diff --git a/src/test/regress/expected/starjoin_geqo.out b/src/test/regress/expected/starjoin_geqo.out
index ce3ee61a8e8..c06487a709f 100644
--- a/src/test/regress/expected/starjoin_geqo.out
+++ b/src/test/regress/expected/starjoin_geqo.out
@@ -6,9 +6,10 @@
 -- d dimensions.  So the dimensions count as only ceil(log2(d+1)) rels toward
 -- geqo_threshold, and a star gets planned by the standard join search even
 -- if it has more rels than geqo_threshold.  With enough other rels GEQO is
--- still used, and the pruning does not make it any cheaper, because GEQO
--- evaluates a fixed number of join orders, each joining all the rels.
--- Checking the canonical order only adds overhead.
+-- still used, but it's not pruned.  The pruning would not make it any
+-- cheaper, because GEQO evaluates a fixed number of join orders, each
+-- joining all the rels, so checking the canonical order would only add
+-- overhead.
 --
 -- Each check is a single boolean, phrased as the desired behavior.  EXPLAIN
 -- does not show whether a query was planned by GEQO, so the checks detect
@@ -172,8 +173,8 @@ RESET join_collapse_limit;
 -- applies to explicit joins, from_collapse_limit to subqueries), so with the
 -- default settings a star with 11 dimensions (12 rels) would be planned by
 -- GEQO.  But the dimensions count as only 4 rels, so the star is planned by
--- the standard join search.  With enough other rels GEQO is still used, and
--- it's pruned too.
+-- the standard join search.  With enough other rels GEQO is still used, but
+-- it's not pruned.
 --
 SELECT $$
 SELECT count(*)
@@ -230,12 +231,12 @@ SELECT starjoin_geqo_used(:'q_star_list_c1', true) AS star_list_c1_geqo;
  t
 (1 row)
 
--- XXX GEQO is pruned too, so the GUC changes the plan
+-- GEQO is not pruned, so the GUC does not change the plan
 SELECT starjoin_geqo_plan(:'q_star_list_c1', true)
        = starjoin_geqo_plan(:'q_star_list_c1', false) AS geqo_plan_unaffected;
  geqo_plan_unaffected 
 ----------------------
- f
+ t
 (1 row)
 
 RESET geqo_threshold;
diff --git a/src/test/regress/expected/starjoin_other.out b/src/test/regress/expected/starjoin_other.out
index 3efe1b3dd2a..050f8fb5785 100644
--- a/src/test/regress/expected/starjoin_other.out
+++ b/src/test/regress/expected/starjoin_other.out
@@ -259,30 +259,6 @@ SELECT starjoin_other_cost(:'q_broken_ec', true)
 RESET ROLE;
 DROP TABLE starjoin_other_bf, starjoin_other_bk, starjoin_other_bd;
 DROP ROLE regress_starjoin_other;
--- GEQO must also be free to join the selective dimension first.
-SET geqo_threshold = 2;
-SELECT starjoin_other_cost(:'q_extra_clause', true)
-       <= starjoin_other_cost(:'q_extra_clause', false) * 1.01 AS extra_clause_geqo_ok;
- extra_clause_geqo_ok 
-----------------------
- t
-(1 row)
-
-SELECT starjoin_other_cost(:'q_extra_inequality', true)
-       <= starjoin_other_cost(:'q_extra_inequality', false) * 1.01 AS extra_inequality_geqo_ok;
- extra_inequality_geqo_ok 
---------------------------
- t
-(1 row)
-
-SELECT starjoin_other_cost(:'q_extra_ec_member', true)
-       <= starjoin_other_cost(:'q_extra_ec_member', false) * 1.01 AS extra_ec_member_geqo_ok;
- extra_ec_member_geqo_ok 
--------------------------
- t
-(1 row)
-
-RESET geqo_threshold;
 --
 -- OR clause, from which extract_restriction_or_clauses() derives a
 -- restriction on the dimension, which excludes the dimension from the
diff --git a/src/test/regress/sql/starjoin_geqo.sql b/src/test/regress/sql/starjoin_geqo.sql
index 4f004472bfc..fd9916a7296 100644
--- a/src/test/regress/sql/starjoin_geqo.sql
+++ b/src/test/regress/sql/starjoin_geqo.sql
@@ -6,9 +6,10 @@
 -- d dimensions.  So the dimensions count as only ceil(log2(d+1)) rels toward
 -- geqo_threshold, and a star gets planned by the standard join search even
 -- if it has more rels than geqo_threshold.  With enough other rels GEQO is
--- still used, and the pruning does not make it any cheaper, because GEQO
--- evaluates a fixed number of join orders, each joining all the rels.
--- Checking the canonical order only adds overhead.
+-- still used, but it's not pruned.  The pruning would not make it any
+-- cheaper, because GEQO evaluates a fixed number of join orders, each
+-- joining all the rels, so checking the canonical order would only add
+-- overhead.
 --
 -- Each check is a single boolean, phrased as the desired behavior.  EXPLAIN
 -- does not show whether a query was planned by GEQO, so the checks detect
@@ -159,8 +160,8 @@ RESET join_collapse_limit;
 -- applies to explicit joins, from_collapse_limit to subqueries), so with the
 -- default settings a star with 11 dimensions (12 rels) would be planned by
 -- GEQO.  But the dimensions count as only 4 rels, so the star is planned by
--- the standard join search.  With enough other rels GEQO is still used, and
--- it's pruned too.
+-- the standard join search.  With enough other rels GEQO is still used, but
+-- it's not pruned.
 --
 SELECT $$
 SELECT count(*)
@@ -200,7 +201,7 @@ SET geqo_threshold = 7;
 SELECT NOT starjoin_geqo_used(:'q_star_list_c1', true) AS star_list_c1_standard_search;
 SET geqo_threshold = 6;
 SELECT starjoin_geqo_used(:'q_star_list_c1', true) AS star_list_c1_geqo;
--- XXX GEQO is pruned too, so the GUC changes the plan
+-- GEQO is not pruned, so the GUC does not change the plan
 SELECT starjoin_geqo_plan(:'q_star_list_c1', true)
        = starjoin_geqo_plan(:'q_star_list_c1', false) AS geqo_plan_unaffected;
 RESET geqo_threshold;
diff --git a/src/test/regress/sql/starjoin_other.sql b/src/test/regress/sql/starjoin_other.sql
index fb74645b1c6..697728ad641 100644
--- a/src/test/regress/sql/starjoin_other.sql
+++ b/src/test/regress/sql/starjoin_other.sql
@@ -273,16 +273,6 @@ RESET ROLE;
 DROP TABLE starjoin_other_bf, starjoin_other_bk, starjoin_other_bd;
 DROP ROLE regress_starjoin_other;
 
--- GEQO must also be free to join the selective dimension first.
-SET geqo_threshold = 2;
-SELECT starjoin_other_cost(:'q_extra_clause', true)
-       <= starjoin_other_cost(:'q_extra_clause', false) * 1.01 AS extra_clause_geqo_ok;
-SELECT starjoin_other_cost(:'q_extra_inequality', true)
-       <= starjoin_other_cost(:'q_extra_inequality', false) * 1.01 AS extra_inequality_geqo_ok;
-SELECT starjoin_other_cost(:'q_extra_ec_member', true)
-       <= starjoin_other_cost(:'q_extra_ec_member', false) * 1.01 AS extra_ec_member_geqo_ok;
-RESET geqo_threshold;
-
 --
 -- OR clause, from which extract_restriction_or_clauses() derives a
 -- restriction on the dimension, which excludes the dimension from the
-- 
2.56.1

