Most Supabase RLS setups have a helper like is_admin(). It's a SECURITY DEFINER function that policies call to avoid infinite recursion. Mine lived in the public schema, next to my tables.
Everything in public is exposed through the Data API. So any signed-in user could call it directly:
POST /rest/v1/rpc/is_admin → 200 true / false
That's not a leak. The function only answers about the caller. But it's an endpoint nobody needs, and a reader on my post about the recursion error suggested moving it to a schema the API doesn't expose. I replied that authenticated would then need USAGE on that schema. I tested it, and I was wrong.
Step 1: find out who uses the helpers
Before moving anything, I listed everything that depends on the three helpers (is_admin(), is_org_member(uuid), is_org_owner(uuid)):
- 16 policies (14 in
public, 2 onstorage.objects), all forauthenticated - 0 views, and no function body calls them (I searched every migration in the repo)
- 0 calls from the app code. It only calls two other RPCs.
-
is_org_ownerwas used by nothing at all. It was only reachable as an RPC.
If your app calls a helper with supabase.rpc(...), moving it will break that call. Check first.
Don't rely on pg_depend alone for functions. It records policies and BEGIN ATOMIC bodies, but not calls inside plpgsql or plain SQL function bodies (thanks to Sunnatulla in the comments for pointing this out). Search the bodies too:
select n.nspname, p.proname
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where p.prosrc ilike '%is_admin%'
or p.prosrc ilike '%is_org_%';
Step 2: move them
ALTER FUNCTION ... SET SCHEMA keeps the function's OID. Policies point to functions by OID, so the 16 policies didn't need to be recreated. Their text just changes from is_admin() to private.is_admin().
create schema if not exists private;
-- no role gets USAGE (or CREATE) on the schema
revoke all on schema private from public, anon, authenticated, service_role;
alter function public.is_admin() set schema private;
alter function public.is_org_member(uuid) set schema private;
alter function public.is_org_owner(uuid) set schema private;
-- EXECUTE only for authenticated (the policies need it)
revoke execute on function private.is_admin() from public, anon, service_role;
revoke execute on function private.is_org_member(uuid) from public, anon, service_role;
revoke execute on function private.is_org_owner(uuid) from public, anon, service_role;
grant execute on function private.is_admin() to authenticated;
grant execute on function private.is_org_member(uuid) to authenticated;
grant execute on function private.is_org_owner(uuid) to authenticated;
private must stay out of the Data API's exposed schemas. In my hosted project, that setting is under Integrations → Data API → Settings.
The surprise: no USAGE needed
I tested three states inside transactions that always rolled back, signed in as an admin, two users in company A and a user in company B. I counted rows in 8 tables and storage.objects:
| Schema USAGE | Function EXECUTE | Row counts | Calling private.is_admin() by name |
|---|---|---|---|
| granted | granted | same as before | works |
| none | granted | same as before | 42501 permission denied for schema private |
| none | none | every read fails | 42501 permission denied for function is_admin |
The policies kept working without USAGE. They already hold the function's OID, and schema USAGE is checked when a name is looked up. EXECUTE is still checked, so it has to stay.
Leaving USAGE off is a small extra layer: nobody can call the helpers by name from SQL either.
What the API returns now
-
POST /rest/v1/rpc/is_admin→404,PGRST202(the function isn't in the schema cache) - The same call with
Content-Profile: private→406,PGRST106("Invalid schema: private") -
select public.is_admin()→42883, the function no longer exists inpublic
Side check: does FORCE ROW LEVEL SECURITY break the helper?
I wondered whether FORCE ROW LEVEL SECURITY on profiles would make is_admin() return false, since the function's owner would then be subject to RLS. It didn't. In both my local and hosted projects, postgres isn't a superuser but has BYPASSRLS, and that wins over FORCE. The admin still got true, and row counts didn't change. So I didn't add it.
Things that bit me (or almost did)
-
Old scripts. My earlier verify and rollback SQL called
public.is_admin()by name. After the move they fail with42883. -
Default privileges. I had closed default privileges for
publiconly (previous post). A new function inprivatestill getsEXECUTEforPUBLICby default, so the migration revokes it per function. -
Local vs hosted again. Locally, the helpers also had
EXECUTEforservice_role. In production they didn't. The end state is the same, but the rollback has two versions. -
Functions that call the helpers. A SECURITY INVOKER function that calls
private.is_admin()by name fails with42501, because its body looks the name up as the caller, who has noUSAGEonprivate. Call the helpers only from policies or SECURITY DEFINER functions, or grantUSAGEto that role. A reader pointed this out; my project has no such function.
How I checked it
- Locally: the verify SQL ran before and after, with a rollback and a re-apply in between.
- My access-check script grew from 482 to 510 checks, and all pass on every pull request. Rolling the migration back makes 47 of them fail, so they really test it.
- In production: a read-only query first, then the migration in the SQL Editor. Six fingerprints (table, column and other function grants, policies, RLS flags) were the same before and after. A probe that always rolls back confirmed the
42501and42883results, and that signed-in reads still work.
Quick checklist
- List what uses your helpers: policies, views, function bodies (search
pg_proc.prosrc, sincepg_dependmisses plpgsql) andrpc()calls in your app. - Move them with
ALTER FUNCTION ... SET SCHEMA private. Don't recreate the policies. - Give no role
USAGEonprivate. KeepEXECUTEfor the role your policies run as. - Make sure
privateisn't in the Data API's exposed schemas. - Check that
/rpc/your_helpernow returns 404, and that signed-in users still see the same rows.
The pull request with the migration, verify and rollback SQL: https://github.com/kuros-works/client-portal-demo/pull/25
Want someone to check things like this in your Supabase project? I review RLS policies and privileges as a fixed-price job on Upwork: https://www.upwork.com/services/product/development-it-a-supabase-rls-security-review-for-your-lovable-bolt-or-cursor-app-2126067179511646761
Top comments (3)
pg_depend only records policies and BEGIN ATOMIC bodies, so step 1 can miss callers. On PG17 a plpgsql and a plain sql function calling public.is_admin() weren't listed. After the move both fail with 42883, and switching them to private.is_admin() gives 42501, because invoker bodies look the name up as the caller.
Good catch, thank you. You're right: pg_depend only records policies and BEGIN ATOMIC bodies, so a plpgsql or plain SQL function calling public.is_admin() wouldn't show up in step 1. I just searched every migration in the repo, and all calls to the three helpers are inside policies, so nothing was missed this time. But the checklist should cover it. I'll add a search of function bodies (pg_proc.prosrc) to step 1, and your second point: a SECURITY INVOKER function that calls private.is_admin() by name needs USAGE on private for the caller.
Updated the post: step 1 now searches function bodies (pg_proc.prosrc), the checklist covers it, and I added your point about SECURITY INVOKER functions needing USAGE. Thanks again.