在实际模式上执行另一个模式的触发器功能
我的问题很容易用一个例子来解释:我有一个“通用”模式(公共模式?),我在其中存储集群应用程序之间的通用数据。
对于我的应用程序的每个实例,我都有一个角色(用作应用程序用户)。 我有一个公共角色,app_users
,对公共模式具有只读
权限,并且每个应用程序角色都是成员
>app_users。
现在我的问题是:如何在 app_a 方案上设置触发器来执行通用方案中的函数(过程),但影响(且仅影响)app_a 表?
我的意思是:
// common_scheme, dummy function to emulate the mysql on update = now()
CREATE OR REPLACEFUNCTION update_etime() RETURNS TRIGGER AS $$
BEGIN
NEW.etime = date_part('epoch'::text, now())::int;
RETURN NEW;
END;
$$ language plpgsql;
// now, in the app_foo scheme, i have the table:
CREATE TABLE foo_table (fid serial not null primary key unique, label char(25));
// and the trigger:
CREATE TRIGGER foo_table_update_etime BEFORE UPDATE ON foo_talbe FOR EACH ROW EXECUTE PROCEDURE update_etime();
// ERROR: function update_etime() does not exist
CREATE TRIGGER foo_table_update_etime BEFORE UPDATE ON foo_talbe FOR EACH ROW EXECUTE PROCEDURE common_scheme.update_etime();
// ERROR: function common_scheme.update_etime() does not exist
将访问 app_foo 的用户具有 common_schema 中 update_etime() 函数的执行权限。
有什么想法吗?
我用谷歌搜索过,但我发现从其他模式调用函数的唯一解决方案是类似 execute 'select * from ' ||架构名称 || '.table_name';
但我认为这在我的情况下不起作用,因为该函数必须与“本地”方案一起使用。
my problem is easy to explain with an example: I have a 'common' schema (the public one?) where I store common data between a clustered application.
For every instance of my application, I have a role (used as the application user).
And i have a common role, app_users
, with read-only
privileges on the common schema, and every application role is a member
of app_users
.
Now my problem is: how can i set a trigger on the app_a scheme that execute a function (procedure) in the common scheme, but affect the (and only the) app_a tables?
I mean:
// common_scheme, dummy function to emulate the mysql on update = now()
CREATE OR REPLACEFUNCTION update_etime() RETURNS TRIGGER AS $
BEGIN
NEW.etime = date_part('epoch'::text, now())::int;
RETURN NEW;
END;
$ language plpgsql;
// now, in the app_foo scheme, i have the table:
CREATE TABLE foo_table (fid serial not null primary key unique, label char(25));
// and the trigger:
CREATE TRIGGER foo_table_update_etime BEFORE UPDATE ON foo_talbe FOR EACH ROW EXECUTE PROCEDURE update_etime();
// ERROR: function update_etime() does not exist
CREATE TRIGGER foo_table_update_etime BEFORE UPDATE ON foo_talbe FOR EACH ROW EXECUTE PROCEDURE common_scheme.update_etime();
// ERROR: function common_scheme.update_etime() does not exist
The user that will access app_foo has the execute privilege on update_etime() function in common_schema.
Any idea?
I've googled around but the only solution I fount to call functions from other schemas is something like execute 'select * from ' || schema_name || '.table_name';
but i dont think this will do the trick in my case, becose the function must work with the 'local' scheme.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的第二组语法应该可以工作......带有“
EXECUTE PROCEDURE common_scheme.update_etime();
”的语法如果没有找到该函数,我猜你要么已经在中创建了它与您认为的模式不同的模式,或者您根本没有创建它(请注意,您的示例创建语法有一个错误,“替换”和“函数”之间没有空格,这在尝试创建时会导致错误尝试创建该函数。 a:
作为超级用户验证该函数是否存在并且位于您认为它所在的位置。HTH。
Your second set of syntax should work... the one with "
EXECUTE PROCEDURE common_scheme.update_etime();
"If it isn't finding the function, I'd guess that you either have created it in a different schema than you think it is in, or you haven't created it at all (and note, your example create syntax has a bug, no space between "replace" and "function", which would cause an error when trying to create the function. Try doing a:
As superuser to verify the function exists and is in the location you think it is in. HTH.