> On Nov 25, 2026, at 21:00, Zhijie Hou <[email protected]> wrote:
> 
> Hi,
> 
> While reading through the recent security fixes, I noticed that commit 
> d98d99ba17b updated recordDependencyOnSingleRelExpr() and 
> CheckUsageOnTypesInSingleRelExpr() to include the expression's result type, 
> but missed CheckTypeDependencyInSingleRelExpr().
> 
> When an index uses a storage key type different from the expression's return 
> type (such as hash or GIN array_ops indexes), the column check in 
> check_composite_type_dependency() does not match, and 
> CheckTypeDependencyInSingleRelExpr() returns false, hitting Assert(false):
> 
>  CREATE TYPE atcomp AS (a numeric);
>  CREATE TABLE atref (c int);
>  CREATE FUNCTION atfunc(numeric) RETURNS atcomp IMMUTABLE
>    LANGUAGE plpgsql AS $$BEGIN RETURN ('('||$1||')')::atcomp; END$$;
>  CREATE INDEX atref_hash_idx ON atref USING hash (atfunc(c));
>  ALTER TYPE atcomp ALTER ATTRIBUTE a TYPE bigint;
>  -- TRAP: failed Assert("false"), File: "tablecmds.c", Line: 7490
> 
> (The same Assert(false) can also be reached via get_rels_with_domain() during 
> ALTER DOMAIN ... ADD CONSTRAINT / SET NOT NULL when a GIN, GiST, or BRIN 
> expression index returns a container over the domain.  In non-cassert builds, 
> execution falls through to report_type_used_in_expr() and still rejects the 
> ALTER.)
> 
> Attached is a patch teaching CheckTypeDependencyInSingleRelExpr() to check 
> exprType() in the same way as recordDependencyOnSingleRelExpr(),

I think the fix is in the right direction. Without the patch, the statement is 
still rejected in non-cassert builds, but the code fails to identify the reason 
for the dependency. This patch makes it explicit.

> and passing indrelid rather than the index's own OID when checking indexprs 
> and indpred to match index_create().  Since d98d99ba17b was backpatched, this 
> affects 14 through master.

I was curious why passing the wrong OID didn’t cause an error. After tracing 
the code, I found that the relation OID is only stored in item->objectId during 
collectDependenciesOfExpr() when item->classId is RelationRelationId, while 
ObjectAddressSet(typeAddr, TypeRelationId, typeId) constructs a target address 
with TypeRelationId as its classId and typeId as its objectId. Thus, it doesn’t 
matter which relation OID is passed for the current type dependency check.

But of course, changing to use the table OID is semantically correct.

Also, we could enhance collectDependenciesOfExpr() to collect only certain 
kinds of dependencies, so that in this use case: 1) we can avoid passing the 
relation OID; 2) the resulting addrs will be shorter, making the subsequent 
search cheaper. I'll consider proposing a separate patch for that.

> -- 
> Best Regards,
> Zhijie Hou
> <v1-0001-Fix-composite-type-dependency-check-for-index-exp.patch>

I have a few small comments:

1
```
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;
```

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.

2
```
                                                /*
                                                 * Check if it's the PARTITION 
BY's return type.
                                                 *
                                                 * (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.)
                                                 *
                                                 * 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
                                                 * 
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))
                                                        
report_type_used_in_expr(depobj, origRelation, origTypeName);
```

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.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/






Reply via email to