5

我正在尝试创建一个event trigger, 每当创建表时执行。发生这种情况时,我想插入一个表(它有 2 列idtablename)创建的表的名称。阅读文档我无法找到如何获取表名。

到目前为止,我有这个:

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();
4

1 回答 1

3

为了能够检索一些附加信息,请使用on ddl_command_endtrigger 而不是on 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"Layerspublic

于 2016-10-08T21:10:28.900 回答