SQL 状态:42883,没有函数匹配给定的名称和参数类型。但是那个函数确实存在

mmo*_*tes 5 postgresql casting function search-path

我有一个带有 PostgreSQL 8.1.23 的服务器,它的功能在与postgres用户一起运行时完美运行,但与另一个用户一起显示 SQL 状态:

SQL state: 42883
Run Code Online (Sandbox Code Playgroud)

这是我的功能:

CREATE OR REPLACE FUNCTION fun_validatepost(integer, integer)
  RETURNS integer AS
$BODY$
...
$BODY$
LANGUAGE plpgsql VOLATILE;
ALTER FUNCTION fun_validatepost(integer, integer)
  OWNER TO postgres;
GRANT EXECUTE ON FUNCTION fun_validatepost(integer, integer) TO public;
GRANT EXECUTE ON FUNCTION fun_validatepost(integer, integer) TO postgres;
GRANT EXECUTE ON FUNCTION fun_validatepost(integer, integer) TO someuser;
Run Code Online (Sandbox Code Playgroud)

如果我像这样使用 postgres 用户运行它:

select fun_validatepost(1,230465);
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

-[ RECORD 1 ]-----------+--
fun_validatepost        | 1
Run Code Online (Sandbox Code Playgroud)

但是,如果我与 someuser 执行相同的查询,则会显示以下消息:

ERROR:  function fun_validatepost(integer, integer) does not exist
SQL state: 42883
HINT:  No function matches the given name and argument types. You may need to add explicit type casts
Run Code Online (Sandbox Code Playgroud)

即使执行显式转换,我也会得到相同的结果:

select fun_validatepost from fun_validatepost(1::integer,230465::integer);
Run Code Online (Sandbox Code Playgroud)

同样的错误信息。

我可以做什么才能someuser执行相同的功能?
我的函数或演员有什么问题吗?

Erw*_*ter 5

很可能是 schema 与 schema 的问题search_path。该函数是在创建用户的默认架构中创建的。如果它不在search_path当前用户的目录中,则它不可见。

细节:

通常,您会在架构中创建公共函数public,并将该架构放在每个人的search_path.

CREATE OR REPLACE FUNCTION public.fun_validatepost(integer, integer)
  RETURNS integer AS
$BODY$
...
$BODY$ LANGUAGE plpgsql;
ALTER FUNCTION public.fun_validatepost(integer, integer) OWNER TO postgres;
Run Code Online (Sandbox Code Playgroud)

public仅当不是默认模式时才需要模式限定。

另外,你的GRANT命令毫无意义。EXECUTE默认情况下授予函数权限public。并且一旦授予public,就无需再授予其他用户。特别是不要postgres,无论如何OWNER,它也是超级用户。手册:

PostgreSQL 授予某些类型对象的默认权限 PUBLIC。[...]EXECUTE功能特权;

您确实需要授予创建该函数的位置USAGESCHEMApublic架构USAGE授予public默认授予(每个人)

一边 1

此处转换为integer不会改变任何内容,因为没有小数点的数字文字会自动强制转换为整数。手册中有关常量的详细信息。

一边2

紧急考虑更新到 Postgres 的当前版本。你的软件已经完全过时了。