From 1b337d462da66fc78d49fe06ea2f275f97870ecd 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 27/30] PSEUDOCONST: Ignore pseudoconstants for join
 connectivity

Pseudoconstant quals remain in the regular RestrictInfo lists, so that
required_relids can determine where their gating Result nodes belong.
For flat inner joins, that set can cover the entire join domain, even
though the qual contains no Vars of the current query level.  The
starjoin cluster detection already ignores these quals, but
have_relevant_joinclause() still mistakes the placement relids for join
connectivity, so unrelated rels appear connected to each other in the
join search.

Skip RestrictInfos marked pseudoconstant in have_relevant_joinclause(),
and use it for the cluster detection too, removing the now-redundant
have_relevant_starjoin_clause().  Do not remove the quals from their
lists or change required_relids, so that the gates are still placed and
evaluated at the appropriate semantic level.  Use the pseudoconstant
flag, not just an empty Var set, so that volatile Var-free quals and
non-gating outer-join ON quals retain their treatment.

This is deliberately a generic planner change, not conditional on
enable_starjoin_ordering.  In 1500 random star queries, it makes 384 of
the 603 plans with a pseudoconstant qual more expensive with the GUC
disabled (by up to 16%), as they relied on the qual making all the rels
joinable to each other.

Update STARJOINS.md, also describing the effect on other random queries.
Add starjoin_var_free checks with a volatile negative control, the
execution-time TRUE, FALSE and NULL gates with the GUC enabled and
disabled, in the standard join search and in GEQO, the outer-join ON
semantics, and the cartesian fallback needed by a subproblem without the
fact.
---
 STARJOINS.md                                  |  57 +++++++---
 src/backend/optimizer/path/joinrels.c         |  21 ++--
 src/backend/optimizer/plan/initsplan.c        |  41 +------
 src/backend/optimizer/util/joininfo.c         |   6 +
 .../regress/expected/starjoin_var_free.out    | 105 ++++++++++++++----
 src/test/regress/sql/starjoin_var_free.sql    |  80 +++++++++----
 6 files changed, 203 insertions(+), 107 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index fee20114c28..321a3870c59 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -46,7 +46,9 @@ The optimization consists of these parts:
 - The `enable_starjoin_ordering` GUC (on by default), documented with
   the other planner method configuration parameters.  With the GUC
   disabled, no clusters are built, so the join search is not restricted.
-  The dimensions then count fully toward `geqo_threshold`.
+  The dimensions then count fully toward `geqo_threshold`.  Ignoring
+  pseudoconstant quals for join connectivity does not depend on the GUC
+  (see below).
 - `generate_starjoin_clusters` determines the starjoin clusters of the
   query.  `query_planner` calls it after matching the foreign keys to
   the join clauses, once the lateral, placeholder and special join
@@ -60,8 +62,6 @@ 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.
@@ -272,16 +272,33 @@ 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.
+on `geqo_threshold`.
+
+`have_relevant_joinclause` ignores RestrictInfos marked pseudoconstant.
+The quals remain in the regular RestrictInfo lists, so that
+`required_relids` can determine where their gating Result nodes belong.
+For flat inner joins, that set can cover the entire join domain, even
+though the qual contains no Vars of the current query level.  Otherwise
+`have_relevant_joinclause` would mistake the placement relids for join
+connectivity, and unrelated rels would appear connected.  The cluster
+detection uses the same function for condition 4, while
+`has_extra_starjoin_quals` ignores the quals for condition 8.  The quals
+are not removed from their lists, and `required_relids` is not changed,
+so that the existing clause propagation and plan creation still place
+and evaluate the gates at the appropriate semantic level.  The
+pseudoconstant flag is used, not just an empty Var set, so volatile
+Var-free quals and non-gating outer-join ON quals retain their
+treatment.  This is deliberately a generic planner change, not
+conditional on the GUC.  It affects clause-based join enumeration in the
+standard join search and GEQO, including queries without starjoin
+clusters, and with the GUC disabled.  In the 1500 random star queries,
+it makes 384 of the 603 plans with a pseudoconstant qual more expensive
+with the GUC disabled (by up to 16%), and 1 cheaper (by 38%).  In the
+three other corpora, it makes 45 plans more expensive with the GUC
+disabled (by up to 25%, or 42% for a query planned by GEQO), and 11
+cheaper (by up to 1.9%).  The plans that get more expensive relied on
+the qual making all the rels joinable to each other.  The plans of
+queries without a pseudoconstant qual do not change.
 
 ## Conditions for a dimension
 
