Hi,

On Thu, Nov26, 2026 at 11:06 AM Chao Li wrote:
1
```
+       if (expr == NULL)
+               return false;
```

Is this NULL check really needed? As far as I can see, no current caller
passes NULL. Also, semantically, this function is intended to check type
dependencies in an expression, so it doesn't seem reasonable to pass NULL as
the expression. Maybe we can just use Assert(expr != NULL) instead.

Thanks for the comments.

I perfer to keep "if (expr == NULL) return false;" to be consistent with
the sibling functions in dependency.c like recordDependencyOnSingleRelExpr()
and CheckUsageOnTypesInSingleRelExpr().

2
In the preceding if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
branch, since this patch adds the exprType() check to
CheckTypeDependencyInSingleRelExpr(), the first check becomes redundant.
Maybe we can remove it and merge the two comments.

Agreed.  Since CheckTypeDependencyInSingleRelExpr() also accepts a
List directly, we can drop the foreach loop over partexprs as well and
pass the expression list in one call, matching indexprs below it.  Done
in the attached v2.
--
Best Regards,
Zhijie Hou
From afe27ed93c75f9543da8ebc24e386ef3c09094c5 Mon Sep 17 00:00:00 2001
From: Zhijie Hou <[email protected]>
Date: Wed, 25 Nov 2026 16:45:07 +0800
Subject: [PATCH v2] Fix composite type dependency check for index expressions.

Commit d98d99ba17b taught recordDependencyOnSingleRelExpr() and
CheckUsageOnTypesInSingleRelExpr() to include the expression's result
type, so an expression index records a whole-relation pg_depend entry
on each index expression's return type.  However, it missed updating
CheckTypeDependencyInSingleRelExpr() to match.

check_composite_type_dependency() previously assumed that an index
expression's return type always becomes the index column's storage
type and was already caught by the column check above.  That fails
when the index access method or operator class uses a separate storage
key type, such as in hash indexes (amkeytype = int4), GIN array_ops
indexes (opckeytype = anyelement), or GiST multirange_ops and BRIN
range_inclusion_ops indexes (opckeytype = anyrange).  Such indexes
must still prevent altering the type because ANALYZE stores datums of
the expression's result type in pg_statistic and existing index
entries are not rebuilt.  When altering a composite type (or adding a
constraint to a domain via get_rels_with_domain()) referenced this
way, CheckTypeDependencyInSingleRelExpr() returned false and
check_composite_type_dependency() tripped Assert(false).

Fix this by teaching CheckTypeDependencyInSingleRelExpr() to check
exprType() in the same way as recordDependencyOnSingleRelExpr(), and
simplify the PARTITION BY expression check in
check_composite_type_dependency() accordingly.

Also, pass the indexed table's OID (indrelid) rather than the index's
own OID when checking index expressions and predicates, matching
index_create().

Author: Zhijie Hou <[email protected]>
Reviewed-by: Chao Li <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 14
---
 src/backend/catalog/dependency.c              | 17 +++++
 src/backend/commands/tablecmds.c              | 69 +++++++++----------
 .../regress/expected/alter_column_type.out    |  8 +++
 src/test/regress/sql/alter_column_type.sql    |  6 ++
 4 files changed, 62 insertions(+), 38 deletions(-)

diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c
index e4c73cc8686..9415a80eb0a 100644
--- a/src/backend/catalog/dependency.c
+++ b/src/backend/catalog/dependency.c
@@ -1874,6 +1874,23 @@ CheckTypeDependencyInSingleRelExpr(Node *expr, Oid 
relId, Oid typeId)
 {
        RangeTblEntry rte = {0};
 
+       if (expr == NULL)
+               return false;
+
+       /* Also check the result type, matching recordDependencyOnSingleRelExpr 
*/
+       if (IsA(expr, List))
+       {
+               ListCell   *l;
+
+               foreach(l, (List *) expr)
+               {
+                       if (exprType(lfirst(l)) == typeId)
+                               return true;
+               }
+       }
+       else if (exprType(expr) == typeId)
+               return true;
+
        /* We gin up a rather bogus rangetable list to handle Vars */
        rte.type = T_RangeTblEntry;
        rte.rtekind = RTE_RELATION;
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 442d5233c1f..dfaaf626fd5 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -7384,49 +7384,39 @@ check_composite_type_dependency(ObjectAddress *depobj, 
Oid typeOid,
                                 */
                                if (rel->rd_rel->relkind == 
RELKIND_PARTITIONED_TABLE)
                                {
-                                       List       *partexprs;
-                                       ListCell   *lc;
-
                                        catalog_tup = SearchSysCache1(PARTRELID,
                                                                                
                  ObjectIdGetDatum(RelationGetRelid(rel)));
                                        if (!HeapTupleIsValid(catalog_tup))
                                                elog(ERROR, "cache lookup 
failed for partition key of relation %u",
                                                         RelationGetRelid(rel));
-                                       partexprs = (List *)
-                                               get_node_tree_attr(PARTRELID, 
catalog_tup,
-                                                                               
   Anum_pg_partitioned_table_partexprs);
-                                       foreach(lc, partexprs)
+                                       expr = get_node_tree_attr(PARTRELID, 
catalog_tup,
+                                                                               
          Anum_pg_partitioned_table_partexprs);
+                                       if (expr)
                                        {
-                                               Node       *partexpr = 
lfirst(lc);
-
                                                /*
-                                                * Check if it's the PARTITION 
BY's return type.
+                                                * Check if any PARTITION BY 
expression uses or
+                                                * returns the rowtype.  We 
must reject if the
+                                                * expression uses the rowtype 
because otherwise we'd
+                                                * need to rewrite the 
expression stored in the
+                                                * catalog, as well as the 
partition bounds of all the
+                                                * partitions, and also because 
the data might no
+                                                * longer be in the correct 
partitions according to
+                                                * the new datatype's sort 
order.
                                                 *
-                                                * (This could be allowed as 
long as the table has no
-                                                * partitions.  The problem 
really only arises from
-                                                * the partition bounds stored 
in the 'relpartbound'
-                                                * of the partitions.  But it's 
not worth
-                                                * distinguishing that case.)
+                                                * (If it's the expression's 
return type, this could
+                                                * be allowed as long as the 
table has no partitions,
+                                                * since the problem really 
only arises from the
+                                                * partition bounds stored in 
the 'relpartbound' of
+                                                * the partitions.  But it's 
not worth distinguishing
+                                                * that case.)
                                                 *
                                                 * We had a gap in our 
dependency tracking in the
-                                                * past, and didn't record a 
pg_depend entry for
-                                                * these.  Any missing 
dependencies not caught here
-                                                * are handled by
+                                                * past, and didn't record a 
pg_depend entry for the
+                                                * return type.  Any missing 
dependencies not caught
+                                                * here are handled by
                                                 * 
find_missing_composite_type_dependencies().
                                                 */
-                                               if (exprType(partexpr) == 
typeOid)
-                                                       
report_type_used_in_expr(depobj, origRelation, origTypeName);
-
-                                               /*
-                                                * Check if the expression uses 
the rowtype.  We must
-                                                * reject that case because 
otherwise we'd need to
-                                                * rewrite the expression 
stored in the catalog, as
-                                                * well as the partition bounds 
of all the partitions,
-                                                * and also because the data 
might no longer be in the
-                                                * correct partitions according 
to the new datatype's
-                                                * sort order.
-                                                */
-                                               if 
(CheckTypeDependencyInSingleRelExpr(partexpr, RelationGetRelid(rel), typeOid))
+                                               if 
(CheckTypeDependencyInSingleRelExpr(expr, RelationGetRelid(rel), typeOid))
                                                        
report_type_used_in_expr(depobj, origRelation, origTypeName);
                                        }
                                        ReleaseSysCache(catalog_tup);
@@ -7442,24 +7432,27 @@ check_composite_type_dependency(ObjectAddress *depobj, 
Oid typeOid,
                                 */
                                if (rel->rd_indextuple)
                                {
+                                       Oid                     indrelid = 
rel->rd_index->indrelid;
+
                                        expr = get_node_tree_attr(INDEXRELID, 
rel->rd_indextuple,
                                                                                
          Anum_pg_index_indexprs);
                                        if (expr)
                                        {
-                                               if 
(CheckTypeDependencyInSingleRelExpr(expr, RelationGetRelid(rel), typeOid))
-                                                       
report_type_used_in_expr(depobj, origRelation, origTypeName);
-
                                                /*
-                                                * If it's the *return type* of 
an index expression,
-                                                * it becomes a column type and 
we checked that above
-                                                * already.
+                                                * 
CheckTypeDependencyInSingleRelExpr() also checks
+                                                * each expression's return 
type, which may differ
+                                                * from the index column's 
storage type when the index
+                                                * access method or operator 
class uses a separate key
+                                                * type (for example in hash or 
GIN indexes).
                                                 */
+                                               if 
(CheckTypeDependencyInSingleRelExpr(expr, indrelid, typeOid))
+                                                       
report_type_used_in_expr(depobj, origRelation, origTypeName);
                                        }
                                        expr = get_node_tree_attr(INDEXRELID, 
rel->rd_indextuple,
                                                                                
          Anum_pg_index_indpred);
                                        if (expr)
                                        {
-                                               if 
(CheckTypeDependencyInSingleRelExpr(expr, RelationGetRelid(rel), typeOid))
+                                               if 
(CheckTypeDependencyInSingleRelExpr(expr, indrelid, typeOid))
                                                        
report_type_used_in_expr(depobj, origRelation, origTypeName);
                                        }
                                }
diff --git a/src/test/regress/expected/alter_column_type.out 
b/src/test/regress/expected/alter_column_type.out
index 2388cd08591..1a45387ba90 100644
--- a/src/test/regress/expected/alter_column_type.out
+++ b/src/test/regress/expected/alter_column_type.out
@@ -164,6 +164,10 @@ CREATE FUNCTION atfunc(numeric) RETURNS atcomp IMMUTABLE 
LANGUAGE plpgsql AS $$B
 CREATE INDEX atref_idx ON atref(atfunc(c));
 ALTER TYPE atcomp ALTER ATTRIBUTE a TYPE bigint CASCADE; -- someday this 
should work
 ERROR:  cannot alter type "atcomp" because column "atref_idx.atfunc_numeric_c" 
uses it
+DROP INDEX atref_idx;
+CREATE INDEX atref_hash_idx ON atref USING hash (atfunc(c));
+ALTER TYPE atcomp ALTER ATTRIBUTE a TYPE bigint CASCADE; -- someday this 
should work
+ERROR:  cannot alter type "atcomp" because index atref_hash_idx uses it
 DROP TABLE atref;
 DROP FUNCTION atfunc(numeric);
 DROP TYPE atcomp;
@@ -368,6 +372,10 @@ CREATE INDEX test_tbl1_idx ON 
test_tbl1(construct_test_type_arr(x, y));
 ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
 ERROR:  cannot alter type "test_type1" because column 
"test_tbl1_idx.construct_test_type_arr_x_y" uses it
 DROP INDEX test_tbl1_idx;
+CREATE INDEX test_tbl1_idx ON test_tbl1 USING gin (construct_test_type_arr(x, 
y));
+ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
+ERROR:  cannot alter type "test_type1" because index test_tbl1_idx uses it
+DROP INDEX test_tbl1_idx;
 DROP FUNCTION construct_test_type_arr;
 -- Cannot alter composite type if it appears in an extended statistics
 -- expression. The statistics entry stores datums of the type on disk,
diff --git a/src/test/regress/sql/alter_column_type.sql 
b/src/test/regress/sql/alter_column_type.sql
index d821126a2bc..53dc0362b60 100644
--- a/src/test/regress/sql/alter_column_type.sql
+++ b/src/test/regress/sql/alter_column_type.sql
@@ -148,6 +148,9 @@ CREATE TABLE atref (c int);
 CREATE FUNCTION atfunc(numeric) RETURNS atcomp IMMUTABLE LANGUAGE plpgsql AS 
$$BEGIN RETURN ('('||$1||')')::atcomp; END$$;
 CREATE INDEX atref_idx ON atref(atfunc(c));
 ALTER TYPE atcomp ALTER ATTRIBUTE a TYPE bigint CASCADE; -- someday this 
should work
+DROP INDEX atref_idx;
+CREATE INDEX atref_hash_idx ON atref USING hash (atfunc(c));
+ALTER TYPE atcomp ALTER ATTRIBUTE a TYPE bigint CASCADE; -- someday this 
should work
 DROP TABLE atref;
 DROP FUNCTION atfunc(numeric);
 DROP TYPE atcomp;
@@ -341,6 +344,9 @@ CREATE FUNCTION construct_test_type_arr(x int, y text) 
RETURNS test_type1[]
 CREATE INDEX test_tbl1_idx ON test_tbl1(construct_test_type_arr(x, y));
 ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
 DROP INDEX test_tbl1_idx;
+CREATE INDEX test_tbl1_idx ON test_tbl1 USING gin (construct_test_type_arr(x, 
y));
+ALTER TYPE test_type1 ALTER ATTRIBUTE b TYPE varchar; -- fails
+DROP INDEX test_tbl1_idx;
 DROP FUNCTION construct_test_type_arr;
 
 -- Cannot alter composite type if it appears in an extended statistics
-- 
2.54.0 (Apple Git-157)

Reply via email to