Skip Var resolution when checking expression type dependencies

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: "Zhijie Hou (Fujitsu)" <houzj(dot)fnst(at)fujitsu(dot)com>
Subject: Skip Var resolution when checking expression type dependencies
Date: 2026-11-27 01:38:20
Message-ID: 9F010997-28B6-4303-8422-C0BBD126F7AB@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While reviewing patch [1], I noticed an unnecessary function parameter. Patch [1] mainly fixed a type dependency check for index expressions, and it also changed the relation OID passed to CheckTypeDependencyInSingleRelExpr():
```
- if (CheckTypeDependencyInSingleRelExpr(expr, RelationGetRelid(rel), typeOid))
+ if (CheckTypeDependencyInSingleRelExpr(expr, indrelid, typeOid))
report_type_used_in_expr(depobj, origRelation, origTypeName);
```

The old code passed the index's OID, but the expression refers to columns of the indexed table, so patch [1] changed it to pass the table's OID, which is semantically correct.

I was curious why passing the wrong OID didn't cause an error. After some debugging, I found that the walker uses the relation OID to record column dependencies. CheckTypeDependencyInSingleRelExpr() only looks for a dependency on a particular type, so column objects won’t be checked, whatever OID supplied doesn’t matter. I think asking callers to supply a relation OID here adds a maintenance burden and can leave readers wondering which OID to pass.

The attached patch removes the need to pass a range table to CheckTypeDependencyInExpr() or a relation OID to CheckTypeDependencyInSingleRelExpr(). Once those parameters were gone, the only remaining difference was whether to check the expression's result type. I made that an explicit argument to CheckTypeDependencyInExpr() and removed CheckTypeDependencyInSingleRelExpr(). I made the same change to CheckUsageOnTypesInExpr() and CheckUsageOnTypesInSingleRelExpr().

The reason CheckTypeDependencyInSingleRelExpr() used to take a relation OID, I think, was to build a range table for the walker. If the walker encounters a Var that refers to that range table, it needs the entry to resolve the Var, otherwise it will raise an error. I added a boolean field skipvar to find_expr_references_context so these type checks can tell the walker to skip Var nodes.

This is a pure refactoring patch. It should preserve all existing runtime behavior. I haven't changed any tests, and "make check-world" passed. The patch removes two functions and reduces the code by 42 lines, so I think it's a maintenance improvement.

BTW, I also wonder whether CheckTypeDependencyInExpr() and CheckUsageOnTypesInExpr() really need the checkResultType parameter. But I’d leave that for a separate discussion, as I intended to preserve the existing behavior in this patch.

Please see the attached patch for details. Am I missing anything?

[1] https://postgr.es/m/1ffe1c34-4518-4305-bb8c-9b162da6c453@gmail.com

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

Attachment Content-Type Size
v1-0001-Skip-Var-resolution-when-checking-expression-type.patch application/octet-stream 21.7 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Burd 2026-11-27 02:00:56 Re: nbtree: opt-in leaf key prefix compression
Previous Message Michael Paquier 2026-11-27 01:27:48 Re: Infinite loop in XLogPageRead() on standby