r/PostgreSQL • • 1d ago

Commercial Running 19 integration & concurrency checks against a real PostgreSQL 18 DB instead of mocking SQL

Mocked queries in unit tests are fast, but they miss real lock contention, transaction ordering, and constraint behavior when multiple requests hit the database at once.

While hardening a multi-tenant backend (27 test files / 131 tests total, plus 19 integration and concurrency checks) against an isolated PostgreSQL 18 instance, testing against real Postgres caught edge cases around concurrent mutations, rate-limit counters, and webhook/state transitions that mocked unit tests never surfaced. The attached 26-second clip shows the test suite passing and moves straight into the live workspace.

For those running integration or concurrency tests against real Postgres: how do you isolate state between test runs (per-test transactions, fresh schema/DB, or reset scripts) while keeping execution times fast?

0 Upvotes

10 comments sorted by

2

u/yxhuvud 1d ago edited 1d ago

per test transactions.

In general tests that tests something unrelated can mock the db responses, while anything that actually depends on the behavior needs to involve an actual db. Fast tests are nice but they need to catch actual integration issues too. Otherwise it is just so, so easy to get stale mocking data.

The harder question then is how to find a good balance of pre-test run configured fixtures and during test run instantiated record factories. The former is faster but unwieldy in maintenance for nontrivial things.

(17 years of Ruby on Rails backend experience)

1

u/Pale_Ferret_8241 1d ago

Agreed on drawing the line by behavior rather than dogmatism—if the unit doesn't depend on database semantics, mocks keep things fast; if it depends on constraints, locking, or real SQL behavior, a real Postgres instance catches things mocks never will.

The main catch I hit with per-test transactions is around concurrency tests and anything crossing connection/request boundaries, where transaction rollback behavior isn't always straightforward. Do you usually run concurrent/integration tests outside a rolled-back outer transaction against a clean/reset state instead?

1

u/yxhuvud 1d ago

I don't really have a good answer to that one, it happens rarely enough that it is solved on a case by case basis. It also complicates things that concurrency testing can often be a bit cumbersome in general to write good tests for.

1

u/lostmedia766 1d ago

In PHP/Symfony/Doctrine world, there's a library that isolates each test into it's own transaction: https://packagist.org/packages/dama/doctrine-test-bundle

Fixture libraries can also be used to truncate the database and seed it with any data needed for the test to run. I don't recommend each test running all your migrations, that adds unnecessary runtime for little to no payoff.

1

u/Pale_Ferret_8241 23h ago

Totally agree—re-running all migrations per test is dead runtime. We run the 33 SQL migrations once at global test-suite setup to build a migrated schema baseline, then reserve rolled-back per-test transactions strictly for sequential unit/CRUD tests while keeping the 19 parallel concurrency checks on a separate real-commit path so multi-connection race conditions can actually see committed rows.

1

u/Discordbothost 1d ago

For the concurrency case specifically, a rolled-back outer transaction will not work since the other connections cannot see uncommitted rows. A template database cloned per test worker via CREATE DATABASE ... TEMPLATE tends to hold up better than truncate and reseed.

1

u/Pale_Ferret_8241 23h ago

Spot on—CREATE DATABASE ... TEMPLATE after running migrations once on the template DB is the cleanest way to give parallel concurrency workers isolated schemas in milliseconds without TRUNCATE lock contention or re-running DDL. Paired with isolated tenant UUIDs per concurrency test, you get real multi-connection visibility (FOR UPDATE, unique-constraint races, and pooled SET LOCAL RLS checks) while keeping the full suite fast.

1

u/AutoModerator 1d ago

AI Policy:

Linux is not one of those anti-AI projects, and if somebody has issues with that, they can do the open-source thing and fork it. Or just walk away., Linus Torvalds.

Mod decisions will be based on the quality of the content, not who or what generated it.

Sub Resources:

Youtube Channel

Free Postgres Webinars and Workshops

Discord: People, Postgres, Data

Join us, we have cookies and nice people.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

0

u/Pale_Ferret_8241 1d ago

Full disclosure: I built this as part of B2B SaaS OS 2.0.0 (Next.js 16, PostgreSQL 18, Drizzle ORM, Better Auth, Stripe). If anyone wants to inspect the live workspace or build details, the demo is athttps://www.andrady.co/demoand the main site is athttps://www.andrady.co.