pg_cron으로 반복 작업 예약하기
pg_cron을 사용하여 Supabase 내부에서 주기적인 Edge Function 호출, 정리 작업, 데이터 집계 작업을 안정적인 일정에 따라 자동화합니다.
pg_cron으로 반복 작업 예약하기은(는) CoddyKit의 무료 Supabase Backend as a Service 강의입니다. 이것은 3개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Supabase Backend as a Service 강의 전체를 잠금 해제할 수 있습니다. Supabase Backend as a Service 강의에는 총 3개의 강의가 포함되어 있습니다.
“pg_cron으로 반복 작업 예약하기”에서 뭘 배우나요?
pg_cron을 사용하여 Supabase 내부에서 주기적인 Edge Function 호출, 정리 작업, 데이터 집계 작업을 안정적인 일정에 따라 자동화합니다. 브라우저에서 직접 실행하는 실습 코드로 Supabase Backend as a Service을(를) 배우며, 24/7 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 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 외부 서비스와 통합하기
- 수파베이스와 워커를 활용한 작업 큐
- pg_cron으로 반복 작업 예약하기