Logging DDL Command Start Events
suggest changeEvent types include:
ddl_command_startddl_command_endsql_drop(drops only; PostgreSQL 9.4 and later)table_rewrite(PostgreSQL 9.5 and later)
This example creates an event trigger that logs ddl_command_start. Event trigger functions cannot be written in plain SQL; TG_TAG is available in PL/pgSQL.
CREATE TABLE tab_event_logs (
date_time timestamp,
event_name text,
remarks text
);
CREATE OR REPLACE FUNCTION fn_log_event()
RETURNS event_trigger
LANGUAGE plpgsql
AS
$main$
BEGIN
INSERT INTO tab_event_logs (date_time, event_name, remarks)
VALUES (now(), tg_tag, 'Event Logging');
END;
$main$;
CREATE EVENT TRIGGER trg_log_event ON ddl_command_start
EXECUTE PROCEDURE fn_log_event();
Creating an event trigger requires a superuser. EXECUTE PROCEDURE has worked since event triggers were added in PostgreSQL 9.3. PostgreSQL 11 and later also accept EXECUTE FUNCTION.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents