From 1fed961d29353d9c69538d503d9abaa1dab62d9e 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 06/30] Accept fact-only members in starjoin dimension ECs

Matching equivalence classes with extra members were rejected, as an
additional equality can be merged into the equivalence class of the
foreign key.  But a member of the fact alone, as in
"f.a = d.id AND f.a = f.b", does not make the dimension join selective.
Equalities among the members of the fact only filter fact rows, and each
remaining fact row matches the same dimension row, whichever member a
join clause uses.

Accept matching equivalence classes whose members other than the foreign
key column of the dimension are expressions of the fact alone.
Constants, expressions of several relations, other members of the
dimension, and members of other relations still disqualify the
dimension.  A broken equivalence class disqualifies it too, as it
applies its source clauses as written, so a clause comparing the
dimension with another fact column can reject fact rows in the dimension
join.

Update STARJOINS.md.  Add starjoin_other checks with an equality between
two fact columns joining the equivalence class of the foreign key (the
dimension qualifies), and with a row-level security policy breaking it
(the dimension does not qualify).
---
 STARJOINS.md                                 | 22 ++++++-
 src/backend/optimizer/plan/initsplan.c       | 33 +++++++++-
 src/test/regress/expected/starjoin_other.out | 63 ++++++++++++++++++++
 src/test/regress/sql/starjoin_other.sql      | 59 ++++++++++++++++++
 4 files changed, 173 insertions(+), 4 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index d271e84e390..72dd4541e63 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -192,7 +192,8 @@ The fact has to be a base relation too.
 
 8. All join conditions between the fact and the dimension are accounted
    for by the matched foreign key, with no additional equivalence class
-   members.
+   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,
@@ -203,6 +204,25 @@ The fact has to be a base relation too.
    class of the foreign key with additional members, as an additional
    equality can be merged into it.
 
