From f40496f981f75406bf02ec91a6459d60cec6916d 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 05/30] Reject selective dimension joins in starjoin
 clusters

A fully matched NOT NULL foreign key does not make a join
cardinality-preserving when other conditions connect the same pair of
relations.  The cluster detection only ruled out join clauses to other
relations, so the canonical order could postpone a selective dimension
until all the preceding dimensions had been joined.

Require the loose join clauses and the equivalence classes of the fact
and the dimension to be accounted for by the matched foreign key.
Reject a matching equivalence class with extra members too, as an
additional equality can be merged into it.

Update STARJOINS.md, removing the open issue.  The starjoin_other and
starjoin_plan queries with an extra join clause now get the selective
dimension joined first with the GUC enabled too.  Add starjoin_other
checks with a non-equality join clause and with an extra member of the
equivalence class of the foreign key, and check all three queries with
GEQO too.
---
 STARJOINS.md                                  | 20 ++++--
 src/backend/optimizer/plan/initsplan.c        | 67 +++++++++++++++++--
 src/test/regress/expected/starjoin_other.out  | 67 ++++++++++++++++---
 src/test/regress/expected/starjoin_plan.out   | 34 +++++-----
 .../regress/expected/starjoin_plan_large.out  | 34 +++++-----
 src/test/regress/sql/starjoin_other.sql       | 45 ++++++++++---
 src/test/regress/sql/starjoin_plan.sql        |  4 +-
 src/test/regress/sql/starjoin_plan_large.sql  |  4 +-
 8 files changed, 209 insertions(+), 66 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index 9d8aea0fe4e..d271e84e390 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -43,6 +43,8 @@ The optimization consists of these parts:
   `starjoin_clusters` list of `PlannerInfo`, as `StarJoinClusterInfo`
   nodes.  Like other planner-only nodes, `StarJoinClusterInfo` needs no
   copy, equal, read or query jumble support.
+- `has_extra_starjoin_quals` checks that the foreign key accounts for
+  all the join clauses between the fact and a dimension.
 - `starjoin_order_invalid`, called by `make_join_rel`, rejects joins
   violating the canonical order, just like various other join
   restrictions.
@@ -188,6 +190,19 @@ The fact has to be a base relation too.
 
    Conditions 5 to 7 rule out join order restrictions on the dimension.
 
+8. All join conditions between the fact and the dimension are accounted
+   for by the matched foreign key, with no additional equivalence class
+   members.
+
+   A fully matched NOT NULL foreign key does not make the join preserve
+   the fact rows if other conditions connect the same pair of relations,
+   as they make the join selective.  The canonical order could then
+   postpone a selective dimension until all the preceding dimensions are
+   joined.  This applies to loose join clauses and to equivalence
+   classes of the fact and the dimension, including the equivalence
+   class of the foreign key with additional members, as an additional
+   equality can be merged into it.
+
 ### Foreign key requirements
 
 If the fact has several foreign keys referencing the dimension, the
@@ -245,11 +260,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.
-- 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
-  be joined first, but the canonical order may join it last (the
-  extra_clause_cost_higher check in starjoin_other, and starjoin_plan).
 - The search might forbid additional joins, e.g. joinrels containing a
   dimension of a cluster but not its fact.
 
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index f782d0bc166..5a36409dd67 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -149,6 +149,8 @@ static Relids get_join_domain_min_rels(PlannerInfo *root, Relids domain_relids);
 static void check_mergejoinable(RestrictInfo *restrictinfo);
 static void check_hashjoinable(RestrictInfo *restrictinfo);
 static void check_memoizable(RestrictInfo *restrictinfo);
