From 96ab768690389db1d589a00f0dadadf09ec4a8a8 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 24/30] FK: Accept domains over arrays and ranges for
 starjoin dimensions

Commit c5ab31db7a4 requires each operand of a starjoin key comparison to
expose the operator's declared input type or, for polymorphic inputs,
the key column's own type.  However, the parser passes a domain over an
array, range or multirange type to a polymorphic operator such as
anyarray = anyarray by relabeling it to its base type, so keys of such
domain types stopped qualifying, although the relabel doesn't change the
value.

Accept the domain's base type too.  RI's queries cast the key columns to
the operator's input types, so the parser applies the same relabel
there, and the operator gets the same values with the same actual types.

Update STARJOINS.md.  Add starjoin_fk_coercion cases with a domain over
int[] on the referencing side and a domain over int4range on the
referenced side.
---
 STARJOINS.md                                  | 14 ++++++++----
 src/backend/optimizer/plan/initsplan.c        |  7 ++++--
 .../regress/expected/starjoin_fk_coercion.out | 22 +++++++++++++++++--
 src/test/regress/sql/starjoin_fk_coercion.sql | 19 ++++++++++++++--
 4 files changed, 52 insertions(+), 10 deletions(-)

diff --git a/STARJOINS.md b/STARJOINS.md
index 917b8ee39db..ef43f02d657 100644
--- a/STARJOINS.md
+++ b/STARJOINS.md
@@ -445,9 +445,14 @@ checks pass each column to that operator using only binary-coercible
 casts too.  The operator OID alone is not enough, as a binary cast
 through a third type can bypass the implicit cast used by the
 referential integrity checks.  For polymorphic inputs, the relabel must
-not change the actual type seen by the operator either.  The equivalence
-class source clauses not involving the dimension only filter fact rows.
-But such a clause can bring the foreign key column into the equivalence
+not change the actual type seen by the operator either.  The parser
+passes a domain over an array, range or multirange type to a polymorphic
+operator such as "anyarray = anyarray" by relabeling it to its base
+type, so the base type of such a domain is accepted too.  The
+referential integrity checks apply the same relabel, so the operator
+gets the same values with the same actual types.  The equivalence class
+source clauses not involving the dimension only filter fact rows.  But
+such a clause can bring the foreign key column into the equivalence
 class, while the clauses involving the dimension name another fact
 column, so the members that are the foreign key column are checked the
 same way as the operands.  Genuine cross-type operators and
@@ -641,4 +646,5 @@ the plans, the planning cost, or the regression tests.
     loose qual and as an equivalence class (including through another
     fact column).  A commuted name/text comparison, a binary
     varchar-to-text conversion and a fact-only expression in the
-    equivalence class still qualify.
+    equivalence class still qualify.  So do domains over int[] on the
+    referencing side and over int4range on the referenced side.
diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index dbe12a71e13..dd5dc9777f0 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -4273,11 +4273,14 @@ starjoin_fk_operand_matches(Node *arg, Oid inputtype)
 	/*
 	 * Normally the operand exposes the operator's declared input type.  A
 	 * polymorphic operator sees the actual type instead; in that case don't
-	 * let a relabel substitute a different actual type.
+	 * let a relabel substitute a different actual type.  The exception is a
+	 * domain over an array, range or multirange, which the parser relabels to
+	 * its base type, in RI's queries too.
 	 */
 	if (argtype != inputtype &&
 		((!IsPolymorphicType(inputtype) && inputtype != RECORDOID) ||
-		 argtype != var->vartype))
+		 (argtype != var->vartype &&
+		  argtype != getBaseType(var->vartype))))
 		return false;
 	return IsBinaryCoercible(var->vartype, inputtype);
 }
diff --git a/src/test/regress/expected/starjoin_fk_coercion.out b/src/test/regress/expected/starjoin_fk_coercion.out
index 5576fd468b8..22073a3c88b 100644
--- a/src/test/regress/expected/starjoin_fk_coercion.out
+++ b/src/test/regress/expected/starjoin_fk_coercion.out
@@ -29,18 +29,26 @@ CREATE OPERATOR CLASS pg_temp.loose_ops FOR TYPE text USING btree AS
 CREATE TEMP TABLE words (k text COLLATE "C" NOT NULL);
 CREATE UNIQUE INDEX ON words (k pg_temp.loose_ops);
 INSERT INTO words VALUES ('abc');
+CREATE DOMAIN pg_temp.int_array AS int[];
+CREATE DOMAIN pg_temp.int_range AS int4range;
+CREATE TEMP TABLE arrays (k int[] PRIMARY KEY);
+INSERT INTO arrays VALUES ('{1}');
+CREATE TEMP TABLE ranges (k pg_temp.int_range PRIMARY KEY);
+INSERT INTO ranges VALUES ('[1,2)');
 CREATE TEMP TABLE fact (
   v varchar COLLATE "C" NOT NULL REFERENCES names,
   t text COLLATE "C" NOT NULL REFERENCES names,
   folded pg_temp.folded COLLATE "C" NOT NULL REFERENCES words(k),
   plain varchar COLLATE "C" NOT NULL REFERENCES words(k),
   x text COLLATE "C" NOT NULL,
+  arr pg_temp.int_array NOT NULL REFERENCES arrays,
+  rng int4range NOT NULL REFERENCES ranges,
   a int NOT NULL REFERENCES d, b int NOT NULL REFERENCES d,
   c int NOT NULL REFERENCES d, d int NOT NULL REFERENCES d
 );
