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.
Scheduled jobs (pg_cron)
Section titled “Scheduled jobs (pg_cron)”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.
# 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, activecurl -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, oncecurl -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-holdsname: 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 as30 seconds.command: SQL, up to 8000 characters. Put real logic in a function and call it, as above.SET ROLE,RESET ROLEandSET SESSION AUTHORIZATIONare refused (400 invalid_cron_job): the job already runs asservice_role.PATCHwith{"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 inruns. runsreturns the last 50 runs by default (up to 500).limitsreturns the effectivemax_jobs,min_interval_secondsand the currentjob_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.
Queues (pgmq)
Section titled “Queues (pgmq)”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 idselect * from pgmq.read('emails', 30, 5); -- hide up to 5 messages for 30 s while you workselect pgmq.archive('emails', 1); -- done; use pgmq.delete to drop insteadA 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.
Expose a queue to your users
Section titled “Expose a queue to your users”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 bigintlanguage 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" })Queues API and dashboard
Section titled “Queues API and dashboard”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.
Draining a queue
Section titled “Draining a queue”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.