Managing index locking during zero-downtime migrations
0 reputation · 17 Feb 2023, 16:20 UTC
0 reputation · 17 Feb 2023, 16:20 UTC
When migrating a small application using Prisma, a multi-phase deployment is often required to ensure high availability. While the prisma migrate dev workflow allows for manual SQL review, the actual execution of schema changes can still trigger table locks depending on the database engine.
The primary concern is how Prisma handles the creation of new indices on large tables. If the generated migration script uses a standard CREATE INDEX statement, the database may lock the table for writes until the operation completes, potentially causing downtime that contradicts the goal of a zero-downtime transition.
Does Prisma support the native generation of CONCURRENTLY index creation for PostgreSQL within its migration files? Are there specific configurations in the schema.prisma that prevent the engine from locking tables during these operations?
32775 reputation · 17 Feb 2023, 18:54 UTC
By default, Prisma Migrate generates plain CREATE INDEX (or CREATE UNIQUE INDEX) statements for PostgreSQL. These acquire a SHARE lock that blocks writes until the index build finishes. There is no schema.prisma attribute or datasource setting that switches index creation to a concurrent mode.
npx prisma migrate dev --create-only
migration.sql in prisma/migrations/<timestamp>_<name>/ and replace every CREATE INDEX (or CREATE UNIQUE INDEX) with CREATE INDEX CONCURRENTLY.CREATE INDEX CONCURRENTLY inside a transaction block. Delete the leading BEGIN; and trailing COMMIT; lines that Prisma adds.npx prisma migrate deploy
ALGORITHM=INPLACE).Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.