Event triggers automatically run SQL or external actions when INSERT, UPDATE, DELETE, or ALTER events occur in ClickHouse tables.
Event triggers let you automate auditing, real-time ETL, and alerting without external schedulers. They execute immediately after the specified event, guaranteeing near-zero latency.
Create a trigger with CREATE TRIGGER
. Specify timing (AFTER
), event type (INSERT
, UPDATE
, DELETE
, ALTER
), target table, and action statement or function.
The syntax resembles PostgreSQL but omits procedural languages.The action is any valid ClickHouse query.
Yes. Use NEW
for inserted/updated rows and OLD
for deleted rows. Each is a tuple matching the table schema.
Disable with ALTER TRIGGER ... DISABLE
.Drop permanently using DROP TRIGGER
.
Keep the action idempotent, avoid heavy aggregation, log errors to a dedicated table, and version-control trigger definitions.
The example below writes every new order’s total to order_audit
, a lightweight MergeTree table.
CREATE TRIGGER orders_audit_trg
AFTER INSERT ON Orders
AS INSERT INTO order_audit (order_id,total_amount,ts)
SELECT id,total_amount,now()
FROM NEW;
Each trigger name must be unique in the database.Append _trg
or a timestamp to avoid clashes.
Slow queries inside a trigger block the original DML. Move heavy work to an asynchronous pipeline or materialized view.
Only if the action query is slow. Keep the logic lightweight or async.
Use url
table engine inside the action to insert into a remote endpoint.
.
No. Create separate triggers for each event type to simplify debugging and maintenance.
Yes, DDL replication ensures triggers exist on all shards, provided ZooKeeper or ClickHouse Keeper is configured.