Cron

Quickstart


Schedule a Job

  1. Go to the Jobs section to schedule your first Job.
  2. Click on Create job button or navigate to the new Cron Job form here.
  3. Name your Cron Job.
  4. Choose a schedule for your Job by inputting cron syntax (refer to the syntax chart in the form) or natural language.
  5. Input SQL snippet or select a Database function, HTTP request, or Supabase Edge Function.

Edit a Job

  1. Go to the Jobs section and find the Job you'd like to edit.
  2. Click on the three vertical dots menu on the right side of the Job and click Edit cron job.
  3. Make your changes and then click Save cron job.

Activate/Deactivate a Job

  1. Go to the Jobs section and find the Job you'd like to unschedule.
  2. Toggle the Active/Inactive switch next to Job name.

Unschedule a Job

  1. Go to the Jobs section and find the Job you'd like to delete.
  2. Click on the three vertical dots menu on the right side of the Job and click Delete cron job.
  3. Confirm deletion by entering the Job name.

Inspecting Job Runs

  1. Go to the Jobs section and find the Job you want to see the runs of.
  2. Click on the History button next to the Job name.

Examples

Delete data every week

Delete old data every Saturday at 3:30AM (GMT):


_10
select cron.schedule (
_10
'saturday-cleanup', -- name of the cron job
_10
'30 3 * * 6', -- Saturday at 3:30AM (GMT)
_10
$$ delete from events where event_time < now() - interval '1 week' $$
_10
);

Run a vacuum every day

Vacuum every day at 3:00AM (GMT):


_10
select cron.schedule('nightly-vacuum', '0 3 * * *', 'VACUUM');

Call a database function every 5 minutes

Create a hello_world() database function and then call it every 5 minutes:


_10
select cron.schedule('call-db-function', '*/5 * * * *', 'SELECT hello_world()');

Call a database stored procedure

To use a stored procedure, you can call it like this:


_10
select cron.schedule('call-db-procedure', '*/5 * * * *', 'CALL my_procedure()');

Invoke Supabase Edge Function every 30 seconds

Make a POST request to a Supabase Edge Function every 30 seconds:


_14
select
_14
cron.schedule(
_14
'invoke-function-every-half-minute',
_14
'30 seconds',
_14
$$
_14
select
_14
net.http_post(
_14
url:='https://project-ref.supabase.co/functions/v1/function-name',
_14
headers:=jsonb_build_object('Content-Type','application/json', 'Authorization', 'Bearer ' || 'YOUR_ANON_KEY'),
_14
body:=jsonb_build_object('time', now() ),
_14
timeout_milliseconds:=5000
_14
) as request_id;
_14
$$
_14
);

Caution: Scheduling System Maintenance

Be extremely careful when setting up Jobs for system maintenance tasks as they can have unintended consequences.

For instance, scheduling a command to terminate idle connections with pg_terminate_backend(pid) can disrupt critical background processes like nightly backups. Often, there is an existing Postgres setting, such as idle_session_timeout, that can perform these common maintenance tasks without the risk.

Reach out to Supabase Support if you're unsure if that applies to your use case.