+static bool has_extra_starjoin_quals(PlannerInfo *root, RelOptInfo *dim_rel,
+									 ForeignKeyOptInfo *fkinfo);
 
 
 /*****************************************************************************
@@ -4112,6 +4114,61 @@ check_memoizable(RestrictInfo *restrictinfo)
 	}
 }
 
+/*
+ * Does the dimension have join conditions not accounted for by this FK?
+ */
+static bool
+has_extra_starjoin_quals(PlannerInfo *root, RelOptInfo *dim_rel,
+						 ForeignKeyOptInfo *fkinfo)
+{
+	ListCell   *lc;
+	int			i;
+
+	foreach(lc, dim_rel->joininfo)
+	{
+		RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
+		int			colno;
+
+		for (colno = 0; colno < fkinfo->nkeys; colno++)
+		{
+			if (list_member_ptr(fkinfo->rinfos[colno], rinfo))
+				break;
+		}
+		if (colno == fkinfo->nkeys)
+			return true;
+	}
+
+	/*
+	 * Inner-join equalities normally live in ECs, not joininfo.  Even an EC
+	 * matching the FK can contain additional, selective equalities, so accept
+	 * only ECs containing just the two FK column expressions.
+	 */
+	i = -1;
+	while ((i = bms_next_member(dim_rel->eclass_indexes, i)) >= 0)
+	{
+		EquivalenceClass *ec = list_nth_node(EquivalenceClass,
+											 root->eq_classes, i);
+		int			colno;
+
+		if (list_length(ec->ec_members) <= 1 ||
+			!bms_is_member(fkinfo->con_relid, ec->ec_relids))
+			continue;
+
+		if (list_length(ec->ec_members) != 2)
+			return true;
+
+		for (colno = 0; colno < fkinfo->nkeys; colno++)
+		{
+			if (fkinfo->eclass[colno] == ec)
+				break;
+		}
+		if (colno == fkinfo->nkeys)
+			return true;
+	}
+
+	return false;
+}
+
 /*
  * generate_starjoin_clusters
  *		Detect star-shaped sub-joins and determine canonical join order.
@@ -4137,6 +4194,9 @@ check_memoizable(RestrictInfo *restrictinfo)
  *
  *	7. The dimension is not constrained by any PlaceHolderInfo.
  *
+ *	8. All join conditions between the fact and the dimension are accounted
+ *	   for by the matched FK, with no additional EC members.
+ *
  * Under these conditions, the join is strictly cardinality-preserving with
  * respect to the fact table, and all join orderings for a cluster produce
  * the same row counts.  The costs are not necessarily the same (e.g. the
@@ -4144,10 +4204,6 @@ check_memoizable(RestrictInfo *restrictinfo)
  * batches), so considering only a single canonical ordering may miss the
  * cheapest plan, in exchange for considering far fewer join orders.
  *
- * XXX We don't check that the foreign key is the only join condition between
- * the fact and the dimension.  Additional join clauses (e.g. a second foreign
- * key) may eliminate fact rows, in which case the order does matter.
- *
  * Dimensions that fail the preconditions are simply not added to any
  * cluster; the planner then handles them in the ordinary way.  A fact
  * with fewer than two qualifying dimensions produces no cluster (there
@@ -4358,6 +4414,9 @@ generate_starjoin_clusters(PlannerInfo *root)
 			if (!ok)
 				continue;
 
+			if (has_extra_starjoin_quals(root, dim_rel, fkinfo))
+				continue;
+
 			/* Qualifies as a dimension. */
 			dim_relids = bms_add_member(dim_relids, (int) d);
 			ndims++;
diff --git a/src/test/regress/expected/starjoin_other.out b/src/test/regress/expected/starjoin_other.out
index 27b4eb7c935..6e3178d608e 100644
--- a/src/test/regress/expected/starjoin_other.out
+++ b/src/test/regress/expected/starjoin_other.out
@@ -147,12 +147,9 @@ VACUUM ANALYZE starjoin_other_p1, starjoin_other_p2, starjoin_other_p3,
 --
 -- Extra (non-FK) join clause between the fact and a dimension
 --
