3

I need to use db-migrate to add an index to a Postgres database with CREATE INDEX CONCURRENTLY. However, db-migrate wraps all migrations in a transaction by default, and trying to create a concurrent index inside a transaction results in this error code:

CREATE INDEX CONCURRENTLY cannot run inside a transaction block

I can't find any way to disable transactions as part of the db-migrate options, either CLI options or (preferably) as a configuration directive on the migration itself. Any idea if this can be accomplished?

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169

2 Answers2

3

It turns out that this can be solved on the command line by using --non-transactional. Reading the source, I can see that this sets an internal flag called notransactions, but it's not clear to me whether this can be set as part of the migration configuration or must be passed on the command line.

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
1

I kept getting the errors even when running with --non-transactional flag.

The solution for me was to run with --non-transactional AND have each CREATE INDEX CONCURRENTLY statement in its own separate migration file. It turns out you can't have more than one of it in the same file (same transaction block).

duartealexf
  • 131
  • 1
  • 9