Scheduling functions in the Cronjob DuckDB extension
Cronjob's scheduler needs background threads, which this site's browser runtime does not support. Run these examples in native DuckDB or Haybarn.
Function category
Scheduling
4 functionsRegister, list, and cancel cron-style scheduled SQL queries. Six-field cron expressions with second-level resolution. Schedules live in memory inside the DuckDB process — when the process exits, every job is forgotten. For restart-safe scheduling, drive the DuckDB run from an external orchestrator (system cron(8), systemd timers, Kubernetes CronJob).
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
query
|
Type
VARCHAR
|
Mode Positional | Description |
Argument
schedule
|
Type
VARCHAR
|
Mode Positional | Description |
Description
Register a SQL query to run on a six-field cron expression (second minute hour day-of-month month day-of-week, evaluated in UTC). Returns a job_id (e.g. task_0) you can later pass to cron_delete or look up in cron_jobs. The schedule fires only while this database is open — when the process exits or the database closes, the registration is gone.
DuckDB 2.0 build: cron is VOLATILE, so every row schedules its own job — SELECT cron(q, s) FROM jobs_to_schedule reads each row's query and schedule, and EXPLAIN/PREPARE never schedule anything. A NULL query or schedule returns NULL and schedules nothing. The query is checked for syntax errors and the schedule must match at least one real date: '0 0 0 30 2 *' (February 30th) is an error instead of a job that runs continuously. The Haybarn 1.5 build takes the schedule from the first row only and accepts these inputs.
SELECT cron('SELECT now()', '*/15 * 1-4 * * *');
SELECT cron('CALL daily_metrics_export()', '0 0 7 ? * MON-FRI');
SELECT cron('CREATE OR REPLACE TABLE hourly_rollup AS SELECT date_trunc(''hour'', ts) AS hour, count(*) AS n FROM events GROUP BY 1', '0 0 * * * *');
Related functions
- cron_jobs() — Table function listing every scheduled job in this database — `job_id`, `query`, `schedule`, `next_run`, `status` (`Active`/`Inactive`), `last_run`, and `last_result` (`Success` or `Error: <message>`)
- cron_delete() — Cancel a scheduled job by `job_id` (the value returned from `cron` or visible in `cron_jobs`)
- cron_job_history() — **DuckDB 2
Signature
Arguments (Positional)
| Argument | Type | Mode | Description |
|---|---|---|---|
Argument
job_id
|
Type
VARCHAR
|
Mode Positional | Description |
Description
Cancel a scheduled job by job_id (the value returned from cron or visible in cron_jobs). Returns TRUE when the job is removed and raises an error if no job has that id. A run that is already in progress finishes; no further runs start. Closing the database also cancels every job.
DuckDB 2.0 build: cron_delete(NULL) returns NULL, and the function is VOLATILE.
SET VARIABLE job = (SELECT cron('ANALYZE', '0 30 1 * * *'));
SELECT cron_delete(getvariable('job'));
SELECT cron_delete(job_id) FROM cron_jobs();
Related functions
- cron() — Register a SQL query to run on a six-field cron expression (`second minute hour day-of-month month day-of-week`, evaluated in UTC)
- cron_jobs() — Table function listing every scheduled job in this database — `job_id`, `query`, `schedule`, `next_run`, `status` (`Active`/`Inactive`), `last_run`, and `last_result` (`Success` or `Error: <message>`)
- cron_job_history() — **DuckDB 2
Signature
Description
DuckDB 2.0 build only. Table function listing the most recent runs of every scheduled job — up to 100 per job — with job_id, started_at and finished_at (TIMESTAMPTZ), success (BOOLEAN) and result (Success or Error: <message>). History is kept in memory and disappears when the job is deleted or the database closes.
SELECT * FROM cron_job_history() ORDER BY started_at DESC;
SELECT job_id, started_at, result FROM cron_job_history() WHERE NOT success;
SELECT job_id, max(finished_at - started_at) AS slowest FROM cron_job_history() GROUP BY job_id;
Related functions
- cron() — Register a SQL query to run on a six-field cron expression (`second minute hour day-of-month month day-of-week`, evaluated in UTC)
- cron_jobs() — Table function listing every scheduled job in this database — `job_id`, `query`, `schedule`, `next_run`, `status` (`Active`/`Inactive`), `last_run`, and `last_result` (`Success` or `Error: <message>`)
- cron_delete() — Cancel a scheduled job by `job_id` (the value returned from `cron` or visible in `cron_jobs`)
Signature
Description
Table function listing every scheduled job in this database — job_id, query, schedule, next_run, status (Active/Inactive), last_run, and last_result (Success or Error: <message>). Filter on last_result to find failing jobs.
DuckDB 2.0 build: next_run and last_run are TIMESTAMPTZ (previously VARCHAR text in the server's local time), last_run is when the last run started, and three columns are added: run_count, error_count and created_at. Jobs are listed in creation order and listings of any size are returned. The Haybarn 1.5 build returns next_run/last_run as VARCHAR.
SELECT * FROM cron_jobs();
SELECT job_id, schedule FROM cron_jobs() WHERE last_run IS NULL;
SELECT job_id, last_run, last_result FROM cron_jobs() WHERE last_result LIKE 'Error:%';