@@ -328,7 +345,7 @@ The fact has to be a base relation too.
 
 4. The dimension has no relevant join clauses to any rel other than the
    fact (including equivalence class joins), ignoring pseudoconstant
-   quals (see `have_relevant_starjoin_clause`).
+   quals (see `have_relevant_joinclause`).
 
    So the dimension can be joined only to the fact (or to joinrels
    including it), which makes the dimensions interchangeable.
@@ -557,6 +574,12 @@ the plans, the planning cost, or the regression tests.
   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.
+- Ignoring pseudoconstant quals in `have_relevant_joinclause` changes
+  the plans also with the GUC disabled.  It makes 384 of the 603 plans
+  with a pseudoconstant qual in the 1500 random star queries more
+  expensive (by up to 16%), and 45 plans in the three other corpora (by
+  up to 25%, or 42% for a query planned by GEQO), as they relied on the
+  qual making all the rels joinable to each other.
 
 ## Regression tests
 
@@ -666,4 +689,8 @@ the plans, the planning cost, or the regression tests.
   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.  It also checks that such a qual does not double the planning
-  memory of the star with 11 dimensions.
+  memory of the star with 11 dimensions.  It has a volatile negative
+  control, and checks the execution-time TRUE, FALSE and NULL gates with
+  the GUC enabled and disabled, in the standard join search and in GEQO,
+  the outer-join ON semantics, and the cartesian fallback needed by a
+  subproblem without the fact.
diff --git a/src/backend/optimizer/path/joinrels.c b/src/backend/optimizer/path/joinrels.c
index 0f862d5092b..55029d1bbcb 100644
--- a/src/backend/optimizer/path/joinrels.c
+++ b/src/backend/optimizer/path/joinrels.c
@@ -2319,11 +2319,9 @@ 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, 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).
+		 * 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.
 		 *
 		 * 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
@@ -2339,13 +2337,12 @@ 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, 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
+		 * 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
 		 * 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 d59b15e9987..58a451d77ab 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -4128,45 +4128,10 @@ 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.
+ * Pseudoconstant gating quals do not relate fact and dimension rows.
  */
 static bool
 has_extra_starjoin_quals(PlannerInfo *root, RelOptInfo *dim_rel,
@@ -4539,7 +4504,7 @@ has_starjoin_fk(PlannerInfo *root, RelOptInfo *fact_rel, RelOptInfo *dim_rel)
  *
  *	4. The dimension has no relevant join clauses to any rel other than
  *	   the fact (including equivalence-class joins), ignoring pseudoconstant
- *	   quals (see have_relevant_starjoin_clause).
+ *	   quals (see have_relevant_joinclause).
  *
  *	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).
@@ -4736,7 +4701,7 @@ generate_starjoin_clusters(PlannerInfo *root)
 				if (other_rel == NULL ||
 					other_rel->reloptkind != RELOPT_BASEREL)
 					continue;
