CREATE TASK lets you run ClickHouse queries automatically on a cron-like schedule without external tools.
Automated jobs refresh roll-ups, clean logs, and feed dashboards without manual effort. Native scheduling avoids external cron servers and keeps logic close to data.
CREATE TASK stores a query in system.tasks and executes it per a cron expression.Tasks survive restarts and can be ENABLED or DISABLED.
Use CREATE TASK with ON SCHEDULE to insert aggregated data into a reporting table every night.
CREATE TASK daily_sales_rollup
ON SCHEDULE '0 2 * * *'
AS INSERT INTO sales_daily (order_date, total_amount)
SELECT toDate(order_date), sum(total_amount)
FROM Orders
GROUP BY toDate(order_date);
ALTER TASK … DISABLE stops execution while keeping the definition.ALTER TASK … ENABLE resumes the schedule.
ALTER TASK with a new CRON string updates when the query runs. The task ID remains the same, so monitoring scripts stay intact.
system.task_log records start time, duration, state, and errors. Query it to build alerts or dashboards.
1. Aggregate into new tables, not source tables. 2. Guard against duplicates with toStartOfInterval filters. 3.Log errors to Slack via Webhooks.
Missing timezone: Cron strings run in server TZ. Set timezone parameter when servers span regions.
Heavy joins at peak: Schedule ETL during off-hours to prevent resource contention.
Chain tasks by appending INSERT … SELECT from yesterday’s roll-up, or orchestrate with Airflow when dependencies grow.
.
No, the query body must be a fully formed statement. Use macros like today() for dynamic dates.
Tasks don’t rerun automatically for downtime gaps. Manually execute the query or adjust the WHERE clause to cover missed periods.
Yes, since v21.12 it’s marked stable, but always monitor system.task_log and set memory limits in the query.