All packsMulti-Tenant Auth & RBAC CoreSolve
postgres set_config vs set session leak pgbouncer transaction pooling
SET leaks tenant context across a pooled connection
A session-scoped SET for tenant identity survives past the request that set it under transaction-mode pooling, leaking one request's org into the next.
What goes wrong
Setting the current tenant with a plain SET app.org_id = '...' persists for the lifetime of the underlying Postgres connection, not the logical request. Under transaction-mode connection pooling (PgBouncer, Neon's pooler) -- the standard way to serve more concurrent requests than raw Postgres connections allow -- the next request that happens to reuse that same pooled connection inherits the previous request's tenant context, invisibly, until concurrent production traffic exposes it.
Why agents get it wrong
SET is the first thing that comes up when searching how to pass a value into an RLS policy, it works perfectly in local development against a direct, unpooled connection, and the bug requires two things to line up (transaction-mode pooling and a specific interleaving of requests) that a local dev environment or a single-connection test almost never reproduces.
What Multi-Tenant Auth & RBAC Core pre-decides
Core decision 3 wraps every tenant-scoped query in withContext() (a frozen helper in src/lib/db.ts), which sets identity with set_config('app.user_id'|'app.org_id', $x, true) -- the third argument, is_local=true, means the setting dies at COMMIT or ROLLBACK, not at connection close. That's what makes it safe under transaction-mode pooling: nothing outlives the transaction it was set inside.
Source: ARCHITECTURE.md core decision 3, 'Traps this pack pre-empts'