In order to run integration tests concurrently, I wish to create a sample database at the beginning of every test. Theses test databases are "clones" of an immutable reference database (the template in CREATE test_db Template test_reference).

The cloning (and dropping at the end) operation remains fast enough.

The problem is, I can't clone that reference database concurrently or I get the same kind of error as in the following question: this is a bottleneck. Is there a way to tell Postgres that I know those concurrent accesses are safe ?

PS: I am not interested for now to change my test setup to the well known strategy which consist in running all my tests in non committing transactions

  • I don't think there is a way. Feb 14, 2023 at 12:49
  • I don't get that error with concurrent creations (of differently named new databases) using the same template database. I only get that if someone is logged in to the template at the time the creations are occurring, which is not the workflow you describe. I think you need to provide a minimal reproducible case, as what you have described does not constitute one.
    – jjanes
    Feb 14, 2023 at 19:01
  • may I ask how you performed concurrent requests ? I use a connection pool which indeed generates multiple sessions (and I get the error). But If I use a single session, the time to create a database grows linearly (I assume the calls get queued). I can't run the CREATE DATABASE command within a transaction so I wonder how you set up your concurrent queries.
    – laurent
    Feb 14, 2023 at 20:32
  • @jjanes: the way I understand it: the tests are run in parallel. Two (or more) tests want to clone the template database pretty much at the same time. But create database will connect to the template database to read from. As long as the create database runs, there will be a connection to the template database preventing other CREATE DATABASE statements to use that template.
    – user1822
    Feb 15, 2023 at 6:56
  • Exactly @a_horse_with_no_name. Do you think I need to rephrase my question ?
    – laurent
    Feb 15, 2023 at 8:28

1 Answer 1


As stated by @jjanes in the comments. There is actually no issue: the mistake I made was to use a client connected to the template database to perform the "cloning". If I connect the client to whatever other database, there is no issue and I manage to perform concurrent copies.

PS: however, the performances are not that great either

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.