Supabase Database Webhooks and pg_net requests that never arrive – how to debug them

By Abheesht Tripathi · Published

Symptom: rows are inserted, the Database Webhook is enabled, but the Edge Function or endpoint never runs, returns 401, or times out

How it works (and why failures are silent)

A Supabase Database Webhook is a trigger on your table that calls supabase_functions.http_request(), which uses the pg_net extension. pg_net is asynchronous: the trigger only adds the request to a queue, and a background worker sends it after your transaction commits. That design keeps inserts fast, but it means:

Step 1: look at the responses

select id, status_code, timed_out, error_msg,
       left(content::text, 200) as body, created
from net._http_response
order by created desc
limit 20;

Step 2: authenticate the call

Edge Functions check for a valid JWT by default. Either send the Authorization header from the webhook (the dashboard's Edge Function webhook type can add it), or deploy the function with --no-verify-jwt and check a shared secret yourself, so only your database can call it:

// supabase/functions/order-created/index.ts
Deno.serve(async (req) => {
  if (req.headers.get('x-webhook-secret') !== Deno.env.get('DB_WEBHOOK_SECRET')) {
    return new Response('forbidden', { status: 403 });
  }
  const payload = await req.json();   // { type, table, schema, record, old_record }
  if (payload.type === 'INSERT') {
    // do the work; make it safe to run twice for the same record.id
  }
  return new Response('ok');
});
supabase secrets set DB_WEBHOOK_SECRET=<long-random-string>
supabase functions deploy order-created --no-verify-jwt

Step 3: fix timeouts

pg_net waits only as long as the request's timeout (5 seconds by default for net.http_post; the webhook form has its own setting). Return a response quickly and do slow work afterwards, or raise the timeout. If you write the trigger yourself, you control everything, including where the secret comes from:

create or replace function public.notify_order_created()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
  perform net.http_post(
    url := 'https://<project-ref>.supabase.co/functions/v1/order-created',
    headers := jsonb_build_object(
      'Content-Type', 'application/json',
      'x-webhook-secret',
        (select decrypted_secret from vault.decrypted_secrets where name = 'db_webhook_secret')
    ),
    body := jsonb_build_object('type', 'INSERT', 'record', to_jsonb(new)),
    timeout_milliseconds := 5000
  );
  return new;
end;
$$;

create trigger order_created
  after insert on public.orders
  for each row execute function public.notify_order_created();

Step 4: is the worker sending?

select count(*) from net.http_request_queue;   -- should drain to 0 within seconds

A queue that only grows means the background worker isn't processing it. The pg_net README provides select net.worker_restart(); to reload its settings and restart it. If nothing is queued at all, check that the trigger exists and is enabled (select tgname, tgenabled from pg_trigger where tgrelid = 'public.orders'::regclass;).

Don't lose events

Because there are no automatic retries, treat the webhook as a nudge, not as the only record. A reliable pattern: also write the event to an outbox table in the same transaction, mark it done when the function finishes, and let a pg_cron job re-send rows that are still pending after a few minutes. The receiver must be idempotent, keyed on the record id.

How to verify

Insert a test row, then within a few seconds net._http_response shows a new row with status_code = 200, and the function's logs show the call. Call the function without the secret header (for example with curl): 403.

Related guides

More in Edge Functions, webhooks and APIs · all guides