pgtestdb's Template Cloning Approach to Testing is Fast

pgtestdb's Template Cloning Approach to Testing is Fast

I explored pgtestdb, a Go/Postgres testing package that leverages Postgres template databases for rapid setup. While cloning databases takes roughly 100ms, similar to migrating schemas, I found that reusing test schemas can make the overall suite run 3.5 times faster. Although I will keep River's current schema-based method for its isolation benefits, I recommend pgtestdb for users needing efficient end-to-end testing.

100 ms to bootstrap a test database is pretty fast, but if you're building a full application that's going to have 10,000 tests, ideally you want a test setup on the order of 10x faster.
  1. TexanFeller

    For a long time I've used similar approaches for testing against Postgres, especially when using tools like Testcontainers. It's saved a massive amount of time waiting for my tests to run over the years and it's incredibly simple to implement, even manually.

    1. Set up a container image based on our prod Postgres DB's version with current prod migrations pre-applied.

    2. Configure Testcontainers to be reuse the container between tests, at least within the same file.

    3. ~25 lines of init code that apply local-branch migrations to a template DB and copy test data to it on the first test.

    4. When the template DB already exists, test setup just drop the DB and copy it fresh from the template DB.

    I find tests that actually perform the actions catch a heck of a lot more bugs and are easier to setup up than code that uses a lot of mocking or "test implementations" of a class. And with just a little care the performance penalty is negligible and way more than worth it.

  2. peterldowns

    Just want to say thank you, Brandur, for checking out pgtesdb and benchmarking it so thoroughly. I’m pleased that it performs so well and I’m going to throw some tokens and brainpower at potentially implementing a cleanup+reuse+pool of successful dbs instead of always tearing them down.

    Some confusion in the threads below —

    pgtestdb is just a primitive for “give my test a clean db, fast.” With your postgres running on ramdisk, I don’t think there’s any faster way to make a clean and fully migrated db — and your migrations only run one time, no matter how many test processes you have operating concurrently or how many tests are in parallel within those processes.

    You can actually combine it with test transactions, you’re totally allowed to do anything you want with the db! It just so happens that it’s fast enough (in my experience) to Just Give Every Test Its Own Database, for quite a large number of tests.

    Really cool upside of AI is enabling experiments like this one that previously would have been prohibitively time consuming. Thanks again, Brandur.

  3. rgbrgb

    In the past few years I've been using a "dirty db" approach to testing where I run parallel integration tests against a single postgres-based backend without any cleanup between tests. Every test hits the same db with unique ID's. The constraint is that you can't assert exact counts, you need to assert that specific ID's exist. With this setup, the db just gets setup once and all the tests can run in parallel. Integration points are where I've seen the most breakage in prod, so I like to test with a real db. Parallelism makes it fast and incidentally this approach sometimes catches racy interactions that otherwise might only see with concurrent prod requests (heisenbugs in waiting). Many tests hitting the API's in parallel ends up being a better simulation of prod behavior.

  4. tux3

    If you don't need SET LOCAL or different isolation levels in your tests, the fastest by far in my experience is to have a template DB per process (perhaps migrating it once and then using it as template to make copies), then run your tests wrapped in a transaction that you rollback at the end.

    Postgres rollback is essentially instant, since all the cleanup is left to a later vacuum. Postgres can handle "nested transactions" (savepoints), so in most cases you don't have to modify your code.

    For the small set of tests that aren't compatible with being wrapped in a transaction, you run them serially in each process and either DELETE or TRUNCATE CASCADE in between for cleanup. DELETE is a bit faster, but then you have to deal with foreign key issues yourself.

    There's more that can go wrong when relying on transaction rollback. But in terms of speed, I don't know of a faster way.

  5. pmontra

    The original developer of a Rails project I inherited decided to seed the test db with db/seed.rb and tie every test to the content of the seed. There were a lot of tests so rewriting then was not an option, not immediately.

    I wrote the new tests in the canonical way but I still had the problem of all those seconds spent seeding the db even when I run a single test. I wrote a couple of scripts that dumped the db at the end of the tests (if a dump did not exist yet) and reloaded it at the beginning of the tests. This is much faster. I still have to clear the test db and reseed it when I switch branch, because I don't have a dump list branch.

    Maybe I can create a template with the data in it. Or finally rewrite every single old test.

More from this day

2026-08-01