Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Table Name in Event Trigger

I have an event trigger that executes on create, alter and drop table.

create event trigger CustomizeTable
   on ddl_command_end
   when tag in ( 'create table', 'alter table', 'drop table' )
   execute procedure CustomizeTable();

Within the procedure I want to create a trigger on the newly created table.

create or replace function CustomizeTable() returns event_trigger as 
$$
begin
      EXECUTE 'create trigger DoAudit after update on XXXXXX...
end;
$$
language plpgsql;

How can I get the table name within the event trigger?

I tried using TG_TABLE_NAME as explained here but it seems that this only works on non-event-triggers.

like image 952
ChrisB Avatar asked Oct 03 '16 14:10

ChrisB


1 Answers

You'll have to use the event trigger information functions described in the documentation.

For example, to get the name a newly created table, use

SELECT objid::regclass
FROM pg_event_trigger_ddl_commands();
like image 87
Laurenz Albe Avatar answered Sep 29 '22 08:09

Laurenz Albe