From 33e8fa54ea5853d5c80d037b2ee7b43dc91328b5 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 26/30] PSEUDOCONST: Ignore pseudoconstant quals in starjoin
 clusters

A Var-free qual that is not volatile (e.g. "now() > '2000-01-01'", or an
uncorrelated EXISTS evaluated as an InitPlan) gets the relids of the
whole join domain, so it's in the joininfo list of every rel.  So every
dimension failed the check that it has no join clauses to rels other
than the fact, and a single such qual disabled all starjoin clusters in
the query, including their effect on geqo_threshold.

Such quals don't relate the dimension to any rel, and are evaluated only
once, in a gating Result node.  So ignore them when checking the join
clauses of the dimensions, in a new function
have_relevant_starjoin_clause() and in has_extra_starjoin_quals().

The quals still make the dimensions joinable to each other, and to all
the other rels.  That's fine thanks to commits b501ed8a135 and
04d202a6ad1, which allow a joinrel with a dimension but without the fact
only if it has a single dimension, joined to rels without join clauses
(ignoring pseudoconstant quals).

Update STARJOINS.md, removing the open issue.  The starjoin_var_free
checks marked XXX now pass.  Add a check that the qual does not double
the planning memory of the star with 11 dimensions, and starjoin_other
checks that the qual neither makes the search join the dimensions to
rels with join clauses, nor prevents joining them to rels without join
clauses.
---
 STARJOINS.md                                  | 66 +++++++++++--------
 src/backend/optimizer/path/joinrels.c         | 21 +++---
 src/backend/optimizer/plan/initsplan.c        | 57 +++++++++++++---
 src/test/regress/expected/starjoin_other.out  | 60 +++++++++++++++++
 .../regress/expected/starjoin_var_free.out    | 56 ++++++++++------
 src/test/regress/sql/starjoin_other.sql       | 49 ++++++++++++++
 src/test/regress/sql/starjoin_var_free.sql    | 47 ++++++++-----
 7 files changed, 277 insertions(+), 79 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index 8ab6e61d4e9..fee20114c28 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -60,6 +60,8 @@ The optimization consists of these parts:
 - `starjoin_fk_comparisons_match`, `starjoin_fk_operand_matches` and
   `starjoin_equality_implies` check the operators, input coercions and
   collations of the join clauses.
+- `have_relevant_starjoin_clause` checks for join clauses other than
+  pseudoconstant quals.
 - `starjoin_order_invalid`, called by `make_join_rel`, rejects joins
   violating the canonical order, just like various other join
   restrictions.
@@ -157,13 +159,12 @@ cost the same as with the GUC disabled.  The numbers of queries with
 plans more expensive than with the GUC disabled (by more than 0.1%)
 dropped from 493 to 285, from 199 to 32 and from 235 to 29 (there were
 none in the third corpus).  No query failed to plan or tripped an
-assertion.  Star queries with a pseudoconstant qual were not affected,
-as they have no starjoin clusters (see the open issues).  The search
-considers joining the dimensions to the rels without join clauses, which
-increased the planning memory with the GUC enabled by 13% for the 897
-star queries without a pseudoconstant qual, and by 2.7%, 4.3% and 0% for
-the three other corpora (by up to 48% for a single query).  That's still
-only 20%, 80%, 34% and 74% of the planning memory with the GUC disabled.
+assertion.  The search considers joining the dimensions to the rels
+without join clauses, which increased the planning memory with the GUC
+enabled by 13% for the 897 star queries without a pseudoconstant qual,
+and by 2.7%, 4.3% and 0% for the three other corpora (by up to 48% for a
+single query).  That's still only 20%, 80%, 34% and 74% of the planning
+memory with the GUC disabled.
 
 Without the condition on the join clauses (allowing a single dimension
 joined to any rel), or without any restriction at all, the plan costs
@@ -261,6 +262,27 @@ joinrels by d+1.  For example, for a query joining 4 facts with 10
 dimensions each, the standard join search would take 1.2s and 440MB,
 compared to 120ms with GEQO.
 
