From 04ce3fbefaacd23f17957b2feea32d5fbee130c7 Mon Sep 17 00:00:00 2001 From: Zhijie Hou Date: Wed, 25 Nov 2026 16:45:07 +0800 Subject: [PATCH v1] 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(). 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 Backpatch-through: 14 --- src/backend/catalog/dependency.c | 17 +++++++++++++++++ src/backend/commands/tablecmds.c | 17 ++++++++++------- src/test/regress/expected/alter_column_type.out | 8 ++++++++ src/test/regress/sql/alter_column_type.sql | 6 ++++++ 4 files changed, 41 insertions(+), 7 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 9786a5af58a..a0b05fa77bb 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -7447,24 +7447,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)