Event triggers fire a function automatically when specific DDL events (CREATE, ALTER, DROP) occur, enabling auditing, security checks, or automation.
Amazon Redshift is PostgreSQL-compatible, but it does not implement the CREATE EVENT TRIGGER
command. You can, however, mimic event-trigger behavior with classic table triggers, system tables, and external services like Amazon EventBridge.
Create AFTER triggers on catalog tables (e.g., pg_table_def
) inside a separate auditor schema.Combine the trigger with an SNS or EventBridge rule to push notifications when objects are added, altered, or dropped.
Ecommerce teams often want to know when the Orders
table changes. The code below logs any DDL touching that table.
CREATE TABLE audit.ddl_log (
id bigint IDENTITY(1,1),
ddl_time timestamp default SYSDATE,
ddl_user varchar(100),
object_name varchar(200),
ddl_sql text
);.
CREATE OR REPLACE FUNCTION audit.log_ddl()
RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO audit.ddl_log(ddl_user,object_name,ddl_sql)
VALUES (current_user, TG_TABLE_NAME, pg_last_query());
RETURN NULL;
END;
$$;
-- Attach AFTER trigger to system catalog table
CREATE TRIGGER orders_ddl_watch
AFTER INSERT OR UPDATE OR DELETE ON pg_table_def
FOR EACH STATEMENT
EXECUTE PROCEDURE audit.log_ddl();
If you migrate to PostgreSQL 15, the canonical pattern looks like:
CREATE EVENT TRIGGER ddl_audit ON ddl_command_end
WHEN TAG IN ('CREATE TABLE','ALTER TABLE','DROP TABLE')
EXECUTE PROCEDURE audit.log_ddl();
Store audit tables in a dedicated schema, restrict GRANTs, and set WLM queues so audit code never blocks production queries.Use federated queries to pipe logs into S3 or CloudWatch for long-term retention.
Have CI/CD jobs read the audit table and flag unexpected DDL in pull requests. This keeps your ecommerce database stable and PCI-compliant.
.
No. The catalog is absent because event triggers are unsupported.
Minimal. They are statement-level and run infrequently, but keep them in a low-priority WLM queue.
If your workload depends heavily on event triggers and advanced PL/pgSQL, a PostgreSQL-based warehouse may offer more flexibility.