--- The canonical order relies on each join to a dimension preserving the
--- cardinality of the fact, so that all join orders of a cluster produce the
--- same row counts.  But an additional join clause between the fact and the
--- dimension (not matching the foreign key) makes the join selective, so the
--- dimension should be joined first.  The cluster conditions check join
--- clauses to other rels, but not additional join clauses to the fact.
+-- An additional join clause between the fact and a dimension can make the
+-- join selective.  Exclude that dimension from the cluster, including when
+-- the clause is represented by an EC rather than joininfo.
 --
 SELECT $$
 SELECT count(*) FROM starjoin_other_pf AS pf
@@ -160,15 +157,65 @@ SELECT count(*) FROM starjoin_other_pf AS pf
   JOIN starjoin_other_p2 AS p2 ON pf.d2 = p2.id
   JOIN starjoin_other_p3 AS p3 ON pf.d3 = p3.id AND pf.x = p3.w
 $$ AS q_extra_clause \gset
--- the canonical order joins p3 last, so the plan with the GUC enabled is
--- more expensive
 SELECT starjoin_other_cost(:'q_extra_clause', true)
-       > starjoin_other_cost(:'q_extra_clause', false) * 1.01 AS extra_clause_cost_higher;
- extra_clause_cost_higher 
+       <= starjoin_other_cost(:'q_extra_clause', false) * 1.01 AS extra_clause_cost_ok;
+ extra_clause_cost_ok 
+----------------------
+ t
+(1 row)
+
+-- A non-equality join qual stays in joininfo.
+SELECT $$
+SELECT count(*) FROM starjoin_other_pf AS pf
+  JOIN starjoin_other_p1 AS p1 ON pf.d1 = p1.id
+  JOIN starjoin_other_p2 AS p2 ON pf.d2 = p2.id
+  JOIN starjoin_other_p3 AS p3 ON pf.d3 = p3.id AND pf.x > p3.w + 1000
+$$ AS q_extra_inequality \gset
+SELECT starjoin_other_plan(:'q_extra_inequality', true)
+       = starjoin_other_plan(:'q_extra_inequality', false) AS extra_inequality_plan_ok;
+ extra_inequality_plan_ok 
 --------------------------
  t
 (1 row)
 
+-- An extra equality can merge into the same EC that matches the FK.
+SELECT $$
+SELECT count(*) FROM starjoin_other_pf AS pf
+  JOIN starjoin_other_p1 AS p1 ON pf.d1 = p1.id
+  JOIN starjoin_other_p2 AS p2 ON pf.d2 = p2.id
+  JOIN starjoin_other_p3 AS p3 ON pf.d3 = p3.id AND pf.x + p3.w = p3.id
+$$ AS q_extra_ec_member \gset
+SELECT starjoin_other_plan(:'q_extra_ec_member', true)
+       = starjoin_other_plan(:'q_extra_ec_member', false) AS extra_ec_member_plan_ok;
+ extra_ec_member_plan_ok 
+-------------------------
+ t
+(1 row)
+
+-- 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/expected/starjoin_plan.out b/src/test/regress/expected/starjoin_plan.out
index 88745c94bbd..fe3bf8ce98f 100644
--- a/src/test/regress/expected/starjoin_plan.out
+++ b/src/test/regress/expected/starjoin_plan.out
@@ -36,8 +36,8 @@ INSERT INTO starjoin_plan_pf SELECT i, 1 + (i * 7) % 1000, 1 + (i * 13) % 1000,
 VACUUM ANALYZE starjoin_plan_p1, starjoin_plan_p2, starjoin_plan_p3, starjoin_plan_pf;
 SET max_parallel_workers_per_gather = 0;
 -- Q1: extra (non-FK) join qual between fact and the last dimension. It makes
--- the join to p3 selective, but p3 is still a dimension, so with the GUC on
--- p3 is joined last, while with the GUC off it's joined first.
+-- the join to p3 selective, so p3 must be excluded from the cluster and may
+-- be joined first with the GUC on as well as off.
 -- join the fact to p1, p2 and p3, with an extra join qual for p3, GUC on
 SET enable_starjoin_ordering = on;
 SELECT pg_temp.starjoin_explain($$
@@ -46,25 +46,25 @@ SELECT count(*) FROM starjoin_plan_pf AS pf
   JOIN starjoin_plan_p2 AS p2 ON pf.d2 = p2.id
   JOIN starjoin_plan_p3 AS p3 ON pf.d3 = p3.id AND pf.x = p3.w
 $$) AS "QUERY PLAN";
