Skip to content

Scheduled jobs and queues

Both features live in your project’s Postgres, so a job or a queue message commits and rolls back together with your data. They suit database-side work: expiring rows, rolling up numbers, retrying a payment callback. For work that calls other services use Edge Functions or Flows.

There is no cron schema in your database. You manage jobs through the Management API or the dashboard page Database → Cron jobs. Every job route needs the project admin role (scopes database:read to read, database:write to change); creating, replacing, pausing and deleting a job is recorded in the audit log.

Terminal window
# create or replace a job: 5-field cron expression, or "N seconds"
curl -s -X POST -H "Authorization: Bearer $PAT" -H 'content-type: application/json' -d '{"name":"release-expired-holds","schedule":"* * * * *","command":"select public.release_expired_holds()"}' $BASE_API/v1/projects/shop/cron/jobs
curl -s -H "Authorization: Bearer $PAT" $BASE_API/v1/projects/shop/cron/jobs # name, schedule, command, active
curl -s -H "Authorization: Bearer $PAT" "$BASE_API/v1/projects/shop/cron/runs?name=release-expired-holds&limit=20"
curl -s -H "Authorization: Bearer $PAT" $BASE_API/v1/projects/shop/cron/limits # max_jobs, min_interval_seconds, job_count
# pause / resume (the schedule and command are kept)
curl -s -X PATCH -H "Authorization: Bearer $PAT" -H 'content-type: application/json' -d '{"active":false}' $BASE_API/v1/projects/shop/cron/jobs/release-expired-holds
# run now, once
curl -s -X POST -H "Authorization: Bearer $PAT" $BASE_API/v1/projects/shop/cron/jobs/release-expired-holds/run
curl -s -X DELETE -H "Authorization: Bearer $PAT" $BASE_API/v1/projects/shop/cron/jobs/release-expired-holds
  • name: letters, digits and _ . : -, up to 64 characters. Posting an existing name replaces the job (and makes it active again).
  • schedule: a standard 5-field cron expression, or an interval such as 30 seconds.
  • command: SQL, up to 8000 characters. Put real logic in a function and call it, as above. SET ROLE, RESET ROLE and SET SESSION AUTHORIZATION are refused (400 invalid_cron_job): the job already runs as service_role.
  • PATCH with {"active": false} pauses a job, {"active": true} resumes it.
  • Run now executes the command once, immediately, with a 30 second timeout, also for a paused job. It returns { name, status: "succeeded" | "failed", duration_ms, rows?, error? }. A manual run does not appear in runs.
  • runs returns the last 50 runs by default (up to 500).
  • limits returns the effective max_jobs, min_interval_seconds and the current job_count.

Default limits are 20 jobs per project and a minimum interval of 60 seconds; a schedule faster than that is refused (422 cron_limit_exceeded). On Cloud the limits follow your plan and the error is 402 plan_limit_exceeded, see Plans and limits.

Turn on the pgmq extension, then use it with the secret key or the SQL editor (service_role):

select pgmq.create('emails');
select pgmq.send('emails', '{"to":"a@example.com"}'); -- returns the message id
select * from pgmq.read('emails', 30, 5); -- hide up to 5 messages for 30 s while you work
select pgmq.archive('emails', 1); -- done; use pgmq.delete to drop instead

A message that is read but not archived or deleted becomes visible again when its visibility timeout (the 30 above) passes, so a crashed worker never loses it. Make consumers idempotent for the same reason.

The anon and authenticated roles cannot use the pgmq schema. Wrap the operation in a SECURITY DEFINER function that checks who is calling:

create function public.enqueue_email(p_to text) returns bigint
language plpgsql security definer set search_path = '' as $$
begin
if auth.uid() is null then
raise exception 'sign in first' using errcode = '42501';
end if;
return pgmq.send('emails', jsonb_build_object('to', p_to, 'user_id', auth.uid()));
end $$;
revoke execute on function public.enqueue_email(text) from public, anon;
grant execute on function public.enqueue_email(text) to authenticated;
await base.rpc("enqueue_email", { p_to: "a@example.com" })

You can also inspect and operate queues without SQL, from the dashboard page Database → Queues or the Management API. Listing queues and reading metrics need the developer role (database:read); creating or dropping a queue and every message operation need the admin role (database:write), because message payloads may hold user data. The pgmq extension must be enabled first (409 extension_not_enabled otherwise).

Method and path (under /v1/projects/:ref) What it does
GET /queues Queues with metrics: length, visible length, oldest and newest message age, total messages. enabled: false when pgmq is not installed.
POST /queues Create { name }.
DELETE /queues/:queue Drop a queue with its messages and archive.
GET /queues/:queue/metrics Metrics of one queue.
GET /queues/:queue/messages Peek messages without changing visibility (?limit= up to 200, ?archived=true for the archive).
POST /queues/:queue/messages Send { message, delay_seconds? } (message up to 256 KiB as JSON).
POST /queues/:queue/messages/read Read { vt_seconds?, qty? } (up to 100 messages).
POST /queues/:queue/messages/:id/archive Archive a message.
DELETE /queues/:queue/messages/:id Delete a message.
POST /queues/:queue/purge Delete every message; the archive is kept.

A queue name starts with a letter or _, uses letters, digits and _, and is at most 47 characters. A project can have up to 100 queues (422 queue_limit_exceeded); a duplicate name returns 409 queue_exists.

Drain the queue from a scheduled job (SQL), or from an Edge Function that holds the secret key and calls pgmq.read through your own service_role RPC.