All guidesDeep-dive articles

PostgreSQL function grants

Who can call a Supabase SECURITY DEFINER function?

By Ohad Krispin, founder of VibeRaven · LinkedIn

Short answer

Check effective EXECUTE on the exact function signature with has_function_privilege, and check schema USAGE separately. Before you revoke anything, find the policies and application paths that call the function. A private RLS helper may still need an authenticated caller.

On this page

Audit a function before changing its grants

  1. 1
    Run this read-only query in your local Supabase database. Replace public.can_read_invoice(uuid) with the exact schema, name and argument types from the migration. A missing signature produces zero rows. A function with overloads needs a check for each relevant signature.
    SQL
    select p.oid::regprocedure as signature,
           p.prosecdef as security_definer,
           pg_get_userbyid(p.proowner) as owner,
           r.rolname as role,
           has_schema_privilege(r.oid, p.pronamespace, 'USAGE') as schema_usage,
           has_function_privilege(r.oid, p.oid, 'EXECUTE') as can_execute
    from pg_proc as p
    cross join pg_roles as r
    where p.oid = to_regprocedure('public.can_read_invoice(uuid)')
      and r.rolname in ('anon', 'authenticated', 'service_role')
    order by r.rolname;
  2. 2
    Search the migrations, application code and tests for references to the function name. Map each match to its actual runtime role. Include policies, triggers, jobs, wrappers and renamed or indirect RPC calls.
    Terminal
    git grep -n -F 'can_read_invoice'
  3. 3
    Compare that list with dependencies PostgreSQL recorded. The catalog includes stored dependencies such as policy expressions. Application calls, dynamic SQL and calls inside a string-defined function body require separate review.
    SQL
    select pg_describe_object(d.classid, d.objid, d.objsubid) as dependent
    from pg_depend as d
    where d.refclassid = 'pg_proc'::regclass
      and d.refobjid = to_regprocedure('public.can_read_invoice(uuid)')
    order by dependent;
  4. 4
    Decide which roles must execute it and whether it belongs in a Data API exposed schema. For a policy helper, a non-exposed schema can keep it off direct RPC while the policy still calls it. In a local migration, remove unintended direct grants and any unintended grant through PUBLIC or an inherited role. Re-query the effective privileges, then run the actual policy query or application flow with a known allowed test account before rolling out.

Official docs behind this checklist

FAQ for builders before launch

Why is EXECUTE still true after I revoke it from anon?

An effective grant can come from PUBLIC or a role that anon inherits. Inspect those grant paths for the exact overload before changing the migration.

Does authenticated EXECUTE limit a function to the caller's own rows?

EXECUTE controls who can invoke the function. SECURITY DEFINER runs with the owner's privileges. A function that handles tenant or user data still needs a check inside the function for the caller's allowed scope.