Repository navigation
Description
Concurrent Migrate() applies the same migration twice
Since EF Core 9, Database.Migrate() takes a database lock so that several instances of an app can start together. With Npgsql the lock does not cover the whole run, and instances re-apply migrations that another instance already applied.
Repro (dotnet run against any PostgreSQL; REPRO_PG sets the connection string): https://github.com/PieterjanDeClippel/efcore-npgsql-migration-lock-race
The program recreates an empty database and runs three MigrateAsync() calls together. There are three migrations, and the second sleeps three seconds, like a data migration or an index build. On Npgsql.EntityFrameworkCore.PostgreSQL 11.0.0 with PostgreSQL 18, 3 of 3 runs fail:
OK instance 1
FAIL instance 2: PostgresException: 42701: column "Email" of relation "People" already exists
FAIL instance 3: PostgresException: 42701: column "Phone" of relation "People" already exists
history rows: 3 (3 expected)
The same scenario on SQL Server (sp_getapplock, LockReleaseBehavior.Connection) applies each migration once.
Cause:
NpgsqlHistoryRepositorytakesLOCK TABLE "__EFMigrationsHistory" IN ACCESS EXCLUSIVE MODEwithLockReleaseBehavior.Transaction, so the lock ends with each migration's transaction.- EF's
Migrator.MigrateImplementationAsyncreads the applied migrations once, under the first lock, and plans the whole run from that. MigrationCommandExecutorcommits each migration, then begins a new transaction and reacquires the lock for the next one, without re-reading the history.- Between instance A's commit of migration 1 and its next lock, instance B (blocked on
LOCK TABLE) gets the lock, sees migration 1 applied, and applies migration 2. A then gets the lock back and applies migration 2 again from its stale plan.
Proposed fix: a session-level pg_advisory_lock with LockReleaseBehavior.Connection, so the migrator takes the lock once, before reading the history, and holds it for the run, as SQL Server does. The lock key is a number computed from the history table's schema-qualified name. The patch against hotfix/11.0.1 is on the repro's fix branch (upstream/efcore.pg-hotfix-11.0.1.patch); I have not run it through the provider's test suite. The same branch has the identical code as a workaround for 11.0.0 (ReplaceService<IHistoryRepository, AdvisoryLockHistoryRepository>()), which makes the repro pass 3 of 3 runs.
The trade-off is that a session lock does not hold through PgBouncer in transaction pooling mode, where LOCK TABLE in a transaction does. The alternative is a fix in EF Core's migrator, re-reading the history after reacquiring a transaction-scoped lock, which would cover any provider using LockReleaseBehavior.Transaction. If you would rather go that way, I can move this to dotnet/efcore.
Activity
#536: link the upstream reports for the S3 and S19 findings
#536: apply the workarounds for the S3 and S19 upstream bugs
roji commentedon Nov 25, 2026
AI Triage
The below is an AI-generated analysis and may contain inaccuracies.
Confirmed: concurrent migration runs can execute the same migration twice and fail with PostgreSQL error
42701: column "Email" of relation "People" already exists. This is a regression relative to EF Core 9 for the ordinary, transaction-compatible migrations in the repro, but it was already present in EF Core 10, not introduced in 11.These runs used PostgreSQL 18. The 11.0.0 runs used the .NET 11 RC2 runtime; the other repeated runs also used RC2 except where noted. I also reproduced the failure on Npgsql 10.0.0 / EF Core 10.0.0 with the original three-migration scenario. Race outcomes depend on scheduling. The EF Core 9 result does not establish safety for migrations containing transaction-suppressed operations.
Why: Npgsql's history repository has used a transaction-scoped
LOCK TABLEsince EF Core 9. EF reads and plans migrations once, but commits and reacquires the lock for subsequent migration transactions without refreshing the plan. Another instance can apply a migration in between.The key change appears to be EF Core's return to one transaction per migration in 10; EF 9 normally kept one transaction over the entire run. The provider's EF 11 changes in #3992 and #4071 concern history-table initialization and leave the locking strategy unchanged. The 11.0.0 repro also failed twice when the history table was created before the concurrent calls, so history-table creation is not necessary to trigger this race.
An adapted EF Core 11 SQLite repro, using a long-running SQLite query instead of
pg_sleep, succeeded. SQLite uses an explicitly released lock rather than a transaction-scoped one. SQL Server was not independently tested. The observed failure therefore affects Npgsql's locking strategy, although the stale-plan problem lies in shared EF migration execution and could affect other providers using transaction-scoped locks.Related work: dotnet/efcore#34439 discusses transaction-scoped migration locking and reading history under the lock, but is not the same reported race. No direct upstream duplicate was found. #4163 proposes a session advisory lock; as noted there, transaction-pooling proxies such as PgBouncer need consideration. An upstream history refresh might address this particular between-migrations race, but transaction-suppressed commands and partially executed migrations also need consideration.
minimal repro
Create a console project referencing
Npgsql.EntityFrameworkCore.PostgreSQL11.0.0 and setREPRO_PGto a connection string for a dedicated disposable database. This program deletes and recreates that database.#536: link the upstream reports for the S3 and S19 findings
#536: apply the workarounds for the S3 and S19 upstream bugs