-				if (have_relevant_starjoin_clause(root, dim_rel, other_rel))
+				if (have_relevant_joinclause(root, dim_rel, other_rel))
 				{
 					ok = false;
 					break;
diff --git a/src/backend/optimizer/util/joininfo.c b/src/backend/optimizer/util/joininfo.c
index f75e35f22ae..8ecde11bec6 100644
--- a/src/backend/optimizer/util/joininfo.c
+++ b/src/backend/optimizer/util/joininfo.c
@@ -27,6 +27,9 @@
  *		Detect whether there is a joinclause that involves
  *		the two given relations.
  *
+ * Pseudoconstant quals are not join clauses for this purpose: their
+ * required_relids control placement, not join connectivity.
+ *
  * Note: the joinclause does not have to be evaluable with only these two
  * relations.  This is intentional.  For example consider
  *		SELECT * FROM a, b, c WHERE a.x = (b.y + c.z)
@@ -63,6 +66,9 @@ have_relevant_joinclause(PlannerInfo *root,
 	{
 		RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
+		if (rinfo->pseudoconstant)
+			continue;
+
 		if (bms_overlap(other_relids, rinfo->required_relids))
 		{
 			result = true;
diff --git a/src/test/regress/expected/starjoin_var_free.out b/src/test/regress/expected/starjoin_var_free.out
index 6130062e9f6..7b98672ea25 100644
--- a/src/test/regress/expected/starjoin_var_free.out
+++ b/src/test/regress/expected/starjoin_var_free.out
@@ -1,23 +1,16 @@
 --
 -- Checks for star queries with Var-free quals and enable_starjoin_ordering
 --
--- A Var-free qual that is not volatile, e.g. "now() > '2000-01-01'", is a
--- 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.  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.
+-- A qual marked pseudoconstant is evaluated as a one-time qual in a gating
+-- Result node.  Although its required_relids can cover the whole join domain,
+-- it is not a join condition linking those relations.  Ignore it for both
+-- starjoin eligibility and join enumeration, without changing where it is
+-- evaluated.  Such quals must neither disable clusters nor cause excessive
+-- planning memory use.
 --
--- 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
--- GEQO by making it fail (see starjoin_var_free_geqo_used).
+-- EXPLAIN does not show whether a query was planned by GEQO, so the search
+-- selection checks detect GEQO by making it fail (see
+-- starjoin_var_free_geqo_used).
 --
 -- is the query planned by GEQO? (with enable_starjoin_ordering
 -- enabled/disabled)  GEQO fails to allocate a population this large, while
@@ -45,6 +38,18 @@ BEGIN
   RETURN (plan->0->'Planning'->>'Memory Used')::bigint;
 END;
 $$;
+-- Execute with both search strategies for the eleven-dimension star.
+CREATE FUNCTION starjoin_var_free_check(query text,
+                                        OUT starjoin_on text,
+                                        OUT starjoin_off text)
+LANGUAGE plpgsql AS $$
+BEGIN
+  PERFORM set_config('enable_starjoin_ordering', 'on', true);
+  EXECUTE format('SELECT row(q.*)::text FROM (%s) q', query) INTO starjoin_on;
+  PERFORM set_config('enable_starjoin_ordering', 'off', true);
+  EXECUTE format('SELECT row(q.*)::text FROM (%s) q', query) INTO starjoin_off;
+END;
+$$;
 -- star with 11 dimensions d1-d11
 DO $$
 BEGIN
@@ -75,8 +80,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 the star is planned by the standard
--- join search, even with a Var-free qual.
+-- dimensions count as only 4 rels, so even with a pseudoconstant qual the
+-- star is planned by the standard join search.
 --
 SET join_collapse_limit = 20;
 SELECT $$
@@ -107,7 +112,7 @@ SELECT NOT starjoin_var_free_geqo_used(:'q_star', true) AS star_standard_search;
  t
 (1 row)
 
--- a pseudoconstant qual does not disable the starjoin clusters
+-- A stable Var-free WHERE 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 
@@ -115,7 +120,7 @@ SELECT NOT starjoin_var_free_geqo_used(:'q_star' || $$WHERE now() > '2000-01-01'
  t
 (1 row)
 
--- the same for an uncorrelated sub-SELECT (an InitPlan)
+-- The same applies to 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;
@@ -124,7 +129,7 @@ SELECT NOT starjoin_var_free_geqo_used(:'q_star' ||
  t
 (1 row)
 
--- the same for a qual in the ON clause of one dimension's join
+-- A pseudoconstant in an inner-join ON clause is also a gating qual.
 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;
@@ -143,9 +148,67 @@ SELECT starjoin_var_free_planning_memory(:'q_star' || $$WHERE now() > '2000-01-0
  t
 (1 row)
 
+-- A volatile Var-free qual is not pseudoconstant and still excludes dimensions.
+SELECT starjoin_var_free_geqo_used(:'q_star' || 'WHERE random() < 0.5', true)
+       AS where_volatile_geqo;
+ where_volatile_geqo 
+---------------------
+ t
+(1 row)
+
+-- Execution-time TRUE, FALSE and NULL gates must retain their filtering effect,
+-- with standard search (starjoin on) and GEQO (starjoin off).
+SELECT * FROM starjoin_var_free_check(:'q_star' ||
+         $$WHERE now() > '-infinity'::timestamptz$$);
+ starjoin_on | starjoin_off 
+-------------+--------------
+ (100)       | (100)
+(1 row)
+
+SELECT * FROM starjoin_var_free_check(:'q_star' ||
+         'WHERE EXISTS (SELECT 1 FROM starjoin_var_free_d1 WHERE id = 0)');
+ starjoin_on | starjoin_off 
+-------------+--------------
+ (0)         | (0)
+(1 row)
+
+SELECT * FROM starjoin_var_free_check(:'q_star' ||
+         'WHERE (SELECT bool_or(id > 0) FROM starjoin_var_free_d1 WHERE id = 0)');
+ starjoin_on | starjoin_off 
+-------------+--------------
+ (0)         | (0)
+(1 row)
+
+-- A Var-free outer-join ON qual must not become a gate above the outer join.
+SELECT * FROM starjoin_var_free_check($$
+SELECT count(*), count(d1.id)
+  FROM starjoin_var_free_f AS f
+  LEFT JOIN (starjoin_var_free_d1 AS d1 CROSS JOIN starjoin_var_free_d2 AS d2)
+    ON f.d1 = d1.id AND f.d2 = d2.id AND now() < '-infinity'::timestamptz
+$$);
+ starjoin_on | starjoin_off 
+-------------+--------------
+ (100,0)     | (100,0)
+(1 row)
+
+-- A fact-free subproblem still needs the Cartesian-product fallback.
+SET join_collapse_limit = 1;
+SELECT * FROM starjoin_var_free_check($$
+SELECT count(*)
+  FROM starjoin_var_free_f AS f
+  JOIN (starjoin_var_free_d1 AS d1 CROSS JOIN starjoin_var_free_d2 AS d2)
+    ON f.d1 = d1.id AND f.d2 = d2.id
+ WHERE now() > '-infinity'::timestamptz
+$$);
+ starjoin_on | starjoin_off 
+-------------+--------------
+ (100)       | (100)
+(1 row)
+
 RESET join_collapse_limit;
 DROP FUNCTION starjoin_var_free_geqo_used(text, bool);
 DROP FUNCTION starjoin_var_free_planning_memory(text);
+DROP FUNCTION starjoin_var_free_check(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_var_free.sql b/src/test/regress/sql/starjoin_var_free.sql
index ef80936aef3..a7bcbff83e7 100644
--- a/src/test/regress/sql/starjoin_var_free.sql
+++ b/src/test/regress/sql/starjoin_var_free.sql
@@ -1,23 +1,16 @@
 --
 -- Checks for star queries with Var-free quals and enable_starjoin_ordering
 --
--- A Var-free qual that is not volatile, e.g. "now() > '2000-01-01'", is a
--- 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.  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.
+-- A qual marked pseudoconstant is evaluated as a one-time qual in a gating
+-- Result node.  Although its required_relids can cover the whole join domain,
+-- it is not a join condition linking those relations.  Ignore it for both
+-- starjoin eligibility and join enumeration, without changing where it is
+-- evaluated.  Such quals must neither disable clusters nor cause excessive
+-- planning memory use.
 --
--- 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
--- GEQO by making it fail (see starjoin_var_free_geqo_used).
+-- EXPLAIN does not show whether a query was planned by GEQO, so the search
+-- selection checks detect GEQO by making it fail (see
+-- starjoin_var_free_geqo_used).
 --
 
 -- is the query planned by GEQO? (with enable_starjoin_ordering
@@ -48,6 +41,19 @@ BEGIN
 END;
 $$;
 
+-- Execute with both search strategies for the eleven-dimension star.
+CREATE FUNCTION starjoin_var_free_check(query text,
+                                        OUT starjoin_on text,
+                                        OUT starjoin_off text)
+LANGUAGE plpgsql AS $$
+BEGIN
+  PERFORM set_config('enable_starjoin_ordering', 'on', true);
+  EXECUTE format('SELECT row(q.*)::text FROM (%s) q', query) INTO starjoin_on;
+  PERFORM set_config('enable_starjoin_ordering', 'off', true);
+  EXECUTE format('SELECT row(q.*)::text FROM (%s) q', query) INTO starjoin_off;
+END;
+$$;
+
 -- star with 11 dimensions d1-d11
 DO $$
 BEGIN
@@ -82,8 +88,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 the star is planned by the standard
--- join search, even with a Var-free qual.
+-- dimensions count as only 4 rels, so even with a pseudoconstant qual the
+-- star is planned by the standard join search.
 --
 SET join_collapse_limit = 20;
 
@@ -107,16 +113,16 @@ 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;
 
--- a pseudoconstant qual does not disable the starjoin clusters
+-- A stable Var-free WHERE 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;
 
--- the same for an uncorrelated sub-SELECT (an InitPlan)
+-- The same applies to 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;
 
--- the same for a qual in the ON clause of one dimension's join
+-- A pseudoconstant in an inner-join ON clause is also a gating qual.
 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;
@@ -127,10 +133,42 @@ SELECT starjoin_var_free_planning_memory(:'q_star' || $$WHERE now() > '2000-01-0
        < 2 * starjoin_var_free_planning_memory(:'q_star')
        AS where_now_planning_memory;
 
+-- A volatile Var-free qual is not pseudoconstant and still excludes dimensions.
+SELECT starjoin_var_free_geqo_used(:'q_star' || 'WHERE random() < 0.5', true)
+       AS where_volatile_geqo;
+
+-- Execution-time TRUE, FALSE and NULL gates must retain their filtering effect,
+-- with standard search (starjoin on) and GEQO (starjoin off).
+SELECT * FROM starjoin_var_free_check(:'q_star' ||
+         $$WHERE now() > '-infinity'::timestamptz$$);
+SELECT * FROM starjoin_var_free_check(:'q_star' ||
+         'WHERE EXISTS (SELECT 1 FROM starjoin_var_free_d1 WHERE id = 0)');
+SELECT * FROM starjoin_var_free_check(:'q_star' ||
+         'WHERE (SELECT bool_or(id > 0) FROM starjoin_var_free_d1 WHERE id = 0)');
+
+-- A Var-free outer-join ON qual must not become a gate above the outer join.
+SELECT * FROM starjoin_var_free_check($$
+SELECT count(*), count(d1.id)
+  FROM starjoin_var_free_f AS f
+  LEFT JOIN (starjoin_var_free_d1 AS d1 CROSS JOIN starjoin_var_free_d2 AS d2)
+    ON f.d1 = d1.id AND f.d2 = d2.id AND now() < '-infinity'::timestamptz
+$$);
+
+-- A fact-free subproblem still needs the Cartesian-product fallback.
+SET join_collapse_limit = 1;
+SELECT * FROM starjoin_var_free_check($$
+SELECT count(*)
+  FROM starjoin_var_free_f AS f
+  JOIN (starjoin_var_free_d1 AS d1 CROSS JOIN starjoin_var_free_d2 AS d2)
+    ON f.d1 = d1.id AND f.d2 = d2.id
+ WHERE now() > '-infinity'::timestamptz
+$$);
+
 RESET join_collapse_limit;
 
 DROP FUNCTION starjoin_var_free_geqo_used(text, bool);
 DROP FUNCTION starjoin_var_free_planning_memory(text);
+DROP FUNCTION starjoin_var_free_check(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

