execute a trigger when I create a table

删除回忆录丶 提交于 2019-12-04 23:19:57

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

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.

JotaBe

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

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