database – 获取创建的表名

我正在尝试创建一个事件触发器,每当创建一个表时执行.

当发生这种情况时,我想插入一个表(具有2列id和tablename)创建的表的名称.阅读文档我无法找到如何获得表名.

到目前为止我有这个:

CREATE OR REPLACE FUNCTION insert_layer()
RETURNS event_trigger
AS $$
    DECLARE r RECORD;
    BEGIN
            RAISE NOTICE 'event for % ', tg_tag;
            -- I would like to execute this
            --EXECUTE format('INSERT INTO "public.Layers"(name) VALUES(' || tableNameHere || ')') INTO result;
    END;
$$
LANGUAGE plpgsql;

CREATE EVENT TRIGGER insert_layer_event ON ddl_command_start 
WHEN TAG IN ('CREATE TABLE')
EXECUTE PROCEDURE insert_layer();

最佳答案 为了能够检索一些其他信息,请使用ddl_command_end触发器而不是ddl_command_start.在调用此类触发器的函数中,您可以使用
pg_event_trigger_ddl_commands function

CREATE OR REPLACE FUNCTION insert_layer()
RETURNS event_trigger
AS $$
    DECLARE r RECORD;
    BEGIN
            RAISE NOTICE 'event for % ', tg_tag;
            -- I would like to execute this
            r := pg_event_trigger_ddl_commands(); 
            INSERT INTO public."Layers"(name) VALUES(r.object_identity);
    END;
$$
LANGUAGE plpgsql;

注意代码更改:

1)您不需要使用EXECUTE
2)“public.Layers”表示当前模式中名称与“public.Layers”完全相同的表,而不是模式public中的表.

点赞