CREATE INDEX CONCURRENTLY cannot run inside a transaction block for Liquibase

If you use Liquibase in your project, at some point you might want to add index to your tables. Something like:

create index concurrently if not exists some_index_name_idx
    on table_name (some_field_id);

But you would get an error, saying that “CREATE INDEX CONCURRENTLY cannot run inside a transaction block”. After some googling you find this: https://docs.liquibase.com/concepts/changelogs/attributes/run-in-transaction.html

And you modify your scripts by adding runInTransaction=”false”:

// If using xml syntax
<changeSet id="1"  author="name_here" runInTransaction="false">
    <sql>create index concurrently if not exists some_index_name_idx
    on table_name (some_field_id);</sql>
</changeSet>

However, as for SQL version of scripts, it should work like this:

// SQL syntax
--changeset your.name:1 runInTransaction:false
create index concurrently if not exists some_index_name_idx
    on table_name (some_field_id);

But it does not work – you get the same error “CREATE INDEX CONCURRENTLY cannot run inside a transaction block”.

The solution is to add one more line to the file:

// SQL syntax - note the next line
--liquibase formatted sql
--changeset your.name:1 runInTransaction:false
create index concurrently if not exists some_index_name_idx
    on table_name (some_field_id);

Now everything works as expected.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *