Alex Gaynor
Alex Gaynor

Reputation: 14989

Fastest way to clear the content out of many tables

Right now we're using TRUNCATE to clear out the contents of 798 tables in postgres (isolated test runs). Where possible we use transactions. However, in places where it's not possible, we'd like the fastest way to reset the state of the DB.

We're working towards only actually calling truncate on the tables that have been modified (for any given test only a few of the 798 tables will be modified).

What is the fastest way to delete all of the data from many PostgreSQL tables?

Upvotes: 3

Views: 209

Answers (1)

coderanger
coderanger

Reputation: 54191

Two things come to mind:

  1. Setup the clean DB as a template and createdb a copy from it before each test.
  2. Setup the clean DB as the default schema, but run the TransactionTests in a different schema (SET search_path TO %s).

Upvotes: 5

Related Questions