+### Pseudoconstant quals
+
+A Var-free qual that is not volatile (e.g. "now() > '2000-01-01'", or an
+uncorrelated EXISTS evaluated as an InitPlan) is a pseudoconstant.  It
+gets the relids of the whole join domain, so it's in the joininfo list
+of every rel.  Such quals don't relate the dimension to any rel, and are
+evaluated only once, in a gating Result node.  So they are ignored when
+checking the join clauses of the dimensions (conditions 4 and 8).
+Otherwise every dimension would fail condition 4, and a single such qual
+would disable all starjoin clusters in the query, including their effect
+on `geqo_threshold`.  The quals still make the dimensions joinable to
+each other, and to all the other rels.  That's fine thanks to the rule
+rejecting joinrels with a dimension but without the fact, except for a
+single dimension joined to rels without join clauses (ignoring
+pseudoconstant quals).  Without that rule, the standard join search
+would consider all subsets of the dimensions, and planning a star with
+11 dimensions in starjoin_var_free would use about 32MB of memory
+instead of about 0.2MB.  So the quals neither make the search join the
+dimensions to rels with join clauses, nor prevent it from joining them
+to rels without join clauses.
+
 ## Conditions for a dimension
 
 A relation is a dimension of a fact if it's referenced by a foreign key
@@ -305,8 +327,8 @@ The fact has to be a base relation too.
    remaining fact row still has its match.
 
 4. The dimension has no relevant join clauses to any rel other than the
-   fact (checked via `have_relevant_joinclause`, which also covers
-   equivalence class joins).
+   fact (including equivalence class joins), ignoring pseudoconstant
+   quals (see `have_relevant_starjoin_clause`).
 
    So the dimension can be joined only to the fact (or to joinrels
    including it), which makes the dimensions interchangeable.
@@ -325,10 +347,11 @@ 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 compatible equality semantics
-   and no additional equivalence class members other than expressions of
-   the fact, in equivalence classes that are not broken.
+8. All join conditions between the fact and the dimension (again
+   ignoring pseudoconstant quals) are accounted for by the matched
+   foreign key, with compatible equality semantics and no additional
+   equivalence class members other than expressions of the fact, in
+   equivalence classes that are not broken.
 
    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,
