0Pricing
Supabase Backend as a Service · درس

جدولة المهام المتكررة باستخدام pg_cron

أتمت الأعمال المتكررة داخل Supabase باستخدام pg_cron لجدولة استدعاءات Edge Function الدورية، ومهام التنظيف، ومهام تجميع البيانات وفق جدول زمني موثوق.

جدولة المهام المتكررة باستخدام pg_cron درس مجاني في Supabase Backend as a Service على CoddyKit. هذا هو الدرس 3 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 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

الأسئلة الشائعة

هل درس «جدولة المهام المتكررة باستخدام pg_cron» مجاني؟

نعم — نص درس «جدولة المهام المتكررة باستخدام pg_cron» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Supabase Backend as a Service، انتقل إلى CoddyKit PRO. تتضمن دورة Supabase Backend as a Service 3 دروس في المجموع.

ماذا ستتعلم في «جدولة المهام المتكررة باستخدام pg_cron»؟

أتمت الأعمال المتكررة داخل Supabase باستخدام pg_cron لجدولة استدعاءات Edge Function الدورية، ومهام التنظيف، ومهام تجميع البيانات وفق جدول زمني موثوق. تتمرن على Supabase Backend as a Service مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Supabase Backend as a Service؟

لا تُشترط خبرة سابقة. Supabase Backend as a Service على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 3.

كم من الوقت يستغرق درس «جدولة المهام المتكررة باستخدام pg_cron»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Supabase Backend as a Service هذا؟

نعم. كل درس في Supabase Backend as a Service يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. التكامل مع الخدمات الخارجية
  2. قوائم مهام باستخدام Supabase وWorkers
  3. جدولة المهام المتكررة باستخدام pg_cron
← العودة إلى Supabase Backend as a Service