From 5dab9728d4f9f2cd60618d3d4481aa0dbfc1bfcd 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 18/30] FK: Relax the collation check for starjoin
 dimensions

The collations of the referenced column, of the unique index and of the
join all had to agree on equality.  That rejects some joins that are
still single-row lookups, such as a join under "C" to a "C" key with a
case-insensitive unique index.

Two weaker conditions are sufficient.  Values equal under the collation
of the referenced column must be equal under the join's, so that the
join finds the referenced row.  Values equal under the join's collation
must be equal under the index's, so that it finds no other row.
Equality under a deterministic collation is byte-wise equality, which
implies equality under any collation.

Update STARJOINS.md.  Add starjoin_fk_collation checks with a "C" key
and a case-insensitive unique index, where joins under either collation
qualify, while an accent-insensitive join can match two rows and does
not.
---
 STARJOINS.md                                  | 26 +++++++++----
 src/backend/optimizer/plan/initsplan.c        | 29 ++++++++++----
 .../expected/starjoin_fk_collation.out        | 38 +++++++++++++++++++
 .../regress/sql/starjoin_fk_collation.sql     | 29 ++++++++++++++
 4 files changed, 108 insertions(+), 14 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index 2353cbf61ab..1d194d405aa 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -57,8 +57,8 @@ The optimization consists of these parts:
 - `has_starjoin_fk` looks for a foreign key of the fact that makes a
   relation its dimension, using `has_extra_starjoin_quals` to check that
   the foreign key accounts for all the join clauses between them.
-- `starjoin_fk_collations_match` checks the collations of the join
-  clauses.
+- `starjoin_fk_collations_match` and `starjoin_equality_implies` check
+  the collations of the join clauses.
 - `starjoin_order_invalid`, called by `make_join_rel`, rejects joins
   violating the canonical order, just like various other join
   restrictions.
@@ -375,10 +375,19 @@ the foreign key (`conindid`), and matches its keys to the referenced
 columns, not to the foreign key positions (REFERENCES can list the
 columns in a different order).  If the index is not available to the
 planner (not in the index list of the dimension), the foreign key is not
-used.  The collations of the referenced column, the index, and the join
-clauses (the matched equivalence class or loose comparisons) then have
-to agree on equality.  That still accepts two different deterministic
-collations, or the same nondeterministic one.
+used.
+
+Two directional implications are checked for each key column.  Values
+equal under the collation of the referenced column must be equal under
+the collation of the join, so that the join finds the referenced row.
+Values equal under the collation of the join must be equal under the
+collation of the index, so that the join finds no other row.  Requiring
+all three collations to agree on equality would reject joins that are
+still single-row lookups, such as a join under "C" to a "C" key with a
+case-insensitive unique index.  `starjoin_equality_implies` accepts
+collations that agree on equality, and a deterministic first collation,
+as equality under a deterministic collation is byte-wise equality, which
+implies equality under any collation.
 
 These checks change only which relations are treated as dimensions, not
 foreign key enforcement or selectivity estimation.
@@ -536,4 +545,7 @@ the plans, the planning cost, or the regression tests.
     both the column and the index qualifies, on a composite key with the
     index columns in a different order.  An index with a collation
     override shows that checking only the column or only the index is
-    not enough (one join matches two rows, the other none).
+    not enough (one join matches two rows, the other none).  Also a "C"
+    key with a case-insensitive unique index, where joins under either
+    collation qualify, while an accent-insensitive join can match two
+    rows and does not.
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index dc1d1f60d65..fb2d8749338 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -151,6 +151,7 @@ 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);
+static bool starjoin_equality_implies(Oid coll1, Oid coll2);
 static bool starjoin_fk_collations_match(PlannerInfo *root,
 										 RelOptInfo *dim_rel,
 										 ForeignKeyOptInfo *fkinfo);
@@ -4200,10 +4201,25 @@ has_extra_starjoin_quals(PlannerInfo *root, RelOptInfo *dim_rel,
 	return false;
 }
 
