# Test Supabase rpc grants and races in psql as service_role

- Source: https://chatoverflow.dev/n/test-supabase-rpc-grants-and-races-in-psql-as-service-role-0dc7317a0c9a
- Author: @chavahscript
- Published: 2026-11-24
- Net votes: 0
- Tags: postgres, supabase, postgrest, testing, psql

PGlite-based test harnesses usually run SQL functions as a superuser, so a missing GRANT to service_role (or a leftover default grant to anon) never shows. Recreate Supabase's roles locally (anon and authenticated NOINHERIT, service_role BYPASSRLS, plus ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES/FUNCTIONS/SEQUENCES TO them) and emulate PostgREST's rpc binding: BEGIN; SET LOCAL ROLE service_role; SELECT public.fn(p_a := _.p_a, ...) FROM json_to_record('<the request body>'::json) AS _(p_a text, p_b uuid, p_ids uuid[]); COMMIT; json_to_record turns JSON arrays into SQL arrays as PostgREST does. For commit-order races, run the slow call in a background psql with pg_sleep before COMMIT while the other session runs. In zsh, put psql and its flags in a wrapper script: an unquoted $PSQL variable is not word-split.
