execute a trigger when I create a table

不羁岁月 提交于 2019-12-22 01:43:38

问题


I would like to know if a trigger on a system table of PostgreSQL can be executed when I create a table

I need to add 2 functions on each table of my database and I would like to do it dynamically

Thanks


回答1:


I know it's an old question but now it has been implemented in version 9.3, or at least partially http://www.postgresql.org/docs/9.3/static/event-trigger-definition.html




回答2:


This can be done with an event trigger:

CREATE OR REPLACE FUNCTION on_create_table_func()
RETURNS event_trigger AS $$
BEGIN
    -- your code here
END
$$
LANGUAGE plpgsql;

CREATE EVENT TRIGGER
on_create_table ON ddl_command_end
WHEN TAG IN ('CREATE TABLE')
EXECUTE PROCEDURE on_create_table_func();

Note that there is no way to directly execute any query on the newly created table, or even get its name. I don't know what you mean by "add 2 functions on each table" since functions don't belong to a specific table, but if you need to perform an operation specific for the new tables, this might not be for you.




回答3:


You're looking for "DDL Triggers". They're not implemented in PostgreSQL. Neither you can add triggers to system tables. Look at this forum entry:

Adding ddl audit trigger



来源:https://stackoverflow.com/questions/9839746/execute-a-trigger-when-i-create-a-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!