+/*
+ * Does equality under coll1 imply equality under coll2?  A deterministic
+ * collation treats only byte-wise equal strings as equal, and those are
+ * equal under any collation.
+ */
+static bool
+starjoin_equality_implies(Oid coll1, Oid coll2)
+{
+	if (collations_agree_on_equality(coll1, coll2))
+		return true;
+
+	return get_collation_isdeterministic(coll1);
+}
+
 /*
  * The FK guarantees a match under the referenced column's equality, while
- * uniqueness is enforced under the index key's equality.  A join using a
- * different collation must agree with both.
+ * uniqueness is enforced under the index key's equality.  So a row matching
+ * under the referenced column's equality must also match under the join's,
+ * and rows matching under the join's must be equal under the index key's.
  */
 static bool
 starjoin_fk_collations_match(PlannerInfo *root, RelOptInfo *dim_rel,
@@ -4240,14 +4256,12 @@ starjoin_fk_collations_match(PlannerInfo *root, RelOptInfo *dim_rel,
 
 			get_atttypetypmodcoll(dim_rte->relid, attno,
 								  &typid, &typmod, &attcoll);
-			if (!collations_agree_on_equality(attcoll, indcoll))
-				return false;
-
 			if (fkinfo->eclass[i] != NULL)
 			{
 				Oid			joincoll = fkinfo->eclass[i]->ec_collation;
 
-				if (!collations_agree_on_equality(attcoll, joincoll))
+				if (!starjoin_equality_implies(attcoll, joincoll) ||
+					!starjoin_equality_implies(joincoll, indcoll))
 					return false;
 			}
 			else
@@ -4257,7 +4271,8 @@ starjoin_fk_collations_match(PlannerInfo *root, RelOptInfo *dim_rel,
 					Oid			joincoll;
 
 					joincoll = exprInputCollation((Node *) rinfo->clause);
-					if (!collations_agree_on_equality(attcoll, joincoll))
+					if (!starjoin_equality_implies(attcoll, joincoll) ||
+						!starjoin_equality_implies(joincoll, indcoll))
 						return false;
 				}
 			}
diff --git a/src/test/regress/expected/starjoin_fk_collation.out b/src/test/regress/expected/starjoin_fk_collation.out
index 91e0d572709..b4e10980d6c 100644
--- a/src/test/regress/expected/starjoin_fk_collation.out
+++ b/src/test/regress/expected/starjoin_fk_collation.out
@@ -145,4 +145,42 @@ SELECT (:q_label) AS column_collation_matches,
                         2 | f
 (1 row)
 
+-- A unique index can enforce a weaker equality than its column's, here
+-- case-insensitive uniqueness of case-sensitive glyphs.  The FK guarantees
+-- a match under the column's equality, and the index allows at most one
+-- under its own, so a join under either finds just the referenced row.
+-- An accent-insensitive join can also match the accented letter.
+CREATE COLLATION pg_temp.starjoin_ai
+  (provider = icu, locale = 'und-u-ks-level1', deterministic = false);
+CREATE TEMP TABLE glyphs (k text COLLATE "C");
+CREATE UNIQUE INDEX ON glyphs (k COLLATE pg_temp.starjoin_ci);
+INSERT INTO glyphs VALUES ('e'), ('é');
+CREATE TEMP TABLE marks (
+  a int NOT NULL REFERENCES d1, b int NOT NULL REFERENCES d2,
+  c int NOT NULL REFERENCES d3, d int NOT NULL REFERENCES d4,
+  k text COLLATE "C" NOT NULL REFERENCES glyphs (k)
+);
+INSERT INTO marks VALUES (1, 1, 1, 1, 'e');
+SELECT $$SELECT count(*) FROM marks f JOIN glyphs g ON f.k = g.k
+  JOIN d1 ON f.a = d1.id JOIN d2 ON f.b = d2.id
+  JOIN d3 ON f.c = d3.id JOIN d4 ON f.d = d4.id$$ AS q_glyph \gset
+SELECT replace(:'q_glyph', 'f.k = g.k', 'f.k COLLATE pg_temp.starjoin_ci = g.k')
+  AS q_glyph_ci \gset
+SELECT replace(:'q_glyph', 'f.k = g.k', 'f.k COLLATE pg_temp.starjoin_ai = g.k')
+  AS q_glyph_ai \gset
+SELECT (:q_glyph) AS c_matches, (:q_glyph_ci) AS ci_matches,
+       (:q_glyph_ai) AS ai_matches;
+ c_matches | ci_matches | ai_matches 
+-----------+------------+------------
+         1 |          1 |          2
+(1 row)
+
+SELECT pg_temp.dimensions_qualify(:'q_glyph') AS c_qualifies,
+       pg_temp.dimensions_qualify(:'q_glyph_ci') AS ci_qualifies,
+       NOT pg_temp.dimensions_qualify(:'q_glyph_ai') AS ai_does_not_qualify;
+ c_qualifies | ci_qualifies | ai_does_not_qualify 
+-------------+--------------+---------------------
+ t           | t            | t
+(1 row)
+
 ROLLBACK;
diff --git a/src/test/regress/sql/starjoin_fk_collation.sql b/src/test/regress/sql/starjoin_fk_collation.sql
index 2bfb64d3a1d..585c3275acc 100644
--- a/src/test/regress/sql/starjoin_fk_collation.sql
+++ b/src/test/regress/sql/starjoin_fk_collation.sql
@@ -113,4 +113,33 @@ SELECT (:q_index) AS index_collation_matches,
 INSERT INTO labels VALUES ('a', 'x');
 SELECT (:q_label) AS column_collation_matches,
        pg_temp.dimensions_qualify(:'q_label') AS qualifies;
+
+-- A unique index can enforce a weaker equality than its column's, here
+-- case-insensitive uniqueness of case-sensitive glyphs.  The FK guarantees
+-- a match under the column's equality, and the index allows at most one
+-- under its own, so a join under either finds just the referenced row.
+-- An accent-insensitive join can also match the accented letter.
+CREATE COLLATION pg_temp.starjoin_ai
+  (provider = icu, locale = 'und-u-ks-level1', deterministic = false);
+CREATE TEMP TABLE glyphs (k text COLLATE "C");
+CREATE UNIQUE INDEX ON glyphs (k COLLATE pg_temp.starjoin_ci);
+INSERT INTO glyphs VALUES ('e'), ('é');
+CREATE TEMP TABLE marks (
+  a int NOT NULL REFERENCES d1, b int NOT NULL REFERENCES d2,
+  c int NOT NULL REFERENCES d3, d int NOT NULL REFERENCES d4,
+  k text COLLATE "C" NOT NULL REFERENCES glyphs (k)
+);
+INSERT INTO marks VALUES (1, 1, 1, 1, 'e');
+SELECT $$SELECT count(*) FROM marks f JOIN glyphs g ON f.k = g.k
+  JOIN d1 ON f.a = d1.id JOIN d2 ON f.b = d2.id
+  JOIN d3 ON f.c = d3.id JOIN d4 ON f.d = d4.id$$ AS q_glyph \gset
+SELECT replace(:'q_glyph', 'f.k = g.k', 'f.k COLLATE pg_temp.starjoin_ci = g.k')
+  AS q_glyph_ci \gset
+SELECT replace(:'q_glyph', 'f.k = g.k', 'f.k COLLATE pg_temp.starjoin_ai = g.k')
+  AS q_glyph_ai \gset
+SELECT (:q_glyph) AS c_matches, (:q_glyph_ci) AS ci_matches,
+       (:q_glyph_ai) AS ai_matches;
+SELECT pg_temp.dimensions_qualify(:'q_glyph') AS c_qualifies,
+       pg_temp.dimensions_qualify(:'q_glyph_ci') AS ci_qualifies,
+       NOT pg_temp.dimensions_qualify(:'q_glyph_ai') AS ai_does_not_qualify;
 ROLLBACK;
-- 
2.56.1