@@ -515,18 +538,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 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.  The dimensions then count fully toward
-  `geqo_threshold`, so with the default settings a star with 11
-  dimensions gets planned by GEQO.  The three checks marked XXX in
-  starjoin_var_free show this.  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`).
 - A dimension gets joined to the fact at its position in the canonical
   order, so if a rel without join clauses is joined to a dimension
   that's not the last one, the joins of the following dimensions process
@@ -544,6 +555,8 @@ the plans, the planning cost, or the regression tests.
   results.
 - No regression test makes `starjoin_fk_operand_matches` reject a
   relabel changing the actual type seen by a polymorphic operator.
+- Volatile Var-free quals are not pseudoconstant, so they still disable
+  the clusters, which is conservative.
 
 ## Regression tests
 
@@ -652,4 +665,5 @@ the plans, the planning cost, or the regression tests.
 - starjoin_var_free checks star queries with Var-free quals (in WHERE,
   as an uncorrelated EXISTS, and in the ON clause of a single
   dimension's join), and whether they're planned by the standard join
-  search.  The three checks marked XXX return false.
+  search.  It also checks that such a qual does not double the planning
+  memory of the star with 11 dimensions.
diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c
index 55029d1bbcb..0f862d5092b 100644
--- a/src/backend/optimizer/path/joinrels.c
+++ b/src/backend/optimizer/path/joinrels.c
@@ -2319,9 +2319,11 @@ starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 		 * 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.  They may get joined further,
-		 * and so the search might consider many subsets of the dimensions
-		 * without the fact.
+		 * clauses, in the last-ditch pass, or because of a pseudoconstant
+		 * qual (which is in the joininfo list of every rel, see
+		 * have_relevant_starjoin_clause).  They may get joined further, and
+		 * so the search might consider many subsets of the dimensions without
+		 * the fact (all of them, with a pseudoconstant qual).
 		 *
 		 * Most of them are not needed, as the dimensions can be joined to the
 		 * joinrels with the fact (in the canonical order).  But a rel without
@@ -2337,12 +2339,13 @@ starjoin_order_invalid(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2)
 		 * fact, which is consistent with how the dimensions count toward
 		 * geqo_threshold (see starjoin_geqo_levels).
 		 *
-		 * Outside the last-ditch pass, join_search_one_level joins a
-		 * dimension to a rel without the fact only if that rel has no join
-		 * clauses, and the joinrel then gets joined only to rels with the
-		 * fact (by the join clauses of the dimension).  So it builds joinrels
-		 * with several dimensions but without the fact only as a result of
-		 * the last-ditch pass, and rejecting them maintains the d+1 bound
+		 * Outside the last-ditch pass, and without pseudoconstant quals,
+		 * join_search_one_level joins a dimension to a rel without the fact
+		 * only if that rel has no join clauses, and the joinrel then gets
+		 * joined only to rels with the fact (by the join clauses of the
+		 * dimension).  So it builds joinrels with several dimensions but
+		 * without the fact only as a result of the last-ditch pass, or of
+		 * pseudoconstant quals, and rejecting them maintains the d+1 bound
 		 * even then.
 		 */
 		if (!bms_is_member(c->fact_relid, join_relids))
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index dd5dc9777f0..d59b15e9987 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -4128,8 +4128,45 @@ check_memoizable(RestrictInfo *restrictinfo)
 	}
 }
 
+/*
+ * Does the dimension have a relevant join clause to the other rel?
+ *
+ * This is have_relevant_joinclause(), except that pseudoconstant quals are
+ * ignored.  A pseudoconstant qual (e.g. "now() > '2000-01-01'", or a qual
+ * with an uncorrelated sub-SELECT) gets the relids of the whole join domain
+ * (see distribute_qual_to_rels), so it's in the joininfo list of every rel.
+ * But it's Var-free, so it doesn't relate the dimension to any rel, and it's
+ * evaluated only once, in a gating Result node, so it doesn't change the
+ * number of rows produced by the joins.
+ */
+static bool
+have_relevant_starjoin_clause(PlannerInfo *root, RelOptInfo *dim_rel,
+							  RelOptInfo *other_rel)
+{
+	ListCell   *lc;
+
+	foreach(lc, dim_rel->joininfo)
+	{
+		RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
+
+		if (rinfo->pseudoconstant)
+			continue;
+
+		if (bms_overlap(other_rel->relids, rinfo->required_relids))
+			return true;
+	}
+
+	/* join clauses not emitted into the joininfo lists */
+	if (dim_rel->has_eclass_joins && other_rel->has_eclass_joins)
+		return have_relevant_eclass_joinclause(root, dim_rel, other_rel);
+
+	return false;
+}
+
 /*
  * Does the dimension have join conditions not accounted for by this FK?
+ *
+ * Pseudoconstant quals are ignored, see have_relevant_starjoin_clause.
  */
 static bool
 has_extra_starjoin_quals(PlannerInfo *root, RelOptInfo *dim_rel,
@@ -4143,6 +4180,9 @@ has_extra_starjoin_quals(PlannerInfo *root, RelOptInfo *dim_rel,
 		RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
 		int			colno;
 
+		if (rinfo->pseudoconstant)
+			continue;
+
 		for (colno = 0; colno < fkinfo->nkeys; colno++)
 		{
 			if (list_member_ptr(fkinfo->rinfos[colno], rinfo))
@@ -4498,8 +4538,8 @@ has_starjoin_fk(PlannerInfo *root, RelOptInfo *fact_rel, RelOptInfo *dim_rel)
  *	   nor a TABLESAMPLE clause.
  *
  *	4. The dimension has no relevant join clauses to any rel other than
- *	   the fact (checked via have_relevant_joinclause, which also covers
- *	   equivalence-class joins).
+ *	   the fact (including equivalence-class joins), ignoring pseudoconstant
+ *	   quals (see have_relevant_starjoin_clause).
  *
  *	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).
@@ -4508,10 +4548,10 @@ has_starjoin_fk(PlannerInfo *root, RelOptInfo *fact_rel, RelOptInfo *dim_rel)
  *
  *	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 compatible equality semantics and no
- *	   additional EC members other than expressions of the fact, in ECs
- *	   that are not broken.
+ *	8. All join conditions between the fact and the dimension (again
+ *	   ignoring pseudoconstant quals) are accounted for by the matched FK,
+ *	   with compatible equality semantics and no additional EC members other
+ *	   than expressions of the fact, in ECs that are not broken.
  *
  * Under these conditions, the join is strictly cardinality-preserving with
  * respect to the fact table, and all join orderings for a cluster produce
@@ -4684,8 +4724,7 @@ generate_starjoin_clusters(PlannerInfo *root)
 
 			/*
 			 * The dimension must have no relevant join clause to any rel
-			 * other than the fact.  have_relevant_joinclause() also covers
-			 * equivalence-class joins.
+			 * other than the fact.
 			 */
 			for (r = 1; r < nrels; r++)
 			{
@@ -4697,7 +4736,7 @@ generate_starjoin_clusters(PlannerInfo *root)
 				if (other_rel == NULL ||
 					other_rel->reloptkind != RELOPT_BASEREL)
 					continue;
-				if (have_relevant_joinclause(root, dim_rel, other_rel))
+				if (have_relevant_starjoin_clause(root, dim_rel, other_rel))
 				{
 					ok = false;
 					break;
diff --git a/src/test/regress/expected/starjoin_other.out b/src/test/regress/expected/starjoin_other.out
index 050f8fb5785..4908e2e46b3 100644
--- a/src/test/regress/expected/starjoin_other.out
+++ b/src/test/regress/expected/starjoin_other.out
@@ -549,6 +549,16 @@ SELECT starjoin_other_cost(:'q_clauseless', true)
  t
 (1 row)
 
+-- the same with a pseudoconstant qual, which is in the joininfo list of every
+-- rel, but is not a join clause
+SELECT starjoin_other_cost(:'q_clauseless' || $$WHERE now() > '2000-01-01'$$, true)
+       <= starjoin_other_cost(:'q_clauseless' || $$WHERE now() > '2000-01-01'$$, false) * 1.01
+       AS clauseless_where_now_cost_ok;
+ clauseless_where_now_cost_ok 
+------------------------------
+ t
+(1 row)
+
 -- The dimension still gets joined to the fact in the canonical order.  With
 -- h1 as the first dimension, the joins to h2 and h3 process the multiplied
 -- rows, so the plan with the GUC enabled is more expensive.
@@ -566,6 +576,56 @@ SELECT starjoin_other_cost(:'q_clauseless_first', true)
  t
 (1 row)
 
+-- But without the fact, a dimension must not be joined to rels with join
+-- clauses, or the search might consider each dimension with all subsets of
+-- c1-c5.  A pseudoconstant qual is in the joininfo list of every rel, but it
+-- is not a join clause, so it must not make the rels joinable to each other.
+-- The rels c1-c5 are joinable to each other even without the qual, through
+-- an equivalence class (including f.id) or through a join clause referencing
+-- all of them, so the qual should not add any joinrels.
+SELECT $$
+SELECT count(*) FROM starjoin_other_f AS f
+  JOIN starjoin_other_d1 AS d1 ON f.d1 = d1.id
+  JOIN starjoin_other_d2 AS d2 ON f.d2 = d2.id
+  JOIN starjoin_other_d3 AS d3 ON f.d3 = d3.id
+  JOIN starjoin_other_d4 AS d4 ON f.d4 = d4.id
+  JOIN starjoin_other_c1 AS c1 ON f.id = c1.v
+  JOIN starjoin_other_c2 AS c2 ON c1.v = c2.v
+  JOIN starjoin_other_c3 AS c3 ON c2.v = c3.v
+  JOIN starjoin_other_c4 AS c4 ON c3.v = c4.v
+  JOIN starjoin_other_c5 AS c5 ON c4.v = c5.v
+$$ AS q_ec_joins \gset
+SELECT $$
+SELECT count(*) FROM starjoin_other_f AS f
+  JOIN starjoin_other_d1 AS d1 ON f.d1 = d1.id
+  JOIN starjoin_other_d2 AS d2 ON f.d2 = d2.id
+  JOIN starjoin_other_d3 AS d3 ON f.d3 = d3.id
+  JOIN starjoin_other_d4 AS d4 ON f.d4 = d4.id
+  CROSS JOIN starjoin_other_c1 AS c1
+  CROSS JOIN starjoin_other_c2 AS c2
+  CROSS JOIN starjoin_other_c3 AS c3
+  CROSS JOIN starjoin_other_c4 AS c4
+  JOIN starjoin_other_c5 AS c5 ON f.id < c1.v + c2.v + c3.v + c4.v + c5.v
+$$ AS q_joininfo \gset
+-- single subproblems with 10 rels (below geqo_threshold)
+SET join_collapse_limit = 20;
+SELECT starjoin_other_memory(:'q_ec_joins' || $$WHERE now() > '2000-01-01'$$, true)
+       < starjoin_other_memory(:'q_ec_joins', true) * 1.5
+       AS ec_joins_where_now_memory_ok;
+ ec_joins_where_now_memory_ok 
+------------------------------
+ t
+(1 row)
+
+SELECT starjoin_other_memory(:'q_joininfo' || $$WHERE now() > '2000-01-01'$$, true)
+       < starjoin_other_memory(:'q_joininfo', true) * 1.5
+       AS joininfo_where_now_memory_ok;
+ joininfo_where_now_memory_ok 
+------------------------------
+ t
+(1 row)
+
+RESET join_collapse_limit;
 RESET max_parallel_workers_per_gather;
 DROP FUNCTION starjoin_other_cost(text, bool);
 DROP FUNCTION starjoin_other_memory(text, bool);
diff --git a/src/test/regress/expected/starjoin_var_free.out b/src/test/regress/expected/starjoin_var_free.out
index c2183d8e866..6130062e9f6 100644
--- a/src/test/regress/expected/starjoin_var_free.out
+++ b/src/test/regress/expected/starjoin_var_free.out
@@ -5,17 +5,15 @@
 -- pseudoconstant, evaluated only once (in a gating Result node).  But it gets
 -- the relids of the whole join domain, so it's in the joininfo list of every
 -- rel, and have_relevant_joinclause() finds a join clause between any two
--- rels.  So every dimension fails the check that it has no join clauses to
--- rels other than the fact (see generate_starjoin_clusters), and the query
--- has no starjoin clusters at all.  The dimensions then count fully toward
--- geqo_threshold, and the standard join search does not use the canonical
--- order.
+-- rels.  The starjoin clusters ignore such quals, as otherwise every
+-- dimension would fail the check that it has no join clauses to rels other
+-- than the fact (see generate_starjoin_clusters), and the query would have no
+-- starjoin clusters at all.
 --
--- With enable_starjoin_ordering disabled, the qual makes the standard join
--- search consider all subsets of the rels too, so this is not a regression.
--- The qual also makes the dimensions joinable to each other, so the standard
--- join search would consider all subsets of the dimensions, if joinrels with
--- a dimension but without the fact were allowed (see starjoin_order_invalid).
+-- The qual still makes the dimensions joinable to each other, so the
+-- standard join search would consider all subsets of the dimensions, if
+-- joinrels with a dimension but without the fact were allowed (see
+-- starjoin_order_invalid).  The planning would then use much more memory.
 --
 -- 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
@@ -37,6 +35,16 @@ EXCEPTION WHEN internal_error THEN
   RETURN true;
 END;
 $$;
+-- memory used by the planning (in kB)
+CREATE FUNCTION starjoin_var_free_planning_memory(query text) RETURNS bigint
+LANGUAGE plpgsql SET enable_starjoin_ordering = on AS $$
+DECLARE
+  plan json;
+BEGIN
+  EXECUTE 'EXPLAIN (FORMAT JSON, MEMORY, COSTS OFF) ' || query INTO plan;
+  RETURN (plan->0->'Planning'->>'Memory Used')::bigint;
+END;
+$$;
 -- star with 11 dimensions d1-d11
 DO $$
 BEGIN
@@ -67,8 +75,8 @@ ANALYZE starjoin_var_free_f;
 --
 -- With join_collapse_limit = 20 the star with 11 dimensions (12 rels) is a
 -- single subproblem, reaching the default geqo_threshold = 12.  But the
--- dimensions count as only 4 rels, so without a Var-free qual the star is
--- planned by the standard join search.
+-- dimensions count as only 4 rels, so the star is planned by the standard
+-- join search, even with a Var-free qual.
 --
 SET join_collapse_limit = 20;
 SELECT $$
@@ -99,35 +107,45 @@ SELECT NOT starjoin_var_free_geqo_used(:'q_star', true) AS star_standard_search;
  t
 (1 row)
 
--- XXX a pseudoconstant qual disables the starjoin clusters
+-- a pseudoconstant qual does not disable the starjoin clusters
 SELECT NOT starjoin_var_free_geqo_used(:'q_star' || $$WHERE now() > '2000-01-01'$$, true)
        AS where_now_standard_search;
  where_now_standard_search 
 ---------------------------
- f
+ t
 (1 row)
 
--- XXX the same for an uncorrelated sub-SELECT (an InitPlan)
+-- the same for an uncorrelated sub-SELECT (an InitPlan)
 SELECT NOT starjoin_var_free_geqo_used(:'q_star' ||
          'WHERE EXISTS (SELECT 1 FROM starjoin_var_free_d1 WHERE id = 0)', true)
        AS where_exists_standard_search;
  where_exists_standard_search 
 ------------------------------
- f
+ t
 (1 row)
 
--- XXX the same for a qual in the ON clause of one dimension's join, which
--- excludes all the dimensions, not just that one
+-- the same for a qual in the ON clause of one dimension's join
 SELECT NOT starjoin_var_free_geqo_used(replace(:'q_star', 'f.d11 = d11.id',
          $$f.d11 = d11.id AND now() > '2000-01-01'$$), true)
        AS join_on_standard_search;
  join_on_standard_search 
 -------------------------
- f
+ t
+(1 row)
+
+-- the qual does not make the standard join search consider all subsets of
+-- the dimensions
+SELECT starjoin_var_free_planning_memory(:'q_star' || $$WHERE now() > '2000-01-01'$$)
+       < 2 * starjoin_var_free_planning_memory(:'q_star')
+       AS where_now_planning_memory;
+ where_now_planning_memory 
+---------------------------
+ t
 (1 row)
 
 RESET join_collapse_limit;
 DROP FUNCTION starjoin_var_free_geqo_used(text, bool);
+DROP FUNCTION starjoin_var_free_planning_memory(text);
 DROP TABLE starjoin_var_free_f, starjoin_var_free_d1, starjoin_var_free_d2,
   starjoin_var_free_d3, starjoin_var_free_d4, starjoin_var_free_d5,
   starjoin_var_free_d6, starjoin_var_free_d7, starjoin_var_free_d8,
diff --git a/src/test/regress/sql/starjoin_other.sql b/src/test/regress/sql/starjoin_other.sql
index 697728ad641..0a78a9a9afb 100644
--- a/src/test/regress/sql/starjoin_other.sql
+++ b/src/test/regress/sql/starjoin_other.sql
@@ -514,6 +514,12 @@ $$ AS q_clauseless \gset
 SELECT starjoin_other_cost(:'q_clauseless', true)
        <= starjoin_other_cost(:'q_clauseless', false) * 1.01 AS clauseless_cost_ok;
 
+-- the same with a pseudoconstant qual, which is in the joininfo list of every
+-- rel, but is not a join clause
+SELECT starjoin_other_cost(:'q_clauseless' || $$WHERE now() > '2000-01-01'$$, true)
+       <= starjoin_other_cost(:'q_clauseless' || $$WHERE now() > '2000-01-01'$$, false) * 1.01
+       AS clauseless_where_now_cost_ok;
+
 -- The dimension still gets joined to the fact in the canonical order.  With
 -- h1 as the first dimension, the joins to h2 and h3 process the multiplied
 -- rows, so the plan with the GUC enabled is more expensive.
@@ -528,6 +534,49 @@ $$ AS q_clauseless_first \gset
 SELECT starjoin_other_cost(:'q_clauseless_first', true)
        > starjoin_other_cost(:'q_clauseless_first', false) * 1.01 AS clauseless_first_cost_higher;
 
+-- But without the fact, a dimension must not be joined to rels with join
+-- clauses, or the search might consider each dimension with all subsets of
+-- c1-c5.  A pseudoconstant qual is in the joininfo list of every rel, but it
+-- is not a join clause, so it must not make the rels joinable to each other.
+-- The rels c1-c5 are joinable to each other even without the qual, through
+-- an equivalence class (including f.id) or through a join clause referencing
+-- all of them, so the qual should not add any joinrels.
+SELECT $$
+SELECT count(*) FROM starjoin_other_f AS f
+  JOIN starjoin_other_d1 AS d1 ON f.d1 = d1.id
+  JOIN starjoin_other_d2 AS d2 ON f.d2 = d2.id
+  JOIN starjoin_other_d3 AS d3 ON f.d3 = d3.id
+  JOIN starjoin_other_d4 AS d4 ON f.d4 = d4.id
+  JOIN starjoin_other_c1 AS c1 ON f.id = c1.v
+  JOIN starjoin_other_c2 AS c2 ON c1.v = c2.v
+  JOIN starjoin_other_c3 AS c3 ON c2.v = c3.v
+  JOIN starjoin_other_c4 AS c4 ON c3.v = c4.v
+  JOIN starjoin_other_c5 AS c5 ON c4.v = c5.v
+$$ AS q_ec_joins \gset
+
+SELECT $$
+SELECT count(*) FROM starjoin_other_f AS f
+  JOIN starjoin_other_d1 AS d1 ON f.d1 = d1.id
+  JOIN starjoin_other_d2 AS d2 ON f.d2 = d2.id
+  JOIN starjoin_other_d3 AS d3 ON f.d3 = d3.id
+  JOIN starjoin_other_d4 AS d4 ON f.d4 = d4.id
+  CROSS JOIN starjoin_other_c1 AS c1
+  CROSS JOIN starjoin_other_c2 AS c2
+  CROSS JOIN starjoin_other_c3 AS c3
+  CROSS JOIN starjoin_other_c4 AS c4
+  JOIN starjoin_other_c5 AS c5 ON f.id < c1.v + c2.v + c3.v + c4.v + c5.v
+$$ AS q_joininfo \gset
+
+-- single subproblems with 10 rels (below geqo_threshold)
+SET join_collapse_limit = 20;
+SELECT starjoin_other_memory(:'q_ec_joins' || $$WHERE now() > '2000-01-01'$$, true)
+       < starjoin_other_memory(:'q_ec_joins', true) * 1.5
+       AS ec_joins_where_now_memory_ok;
+SELECT starjoin_other_memory(:'q_joininfo' || $$WHERE now() > '2000-01-01'$$, true)
+       < starjoin_other_memory(:'q_joininfo', true) * 1.5
+       AS joininfo_where_now_memory_ok;
+RESET join_collapse_limit;
+
 RESET max_parallel_workers_per_gather;
 
 DROP FUNCTION starjoin_other_cost(text, bool);
diff --git a/src/test/regress/sql/starjoin_var_free.sql b/src/test/regress/sql/starjoin_var_free.sql
index c1ff1753e97..ef80936aef3 100644
--- a/src/test/regress/sql/starjoin_var_free.sql
+++ b/src/test/regress/sql/starjoin_var_free.sql
@@ -5,17 +5,15 @@
 -- pseudoconstant, evaluated only once (in a gating Result node).  But it gets
 -- the relids of the whole join domain, so it's in the joininfo list of every
 -- rel, and have_relevant_joinclause() finds a join clause between any two
--- rels.  So every dimension fails the check that it has no join clauses to
--- rels other than the fact (see generate_starjoin_clusters), and the query
--- has no starjoin clusters at all.  The dimensions then count fully toward
--- geqo_threshold, and the standard join search does not use the canonical
--- order.
+-- rels.  The starjoin clusters ignore such quals, as otherwise every
+-- dimension would fail the check that it has no join clauses to rels other
+-- than the fact (see generate_starjoin_clusters), and the query would have no
+-- starjoin clusters at all.
 --
--- With enable_starjoin_ordering disabled, the qual makes the standard join
--- search consider all subsets of the rels too, so this is not a regression.
--- The qual also makes the dimensions joinable to each other, so the standard
--- join search would consider all subsets of the dimensions, if joinrels with
--- a dimension but without the fact were allowed (see starjoin_order_invalid).
+-- The qual still makes the dimensions joinable to each other, so the
+-- standard join search would consider all subsets of the dimensions, if
+-- joinrels with a dimension but without the fact were allowed (see
+-- starjoin_order_invalid).  The planning would then use much more memory.
 --
 -- 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
@@ -39,6 +37,17 @@ EXCEPTION WHEN internal_error THEN
 END;
 $$;
 
+-- memory used by the planning (in kB)
+CREATE FUNCTION starjoin_var_free_planning_memory(query text) RETURNS bigint
+LANGUAGE plpgsql SET enable_starjoin_ordering = on AS $$
+DECLARE
+  plan json;
+BEGIN
+  EXECUTE 'EXPLAIN (FORMAT JSON, MEMORY, COSTS OFF) ' || query INTO plan;
+  RETURN (plan->0->'Planning'->>'Memory Used')::bigint;
+END;
+$$;
+
 -- star with 11 dimensions d1-d11
 DO $$
 BEGIN
@@ -73,8 +82,8 @@ ANALYZE starjoin_var_free_f;
 --
 -- With join_collapse_limit = 20 the star with 11 dimensions (12 rels) is a
 -- single subproblem, reaching the default geqo_threshold = 12.  But the
--- dimensions count as only 4 rels, so without a Var-free qual the star is
--- planned by the standard join search.
+-- dimensions count as only 4 rels, so the star is planned by the standard
+-- join search, even with a Var-free qual.
 --
 SET join_collapse_limit = 20;
 
@@ -98,24 +107,30 @@ SELECT starjoin_var_free_geqo_used(:'q_star', false) AS star_geqo_off;
 -- the dimensions count as fewer rels toward geqo_threshold
 SELECT NOT starjoin_var_free_geqo_used(:'q_star', true) AS star_standard_search;
 
--- XXX a pseudoconstant qual disables the starjoin clusters
+-- a pseudoconstant qual does not disable the starjoin clusters
 SELECT NOT starjoin_var_free_geqo_used(:'q_star' || $$WHERE now() > '2000-01-01'$$, true)
        AS where_now_standard_search;
 
--- XXX the same for an uncorrelated sub-SELECT (an InitPlan)
+-- the same for an uncorrelated sub-SELECT (an InitPlan)
 SELECT NOT starjoin_var_free_geqo_used(:'q_star' ||
          'WHERE EXISTS (SELECT 1 FROM starjoin_var_free_d1 WHERE id = 0)', true)
        AS where_exists_standard_search;
 
--- XXX the same for a qual in the ON clause of one dimension's join, which
--- excludes all the dimensions, not just that one
+-- the same for a qual in the ON clause of one dimension's join
 SELECT NOT starjoin_var_free_geqo_used(replace(:'q_star', 'f.d11 = d11.id',
          $$f.d11 = d11.id AND now() > '2000-01-01'$$), true)
        AS join_on_standard_search;
 
+-- the qual does not make the standard join search consider all subsets of
+-- the dimensions
+SELECT starjoin_var_free_planning_memory(:'q_star' || $$WHERE now() > '2000-01-01'$$)
+       < 2 * starjoin_var_free_planning_memory(:'q_star')
+       AS where_now_planning_memory;
+
 RESET join_collapse_limit;
 
 DROP FUNCTION starjoin_var_free_geqo_used(text, bool);
+DROP FUNCTION starjoin_var_free_planning_memory(text);
 
 DROP TABLE starjoin_var_free_f, starjoin_var_free_d1, starjoin_var_free_d2,
   starjoin_var_free_d3, starjoin_var_free_d4, starjoin_var_free_d5,
-- 
2.56.1

