我有一个带PostgreSQL 8.1.23的服务器,其功能在与postgres
用户一起运行时效果很好,但另一个用户显示SQL状态:
SQL state: 42883
这是我的功能:
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;
如果我使用这样的postgres用户运行:
select fun_validatepost(1,230465);
结果如下:
-[ RECORD 1 ]-----------+--
fun_validatepost | 1
但如果我执行与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
即使做了明确的演员,我也会得到相同的结果:
select fun_validatepost from fun_validatepost(1::integer,230465::integer);
相同的错误消息。
我能做什么someuser
可以执行相同的功能?
我的功能或演员有什么问题吗?
答案 0 :(得分:2)
最有可能是架构与架构问题search_path
。该功能在创建用户的默认架构中创建。如果这不在当前用户的search_path
中,则不可见。
详细说明:
通常,您可以在架构public
中创建公共函数,并在everbody 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;
只有在public
不是默认架构的情况下才需要架构资格。
此外,您的GRANT
命令毫无意义。默认情况下,EXECUTE
功能的public
权限授予public
。一旦您授予postgres
,就无需授予其他用户。特别是不要OWNER
,无论如何都是PUBLIC
和超级用户。 The manual:
PostgreSQL为某些类型的对象授予默认权限
EXECUTE
。 [...]USAGE
功能特权;
您需要在创建函数的SCHEMA
上授予public
。默认情况下,USAGE
架构会向public
(每个人)授予integer
。
转换为/* 1 */ #include <fstream>
/* 2 */ #include <iostream>
/* 3 */ #include <string>
/* 4 */
/* 5 */ using namespace std;
/* 6 */ int main()
/* 7 */ {
/* 8 */
/* 9 */ ifstream fin;
/* 10 */ int data[50][8];
/* 11 */ string names[50];
/* 12 */
/* 13 */ fin.open("test.txt");
/* 14 */
/* 15 */ int numOfNames;
/* 16 */ fin >> numOfNames;
/* 17 */
/* 18 */ for (int i = 0; i < numOfNames; i++) {
/* 19 */
/* 20 */ fin >> names[i];
/* 21 */
/* 22 */ data[i][7] = 0; /* use last spot in array for sum, set to 0. */
/* 23 */ for (int j = 0; j < 7; j++) {
/* 24 */ fin >> data[i][j];
/* 25 */ data[i][7] += data[i][j];
/* 26 */ }
/* 27 */ }
/* 28 */
/* 29 */ for (int i = 0; i < numOfNames; i++)
/* 30 */ {
/* 31 */ cout << data[i][7] << endl; /* add each element to the sum here*/
/* 32 */ }
/* 33 */ return 0;
/* 34 */ }
不会改变任何内容,因为没有小数点的数字文字会自动强制转换为整数。 Details about constants in the manual.
Urgently consider updating to a current version of Postgres. Your software is completely outdated.