0Pricing
Supabase Backend as a Service · Lesson

Scheduling Recurring Jobs with pg_cron

Automate recurring work inside Supabase using pg_cron to schedule periodic Edge Function calls, cleanup jobs, and data aggregation tasks on a reliable timeline.

Scheduling Recurring Jobs with pg_cron is a free Supabase Backend as a Service lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Supabase Backend as a Service learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Scheduled Jobs Matter

Complex workflows often need work to happen on a schedule, not just in response to a request. Think nightly cleanups, hourly aggregations, or weekly digest emails.

Supabase ships with the pg_cron extension, letting you run SQL on a recurring timetable directly inside Postgres.

  • No external scheduler to maintain
  • Runs close to your data
  • Can trigger Edge Functions via HTTP

Enabling pg_cron

Enable the extension once per project. In the Supabase dashboard go to Database > Extensions and toggle pg_cron, or run SQL.

It creates a cron.job table that tracks every scheduled task.

create extension if not exists pg_cron;

Anatomy of a Cron Schedule

pg_cron uses standard 5-field cron syntax: minute, hour, day-of-month, month, day-of-week.

  • 0 3 * * * = every day at 03:00
  • */15 * * * * = every 15 minutes
  • 0 0 * * 0 = every Sunday at midnight

Scheduling Your First Job

Use cron.schedule(name, schedule, sql) to register a job. Here we delete expired sessions every night at 2 AM.

select cron.schedule(
  'nightly-session-cleanup',
  '0 2 * * *',
  $$ delete from sessions where expires_at < now() $$
);

Inspecting Scheduled Jobs

Every job lives in cron.job. Query it to confirm your schedule and find the jobid you will need for updates or removal.

select jobid, schedule, jobname, active from cron.job;

Reviewing Run History

pg_cron records each execution in cron.job_run_details. This is your first stop for debugging a job that did not do what you expected.

  • status shows success or failure
  • return_message carries any error text
select jobid, status, return_message, start_time
from cron.job_run_details
order by start_time desc
limit 10;

Triggering an Edge Function from Cron

For real workflow logic, call an Edge Function via HTTP using the pg_net extension. The cron job fires the request; the function does the heavy lifting.

select cron.schedule(
  'hourly-digest',
  '0 * * * *',
  $$ select net.http_post(
       url := 'https://your-project.functions.supabase.co/digest',
       headers := '{"Authorization": "Bearer SERVICE_ROLE_KEY"}'::jsonb
     ) $$
);

Updating a Job

To change a schedule, simply call cron.schedule again with the same job name. pg_cron replaces the existing definition rather than creating a duplicate.

select cron.schedule(
  'nightly-session-cleanup',
  '0 4 * * *',
  $$ delete from sessions where expires_at < now() $$
);

Unscheduling a Job

Remove a job by name with cron.unschedule. Always clean up jobs you no longer need so run history stays meaningful.

select cron.unschedule('nightly-session-cleanup');

Idempotency and Overlap

Scheduled jobs can overlap if one run takes longer than its interval. Design jobs to be idempotent and guard against concurrent runs.

  • Use advisory locks for long jobs
  • Process in bounded batches
  • Mark rows as processed before acting

Timezone Awareness

pg_cron schedules run in the database server timezone, typically UTC. A job set for 0 0 * * * fires at UTC midnight, not your local midnight.

Compute local-time offsets explicitly so reports land when users expect them.

Quick Check

Test your understanding of pg_cron scheduling.

Recap

You can now automate recurring workflow steps inside Supabase.

  • Enable pg_cron and use 5-field cron syntax
  • Schedule with cron.schedule, remove with cron.unschedule
  • Trigger Edge Functions via net.http_post
  • Audit runs in cron.job_run_details
  • Account for UTC timezone and overlapping runs

Frequently asked questions

Is the “Scheduling Recurring Jobs with pg_cron” lesson free?

Yes — the full text of “Scheduling Recurring Jobs with pg_cron” is free to read here on the web, and the Supabase Backend as a Service course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Supabase Backend as a Service course, upgrade to CoddyKit PRO.

What will I learn in “Scheduling Recurring Jobs with pg_cron”?

Automate recurring work inside Supabase using pg_cron to schedule periodic Edge Function calls, cleanup jobs, and data aggregation tasks on a reliable timeline. You practise Supabase Backend as a Service with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Supabase Backend as a Service?

No prior experience is required. Supabase Backend as a Service on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Scheduling Recurring Jobs with pg_cron” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Supabase Backend as a Service lesson?

Yes. Every Supabase Backend as a Service lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Integrating with External Services
  2. Task Queues with Supabase & Workers
  3. Scheduling Recurring Jobs with pg_cron
← Back to Supabase Backend as a Service