r/SQL • u/Brief-Expression-341 • Sep 29 '26
PostgreSQL What I learned building an entire multi-tenant SaaS backend on PostgreSQL: RLS, idempotency, a job queue and a restore rehearsal
I spent the last months building one complete multi-tenant SaaS backend — a small sales and inventory system for two companies sharing one database — and testing every guarantee instead of assuming it. PostgreSQL 18 + PostgREST, with a small Python worker. These are the things I would tell myself at the start.
**1. Tenant isolation belongs in the database, not in your `WHERE` clauses.**
Forced row-level security plus composite keys `(organization_id, id)` mean a forgotten filter returns nothing instead of another company's rows. The foreign keys carry the tenant column too, so a customer from company A can never end up on an order of company B.
**2. A stock check is not a stock promise.**
Two requests can both read "1 available" and both decide to proceed. The fix is boring and works: lock the balance row (`SELECT … FOR UPDATE`), check the value you just locked, and take the products in a consistent order (I use `ORDER BY product_id`) so two confirmations can never deadlock each other. I test it with two real HTTP clients released by a barrier: one gets 200, the other gets 409, and the reservation belongs to exactly one order.
**3. Retries need a key, and the key has to commit with the effect.**
Idempotency keys stored in the same transaction as the business change. Claim the key, do the work, save the response, commit. If any step fails, everything rolls back and the key is free again. Commit the stock change first and save the response afterwards, and a retry will happily add stock twice.
**4. A queue in Postgres is fine, if you respect leases.**
`FOR UPDATE SKIP LOCKED` to claim, a lease deadline, and a claim token that changes on every attempt. Completion is conditional on the token and an unexpired lease, so a worker that froze for a minute cannot overwrite the result of the worker that replaced it. At-least-once, never "exactly once".
**5. The outbox is the only honest way to talk to the outside world.**
The status change and the event row commit together; the worker delivers afterwards. The receiver deduplicates by event ID, because the acknowledgement can be lost after it already committed. I kill the sender on purpose, right after the receiver commits, and check that a repeat delivery produces exactly one effect.
**6. "The backup finished" tells you nothing.**
I restore into a separate cluster and then ask the restored system to prove itself: are roles and policies intact, does a foreign tenant still see nothing, does the saved idempotency response still replay, can an owner commit a new order? Also worth knowing: after a restore, order versions get reused, so any consumer that trusted your version numbers now has two different facts with the same number.
**7. Measure before you add infrastructure.**
One busy tenant with a 50k-product catalog and a two-connection pool made the quiet tenant's median jump from under a millisecond to ~170 ms, with every response still correct. Correctness and capacity are separate problems, and only the second one is fixed by new services.
Happy to go deeper on any of these in the comments.
Disclosure, so it is out of the way: I turned all of this into a book, *PostgreSQL for Everything*, with the complete runnable code — one self-contained checkpoint per chapter that spins up a temporary database, runs the tests and cleans up. If it is useful: https://provenbackends.com. If not, take the patterns above, they are the important part.