[v8] Bind a role and session settings per query in core, not only in the Supabase extension, for RLS multi-tenancy

Open
#31,003 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
node.js, postgresql, typescript

Research direction

Read the existing Supabase binding and RoleBoundDb implementation, then trace how connections are acquired, middleware runs, and cache annotations handle bound scopes. The issue also points to #31002 and #12735 for context. Done means core can bind a custom role and settings per query or transaction, clean up pooled connections, and expose the bound scope to middleware and caching.

Written by the indexing model from the issue text.

Description

What problem are you trying to solve?

Multi-tenant SaaS on PostgreSQL Row Level Security. Policies read an application-defined setting, current_setting('app.tenant_id'), and are written for a non-owner role, tenant_app. Some work also has to run as another role, for example an operator role that sees the tenants table and nothing inside the tenants. Every statement Prisma sends must therefore run on a connection that has the right role and setting at that moment. Nothing may leak to the next user of the pooled connection.

The Supabase extension already does exactly this: asUser(jwt) / asAnon() / asServiceRole() bind role and request.jwt.claims with session set_config beneath the middleware chain, and RESET ALL on release. That machinery is Supabase-specific: JWT-shaped, with fixed roles and claims.

Outside Supabase, the only working hook today is to subclass pg.Pool and override connect() so that every checkout runs set_config(...) before Prisma gets the client. The alternatives don't work:

  • SqlMiddleware doesn't see the connection.
  • pg.Pool's connect event fires only for new physical connections.
  • acquire is synchronous.

Subclassing works, but it:

  • ties the application to pg internals and to Prisma's exact pg version (see #31002);
  • is invisible to Prisma: the docs on cacheAnnotation already warn that RLS-filtered reads on the plain façade aren't protected. A binding Prisma knows about could keep those reads out of the cache, as it does for RoleBoundDb.
Proposed solution

Generalize the Supabase binding into core. For example:

const db = postgres({ contract, url });

// Bound to a role and settings; same surface as RoleBoundDb, including .transaction().
const tenantDb = db.bind({ role: 'tenant_app', settings: { 'app.tenant_id': tenantId } });
await tenantDb.orm.public.Project.all();

It would be applied by the same mechanism as the Supabase extension:

  • session set_config beneath middleware and RESET ALL on release;
  • or SET LOCAL inside a transaction, as an option for transaction-mode poolers.

It would also be visible to middleware and the cache as a bound scope.

A lower-level alternative would also do: an async per-acquire hook, onAcquire(client, ctx), which the runtime awaits before handing a connection to a statement or transaction. Applications could then build the binding on it without subclassing pg.Pool.

Alternatives considered
  • Subclassing pg.Pool: what we do now. It works and is measured with concurrent mixed checkouts, but see the drawbacks above.
  • One transaction per request with SET LOCAL: it holds a connection for the whole request, including non-database work such as mail, files and LLM calls. A caught error aborts the whole transaction. Background jobs need their own path.
  • The Supabase extension: Supabase-only.
  • Filtering by tenant in application code instead of RLS: the database stops being the last line of defence.
Scope and impact

This matters to any multi-tenant application on PostgreSQL RLS with a custom setting or several database roles; #12735 shows long-standing demand. It needs a session-capable connection, the same requirement and documentation as the Supabase extension. It is additive: no change for applications that don't bind.

Dominant language
TypeScript
Stars
47.8k
Forks
2.6k
Avg merge
1d 21h
Merged PRs (30d)
227

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from prisma/orm

All issues in prisma/orm

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.