pg-cron-with-pg-net-edge
Skill10 starsChanged 6 months agoMITView on GitHub
Use when pg_cron needs to call an HTTPS endpoint — typically a Supabase Edge Function — for work too heavy or external for plain SQL. Covers pg_net setup, authentication, payload signing, and reading async responses.
- Reads credentials
In this repository56 other files it gives its agents.
- accessibility-passSkill
- analytics-eventsSkill
- api-error-handlingSkill
- audit-logging-backendSkill
- background-jobs-and-cronSkill
- dark-mode-and-themingSkill
- dashboard-charts-rechartsSkill
- edge-functions-and-webhooksSkill
- email-and-notificationsSkill
- error-states-and-empty-uiSkill
- feature-flags-and-togglesSkill
- file-upload-storageSkill
- i18n-multi-languageSkill
- lovable-prompting-playbookSkill
- lovable-ship-checklistSkill
- migration-playbookSkill
- multi-tenant-backendSkill
- onboarding-copySkill
- optimistic-updatesSkill
- payment-failed-and-recoverySkill
- payment-webhook-idempotencySkill
- payment-webhook-testingSkill
- performance-budgetSkill
- pg-cron-recipesSkill
- pg-cron-scheduled-jobsSkill
- postgres-full-text-searchSkill
- postgres-triggers-and-functionsSkill
- pricing-and-billingSkill
- programmatic-seo-content-qualitySkill
- programmatic-seo-pagesSkill
- rate-limiting-edgeSkill
- realtime-and-subscriptionsSkill
- refactor-safe-diffSkill
- release-notesSkill
- seo-core-web-vitalsSkill
- seo-landing-pageSkill
- seo-meta-tags-spaSkill
- seo-sitemap-generationSkill
- seo-ssr-and-prerenderingSkill
- seo-structured-dataSkill
- server-input-validationSkill
- shadcn-patternsSkill
- soft-delete-and-archivingSkill
- stripe-one-time-payment-webhooksSkill
- stripe-payment-webhooksSkill
- supabase-auth-flowsSkill
- supabase-database-designSkill
- supabase-rls-and-authSkill
- support-replySkill
- tanstack-infinite-queriesSkill
- tanstack-mutations-and-invalidationSkill
- tanstack-prefetch-and-hydrationSkill
- tanstack-query-alternativeSkill
- tanstack-router-data-loadersSkill
- tanstack-start-server-routesSkill
- typed-api-hooks-formsSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 1 is distinctive, and 1 is very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 142
Use sparingly — polling blocks the connection.
- Very specific: line 189
Not cleaning up net._http_response (it grows forever).
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Calling edge functions from pg_cron via pg_net
pg_cron only runs SQL. When you need to send email, hit a third-party API, or run code that doesn't belong in the database, schedule a SQL job that calls an HTTPS endpoint using the pg_net extension. The standard target is a Supabase Edge Function.
Companion skills: pg-cron-scheduled-jobs, pg-cron-recipes, edge-functions-and-webhooks, server-input-validation, rate-limiting-edge.
Enable pg_net
create extension if not exists pg_net;
-- Supabase: also enable in Dashboard → Database → Extensionspg_net exposes net.http_post, net.http_get, etc. Requests are asynchronous — the SQL function returns a request_id immediately and the worker processes the call out-of-band. Responses land in net._http_response.
Minimal pattern
select net.http_post(
url := 'https://YOUR-PROJECT.functions.supabase.co/send-digest',
headers := jsonb_build_object(
'Content-Type', 'application/json',
'Authorization', 'Bearer ' || current_setting('app.cron_secret', true)
),
body := jsonb_build_object('window', 'daily')
) as request_id;Set the secret once per database:
-- Run as superuser / via Supabase SQL editor
alter database postgres set app.cron_secret = 'long-random-string-shared-with-the-function';Restart the connection (or use set local) for the new GUC to take effect.
Edge function side: verify the shared secret
// supabase/functions/send-digest/index.ts
import { createClient } from "npm:@supabase/supabase-js@2";
Deno.serve(async (req) => {
const auth = req.headers.get("authorization") ?? "";
if (auth !== `Bearer ${Deno.env.get("CRON_SECRET")}`) {
return new Response("unauthorized", { status: 401 });
}
const { window } = await req.json();
const supabase = createClient(
Deno.env.get("SUPABASE_URL")!,
Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!,
);
// ... do the heavy work
return Response.json({ ok: true });
});Set CRON_SECRET in Supabase function secrets to the same value as app.cron_secret. Without this check, anyone can hit the public URL.
Wrap in a function + schedule
create or replace function public.invoke_send_digest(window text)
returns bigint
language plpgsql
security definer
as $$
declare
req_id bigint;
begin
select net.http_post(
url := 'https://YOUR-PROJECT.functions.supabase.co/send-digest',
headers := jsonb_build_object(
'Content-Type', 'application/json',
'Authorization', 'Bearer ' || current_setting('app.cron_secret', true)
),
body := jsonb_build_object('window', window),
timeout_milliseconds := 10000
) into req_id;
return req_id;
end;
$$;
revoke all on function public.invoke_send_digest(text) from public, anon, authenticated;
select cron.schedule('send-daily-digest', '0 14 * * *', -- 14:00 UTC daily
$$ select public.invoke_send_digest('daily') $$);Reading responses
-- Last 20 responses
select id, status_code, content, created
from net._http_response
order by created desc
limit 20;
-- Failures only
select r.id, r.status_code, r.content, r.created
from net._http_response r
where r.status_code is null or r.status_code >= 400
order by r.created desc;net._http_response grows unboundedly. Schedule a daily cleanup (pg-cron-recipes has one).
Polling for completion (when you must)
In most flows, fire-and-forget is enough. If you need to react to the response inside SQL, poll:
create or replace function public.wait_for_response(req_id bigint, max_ms int default 30000)
returns net._http_response
language plpgsql
as $$
declare
deadline timestamptz := clock_timestamp() + (max_ms || ' milliseconds')::interval;
r net._http_response;
begin
loop
select * into r from net._http_response where id = req_id;
exit when r.id is not null;
exit when clock_timestamp() > deadline;
perform pg_sleep(0.25);
end loop;
return r;
end;
$$;Use sparingly — polling blocks the connection.
Payload signing (defense in depth)
For sensitive payloads, sign with HMAC and verify in the function:
create or replace function public.invoke_signed(url text, payload jsonb)
returns bigint
language plpgsql
security definer
as $$
declare
body_text text := payload::text;
signature text := encode(
hmac(body_text, current_setting('app.cron_secret', true), 'sha256'),
'hex'
);
begin
return net.http_post(
url := url,
headers := jsonb_build_object(
'Content-Type', 'application/json',
'X-Signature', signature
),
body := payload
);
end;
$$;In the function, recompute HMAC over the raw body and timingSafeEqual compare.
Common targets
| Scenario | Target endpoint |
| Daily digest emails | send-digest edge function |
| Stripe sync | sync-stripe-subscriptions |
| Webhook re-delivery | replay-failed-webhooks |
| AI batch summarization | summarize-pending |
| Recompute aggregates from event store | rebuild-stats |
Avoid
- Hardcoding the bearer secret in SQL — use a database GUC (
alter database ... set). - Calling slow endpoints synchronously from page-blocking SQL.
- Not cleaning up
net._http_response(it grows forever). - Ignoring
status_code— set up an alert on > 0 failed responses in the last hour. - Sending PII in unsigned, unencrypted bodies to non-Supabase hosts.
- Calling the same edge function from cron and from clients without a way to distinguish (add a header like
X-Source: cron).
Checklist
pg_netextension enabled.- Shared secret stored as a database GUC, not in code.
- Edge function verifies
Authorization: Bearer ...againstCRON_SECRET. - Cron job calls a wrapper SQL function, not
net.http_postinline. net._http_responsecleanup job scheduled.- Failure alert (manual or via monitoring) on
status_code >= 400or null.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

