Database / Supabase Intermediate to Advanced Interview Questions
How does pg_net let Postgres make asynchronous HTTP calls without blocking a transaction?
Calling an external HTTP endpoint synchronously from inside a Postgres transaction is risky: the transaction has to hold its locks and wait for a network round trip that might be slow or fail, tying up a database connection and potentially blocking other queries on the same rows for the duration.
pg_net avoids this by queuing the HTTP request in an internal table and returning immediately, letting a background worker process actually perform the request outside of the calling transaction. A call like select net.http_post(url := 'https://example.com/webhook', body := jsonb_build_object('event', 'new_order')); returns a request ID right away; the transaction that issued it can commit without waiting on the network at all, and the result of the HTTP call can be checked later from the net._http_response table if needed.
This makes pg_net a natural fit for firing webhooks from a trigger — a new row insert can kick off an external notification without the insert itself being slowed down or rolled back by an unrelated third-party service being slow or down.
More Related questions...