Get the name of the created table

I try to create event trigger, executed whenever a table is created. When this happens, I would like to insert the name of the created table into the table (which has 2 columns and ). Reading the documents, I can not find how to get the table name. id tablename

So far I have this:

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
source share
1 answer

For more information, use a on ddl_command_endtrigger instead on ddl_command_start. In a function that calls such a trigger, you can use 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;

Note the code changes:

1) EXECUTE
2) "public.Layers" "public.Layers" , Layers public.

+2

Source: https://habr.com/ru/post/1657156/


All Articles