0
Challenge · 74Services

Jobs: Cron, Queues, webhooks & pg_net

Functionsused in ~55% of apps

DevLog's weekly digest has to run at 9am Monday whether or not anyone has a browser tab open. The tempting shortcut is a trigger: "on insert, just call the email API right there." That single line is one of the most common ways a Supabase project goes down — because a synchronous HTTP call inside a trigger holds the transaction, and every lock it took, open for as long as the remote endpoint takes to answer. This concept is about the four correct tools, and the one fact that explains why three of them exist.

Core idea. pg_net makes HTTP calls from SQL asynchronously — it returns a request id immediately and performs the call on a background worker, so a trigger that uses it commits instantly instead of blocking on the network. Cron (pg_cron) schedules SQL, including a pg_net call, on a timer. Database Webhooks are a Dashboard-generated trigger that calls pg_net for you — convenient, but still fire-and-forget, no retries. Queues (pgmq) are the one primitive with actual delivery guarantees, for the jobs that must not silently vanish.
Playground, this isn't a screenshot
1 / 6
INSERT INTO comments

A new comment is inserted. An AFTER INSERT trigger fires — still inside session A's open transaction.

Same trigger, same insert. The only difference is whether the network call happens inside the transaction (synchronous — locks wait on the internet) or outside it (pg_net — the transaction never knows the call is still in flight).

↑ play a little first, then dig in