Fix assert failure in composite type dependency check for index expr

Started by Zhijie Hou3 days ago4 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1Zhijie Hou
houzhijie22@gmail.com

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(),
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.
--
Best Regards,
Zhijie Hou

Attachments:

t259789_1
v1-0001-Fix-composite-type-dependency-check-for-index-exp.patchtext/plain; charset=UTF-8; name=v1-0001-Fix-composite-type-dependency-check-for-index-exp.patchDownload+41-8
#2Chao Li
li.evan.chao@gmail.com
In reply to: Zhijie Hou (#1)
Re: Fix assert failure in composite type dependency check for index expr

On Nov 25, 2026, at 21:00, Zhijie Hou <houzhijie22@gmail.com> 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/

#3Zhijie Hou
houzhijie22@gmail.com
In reply to: Chao Li (#2)
Re: Fix assert failure in composite type dependency check for index expr

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

Attachments:

t259789_3
v2-0001-Fix-composite-type-dependency-check-for-index-exp.patchtext/plain; charset=UTF-8; name=v2-0001-Fix-composite-type-dependency-check-for-index-exp.patchDownload+62-39
#4Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Zhijie Hou (#3)
Re: Fix assert failure in composite type dependency check for index expr

On 26/11/2026 17:22, Zhijie Hou wrote:

On Thu, Nov26, 2026 at 11:06 AM Chao Li wrote:>> 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.

Committed. I did a bunch of rewording of the comments and the commit
message, but not material changes. Thank you!

- Heikki