列重命名后函数调用失败(错误 42703 "column does not exist")

Function call fails after column renaming (error 42703 "column does not exist")

简单场景:

CREATE TABLE foo (bar int);

INSERT INTO foo VALUES (42);

CREATE FUNCTION baz() RETURNS int AS $$
SELECT bar
FROM foo
$$ LANGUAGE SQL;

SELECT baz();

这有效 returns 42.

我不喜欢我的专栏命名,所以我重命名它:

ALTER TABLE foo RENAME bar TO qux;

但是现在如果我再次调用该函数:

SELECT baz();

[42703] ERROR: column "bar" does not exist

SQL function "baz" during inlining

我希望将函数体重命名为 "cascade",因为 PostgreSQL 会阻止创建具有无效列引用的函数。

我真的必须重新创建每个以旧名称引用该列的函数吗?使用版本 10.7.

文档说函数体是a string constant whose meaning depends on the language. This is interpreted at run-time by the language handler, and the database server has no built-in knowledge about how to interpret the function's source text

因此 PostgreSQL 不跟踪任何依赖项,函数按名称而不是按对象 ID 引用 tables 和列(就像视图一样)。

因此 PostgreSQL 无法自动更改函数体字符串,因为其中引用的 table 列以 PostgreSQL 未知的语法更改其名称。