The idea · Lab 2.7.4Every trigger this course has used so far — Lab 2.7.1's pg_notify trigger included — is a row-level trigger: it fires on INSERT, UPDATE, or DELETE against one specific table's data. An event trigger is a completely different mechanism, firing on DDL — schema changes — cluster-wide, regardless of which table or schema is affected, with no per-table CREATE TRIGGER needed anywhere.
This lab builds a DDL audit log the honest way: get a first version working, discover exactly what it misses by testing it against a real DROP TABLE, and only then fix it — rather than presenting the correct two-trigger design as if it were obvious from the start. The gap is real and specific: ddl_command_end fires for every DDL command, but pg_event_trigger_ddl_commands() genuinely does not report DROP operations at all. Seeing this happen firsthand is worth far more than being told about it in advance.
Row-Level Triggers vs Event Triggers
| Row-level trigger | Event trigger |
|---|
| Fires on | INSERT/UPDATE/DELETE against one table's rows | DDL commands, cluster-wide |
| Attached to | One specific table | The whole database (or cluster, for some events) |
| Typical use | Firing pg_notify, maintaining a derived column | Auditing schema changes, blocking dangerous DDL |
The First Version — and What It Misses
CREATE OR REPLACE FUNCTION log_ddl() RETURNS event_trigger AS $$
DECLARE r record;
BEGIN
FOR r IN SELECT * FROM pg_event_trigger_ddl_commands() LOOP
INSERT INTO ddl_audit_log (username, command_tag, object_type, object_identity)
VALUES (current_user, r.command_tag, r.object_type, r.object_identity);
END LOOP;
END;
$$ LANGUAGE plpgsql;
CREATE EVENT TRIGGER ddl_audit ON ddl_command_end EXECUTE FUNCTION log_ddl();
This correctly logs CREATE TABLE and ALTER TABLE — and silently logs nothing at all for DROP TABLE, because pg_event_trigger_ddl_commands() simply does not enumerate dropped objects.
The Fix: A Separate sql_drop Trigger
CREATE OR REPLACE FUNCTION log_ddl_drop() RETURNS event_trigger AS $$
DECLARE r record;
BEGIN
FOR r IN SELECT * FROM pg_event_trigger_dropped_objects() LOOP
INSERT INTO ddl_audit_log (username, command_tag, object_type, object_identity)
VALUES (current_user, 'DROP', r.object_type, r.object_identity);
END LOOP;
END;
$$ LANGUAGE plpgsql;
CREATE EVENT TRIGGER ddl_audit_drop ON sql_drop EXECUTE FUNCTION log_ddl_drop();
A complete audit needs both event triggers — one for creates and alters, a separate one specifically for drops.
Blocking Dangerous DDL Outright
CREATE OR REPLACE FUNCTION guard_protected() RETURNS event_trigger AS $$
DECLARE r record;
BEGIN
FOR r IN SELECT * FROM pg_event_trigger_dropped_objects() LOOP
IF r.object_identity LIKE 'public.protected\_%' THEN
RAISE EXCEPTION 'Refusing to drop protected object: %', r.object_identity;
END IF;
END LOOP;
END;
$$ LANGUAGE plpgsql;
CREATE EVENT TRIGGER guard_drop ON sql_drop EXECUTE FUNCTION guard_protected();
Unlike an audit log, this event trigger has teeth: RAISE EXCEPTION inside it aborts the entire DROP statement, cluster-wide, for every session — no per-table permission trick required.
Key concepts
Event Trigger
A trigger that fires on DDL events (schema changes) rather than DML changes to a specific table's rows. Event triggers are cluster-wide by nature — one CREATE EVENT TRIGGER covers every table and schema in the database, with no per-table attachment needed, which is exactly what makes them suitable for auditing or governance that must apply universally.
ddl_command_end vs sql_drop
ddl_command_end fires after most DDL commands complete and exposes what was created or altered via pg_event_trigger_ddl_commands() — but this function does not enumerate objects that were dropped. sql_drop is a separate event specifically for drop operations, paired with pg_event_trigger_dropped_objects() to see exactly what was removed. A complete DDL audit needs both.
What to remember
- Event triggers fire on DDL cluster-wide, with no per-table attachment — a fundamentally different mechanism from row-level triggers
- pg_event_trigger_ddl_commands() on ddl_command_end does not enumerate dropped objects — a separate sql_drop trigger and pg_event_trigger_dropped_objects() are required for that
- Always test each DDL command type an audit mechanism is meant to cover, rather than assuming coverage from the ones tested first
- Dropping one table produces multiple dropped objects — the table, its automatic composite row type, and that type's array type — all independently reported
- RAISE EXCEPTION inside an event trigger aborts the DDL statement for every session, superusers included, unlike a permissions-based defense