Starting from PostgreSQL 9.5, function pg_event_trigger_ddl_commands()
is available for ddl_command_end
event triggers. Using the TAG
filter, it may be used for processing any ALTERed table. object_identity
(or objid
) may be used to solve the original problem of knowing which table has been ALTERed. As for getting the complete command, it is available, too, but it is of an internal type pg_ddl_command
.
CREATE TABLE t (n INT);
CREATE FUNCTION notice_event() RETURNS event_trigger AS $$
DECLARE r RECORD;
BEGIN
FOR r IN SELECT * FROM pg_event_trigger_ddl_commands() LOOP
RAISE NOTICE 'caught % event on %', r.command_tag, r.object_identity;
END LOOP;
END;
$$
LANGUAGE plpgsql;
CREATE EVENT TRIGGER tr_notice_alter_table
ON ddl_command_end WHEN TAG IN ('ALTER TABLE')
EXECUTE PROCEDURE notice_event();
ALTER TABLE t ADD c CHAR;
outputs:
NOTICE: caught ALTER TABLE event on public.t