-INSERT INTO fact SELECT :'prefix' || 'b', :'prefix', 'ABC', 'abc', 'ABC', 1, 1, 1, 1
+INSERT INTO fact SELECT :'prefix' || 'b', :'prefix', 'ABC', 'abc', 'ABC', '{1}', '[1,2)', 1, 1, 1, 1
   FROM generate_series(1, 100);
-ANALYZE fact, names, words, d;
+ANALYZE fact, names, words, arrays, ranges, d;
 -- As in starjoin_fk_collation, compare planning memory for flat and forced
 -- join order. The four other dimensions always qualify.
 CREATE FUNCTION pg_temp.qualifies(candidate text) RETURNS bool
@@ -109,4 +117,14 @@ SELECT pg_temp.qualifies(:'filter') AS ec_fact_filter, pg_temp.qualifies(:'relay
  t              | f
 (1 row)
 
+-- For polymorphic operators, the parser relabels a domain over an array or
+-- range to its base type, in RI's queries too. Such keys qualify on either
+-- side.
+SELECT pg_temp.qualifies('arrays ar ON ar.k = f.arr') AS domain_fk_qualifies,
+       pg_temp.qualifies('ranges r ON f.rng = r.k') AS domain_key_qualifies;
+ domain_fk_qualifies | domain_key_qualifies 
+---------------------+----------------------
+ t                   | t
+(1 row)
+
 ROLLBACK;
diff --git a/src/test/regress/sql/starjoin_fk_coercion.sql b/src/test/regress/sql/starjoin_fk_coercion.sql
index 275e4cb7b25..576e0b13360 100644
--- a/src/test/regress/sql/starjoin_fk_coercion.sql
+++ b/src/test/regress/sql/starjoin_fk_coercion.sql
@@ -32,18 +32,27 @@ CREATE OPERATOR CLASS pg_temp.loose_ops FOR TYPE text USING btree AS
 CREATE TEMP TABLE words (k text COLLATE "C" NOT NULL);
 CREATE UNIQUE INDEX ON words (k pg_temp.loose_ops);
 INSERT INTO words VALUES ('abc');
+
+CREATE DOMAIN pg_temp.int_array AS int[];
+CREATE DOMAIN pg_temp.int_range AS int4range;
+CREATE TEMP TABLE arrays (k int[] PRIMARY KEY);
+INSERT INTO arrays VALUES ('{1}');
+CREATE TEMP TABLE ranges (k pg_temp.int_range PRIMARY KEY);
+INSERT INTO ranges VALUES ('[1,2)');
 CREATE TEMP TABLE fact (
   v varchar COLLATE "C" NOT NULL REFERENCES names,
   t text COLLATE "C" NOT NULL REFERENCES names,
   folded pg_temp.folded COLLATE "C" NOT NULL REFERENCES words(k),
   plain varchar COLLATE "C" NOT NULL REFERENCES words(k),
   x text COLLATE "C" NOT NULL,
+  arr pg_temp.int_array NOT NULL REFERENCES arrays,
+  rng int4range NOT NULL REFERENCES ranges,
   a int NOT NULL REFERENCES d, b int NOT NULL REFERENCES d,
   c int NOT NULL REFERENCES d, d int NOT NULL REFERENCES d
 );
-INSERT INTO fact SELECT :'prefix' || 'b', :'prefix', 'ABC', 'abc', 'ABC', 1, 1, 1, 1
+INSERT INTO fact SELECT :'prefix' || 'b', :'prefix', 'ABC', 'abc', 'ABC', '{1}', '[1,2)', 1, 1, 1, 1
   FROM generate_series(1, 100);
-ANALYZE fact, names, words, d;
+ANALYZE fact, names, words, arrays, ranges, d;
 
 -- As in starjoin_fk_collation, compare planning memory for flat and forced
 -- join order. The four other dimensions always qualify.
@@ -92,4 +101,10 @@ SELECT pg_temp.qualifies(:'bypass') AS ec_bypass, pg_temp.qualifies(:'binary') A
 \set filter 'words w ON w.k OPERATOR(pg_temp.===) f.plain AND f.plain OPERATOR(pg_temp.===) lower(f.x)'
 \set relay 'words w ON w.k OPERATOR(pg_temp.===) f.x AND f.x OPERATOR(pg_temp.===) f.folded::varchar::text'
 SELECT pg_temp.qualifies(:'filter') AS ec_fact_filter, pg_temp.qualifies(:'relay') AS ec_relay_bypass;
+
+-- For polymorphic operators, the parser relabels a domain over an array or
+-- range to its base type, in RI's queries too. Such keys qualify on either
+-- side.
+SELECT pg_temp.qualifies('arrays ar ON ar.k = f.arr') AS domain_fk_qualifies,
+       pg_temp.qualifies('ranges r ON f.rng = r.k') AS domain_key_qualifies;
 ROLLBACK;
-- 
2.56.1

