| From: | Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> |
|---|---|
| To: | Heikki Linnakangas <hlinnaka(at)iki(dot)fi> |
| Cc: | Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, "Zhijie Hou (Fujitsu)" <houzj(dot)fnst(at)fujitsu(dot)com> |
| Subject: | Re: Skip Var resolution when checking expression type dependencies |
| Date: | 2026-11-28 01:19:11 |
| Message-ID: | C2901C55-5E35-4007-B4D1-D67DE234C409@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
> On Nov 27, 2026, at 16:34, Heikki Linnakangas <hlinnaka(at)iki(dot)fi> wrote:
>
> On 27/11/2026 03:38, Chao Li wrote:
>> 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?
>
> You're not wrong, I also wondered if we should do something like this, while working on the security patch. I decided not to, firstly because it's more refactoring and it didn't seem worth the risk to backpatch.
When I prepared the patch, I saw it as a pure refactoring and only had master in mind.
> But even on master, I think it's actually good that CheckTypeDependencyInSingleRelExpr() has a similar signature and shares as much of the implementation as possible as recordDependencyOnSingleRelExpr(). So overall, I'm -1 on this.
I get your point. I wasn’t satisfied with adding a checkResultType parameter either, since it asks every caller to decide whether to pass true or false, and that’s why I mentioned “I wonder if checkResultType is really needed” in the patch email. With the current interface, though, callers still have to choose between CheckTypeDependencyInExpr() and CheckTypeDependencyInSingleRelExpr(). It wasn’t obvious to me from the names when to use each one.
I think you make a good point about pairing CheckTypeDependencyInSingleRelExpr() with recordDependencyOnSingleRelExpr(). How about keeping CheckTypeDependencyInSingleRelExpr(), dropping its relId parameter, and making that pairing explicit in its header comment?
That would preserve the existing type checks, remove the need for callers to supply a relation OID, and make it clearer when to use the single-relation helper. I made the same change to the USAGE checks. Please see the attached v2.
If you’d still prefer to keep the signatures similar, I’m happy to drop v2. In that case, could we clarify in the header comment for CheckTypeDependencyInSingleRelExpr() that relOid is used to resolve Vars but doesn’t affect the result of the type check? That might save the future folks who wonders about relOid from going down the same path as I did.
>
> There's certainly more we could do in this area, though. It's a bit awkward that we need CheckTypeDependencyInSingleRelExpr() in the first place. If we stored more fine-grained dependency information in pg_depend, so that we could tell whether a dependency e.g. from a function to a type exists because the function has a DEFAULT expression that uses the type, or because the function returns the type, we wouldn't need CheckTypeDependencyInSingleRelExpr() at all.
>
> - Heikki
That sounds reasonable. I took a quick look, but feels like it would require more research on that. I will put this item to my TODO, and spend some time there later.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
| Attachment | Content-Type | Size |
|---|---|---|
| v2-0001-Skip-Var-resolution-when-checking-expression-type.patch | application/octet-stream | 22.1 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Greg Burd | 2026-11-28 01:38:39 | Re: nbtree: opt-in leaf key prefix compression |
| Previous Message | Tom Lane | 2026-11-28 01:16:02 | Re: [PATCH] btree_gist: overflow in gbt_numeric_penalty (sibling of 15b3dc140d9) |