Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway migration hangs for postgres CREATE INDEX CONCURRENTLY

I am trying to run a CREATE INDEX CONCURRENTLY command against a Postgres 9.2 database. I implemented a MigrationResolver as shown in issue 655. When this migration step is run via mvn flyway:migrate or similar, the command starts but hangs in waiting mode.

I verified that the command is executing via the pg_stat_activity table:

test_2015_04_13_110536=# select * from pg_stat_activity;
 datid |        datname         |  pid  | usesysid | usename  | application_name | client_addr | client_hostname | client_port |         backend_start         |          xact_start           |          query_start          |         state_change          | waiting |        state        |                                                                                                   query
-------+------------------------+-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 21095 | test_2015_04_13_110536 | 56695 |    16385 | postgres | psql             |             |                 |          -1 | 2015-04-13 11:10:01.127768-06 | 2015-04-13 11:13:08.936651-06 | 2015-04-13 11:13:08.936651-06 | 2015-04-13 11:13:08.936655-06 | f       | active              | select * from pg_stat_activity;
 21095 | test_2015_04_13_110536 | 56824 |    16385 | postgres |                  | 127.0.0.1   |                 |       52437 | 2015-04-13 11:12:55.438927-06 | 2015-04-13 11:12:55.476442-06 | 2015-04-13 11:12:55.487139-06 | 2015-04-13 11:12:55.487175-06 | f       | idle in transaction | SELECT "version_rank","installed_rank","version","description","type","script","checksum","installed_on","installed_by","execution_time","success" FROM "public"."schema_version" ORDER BY "version_rank"
 21095 | test_2015_04_13_110536 | 56825 |    16385 | postgres |                  | 127.0.0.1   |                 |       52438 | 2015-04-13 11:12:55.443687-06 | 2015-04-13 11:12:55.49024-06  | 2015-04-13 11:12:55.49024-06  | 2015-04-13 11:12:55.490241-06 | t       | active              | CREATE UNIQUE INDEX CONCURRENTLY person_restrict_duplicates_2_idx ON person(name, person_month, person_year)
 (3 rows)

An example project that replicates this problem can be found in my github: chrisphelps/flyway-experiment

My suspicion is that the flyway query against schema version which is idle in transaction is preventing postgres from proceeding with the index creation.

How can I resolve the conflict so that postgres will proceed with the migration? Has anyone been able to apply this sort of migration to postgres via flyway?

like image 207
CPhelps Avatar asked Apr 13 '15 20:04

CPhelps


People also ask

How do you skip Flyway migration?

The hotfix migration can be deployed with Flyway with skipExecutingMigrations=true . The schema history table will be updated with the new migration, but the script itself won't be executed again. skipExecutingMigrations can be used with with cherryPick to skip specific migrations.

Does Flyway support Postgres?

Flyway supports PostgreSQL Along with 20+ other relational databases.

Does Flyway lock table?

Flyway doesn't lock the schema. It then acquires a lock on the metadata table using SELECT * FROM metadatatable FOR UPDATE . This lock is released automatically after the migration completes when the transaction is commited or rolled back.

Does Flyway support multiple databases?

During development you need a fast, automated way to build multiple copies of a database on any development or test server, with each database at the right version.


1 Answers

In the meantime, there is a Resolver included in flyway which looks for some magic in the filename.

Just add the prefix 'NT' (for No-Transaction) to your migration file, i. e.

V01__usual_migration_1.sql

V02__another_migration.sql

NTV03__migration_that_does_not_run_in_transaction.sql

V04__classical_migration_4.sql

etc.

like image 143
Regenschein Avatar answered Sep 30 '22 01:09

Regenschein