From 9aa59aa1c578c59adca7e5c8d967e3bb691dd26c Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Thu, 26 Nov 2026 15:41:33 +0800 Subject: [PATCH v1] Skip Var resolution when checking expression type dependencies Resolving a Var can only add a column dependency. Even so, callers checking for type dependencies had to supply a range table or a relation OID to resolve Vars. For an index expression, this made it easy to pass the index OID where the indexed table's OID was expected. Teach find_expr_references_walker() to skip Vars during type checks. This removes the need for a range table and lets us drop the separate single-relation variants. Let callers of CheckUsageOnTypesInExpr() and CheckTypeDependencyInExpr() specify whether to check each expression's result type, as the single-relation variants did. This is a pure refactoring. No change in runtime behavior is intended. Author: Chao Li --- src/backend/catalog/dependency.c | 139 +++++++++++---------------- src/backend/catalog/heap.c | 7 +- src/backend/catalog/pg_proc.c | 4 +- src/backend/catalog/pg_publication.c | 2 +- src/backend/commands/indexcmds.c | 10 +- src/backend/commands/policy.c | 10 +- src/backend/commands/statscmds.c | 2 +- src/backend/commands/tablecmds.c | 18 ++-- src/backend/commands/trigger.c | 2 +- src/backend/commands/typecmds.c | 6 +- src/backend/parser/parse_utilcmd.c | 4 +- src/backend/rewrite/rewriteDefine.c | 4 +- src/include/catalog/dependency.h | 8 +- 13 files changed, 87 insertions(+), 129 deletions(-) diff --git a/src/backend/catalog/dependency.c b/src/backend/catalog/dependency.c index 7e9a3821a42..d018f5239c9 100644 --- a/src/backend/catalog/dependency.c +++ b/src/backend/catalog/dependency.c @@ -141,6 +141,7 @@ typedef struct { ObjectAddresses *addrs; /* addresses being accumulated */ List *rtables; /* list of rangetables to resolve Vars */ + bool skipvar; /* skip Var references during type checks */ } find_expr_references_context; @@ -1603,8 +1604,9 @@ recordDependencyOnExpr(const ObjectAddress *depender, * immediately record the dependencies, allowing the caller to add to, * filter, or modify the collected dependencies before recording them. * - * rtable is the rangetable to be used to interpret Vars with varlevelsup=0. - * It can be NIL if no such variables are expected. + * rtable is the rangetable for Vars that refer to the query level enclosing + * expr. If rtable is NIL, encountering such a Var is an error. Queries + * within expr supply their own rangetables. * * Note: the returned list may well contain duplicates. The caller should * de-duplicate before recording the dependencies. Within this file, callers @@ -1617,7 +1619,7 @@ void collectDependenciesOfExpr(ObjectAddresses *addrs, Node *expr, List *rtable) { - find_expr_references_context context; + find_expr_references_context context = {0}; context.addrs = addrs; @@ -1652,7 +1654,7 @@ recordDependencyOnSingleRelExpr(const ObjectAddress *depender, DependencyType self_behavior, bool reverse_self) { - find_expr_references_context context; + find_expr_references_context context = {0}; RangeTblEntry rte = {0}; if (expr == NULL) @@ -1786,61 +1788,37 @@ check_usage_on_types(ObjectAddresses *addrs, Oid roleid) /* * CheckUsageOnTypesInExpr - require USAGE on all types named by an expression * - * rtable is the rangetable for interpreting Vars (or NIL if none are - * expected). roleid is the role whose USAGE is required. - */ -void -CheckUsageOnTypesInExpr(Node *expr, List *rtable, Oid roleid) -{ - ObjectAddresses *addrs = new_object_addresses(); - - collectDependenciesOfExpr(addrs, expr, rtable); - eliminate_duplicate_dependencies(addrs); - check_usage_on_types(addrs, roleid); - free_object_addresses(addrs); -} - -/* - * CheckUsageOnTypesInSingleRelExpr - as above, for a single-rel expression - * - * Like recordDependencyOnSingleRelExpr(), this handles expressions whose Vars - * all refer to one relation. roleid is the role whose USAGE is required. + * If checkResultType is true, expr must be an expression or a list of + * expressions. We also check USAGE on each expression's result type, + * matching recordDependencyOnSingleRelExpr(). */ void -CheckUsageOnTypesInSingleRelExpr(Node *expr, Oid relId, Oid roleid) +CheckUsageOnTypesInExpr(Node *expr, Oid roleid, bool checkResultType) { - find_expr_references_context context; - RangeTblEntry rte = {0}; + find_expr_references_context context = {.skipvar = true}; if (expr == NULL) return; context.addrs = new_object_addresses(); - - /* We gin up a rather bogus rangetable list to handle Vars */ - rte.type = T_RangeTblEntry; - rte.rtekind = RTE_RELATION; - rte.relid = relId; - rte.relkind = RELKIND_RELATION; - rte.rellockmode = AccessShareLock; - context.rtables = list_make1(list_make1(&rte)); - find_expr_references_walker(expr, &context); - /* Also check USAGE on the result type */ - if (IsA(expr, List)) + if (checkResultType) { - ListCell *l; - - foreach(l, (List *) expr) + if (IsA(expr, List)) { - add_object_address(TypeRelationId, exprType(lfirst(l)), 0, - context.addrs); + ListCell *l; + + foreach(l, (List *) expr) + { + add_object_address(TypeRelationId, exprType(lfirst(l)), 0, + context.addrs); + } } + else + add_object_address(TypeRelationId, exprType(expr), 0, + context.addrs); } - else - add_object_address(TypeRelationId, exprType(expr), 0, - context.addrs); eliminate_duplicate_dependencies(context.addrs); check_usage_on_types(context.addrs, roleid); @@ -1849,60 +1827,45 @@ CheckUsageOnTypesInSingleRelExpr(Node *expr, Oid relId, Oid roleid) /* * CheckTypeDependencyInExpr - does 'expr' depend on the given type? + * + * If checkResultType is true, expr must be an expression or a list of + * expressions. We also check each expression's result type, + * matching recordDependencyOnSingleRelExpr(). */ bool -CheckTypeDependencyInExpr(Node *expr, List *rtable, Oid typeId) +CheckTypeDependencyInExpr(Node *expr, Oid typeId, bool checkResultType) { - ObjectAddresses *addrs = new_object_addresses(); + find_expr_references_context context = {.skipvar = true}; ObjectAddress typeAddr; bool result; - collectDependenciesOfExpr(addrs, expr, rtable); - - ObjectAddressSet(typeAddr, TypeRelationId, typeId); - result = object_address_present(&typeAddr, addrs); - free_object_addresses(addrs); - - return result; -} - -/* - * CheckTypeDependencyInSingleRelExpr - as above, for a single-rel expression - * - * One difference is that this also checks the return type of the expression. - * That's consistent with recordDependencyOnSingleRelExpr(), which adds a - * dependency on the return type. - */ -bool -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)) + if (checkResultType) { - ListCell *l; - - foreach(l, (List *) expr) + if (IsA(expr, List)) { - if (exprType(lfirst(l)) == typeId) - return true; + ListCell *l; + + foreach(l, (List *) expr) + { + if (exprType(lfirst(l)) == typeId) + return true; + } } + else if (exprType(expr) == 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; - rte.relid = relId; - rte.relkind = RELKIND_RELATION; - rte.rellockmode = AccessShareLock; + context.addrs = new_object_addresses(); + find_expr_references_walker(expr, &context); - return CheckTypeDependencyInExpr(expr, list_make1(&rte), typeId); + ObjectAddressSet(typeAddr, TypeRelationId, typeId); + result = object_address_present(&typeAddr, context.addrs); + free_object_addresses(context.addrs); + + return result; } /* @@ -1947,6 +1910,14 @@ find_expr_references_walker(Node *node, List *rtable; RangeTblEntry *rte; + /* + * Resolving a Var can only add a column dependency, even for a column + * of a composite type returned by a function. Type checks can + * therefore skip Vars. + */ + if (context->skipvar) + return false; + /* Find matching rtable entry, or complain if not found */ if (var->varlevelsup >= list_length(context->rtables)) elog(ERROR, "invalid varlevelsup %d", var->varlevelsup); diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 49ef6652203..d28cd672097 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -2763,7 +2763,7 @@ AddRelationNewConstraints(Relation rel, * types. We are responsible for checking USAGE. */ if (!is_internal) - CheckUsageOnTypesInSingleRelExpr(expr, RelationGetRelid(rel), GetUserId()); + CheckUsageOnTypesInExpr(expr, GetUserId(), true); defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal); @@ -2812,7 +2812,7 @@ AddRelationNewConstraints(Relation rel, * types. We are responsible for checking USAGE. */ if (!is_internal) - CheckUsageOnTypesInSingleRelExpr(expr, RelationGetRelid(rel), GetUserId()); + CheckUsageOnTypesInExpr(expr, GetUserId(), true); } else { @@ -4330,8 +4330,7 @@ StorePartitionKey(Relation rel, */ if (partexprs) { - CheckUsageOnTypesInSingleRelExpr((Node *) partexprs, RelationGetRelid(rel), - GetUserId()); + CheckUsageOnTypesInExpr((Node *) partexprs, GetUserId(), true); recordDependencyOnSingleRelExpr(&myself, (Node *) partexprs, RelationGetRelid(rel), diff --git a/src/backend/catalog/pg_proc.c b/src/backend/catalog/pg_proc.c index 45953ef3748..0456edabff8 100644 --- a/src/backend/catalog/pg_proc.c +++ b/src/backend/catalog/pg_proc.c @@ -676,14 +676,14 @@ ProcedureCreate(const char *procedureName, /* dependencies appearing in new-style SQL routine body */ if (languageObjectId == SQLlanguageId && prosqlbody) { - CheckUsageOnTypesInExpr(prosqlbody, NIL, GetUserId()); + CheckUsageOnTypesInExpr(prosqlbody, GetUserId(), false); collectDependenciesOfExpr(addrs, prosqlbody, NIL); } /* dependency on parameter default expressions */ if (parameterDefaults) { - CheckUsageOnTypesInExpr((Node *) parameterDefaults, NIL, GetUserId()); + CheckUsageOnTypesInExpr((Node *) parameterDefaults, GetUserId(), false); collectDependenciesOfExpr(addrs, (Node *) parameterDefaults, NIL); } diff --git a/src/backend/catalog/pg_publication.c b/src/backend/catalog/pg_publication.c index f11ea8839ca..60c29ee9819 100644 --- a/src/backend/catalog/pg_publication.c +++ b/src/backend/catalog/pg_publication.c @@ -636,7 +636,7 @@ publication_add_relation(Oid pubid, PublicationRelInfo *pri, /* Add dependency on the objects mentioned in the qualifications */ if (pri->whereClause) { - CheckUsageOnTypesInSingleRelExpr(pri->whereClause, relid, GetUserId()); + CheckUsageOnTypesInExpr(pri->whereClause, GetUserId(), true); recordDependencyOnSingleRelExpr(&myself, pri->whereClause, relid, DEPENDENCY_NORMAL, DEPENDENCY_NORMAL, false); diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index c8b88ba4f2a..66bd68f6b91 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -982,13 +982,11 @@ DefineIndex(ParseState *pstate, if (check_rights) { if (indexInfo->ii_Expressions) - CheckUsageOnTypesInSingleRelExpr((Node *) indexInfo->ii_Expressions, - tableId, - root_save_userid); + CheckUsageOnTypesInExpr((Node *) indexInfo->ii_Expressions, + root_save_userid, true); if (indexInfo->ii_Predicate) - CheckUsageOnTypesInSingleRelExpr((Node *) indexInfo->ii_Predicate, - tableId, - root_save_userid); + CheckUsageOnTypesInExpr((Node *) indexInfo->ii_Predicate, + root_save_userid, true); } /* diff --git a/src/backend/commands/policy.c b/src/backend/commands/policy.c index 1da5857e2af..48f989684e2 100644 --- a/src/backend/commands/policy.c +++ b/src/backend/commands/policy.c @@ -752,12 +752,11 @@ CreatePolicy(CreatePolicyStmt *stmt) recordDependencyOn(&myself, &target, DEPENDENCY_AUTO); - CheckUsageOnTypesInExpr(qual, qual_pstate->p_rtable, GetUserId()); + CheckUsageOnTypesInExpr(qual, GetUserId(), false); recordDependencyOnExpr(&myself, qual, qual_pstate->p_rtable, DEPENDENCY_NORMAL); - CheckUsageOnTypesInExpr(with_check_qual, with_check_pstate->p_rtable, - GetUserId()); + CheckUsageOnTypesInExpr(with_check_qual, GetUserId(), false); recordDependencyOnExpr(&myself, with_check_qual, with_check_pstate->p_rtable, DEPENDENCY_NORMAL); @@ -1087,12 +1086,11 @@ AlterPolicy(AlterPolicyStmt *stmt) recordDependencyOn(&myself, &target, DEPENDENCY_AUTO); if (stmt->qual) - CheckUsageOnTypesInExpr(qual, qual_parse_rtable, GetUserId()); + CheckUsageOnTypesInExpr(qual, GetUserId(), false); recordDependencyOnExpr(&myself, qual, qual_parse_rtable, DEPENDENCY_NORMAL); if (stmt->with_check) - CheckUsageOnTypesInExpr(with_check_qual, with_check_parse_rtable, - GetUserId()); + CheckUsageOnTypesInExpr(with_check_qual, GetUserId(), false); recordDependencyOnExpr(&myself, with_check_qual, with_check_parse_rtable, DEPENDENCY_NORMAL); diff --git a/src/backend/commands/statscmds.c b/src/backend/commands/statscmds.c index 4aaf41855ce..bdcb0512f01 100644 --- a/src/backend/commands/statscmds.c +++ b/src/backend/commands/statscmds.c @@ -638,7 +638,7 @@ CreateStatistics(List *relids, CreateStatsStmt *stmt, bool check_rights) if (stxexprs) { if (check_rights) - CheckUsageOnTypesInSingleRelExpr((Node *) stxexprs, relid, GetUserId()); + CheckUsageOnTypesInExpr((Node *) stxexprs, GetUserId(), true); recordDependencyOnSingleRelExpr(&myself, (Node *) stxexprs, diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 9e363a44c20..48ab13378b8 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -7201,12 +7201,12 @@ check_composite_type_dependency(ObjectAddress *depobj, Oid typeOid, /* Is it used in the argument DEFAULTs? */ expr = get_node_tree_attr(PROCOID, catalog_tup, Anum_pg_proc_proargdefaults); - if (expr && CheckTypeDependencyInExpr(expr, NIL, typeOid)) + if (expr && CheckTypeDependencyInExpr(expr, typeOid, false)) report_type_used_in_expr(depobj, origRelation, origTypeName); /* Is it used in the SQL body? */ expr = get_node_tree_attr(PROCOID, catalog_tup, Anum_pg_proc_prosqlbody); - if (expr && CheckTypeDependencyInExpr(expr, NIL, typeOid)) + if (expr && CheckTypeDependencyInExpr(expr, typeOid, false)) report_type_used_in_expr(depobj, origRelation, origTypeName); /* @@ -7241,7 +7241,7 @@ check_composite_type_dependency(ObjectAddress *depobj, Oid typeOid, LockDatabaseObject(ConstraintRelationId, objid, 0, AccessShareLock); expr = get_node_tree_attr(CONSTROID, catalog_tup, Anum_pg_constraint_conbin); - if (expr && CheckTypeDependencyInExpr(expr, NIL, typeOid)) + if (expr && CheckTypeDependencyInExpr(expr, typeOid, false)) report_type_used_in_expr(depobj, origRelation, origTypeName); } else @@ -7277,7 +7277,7 @@ check_composite_type_dependency(ObjectAddress *depobj, Oid typeOid, elog(ERROR, "cache lookup failed for type %u", objid); expr = get_node_tree_attr(TYPEOID, catalog_tup, Anum_pg_type_typdefaultbin); - if (expr && CheckTypeDependencyInExpr(expr, NIL, typeOid)) + if (expr && CheckTypeDependencyInExpr(expr, typeOid, false)) report_type_used_in_expr(depobj, origRelation, origTypeName); ReleaseSysCache(catalog_tup); @@ -7406,7 +7406,7 @@ check_composite_type_dependency(ObjectAddress *depobj, Oid typeOid, RelationGetRelid(rel)); expr = get_node_tree_attr(PARTRELID, catalog_tup, Anum_pg_partitioned_table_partexprs); - if (expr && CheckTypeDependencyInSingleRelExpr(expr, RelationGetRelid(rel), typeOid)) + if (expr && CheckTypeDependencyInExpr(expr, typeOid, true)) report_type_used_in_expr(depobj, origRelation, origTypeName); ReleaseSysCache(catalog_tup); } @@ -7421,16 +7421,14 @@ 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 && CheckTypeDependencyInSingleRelExpr(expr, indrelid, typeOid)) + if (expr && CheckTypeDependencyInExpr(expr, typeOid, true)) report_type_used_in_expr(depobj, origRelation, origTypeName); expr = get_node_tree_attr(INDEXRELID, rel->rd_indextuple, Anum_pg_index_indpred); - if (expr && CheckTypeDependencyInSingleRelExpr(expr, indrelid, typeOid)) + if (expr && CheckTypeDependencyInExpr(expr, typeOid, true)) report_type_used_in_expr(depobj, origRelation, origTypeName); } @@ -8844,7 +8842,7 @@ ATExecCookedColumnDefault(Relation rel, AttrNumber attnum, * privilege checks to its caller, so we must check for USAGE on the types * here. */ - CheckUsageOnTypesInSingleRelExpr(newDefault, RelationGetRelid(rel), GetUserId()); + CheckUsageOnTypesInExpr(newDefault, GetUserId(), true); /* * Remove any old default for the column. We use RESTRICT here for diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index df267427c25..890a0158e53 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -1151,7 +1151,7 @@ CreateTriggerFiringOn(const CreateTrigStmt *stmt, const char *queryString, if (whenRtable != NIL) { if (!isInternal) - CheckUsageOnTypesInExpr(whenClause, whenRtable, GetUserId()); + CheckUsageOnTypesInExpr(whenClause, GetUserId(), false); recordDependencyOnExpr(&myself, whenClause, whenRtable, DEPENDENCY_NORMAL); diff --git a/src/backend/commands/typecmds.c b/src/backend/commands/typecmds.c index 9f57728129f..9fa4e2a2276 100644 --- a/src/backend/commands/typecmds.c +++ b/src/backend/commands/typecmds.c @@ -1062,7 +1062,7 @@ DefineDomain(ParseState *pstate, CreateDomainStmt *stmt) * adds the dependencies on types. We are responsible for checking USAGE. */ if (defaultValueBin) - CheckUsageOnTypesInExpr(stringToNode(defaultValueBin), NIL, GetUserId()); + CheckUsageOnTypesInExpr(stringToNode(defaultValueBin), GetUserId(), false); /* Allocate OID for array type */ domainArrayOid = AssignTypeArrayOid(); @@ -2748,7 +2748,7 @@ AlterDomainDefault(List *names, Node *defaultRaw) * The below call to GenerateTypeDependencies() creates the * dependencies on types. We are responsible for checking USAGE. */ - CheckUsageOnTypesInExpr(defaultExpr, NIL, GetUserId()); + CheckUsageOnTypesInExpr(defaultExpr, GetUserId(), false); /* * Expression must be stored as a nodeToString result, but we also @@ -3656,7 +3656,7 @@ domainAddCheckConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid, * types. We are responsible for checking USAGE. */ if (!is_readd) - CheckUsageOnTypesInExpr(expr, NIL, GetUserId()); + CheckUsageOnTypesInExpr(expr, GetUserId(), false); /* * Domains don't allow variables (this is probably dead code now that diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 583346bf531..3d22a6218a1 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -1500,9 +1500,7 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause) * AddRelationNewConstraints(), so we must check for USAGE on * types here. */ - CheckUsageOnTypesInSingleRelExpr(stringToNode(ccbin), - RelationGetRelid(relation), - GetUserId()); + CheckUsageOnTypesInExpr(stringToNode(ccbin), GetUserId(), true); n = makeNode(Constraint); n->contype = CONSTR_CHECK; diff --git a/src/backend/rewrite/rewriteDefine.c b/src/backend/rewrite/rewriteDefine.c index 542f91638a0..3cf14bc22b3 100644 --- a/src/backend/rewrite/rewriteDefine.c +++ b/src/backend/rewrite/rewriteDefine.c @@ -161,7 +161,7 @@ InsertRule(const char *rulname, /* * Also install dependencies on objects referenced in action and qual. */ - CheckUsageOnTypesInExpr((Node *) action, NIL, GetUserId()); + CheckUsageOnTypesInExpr((Node *) action, GetUserId(), false); recordDependencyOnExpr(&myself, (Node *) action, NIL, DEPENDENCY_NORMAL); @@ -171,7 +171,7 @@ InsertRule(const char *rulname, Query *qry = linitial_node(Query, action); qry = getInsertSelectQuery(qry, NULL); - CheckUsageOnTypesInExpr(event_qual, qry->rtable, GetUserId()); + CheckUsageOnTypesInExpr(event_qual, GetUserId(), false); recordDependencyOnExpr(&myself, event_qual, qry->rtable, DEPENDENCY_NORMAL); } diff --git a/src/include/catalog/dependency.h b/src/include/catalog/dependency.h index 9782d0df2a7..4817b68d555 100644 --- a/src/include/catalog/dependency.h +++ b/src/include/catalog/dependency.h @@ -123,13 +123,9 @@ extern void recordDependencyOnSingleRelExpr(const ObjectAddress *depender, DependencyType self_behavior, bool reverse_self); -extern void CheckUsageOnTypesInExpr(Node *expr, List *rtable, Oid roleid); +extern void CheckUsageOnTypesInExpr(Node *expr, Oid roleid, bool checkResultType); -extern void CheckUsageOnTypesInSingleRelExpr(Node *expr, Oid relId, Oid roleid); - -extern bool CheckTypeDependencyInExpr(Node *expr, List *rtable, Oid typeId); - -extern bool CheckTypeDependencyInSingleRelExpr(Node *expr, Oid relId, Oid typeId); +extern bool CheckTypeDependencyInExpr(Node *expr, Oid typeId, bool checkResultType); extern bool find_temp_object(const ObjectAddresses *addrs, bool local_temp_okay, -- 2.50.1 (Apple Git-155)