-                                         QUERY PLAN                                          
----------------------------------------------------------------------------------------------
+                                              QUERY PLAN                                               
+-------------------------------------------------------------------------------------------------------
  Aggregate (actual rows=1.00 loops=1)
-   ->  Hash Join (actual rows=0.00 loops=1)
-         Hash Cond: ((pf.d3 = p3.id) AND (pf.x = p3.w))
-         ->  Hash Join (actual rows=20000.00 loops=1)
-               Hash Cond: (pf.d2 = p2.id)
-               ->  Hash Join (actual rows=20000.00 loops=1)
-                     Hash Cond: (pf.d1 = p1.id)
+   ->  Nested Loop (actual rows=0.00 loops=1)
+         ->  Nested Loop (actual rows=0.00 loops=1)
+               ->  Hash Join (actual rows=0.00 loops=1)
+                     Hash Cond: ((pf.d3 = p3.id) AND (pf.x = p3.w))
                      ->  Seq Scan on starjoin_plan_pf pf (actual rows=20000.00 loops=1)
                      ->  Hash (actual rows=1000.00 loops=1)
                            Buckets: N  Batches: 1  Memory Usage: NkB
-                           ->  Seq Scan on starjoin_plan_p1 p1 (actual rows=1000.00 loops=1)
-               ->  Hash (actual rows=1000.00 loops=1)
-                     Buckets: N  Batches: 1  Memory Usage: NkB
-                     ->  Seq Scan on starjoin_plan_p2 p2 (actual rows=1000.00 loops=1)
-         ->  Hash (actual rows=1000.00 loops=1)
-               Buckets: N  Batches: 1  Memory Usage: NkB
-               ->  Seq Scan on starjoin_plan_p3 p3 (actual rows=1000.00 loops=1)
+                           ->  Seq Scan on starjoin_plan_p3 p3 (actual rows=1000.00 loops=1)
+               ->  Index Only Scan using starjoin_plan_p1_pkey on starjoin_plan_p1 p1 (never executed)
+                     Index Cond: (id = pf.d1)
+                     Heap Fetches: 0
+                     Index Searches: 0
+         ->  Index Only Scan using starjoin_plan_p2_pkey on starjoin_plan_p2 p2 (never executed)
+               Index Cond: (id = pf.d2)
+               Heap Fetches: 0
+               Index Searches: 0
 (17 rows)
 
 -- same query, GUC off
diff --git a/src/test/regress/expected/starjoin_plan_large.out b/src/test/regress/expected/starjoin_plan_large.out
index e8baa065a49..d53bc4e4ed4 100644
--- a/src/test/regress/expected/starjoin_plan_large.out
+++ b/src/test/regress/expected/starjoin_plan_large.out
@@ -36,8 +36,8 @@ INSERT INTO starjoin_plan_pf SELECT i, 1 + (i * 7) % 10000, 1 + (i * 13) % 10000
 VACUUM ANALYZE starjoin_plan_p1, starjoin_plan_p2, starjoin_plan_p3, starjoin_plan_pf;
 SET max_parallel_workers_per_gather = 0;
 -- Q1: extra (non-FK) join qual between fact and the last dimension. It makes
--- the join to p3 selective, but p3 is still a dimension, so with the GUC on
--- p3 is joined last, while with the GUC off it's joined first.
+-- the join to p3 selective, so p3 must be excluded from the cluster and may
+-- be joined first with the GUC on as well as off.
 -- join the fact to p1, p2 and p3, with an extra join qual for p3, GUC on
 SET enable_starjoin_ordering = on;
 SELECT pg_temp.starjoin_explain($$
@@ -46,25 +46,25 @@ SELECT count(*) FROM starjoin_plan_pf AS pf
   JOIN starjoin_plan_p2 AS p2 ON pf.d2 = p2.id
   JOIN starjoin_plan_p3 AS p3 ON pf.d3 = p3.id AND pf.x = p3.w
 $$) AS "QUERY PLAN";
