Skip to content

Postgres deadlocking when multiple processes run CREATE INDEX CONCURRENTLY #960

@AkuSilvenius

Description

@AkuSilvenius

Describe the Bug
When multiple processes run migrations in parallel (e.g. multiple application replicas running m.Up()), having CREATE INDEX CONCURRENTLY as part of the migration script will result in a deadlock, leaving behind a dirty database version as well as an INVALID index (expected as mentioned in docs).

Steps to Reproduce
I have added a failing test in my fork to mimic the behavior of multiple processes running the migrations in parallel here - it's basically the same test as in master branch here (which includes a migration file with a CREATE INDEX CONCURRENTLY), but just runs multiple times in parallel. Setting concurrency = 1 will pass the test, but any larger number of concurrency will result in a deadlock and a failing test.

Expected Behavior
Run migrations successfully

Migrate Version
v4.15.2

Loaded Source Drivers
s3, github, gitlab, go-bindata, file, bitbucket, github-ee, godoc-vfs, gcs

Loaded Database Drivers
cockroachdb, neo4j, postgresql, redshift, clickhouse, mysql, pgx, postgres, sqlserver, crdb-postgres, mongodb, spanner, cassandra, cockroach, firebird, firebirdsql, mongodb+srv, stub

Go Version
go version go1.20.3 darwin/arm64

Stacktrace

Failing test output
=== CONT  TestPostgres_ConcurrentMigrations/postgres:12
    dktest.go:35: Pulling image: postgres:12
    dktest.go:53: Error parsing image pull response: context deadline exceeded
    dktest.go:82: Created container: dktest.ContainerInfo{ID:"d3535640fb6417f67c459b688313a75d753bc0b145d921c8bc9a4a1be853c140", Name:"dktest_ZEWaTjfqBG", ImageName:"postgres:12", Ports:[]}
    dktest.go:87: Started container: dktest.ContainerInfo{ID:"d3535640fb6417f67c459b688313a75d753bc0b145d921c8bc9a4a1be853c140", Name:"dktest_ZEWaTjfqBG", ImageName:"postgres:12", Ports:[]}
    dktest.go:97: Inspected container: dktest.ContainerInfo{ID:"d3535640fb6417f67c459b688313a75d753bc0b145d921c8bc9a4a1be853c140", Name:"dktest_ZEWaTjfqBG", ImageName:"postgres:12", Ports:[]}
    migrate_testing.go:30: UP
    migrate_testing.go:30: UP
    migrate_testing.go:30: UP
    migrate_testing.go:32: try lock failed in line 0: SELECT pg_advisory_lock($1) (details: pq: deadlock detected)
    migrate_testing.go:32: try lock failed in line 0: SELECT pg_advisory_lock($1) (details: pq: deadlock detected)
    dktest.go:132: Stopped container: dktest.ContainerInfo{ID:"d3535640fb6417f67c459b688313a75d753bc0b145d921c8bc9a4a1be853c140", Name:"dktest_ZEWaTjfqBG", ImageName:"postgres:12", Ports:[5432/tcp -> :46663]}
    dktest.go:138: Removed container: dktest.ContainerInfo{ID:"d3535640fb6417f67c459b688313a75d753bc0b145d921c8bc9a4a1be853c140", Name:"dktest_ZEWaTjfqBG", ImageName:"postgres:12", Ports:[5432/tcp -> :46663]}
    --- FAIL: TestPostgres_ConcurrentMigrations/postgres:12 (62.80s)

Additional context
This looks to be exactly the same problem as reported and fixed in the flyway library here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions