Skip to content
VaultDBDocs

Scheduled queries

A scheduled query runs a SQL statement against a database on a cron schedule. Scheduled queries are available on the Starter plan and above. The number you can have is counted across all databases on your account: 3 on Starter, 10 on Pro, unlimited on Business.

Managing scheduled queries needs the Databases.schedules permission. Creating one also needs Databases.sql, because the statement runs as raw SQL.

Create a scheduled query

curl -X POST https://api.vaultdb.io/api/databases/shop/schedules \
  -H "Authorization: Bearer $VAULTDB_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Delete expired sessions",
    "cron_expression": "0 3 * * *",
    "sql": "DELETE FROM sessions WHERE expires_at < CURRENT_TIMESTAMP"
  }'
Field Required Description
name Yes A label for you.
cron_expression Yes Five fields: minute, hour, day of month, month, day of week. Each field takes *, a number, a range (1-5), a step (*/15, 1-30/5) or a comma-separated list of those.
sql Yes The statement to run.

Times are in UTC. 0 3 * * 1 runs at 03:00 UTC every Monday. Day of week runs from 0 (Sunday) to 6 (Saturday).

Returns 201 with { "message", "schedule" }. The schedule includes next_run_at, the next time it will run.

The statement is not checked when you create the schedule. A statement that fails records its error on each run (see below). Test the statement with raw SQL first.

List and delete scheduled queries

curl https://api.vaultdb.io/api/databases/shop/schedules \
  -H "Authorization: Bearer $VAULTDB_API_KEY"

curl -X DELETE https://api.vaultdb.io/api/databases/shop/schedules/SCHEDULE_ID \
  -H "Authorization: Bearer $VAULTDB_API_KEY"

Each schedule in the list has id, name, cron_expression, sql, status, last_run_at, next_run_at, last_result and created_at. sql and last_result are null unless the caller also has the Databases.sql permission, because the result is the output of the statement and is not filtered by row-level policies. There is no endpoint to edit or pause a schedule: delete it and create a new one.

How schedules run

  • VaultDB checks for due schedules once a minute, so a schedule can start up to about a minute after its time. Each check runs at most 50 due schedules, oldest first; the rest run on the next check.
  • A statement may run for up to 10 seconds. After that it is stopped and the run is recorded as an error. A run that starts while too many SQL statements are already running records ERROR: Too many SQL statements are running and runs again at its next time.
  • last_result holds the outcome of the latest run. It starts with OK: (followed by the row count for a read, and the first 2,000 characters of the result; a read keeps at most 1,000 rows and then says first 1000 rows of more), ERROR: (followed by the error message), or SKIPPED: (followed by the reason: the account was not active, or the plan does not run this schedule).
  • A failed run does not stop the schedule. It runs again at its next time.
  • The statement follows the raw SQL rules: only SELECT, INSERT, UPDATE, DELETE, REPLACE, WITH and VALUES statements run, anything naming sqlite_ or pragma_ is refused, and a write must stay within your plan's storage, rows per table and bulk insert limits.
  • Schedules follow your current plan. On the Free plan none of them run. On a plan that allows fewer schedules than you have, the oldest ones run, up to the plan's number, and the rest are skipped until you upgrade or delete some.
  • Writes made by a scheduled query do not send webhooks or realtime events, and do not apply row-level policies.