在postgres
数据库中,有一个表base1
,它是查看view1
的基表。
如果创建,删除或重命名base1
中的列,我想使用ddl触发器重新创建视图view1
。
create event trigger base1_views
on ddl_command_end
when tag in( 'ALTER TABLE' )
execute procedure base1_views_fn();
create function base1_views_fn() returns void as $$
declare
buf varchar;
begin
-- is table being altered = 'base'?
-- add, drop or renaming a column?
buf = 'drop view view1';
execute buf;
buf = 'create view view1 as select * from base1 where ...';
execute buf;
end;
$$ language 'plpgsql';
在函数base1_views_fn()
中,我们如何获取表名以及我们是否要更改列?
答案 0 :(得分:2)
此处解决了同样的问题:How to get SQL text from Postgres event trigger
底线,截至目前,等到将来版本中将更多功能添加到EVENT TRIGGER
答案 1 :(得分:1)
触发器中plpgsql可用的变量在此处定义:
http://www.postgresql.org/docs/9.3/static/plpgsql-trigger.html#PLPGSQL-EVENT-TRIGGER-EXAMPLE
我从文中无法分辨的是有多少'事件'变量。当然有两个:
TG_EVENT
Data type text; a string representing the event the trigger is fired for.
TG_TAG
Data type text; variable that contains the command tag for which the trigger is fired.
您可以在功能中打印这些内容,以查看它们是否包含您要查找的表格信息。该文档显示了一系列常规事件的其他变量。我不知道这些是否会有所帮助,但是,TG_TABLE_NAME可能已经确定了吗?