Re: Skip Var resolution when checking expression type dependencies

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: "Zhijie Hou (Fujitsu)" <houzj(dot)fnst(at)fujitsu(dot)com>
Subject: Re: Skip Var resolution when checking expression type dependencies
Date: 2026-11-27 08:34:44
Message-ID: 5e6e6ae6-c9f5-4d8c-8235-6dffa53e0b97@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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.
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.

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2026-11-27 08:40:02 Re: Let OpenSSL auto-select DH parameters by default
Previous Message Jakub Wartak 2026-11-27 08:11:02 Re: enhancing pg_basebackup speeds up to ~23Gbps (small fixes + io_uring/Direct I/O)