+   But a member of the fact alone, as in "f.a = d.id AND f.a = f.b",
+   does not make the join selective.  All members of an equivalence
+   class are compared with the same operator family and collation, and
+   that equality is transitive, so equalities among the members of the
+   fact only filter fact rows.  Each remaining fact row matches the same
+   dimension row, whichever member a join clause uses.  Constants,
+   expressions of several relations, other members of the dimension, and
+   members of other relations still disqualify the dimension.
+
+   A broken equivalence class (without a usable equality operator for
+   some pair of its members) disqualifies the dimension too.  It applies
+   its source clauses as written, instead of equating the members of the
+   fact at the fact scan, so a clause comparing the dimension with
+   another fact column can reject fact rows in the dimension join.
+   Security quals can break the equivalence class of the foreign key:
+   once the quals of the query join an equivalence class, only leakproof
+   equalities may be derived, so a row-level security policy equating a
+   date and a timestamp column of the fact breaks it.
+
 ### Foreign key requirements
 
 If the fact has several foreign keys referencing the dimension, the
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index 5a36409dd67..8a4e994ed16 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -4141,20 +4141,46 @@ has_extra_starjoin_quals(PlannerInfo *root, RelOptInfo *dim_rel,
 	/*
 	 * 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.
+	 * only ECs whose members other than the dimension's FK column expression
+	 * belong to the fact alone.  Equalities among those only filter fact
+	 * rows, and the EC's equality is transitive.
 	 */
 	i = -1;
 	while ((i = bms_next_member(dim_rel->eclass_indexes, i)) >= 0)
 	{
 		EquivalenceClass *ec = list_nth_node(EquivalenceClass,
 											 root->eq_classes, i);
+		int			ndim = 0;
 		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)
+		/*
+		 * A broken EC (one lacking a usable equality operator for some pair
+		 * of members, e.g. because security quals allow only leakproof ones)
+		 * does not equate the fact's members at the fact scan.  It applies
+		 * its source clauses as written instead, so a clause comparing the
+		 * dimension with another fact column can reject fact rows in the
+		 * dimension join.
+		 */
+		if (ec->ec_broken)
+			return true;
+
+		foreach_node(EquivalenceMember, em, ec->ec_members)
+		{
+			int			relid;
+
+			/* a constant, or an expression of several rels */
+			if (!bms_get_singleton_member(em->em_relids, &relid))
+				return true;
+			if (relid == dim_rel->relid)
+				ndim++;
+			else if (relid != fkinfo->con_relid)
+				return true;
+		}
+		if (ndim != 1)
 			return true;
 
 		for (colno = 0; colno < fkinfo->nkeys; colno++)
@@ -4195,7 +4221,8 @@ has_extra_starjoin_quals(PlannerInfo *root, 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 no additional EC members.
+ *	   for by the matched FK, with 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
diff --git a/src/test/regress/expected/starjoin_other.out b/src/test/regress/expected/starjoin_other.out
index 6e3178d608e..192a5cafc72 100644
--- a/src/test/regress/expected/starjoin_other.out
+++ b/src/test/regress/expected/starjoin_other.out
@@ -192,6 +192,69 @@ SELECT starjoin_other_plan(:'q_extra_ec_member', true)
  t
 (1 row)
 
+-- EC members of the fact alone only filter fact rows.  The dimension still
+-- qualifies, so the search uses about as much memory as the canonical order.
+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.d3 = pf.x
+$$ AS q_fact_ec_member \gset
+SET join_collapse_limit = 1;
+SELECT starjoin_other_memory(:'q_fact_ec_member', true) AS memory_fact_ec_member \gset
+RESET join_collapse_limit;
+SELECT starjoin_other_memory(:'q_fact_ec_member', true)
+       <= :memory_fact_ec_member * 1.1 AS fact_ec_member_canonical;
+ fact_ec_member_canonical 
+--------------------------
+ t
+(1 row)
+
+-- A broken EC applies its source clauses as written, instead of equating the
+-- fact's members at the fact scan, so here bf.x = bk.k filters in the
+-- dimension join.  Security quals can break the EC: the policy's date =
+-- timestamp equality isn't leakproof, and once the query's quals join the
+-- EC, it may derive only leakproof equalities.  The dimension must not
+-- qualify, so that the selective join can go first.
+CREATE ROLE regress_starjoin_other;
+SET ROLE regress_starjoin_other;
+CREATE TEMP TABLE starjoin_other_bd (id int PRIMARY KEY);
+CREATE TEMP TABLE starjoin_other_bk (k date PRIMARY KEY);
+CREATE TEMP TABLE starjoin_other_bf (
+  d1 int NOT NULL REFERENCES starjoin_other_bd,
+  d2 int NOT NULL REFERENCES starjoin_other_bd,
+  d3 int NOT NULL REFERENCES starjoin_other_bd,
+  k date NOT NULL REFERENCES starjoin_other_bk,
+  ts timestamp NOT NULL, x date NOT NULL);
+INSERT INTO starjoin_other_bd SELECT i FROM generate_series(1, 1000) i;
+INSERT INTO starjoin_other_bk
+SELECT date '2000-01-01' + i FROM generate_series(1, 1000) i;
+INSERT INTO starjoin_other_bf
+SELECT 1 + i % 1000, 1 + (i * 7) % 1000, 1 + (i * 13) % 1000,
+       date '2000-01-01' + 1 + i % 1000, date '2000-01-01' + 1 + i % 1000,
+       date '2000-01-01' + i % 5000
+  FROM generate_series(1, 10000) i;
+ANALYZE starjoin_other_bd, starjoin_other_bk, starjoin_other_bf;
+ALTER TABLE starjoin_other_bf ENABLE ROW LEVEL SECURITY;
+ALTER TABLE starjoin_other_bf FORCE ROW LEVEL SECURITY;
+CREATE POLICY starjoin_other_bf_ts ON starjoin_other_bf USING (k = ts);
+SELECT $$
+SELECT count(*) FROM starjoin_other_bf AS bf
+  JOIN starjoin_other_bd AS d1 ON bf.d1 = d1.id
+  JOIN starjoin_other_bd AS d2 ON bf.d2 = d2.id
+  JOIN starjoin_other_bd AS d3 ON bf.d3 = d3.id
+  JOIN starjoin_other_bk AS bk ON bf.k = bk.k AND bf.x = bk.k
+$$ AS q_broken_ec \gset
+SELECT starjoin_other_cost(:'q_broken_ec', true)
+       <= starjoin_other_cost(:'q_broken_ec', false) * 1.01 AS broken_ec_cost_ok;
+ broken_ec_cost_ok 
+-------------------
+ t
+(1 row)
+
+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)
diff --git a/src/test/regress/sql/starjoin_other.sql b/src/test/regress/sql/starjoin_other.sql
index 0519cae0913..7ecfe6d188b 100644
--- a/src/test/regress/sql/starjoin_other.sql
+++ b/src/test/regress/sql/starjoin_other.sql
@@ -209,6 +209,65 @@ $$ 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;
 
+-- EC members of the fact alone only filter fact rows.  The dimension still
+-- qualifies, so the search uses about as much memory as the canonical order.
+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.d3 = pf.x
+$$ AS q_fact_ec_member \gset
+
+SET join_collapse_limit = 1;
+SELECT starjoin_other_memory(:'q_fact_ec_member', true) AS memory_fact_ec_member \gset
+RESET join_collapse_limit;
+SELECT starjoin_other_memory(:'q_fact_ec_member', true)
+       <= :memory_fact_ec_member * 1.1 AS fact_ec_member_canonical;
+
+-- A broken EC applies its source clauses as written, instead of equating the
+-- fact's members at the fact scan, so here bf.x = bk.k filters in the
+-- dimension join.  Security quals can break the EC: the policy's date =
+-- timestamp equality isn't leakproof, and once the query's quals join the
+-- EC, it may derive only leakproof equalities.  The dimension must not
+-- qualify, so that the selective join can go first.
+CREATE ROLE regress_starjoin_other;
+SET ROLE regress_starjoin_other;
+CREATE TEMP TABLE starjoin_other_bd (id int PRIMARY KEY);
+CREATE TEMP TABLE starjoin_other_bk (k date PRIMARY KEY);
+CREATE TEMP TABLE starjoin_other_bf (
+  d1 int NOT NULL REFERENCES starjoin_other_bd,
+  d2 int NOT NULL REFERENCES starjoin_other_bd,
+  d3 int NOT NULL REFERENCES starjoin_other_bd,
+  k date NOT NULL REFERENCES starjoin_other_bk,
+  ts timestamp NOT NULL, x date NOT NULL);
+INSERT INTO starjoin_other_bd SELECT i FROM generate_series(1, 1000) i;
+INSERT INTO starjoin_other_bk
+SELECT date '2000-01-01' + i FROM generate_series(1, 1000) i;
+INSERT INTO starjoin_other_bf
+SELECT 1 + i % 1000, 1 + (i * 7) % 1000, 1 + (i * 13) % 1000,
+       date '2000-01-01' + 1 + i % 1000, date '2000-01-01' + 1 + i % 1000,
+       date '2000-01-01' + i % 5000
+  FROM generate_series(1, 10000) i;
+ANALYZE starjoin_other_bd, starjoin_other_bk, starjoin_other_bf;
+ALTER TABLE starjoin_other_bf ENABLE ROW LEVEL SECURITY;
+ALTER TABLE starjoin_other_bf FORCE ROW LEVEL SECURITY;
+CREATE POLICY starjoin_other_bf_ts ON starjoin_other_bf USING (k = ts);
+
+SELECT $$
+SELECT count(*) FROM starjoin_other_bf AS bf
+  JOIN starjoin_other_bd AS d1 ON bf.d1 = d1.id
+  JOIN starjoin_other_bd AS d2 ON bf.d2 = d2.id
+  JOIN starjoin_other_bd AS d3 ON bf.d3 = d3.id
+  JOIN starjoin_other_bk AS bk ON bf.k = bk.k AND bf.x = bk.k
+$$ AS q_broken_ec \gset
+
+SELECT starjoin_other_cost(:'q_broken_ec', true)
+       <= starjoin_other_cost(:'q_broken_ec', false) * 1.01 AS broken_ec_cost_ok;
+
+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)
-- 
2.56.1

