Database triggers execute automatically inside PostgreSQL in response to DML operations. Combining triggers with the pg_net extension allows PostgreSQL to dispatch asynchronous HTTP webhooks directly to external services or Supabase Edge Functions without blocking transactions.
Automated Profile Creation on User Signup
The most common architectural pattern in Supabase is automatically generating a public.profiles row the moment a user signs up through auth.users:
-- 1. Create the trigger function
CREATE OR REPLACE FUNCTION public.handle_new_user()
RETURNS TRIGGER
LANGUAGE plpgsql
SECURITY DEFINER
SET search_path = public AS $$
BEGIN
INSERT INTO public.profiles (id, username, display_name, avatar_url)
VALUES (
NEW.id,
-- Extract username or fall back to email prefix
COALESCE(
NEW.raw_user_meta_data->>'username',
SPLIT_PART(NEW.email, '@', 1) || '_' || SUBSTR(NEW.id::text, 1, 5)
),
NEW.raw_user_meta_data->>'full_name',
NEW.raw_user_meta_data->>'avatar_url'
);
RETURN NEW;
END;
$$;
-- 2. Bind the trigger to auth.users
CREATE OR REPLACE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW EXECUTE FUNCTION public.handle_new_user();
Outbound HTTP Webhooks with pg_net
The pg_net extension provides non-blocking asynchronous HTTP client capabilities inside SQL:
-- Enable the extension
CREATE EXTENSION IF NOT EXISTS pg_net;
-- Trigger that calls an external Stripe or Resend Edge Function on new orders
CREATE OR REPLACE FUNCTION public.notify_order_creation()
RETURNS TRIGGER
LANGUAGE plpgsql
SECURITY DEFINER AS $$
DECLARE
v_request_id BIGINT;
v_payload JSONB;
BEGIN
v_payload := jsonb_build_object(
'order_id', NEW.id,
'customer_id', NEW.customer_id,
'amount', NEW.total_amount,
'created_at', NEW.created_at
);
-- Dispatches an asynchronous HTTP POST request!
-- Does NOT block the transaction commit!
SELECT net.http_post(
url := 'https://xyz.supabase.co/functions/v1/process-order',
headers := jsonb_build_object(
'Content-Type', 'application/json',
'Authorization', 'Bearer ' || current_setting('app.service_role_key', true)
),
body := v_payload
) INTO v_request_id;
RETURN NEW;
END;
$$;
CREATE TRIGGER on_order_created
AFTER INSERT ON public.customer_orders
FOR EACH ROW EXECUTE FUNCTION public.notify_order_creation();
Immutable Audit Logging Triggers
CREATE TABLE account_audit_log (
log_id BIGSERIAL PRIMARY KEY,
account_id UUID NOT NULL,
action TEXT NOT NULL,
old_data JSONB,
new_data JSONB,
performed_by UUID,
timestamp TIMESTAMPTZ DEFAULT NOW() NOT NULL
);
CREATE OR REPLACE FUNCTION audit_account_changes()
RETURNS TRIGGER LANGUAGE plpgsql SECURITY DEFINER AS $$
BEGIN
INSERT INTO account_audit_log (
account_id, action, old_data, new_data, performed_by
) VALUES (
COALESCE(NEW.id, OLD.id),
TG_OP,
CASE WHEN TG_OP IN ('UPDATE', 'DELETE') THEN to_jsonb(OLD) ELSE NULL END,
CASE WHEN TG_OP IN ('INSERT', 'UPDATE') THEN to_jsonb(NEW) ELSE NULL END,
(SELECT auth.uid())
);
RETURN COALESCE(NEW, OLD);
END;
$$;
Exercises
- Create a trigger that validates an order's inventory count before allowing an
INSERTintoorder_items. - Write a
pg_nettrigger that alerts a Discord or Slack webhook when a high-value transaction occurs. - Explain why asynchronous HTTP calls via
pg_netare preferred over synchronous database network extensions.