activerecord-postgresql-extensions icon indicating copy to clipboard operation
activerecord-postgresql-extensions copied to clipboard

create index concurrently fails in standard migrations

Open markmansour-zz opened this issue 13 years ago • 5 comments

Creating concurrent indexes cannot be done within a transaction in PG.

All AR migrations occur within a transaction.

If you try, you'll get the following error

PGError: ERROR: CREATE INDEX CONCURRENTLY cannot run inside a transaction block

to work around this issue I needed to do the following in my migration

    execute "END"  # manually end the current transaction
    create_index 'index_appointments_on_cancellation_reason', :appointments, { :column => :cancellation_reason}, { :concurrently => true }
    execute "BEGIN" # manually start a new transaction

it would be nice to somehow remove this boilerplate.

markmansour-zz avatar Sep 21 '12 00:09 markmansour-zz

I'll take a look at this when I have a chance. This looks like it has to do with migrations being wrapped completely in transactions to allow for rollbacks on DDL changes.

dark-panda avatar Oct 22 '12 15:10 dark-panda

Sweet. We aren't ready to make the jump to Rails 4 yet, but I have done some work on Rails 4 compatibility in the rails-4 branch. Wonder if this would be worth a backport.

dark-panda avatar Sep 09 '13 19:09 dark-panda

For anyone seeing this from a google search, in modern rails you can add disable_ddl_transaction! to the top of your migration class to avoid this.

jaggederest avatar Feb 04 '15 23:02 jaggederest

I got the same error message using Goose for GoLang and this worked --

(removing the execute commands and adding the semicolons)

END;  # manually end the current transaction
CREATE INDEX CONCURRENTLY index_appointments_on_cancellation_reason ON appointments (cancellation_reason);
BEGIN; # manually start a new transaction

amandakievet avatar Jun 03 '15 16:06 amandakievet