Intermediate LevelPostgreSQL
How do Event Triggers monitor and intercept DDL schema changes?
❓ Interview Prompt: Explain Event Triggers (`CREATE EVENT TRIGGER`), DDL event hooks (`ddl_command_start`, `ddl_command_end`, `sql_drop`), and schema auditing.
Unlike standard triggers that fire on DML row changes ('INSERT'/'UPDATE'), Event Triggers fire when DDL schema changes ('CREATE TABLE', 'ALTER TABLE', 'DROP') execute:
Use Cases:
•
Auditing DDL schema migrations.
•
Blocking unauthorized table drops in production databases.
Code Implementation Example
sql
CREATE OR REPLACE FUNCTION audit_ddl_changes()
RETURNS event_trigger AS $$
BEGIN
RAISE NOTICE 'DDL Command Executed: %', tg_tag;
END;
$$ LANGUAGE plpgsql;
CREATE EVENT TRIGGER log_ddl_event
ON ddl_command_end
EXECUTE FUNCTION audit_ddl_changes();💡Intercepting DDL schema changes via Event Triggers.
🎯 Interview Key Takeaway
Use Event Triggers to audit or restrict DDL schema modifications across production databases.
#Event Triggers#DDL#Auditing#Intermediate
Back to all PostgreSQL Questions→