Our analytics feature turns a question about a form's responses into SQL. We wanted that SQL to run as a role that can read three tables and nothing else, so that a bug in the query builder would hit a permission error instead of someone else's data.
The setup is the textbook one:
create role ai_reader_role nologin; grant select on public.submissions, public.submission_values, public.form_versions to ai_reader_role; Each query then runs in a read-only transaction that drops to that role:
begin read only; set local role ai_reader_role; set local statement_timeout = '5s'; -- the generated query rollback; On Supabase, the set local role line failed:
ERROR: permission denied to set role "ai_reader_role" (SQLSTATE 42501) Why you can't become a role you just created
Two facts combine here.
postgres isn't a superuser on Supabase. It has CREATEROLE and BYPASSRLS, but not SUPERUSER:
select rolname, rolsuper, rolcreaterole, rolbypassrls from pg_roles where rolname = 'postgres'; -- postgres | f | t | t On PostgreSQL 16 and later, creating a role doesn't give you SET on it. When a non-superuser with CREATEROLE creates a role, PostgreSQL grants the new role back to its creator with ADMIN OPTION. ADMIN lets you grant the role to others. Becoming the role is a separate option, SET, and it's only added automatically if the createrole_self_grant setting asks for it. That setting defaults to an empty string.
You can see this in pg_auth_members:
select m.rolname as member, g.rolname as grantor, am.admin_option, am.inherit_option, am.set_option from pg_auth_members am join pg_roles m on m.oid = am.member join pg_roles g on g.oid = am.grantor where am.roleid = 'ai_reader_role'::regrole; -- member | grantor | admin_option | inherit_option | set_option -- postgres | supabase_admin | t | f | f Fix 1: grant yourself SET, but not INHERIT
Because postgres holds ADMIN OPTION on the role, it can grant itself the missing option:
grant ai_reader_role to postgres with set true, inherit false; SET TRUE is what set role checks. INHERIT FALSE stops postgres from picking up the role's privileges in its own right, which it doesn't need. Afterwards, pg_auth_members has a second row for the same pair, granted by postgres, with set_option = t.
For roles you create later, the PostgreSQL docs describe createrole_self_grant (for example set createrole_self_grant = 'set' before create role) as the way to get this grant automatically. Our role already existed, so we used the explicit grant.
Error 2: permission denied for a table you never queried
With the switch working, the first real query failed in a new way:
select count(*) from public.submissions where form_id = $1; -- ERROR: permission denied for table forms (SQLSTATE 42501) The query doesn't mention forms, but the row-level security policy on submissions does:
-- submissions_workspace_select, which applies TO public exists ( select 1 from forms f where f.id = submissions.form_id and f.workspace_id in (select get_user_workspace_ids(auth.uid())) ) The PostgreSQL docs for CREATE POLICY state the rule: policy expressions "will be run with the rights of the user running the overall query", so anyone under a policy "must be able to access any tables or functions referenced in the expression". Our reader role can't read forms, by design.
Granting select on forms would make it worse. The analytics connection has no user JWT, so auth.uid() is null, the policy is false for every row, and every answer would be zero. A wrong number that looks real is worse than an error.
Fix 2: give the reader role its own policy
Our tenant isolation for analytics doesn't come from RLS. The API first loads the form through the caller's own RLS-bound client, so a form you can't see is a 404 before any SQL exists. Then the query builder pins form_id = $1 in every query it emits. The role's job is to limit which tables a bug can reach, not which rows. So the reader role gets a policy of its own:
create policy submissions_ai_reader_select on public.submissions for select to ai_reader_role using (true); -- and the same for submission_values and form_versions Only do this if row scoping really is enforced somewhere else. For that role, using (true) means every row.
Why that works while the old policy still applies
The original policy is TO public, so it still applies to ai_reader_role, and it still reads forms. Postgres combines permissive policies with OR, so the role's effective filter is true OR exists (select ... from forms ...). The planner folds that to true and drops the subquery, so forms is never permission-checked. We confirmed it as ai_reader_role:
select 1 where exists (select 1 from forms); -- permission denied for table forms select 1 where true or exists (select 1 from forms); -- returns 1 select 1 where false or exists (select 1 from forms); -- permission denied for table forms EXPLAIN on the count over submissions shows a plain index-only scan, with no subplan on forms.
That's an optimizer detail, though, and you may not want to depend on it. The sturdier version is to give user-facing policies an explicit TO authenticated. A policy created without a TO clause applies to public, which means every role, including service roles like this one.
Check that it stays confined
We keep a read-only script that runs against the live database inside begin read only ... rollback. It checks that the role switch works and the three allowed reads succeed. It also expects a 42501 denial on six tables the role should never see, including forms, users and api_keys. If any check changes, it exits non-zero.
Also make the role switch fail closed. If your code catches a failed set role and runs the query anyway, the query runs as the connection user. On Supabase that's postgres, which bypasses RLS, and nothing will look wrong.
Takeaway
On PostgreSQL 16 and later, CREATE ROLE gives the creator ADMIN on the new role, not SET. If you plan to SET ROLE to it, grant it WITH SET TRUE. Then read every RLS policy on the tables that role touches. If a policy's expression reads a table the role can't, the query fails, even when the query never names that table.
From DodoForm, an AI form builder. The queries and outputs above come from our production database, PostgreSQL 17.6 on Supabase.