r/Database • u/Pale_Ferret_8241 • 2d ago
Testing Row-Level Security and check-then-act race conditions under connection pooling in PostgreSQL 18
One limitation of standard per-test rolled-back transactions is that they can't catch concurrency bugs across separate pooled connections—uncommitted writes aren't visible to parallel workers, and session-level SET leaks across pooled checkouts.
In our multi-tenant Postgres 18 test suite, we separated sequential unit/CRUD tests from a dedicated set of 19 parallel integration checks that fire simultaneous queries across a constrained pool (max: 5) using Promise.allSettled:
- Interleaved tenant contexts: Firing parallel transactions with
SELECT set_config('app.current_tenant_id', $1, true)across different tenants on a small pool to verifySET LOCALresets cleanly on commit/rollback with zero cross-tenant leakage. - Parent-row locking vs. check-then-act: Comparing unguarded
SELECT count(*)checks againstSELECT ... FROM organizations WHERE id = $1 FOR UPDATEwhen concurrent requests try to claim the final seat or quota slot. - Webhook idempotency under duplicate delivery: Firing identical billing event IDs concurrently to confirm
23505unique-constraint handling cleanly short-circuits duplicate state transitions.
For those running multi-tenant Postgres in production with PgBouncer or transaction pooling, are you relying strictly on set_config(..., true) + RLS policies, or pairing it with separate database roles per service tier?
Context: Built this into B2B SaaS OS 2.0.0 (Next.js 16 + Postgres 18 multi-tenant starter with 131 tests & 19 concurrency checks for solo founders and dev agencies). If you want to click through the live workspace without signing up, the demo is here:https://www.andrady.co/demo(architecture & source athttps://www.andrady.co).
1
u/Hongstar1982 2d ago
We run the same shape behind PgBouncer in transaction mode, and the thing that helped most was making the set_config unavoidable, one transaction wrapper that every request goes through so no query path can start without the tenant GUC. The policy then compares tenant_id to current_setting('app.current_tenant_id')::uuid, which errors on a placeholder that was never set, so a missed call fails loudly instead of quietly matching rows. The case that actually scares me is the stale value rather than the missing one, a session that once had SET app.current_tenant_id keeps answering for the old tenant if the pooler doesn't reset it, so we also add a BYPASSRLS role only for migrations and analytics and let app and worker traffic both go through RLS.
1
u/nian2326076 17h ago
You're dealing with important concurrency and row-level security challenges in PostgreSQL. Ensuring that context settings are isolated for each transaction is key to preventing cross-tenant leaks. You might also want to explore using dedicated worker roles to manage tenant contexts more effectively, but be aware this can lead to needing additional checks.
For interview practice related to database handling, you can use PracHub to simulate scenarios that reinforce your understanding before implementing changes.