-                                          QUERY PLAN                                          
-----------------------------------------------------------------------------------------------
+                                              QUERY PLAN                                               
+-------------------------------------------------------------------------------------------------------
  Aggregate (actual rows=1.00 loops=1)
-   ->  Hash Join (actual rows=0.00 loops=1)
-         Hash Cond: ((pf.d3 = p3.id) AND (pf.x = p3.w))
-         ->  Hash Join (actual rows=200000.00 loops=1)
-               Hash Cond: (pf.d2 = p2.id)
-               ->  Hash Join (actual rows=200000.00 loops=1)
-                     Hash Cond: (pf.d1 = p1.id)
+   ->  Nested Loop (actual rows=0.00 loops=1)
+         ->  Nested Loop (actual rows=0.00 loops=1)
+               ->  Hash Join (actual rows=0.00 loops=1)
+                     Hash Cond: ((pf.d3 = p3.id) AND (pf.x = p3.w))
                      ->  Seq Scan on starjoin_plan_pf pf (actual rows=200000.00 loops=1)
                      ->  Hash (actual rows=10000.00 loops=1)
                            Buckets: N  Batches: 1  Memory Usage: NkB
-                           ->  Seq Scan on starjoin_plan_p1 p1 (actual rows=10000.00 loops=1)
-               ->  Hash (actual rows=10000.00 loops=1)
-                     Buckets: N  Batches: 1  Memory Usage: NkB
-                     ->  Seq Scan on starjoin_plan_p2 p2 (actual rows=10000.00 loops=1)
-         ->  Hash (actual rows=10000.00 loops=1)
-               Buckets: N  Batches: 1  Memory Usage: NkB
-               ->  Seq Scan on starjoin_plan_p3 p3 (actual rows=10000.00 loops=1)
+                           ->  Seq Scan on starjoin_plan_p3 p3 (actual rows=10000.00 loops=1)
+               ->  Index Only Scan using starjoin_plan_p1_pkey on starjoin_plan_p1 p1 (never executed)
+                     Index Cond: (id = pf.d1)
+                     Heap Fetches: 0
+                     Index Searches: 0
+         ->  Index Only Scan using starjoin_plan_p2_pkey on starjoin_plan_p2 p2 (never executed)
+               Index Cond: (id = pf.d2)
+               Heap Fetches: 0
+               Index Searches: 0
 (17 rows)
 
 -- same query, GUC off
diff --git a/src/test/regress/sql/starjoin_other.sql b/src/test/regress/sql/starjoin_other.sql
index 41875fec76a..0519cae0913 100644
--- a/src/test/regress/sql/starjoin_other.sql
+++ b/src/test/regress/sql/starjoin_other.sql
@@ -173,12 +173,9 @@ VACUUM ANALYZE starjoin_other_p1, starjoin_other_p2, starjoin_other_p3,
 --
 -- Extra (non-FK) join clause between the fact and a dimension
 --
--- The canonical order relies on each join to a dimension preserving the
--- cardinality of the fact, so that all join orders of a cluster produce the
--- same row counts.  But an additional join clause between the fact and the
--- dimension (not matching the foreign key) makes the join selective, so the
--- dimension should be joined first.  The cluster conditions check join
--- clauses to other rels, but not additional join clauses to the fact.
+-- An additional join clause between the fact and a dimension can make the
+-- join selective.  Exclude that dimension from the cluster, including when
+-- the clause is represented by an EC rather than joininfo.
 --
 SELECT $$
 SELECT count(*) FROM starjoin_other_pf AS pf
