Execute long running liquibase database updates asynchronously

Viewed 2036

We are using liquibase to manage changes in the structure of the underlying relational database of our application. As database backend we are using PostgreSQL.

Now there are tables with millions of entries and we need to add indexes to some of these large tables. Because of the size of the tables index creation takes a considerably long time. This blocks the application start, as the liquibase changesets are executed during the bootup phase of our application to ensure a proper persistence backend to be available before the application is actually running.

While structure changes need to be made before the application is started for obvious reasons, adding an index could be made while the application is already running. Hence my question:

Is there a way to perform index creation asynchronously with liquibase?

We already tried to use the CONCURRENT option provided by PostgreSQL. This helps when creating an index during application runtime as the respective tables will not be locked by the index creation. But the respective liquibase changesets will still wait for the index creation to finish before the next changeset is executed.

1 Answers

This is not directly possible, but you can work-around the problem:

You will need to manually run the index creation before you run the liquibase. (This will allow you to use the "concurrently" option).

In order to keep a record of the change, you can add the index creation script (without the "concurrently" option) and with a precondition with onFail="MARK_RAN " to liquibase.

When liquibase is next run, it will mark the changeset as run.

Related