Your supabase rpc works in the SQL Editor. The app calls the same function and gets a 404, a permission error, an empty response, or a “success” that changes nothing.
That is not a contradiction. The SQL Editor proves that SQL can run in a privileged context. It does not prove that the function is exposed through the Data API, that PostgREST can find the signature, that the browser role may execute it, or that row-level security allows the rows the function touches.
What the failure usually looks like
- PGRST202 / 404: PostgREST cannot find the function signature. The function may be missing, in the wrong schema, renamed, or called with mismatched argument keys.
- PGRST106 / 406: the requested schema is not exposed to the API.
- 42501 permission denied: the function was found, but the calling role lacks
EXECUTEor a table privilege inside an invoker function. - 204 but nothing changed: SQL ran, returned no content, or matched zero rows.
- Too much data comes back: a careless
security definerfunction can bypass RLS.
Save status, JSON error code, function name, argument object, project ref, user state, and UTC time before changing anything. Otherwise the AI will rewrite the backend around the wrong layer.
Why the SQL Editor gives false confidence
The SQL Editor is not your production caller. Your app normally calls the Data API as anon with a user JWT or as authenticated. Server-side code may use the service role. Those roles have different grants and different RLS outcomes. The owner of the function adds another layer: security invoker runs with the caller’s privileges; security definer runs with the creator’s privileges.
Supabase recommends preferring security invoker, which is also the Postgres default. A definer function created in the Dashboard or by a migration is commonly owned by postgres and can bypass RLS. That is useful only for a narrow controlled operation. It is dangerous when AI copied the keyword merely to make an error disappear.
supabase rpc production click path
- Reproduce the real API failure. Use the deployed app flow or the same role in a small test. Do not start in the SQL Editor.
- Confirm the function exists in the production project. Preview and production often point at different Supabase projects. Inspect the real signature:
select n.nspname as schema,
p.proname as name,
pg_get_function_identity_arguments(p.oid) as args,
p.prosecdef as is_security_definer
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where p.proname = 'your_function_name';
- Check schema exposure. If the function lives in a custom schema, the hosted API must expose that schema intentionally. Do not expose it casually: an exposed definer function becomes callable through the Data API with the creator’s privileges.
- Compare argument names exactly. In
supabase.rpc('calculate_total', { user_id: id }), object keys are parameter names.userIdanduser_idare different. Extra keys, missing non-default parameters, overloaded functions, and misspellings can cause PGRST202. - Check value types after the names match. Validate UUIDs, bigint precision, jsonb, dates, and nullability; cast deliberately in SQL.
- Only then suspect a stale schema cache. If the inspection matches exactly, confirm project and deployment first. Do not create a duplicate function.
- Verify EXECUTE for the real caller. Missing privileges can surface as
42501. Grant the minimum:
revoke execute on function public.your_function_name(uuid)
from public, anon, authenticated;
grant execute on function public.your_function_name(uuid)
to authenticated;
- Remember that EXECUTE is not row authorization. It controls who may call the function, not which rows a definer function may return.
- For security invoker, test as the caller. The caller needs table privileges, and RLS must allow the work. Updates that read columns or use
RETURNINGcan also need select privileges. - For security definer, write it like privileged backend code. Set
search_path = '', schema-qualify every object, derive identity fromauth.uid(), and never trust a browser-supplied user id:
create or replace function public.mark_invitation_accepted(invitation_id uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
begin
update public.invitations i
set accepted_at = now()
where i.id = invitation_id
and i.recipient_user_id = (select auth.uid());
return found;
end;
$$;
- Define what success returns. A void function can return no content; a zero-row update can still execute. If the UI needs proof, return data deliberately and read
error. - Verify through the caller. Query the affected row as the same user. If the write happened but RLS prevents reading it back, the app looks broken even though the database changed.
- Move the function into migrations. Put function, grants, and negative permission tests into the release flow — see supabase migrations.
- Choose RPC versus Edge Function deliberately. Database functions fit Postgres work; Edge Functions fit TypeScript, external APIs, and orchestration — compare supabase edge functions.
The typical AI-built mistakes
The AI tested as god mode. It proves the function in the SQL Editor and concludes permissions are fine. Test with the production role and with a non-owner account.
The AI adds security definer as a panic patch. That can bypass the policy that protected users. If definer is required, pin the search path, check ownership, narrow grants, and test forbidden users. The baseline is supabase rls.
The client passes identity as input. A browser-supplied user_id is attacker-controlled. For signed-in users, derive identity from the JWT with auth.uid().
Nobody checked the negative case. Can a stranger or anonymous caller execute it? Can a signed-in user enumerate ids? Test that before launch. If several production pieces moved at once, use the hub checklist in lovable deploy.
Production checklist
- API error, arguments, project ref, role, and UTC time are saved.
- The function exists in the correct project and schema.
- Schema exposure is deliberate.
- Client object keys match SQL parameter names exactly.
- UUID, bigint, date, jsonb, and nullable values are validated.
EXECUTEis revoked from broad roles and granted only where needed.- Invoker versus definer is intentional.
- Every definer function has a pinned search path, ownership checks, and negative tests.
- The client handles errors, empty data, and zero-row updates distinctly.
- Final verification uses the production caller, not the SQL Editor and not the service role.
FAQ
Why does my supabase rpc work in SQL Editor but not in my app?
The SQL Editor does not prove what the app role may do. Check the exact API error, function signature, exposed schema, EXECUTE grant, RLS, and caller role.
What does PGRST202 mean for a Supabase RPC call?
PGRST202 means PostgREST could not find that function signature in its schema cache. Compare the names in the error with the database signature, then fix the migration, exposed schema, or client argument keys.
Is security definer safe for a Supabase RPC function?
Only when constrained. Prefer invoker. If definer is necessary, pin search_path, schema-qualify objects, derive ownership from auth.uid(), and narrow EXECUTE grants.