@@ -187,10 +184,40 @@ SELECT count(*) FROM starjoin_other_pf AS pf
   JOIN starjoin_other_p3 AS p3 ON pf.d3 = p3.id AND pf.x = p3.w
 $$ AS q_extra_clause \gset
 
--- the canonical order joins p3 last, so the plan with the GUC enabled is
--- more expensive
 SELECT starjoin_other_cost(:'q_extra_clause', true)
-       > starjoin_other_cost(:'q_extra_clause', false) * 1.01 AS extra_clause_cost_higher;
+       <= starjoin_other_cost(:'q_extra_clause', false) * 1.01 AS extra_clause_cost_ok;
+
+-- A non-equality join qual stays in joininfo.
+SELECT $$
+SELECT count(*) FROM starjoin_other_pf AS pf
+  JOIN starjoin_other_p1 AS p1 ON pf.d1 = p1.id
+  JOIN starjoin_other_p2 AS p2 ON pf.d2 = p2.id
+  JOIN starjoin_other_p3 AS p3 ON pf.d3 = p3.id AND pf.x > p3.w + 1000
+$$ AS q_extra_inequality \gset
+
+SELECT starjoin_other_plan(:'q_extra_inequality', true)
+       = starjoin_other_plan(:'q_extra_inequality', false) AS extra_inequality_plan_ok;
+
+-- An extra equality can merge into the same EC that matches the FK.
+SELECT $$
+SELECT count(*) FROM starjoin_other_pf AS pf
+  JOIN starjoin_other_p1 AS p1 ON pf.d1 = p1.id
+  JOIN starjoin_other_p2 AS p2 ON pf.d2 = p2.id
+  JOIN starjoin_other_p3 AS p3 ON pf.d3 = p3.id AND pf.x + p3.w = p3.id
+$$ AS q_extra_ec_member \gset
+
+SELECT starjoin_other_plan(:'q_extra_ec_member', true)
+       = starjoin_other_plan(:'q_extra_ec_member', false) AS extra_ec_member_plan_ok;
+
+-- 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
diff --git a/src/test/regress/sql/starjoin_plan.sql b/src/test/regress/sql/starjoin_plan.sql
index 569130d39fa..d568fcf5ac3 100644
--- a/src/test/regress/sql/starjoin_plan.sql
+++ b/src/test/regress/sql/starjoin_plan.sql
@@ -44,8 +44,8 @@ VACUUM ANALYZE starjoin_plan_p1, starjoin_plan_p2, starjoin_plan_p3, starjoin_pl
 SET max_parallel_workers_per_gather = 0;
 
 -- Q1: extra (non-FK) join qual between fact and the last dimension. It makes
--- the join to p3 selective, but p3 is still a dimension, so with the GUC on
--- p3 is joined last, while with the GUC off it's joined first.
+-- the join to p3 selective, so p3 must be excluded from the cluster and may
+-- be joined first with the GUC on as well as off.
 
 -- join the fact to p1, p2 and p3, with an extra join qual for p3, GUC on
 SET enable_starjoin_ordering = on;
diff --git a/src/test/regress/sql/starjoin_plan_large.sql b/src/test/regress/sql/starjoin_plan_large.sql
index fa912e14b75..ad52a97df3f 100644
--- a/src/test/regress/sql/starjoin_plan_large.sql
+++ b/src/test/regress/sql/starjoin_plan_large.sql
@@ -44,8 +44,8 @@ VACUUM ANALYZE starjoin_plan_p1, starjoin_plan_p2, starjoin_plan_p3, starjoin_pl
 SET max_parallel_workers_per_gather = 0;
 
 -- Q1: extra (non-FK) join qual between fact and the last dimension. It makes
--- the join to p3 selective, but p3 is still a dimension, so with the GUC on
--- p3 is joined last, while with the GUC off it's joined first.
+-- the join to p3 selective, so p3 must be excluded from the cluster and may
+-- be joined first with the GUC on as well as off.
 
 -- join the fact to p1, p2 and p3, with an extra join qual for p3, GUC on
 SET enable_starjoin_ordering = on;
-- 
2.56.1

