# Scheduled jobs: pg_cron schedules, Edge Functions execute

pg_cron is a scheduler, not an application runtime. SQL job bodies are fine for `delete from` cleanup; anything involving APIs, secrets, or branching logic belongs in an Edge Function that cron invokes over HTTP.

## Checkable procedure

1. Write the Edge Function first, with its own auth: it should accept only calls bearing a job secret header, since pg_cron calls it over HTTP like any client.
2. Deploy the function and store its URL and the job secret in the vault or function secrets, not in the cron job SQL string.
3. Schedule with `cron.schedule('job-name', 'cron-expr', $$ select net.http_post(...) $$)` using pg_net to POST to the function URL with the secret header. Name the job for later cleanup.
4. Keep the cron body to the single HTTP call. Retries, backoff, and business logic live in the function where you can log and alert properly.
5. Monitor both sides: `cron.job_run_details` for the schedule firing, and the function logs for the execution result. A 200 from the schedule with a 500 inside the function is the silent failure mode.

## Ordering constraints

Function deployed and tested manually before the cron row exists. Secret rotated in one place (function secrets) rather than in every cron body.

## Verification

Invoke the function manually with the secret and confirm the effect. Then let one scheduled run fire and confirm exactly one execution with the expected result in the function logs.