pg_cronによる定期ジョブのスケジュール実行
pg_cronを使ってSupabase内の定期的な処理を自動化し、Edge Functionの定期呼び出し、クリーンアップジョブ、データ集計タスクを信頼できるスケジュールで実行します。
「pg_cronによる定期ジョブのスケジュール実行」はCoddyKit上の無料Supabase Backend as a Serviceレッスンです。 これはレッスン3/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSupabase Backend as a Service学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Supabase Backend as a Serviceコースには全3レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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 minutes0 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.
statusshows success or failurereturn_messagecarries 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_cronand use 5-field cron syntax - Schedule with
cron.schedule, remove withcron.unschedule - Trigger Edge Functions via
net.http_post - Audit runs in
cron.job_run_details - Account for UTC timezone and overlapping runs
AI チューターと学ぶ Supabase Backend as a Service — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 11
- レッスン
- 40
よくある質問
「pg_cronによる定期ジョブのスケジュール実行」レッスンは無料ですか?
はい。「pg_cronによる定期ジョブのスケジュール実行」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Supabase Backend as a Serviceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Supabase Backend as a Serviceコースには全3レッスンが含まれています。
「pg_cronによる定期ジョブのスケジュール実行」で何を学びますか?
pg_cronを使ってSupabase内の定期的な処理を自動化し、Edge Functionの定期呼び出し、クリーンアップジョブ、データ集計タスクを信頼できるスケジュールで実行します。 ブラウザで直接実行するハンズオンコードでSupabase Backend as a Serviceを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Supabase Backend as a Serviceを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSupabase Backend as a Serviceは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/3です。
「pg_cronによる定期ジョブのスケジュール実行」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSupabase Backend as a Serviceレッスンでコードを書いて実行できますか?
はい。すべてのSupabase Backend as a Serviceレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 外部サービスとの統合
- Supabaseとワーカーによるタスクキュー
- pg_cronによる定期ジョブのスケジュール実行