Repository navigation
Hold the migration lock for the whole migration run - #4163
Open
PieterjanDeClippel wants to merge 1 commit into
Open
PieterjanDeClippel wants to merge 1 commit into
PieterjanDeClippel wants to merge 1 commit into
Conversation
NpgsqlHistoryRepository locked the history table with LOCK TABLE ... ACCESS EXCLUSIVE and LockReleaseBehavior.Transaction, so the lock ended with each migration's transaction. The migrator plans the whole run from the history it reads under the first lock, and reacquires the lock per migration without re-reading it. Another instance could apply the next migration in between, and the first instance then applied it again. Take a session-level pg_advisory_lock with LockReleaseBehavior.Connection instead, keyed on the history table's schema-qualified name, and release it with pg_advisory_unlock, as SQL Server does with sp_getapplock. Fixes npgsql#4162 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PieterjanDeClippel
added a commit
to MintPlayer/MintPlayer.Spark
that referenced
this pull request
Nov 24, 2026
S3: synchronize scaffolds on the context that ran HasPendingModelChanges(), through FreshSnapshotMigrationsAssembly in the design-time services (dotnet/efcore#36016, fix upstream in dotnet/efcore#39784). Rerun: every version scaffolds the expected operations on both databases; the trap still reproduces without the decorator. S19: the PostgreSQL provider registers AdvisoryLockHistoryRepository, a session advisory lock held for the whole Migrate() run (npgsql/efcore.pg#4162, fix upstream in npgsql/efcore.pg#4163). New scenario 1d: three instances apply each migration once with EF's lock alone. D33's guard stays for CREATE DATABASE and the data migrations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PieterjanDeClippel
added a commit
to MintPlayer/MintPlayer.Spark
that referenced
this pull request
Nov 25, 2026
…tes them - Framework schema (D24) in schema `spark` with its own history and hand-written migrations (SparkFrameworkDbContext, SparkFramework0001); SparkDbContext maps the same tables excluded from migrations. - RelationalSchemaMigrator (ISparkSchemaMigrator, D33): the dialect's guard in the maintenance database (sp_getapplock / pg_advisory_lock), database creation, framework then app migrations, a refusal naming --spark-synchronize-model when the model has changes no migration covers, and the idempotent script. PostgreSQL registers AdvisoryLockHistoryRepository (S19, until npgsql/efcore.pg#4163). - RelationalSchema: CreateFromModelAsync / ApplyDifferencesAsync for test databases; tests no longer use EnsureCreated. - SparkMigrationsModelDiffer: a required collection column added to existing rows gets the empty collection as its SQL default (found by D16: PostgreSQL refused the column). D16 now passes on SQL Server and PostgreSQL. - MintPlayer.Spark.Relational.Design (O17): RelationalSchemaScaffolder, found by core through SparkSchemaDesignAssembly + [assembly: SparkSchemaScaffolder]; --spark-synchronize-model writes the migration into Migrations/<Provider>. FreshSnapshotMigrationsAssembly (dotnet/efcore#36016) and the recreated-table refusal; DevelopmentDependency, EF Design PrivateAssets=none, buildTransitive Publish=false + SPARKDESIGN001. - Relational suite 100/100 against SQL Server and PostgreSQL; Spark.Tests command and layering classes green. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PieterjanDeClippel
added a commit
to MintPlayer/MintPlayer.Spark
that referenced
this pull request
Nov 26, 2026
S3: synchronize scaffolds on the context that ran HasPendingModelChanges(), through FreshSnapshotMigrationsAssembly in the design-time services (dotnet/efcore#36016, fix upstream in dotnet/efcore#39784). Rerun: every version scaffolds the expected operations on both databases; the trap still reproduces without the decorator. S19: the PostgreSQL provider registers AdvisoryLockHistoryRepository, a session advisory lock held for the whole Migrate() run (npgsql/efcore.pg#4162, fix upstream in npgsql/efcore.pg#4163). New scenario 1d: three instances apply each migration once with EF's lock alone. D33's guard stays for CREATE DATABASE and the data migrations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PieterjanDeClippel
added a commit
to MintPlayer/MintPlayer.Spark
that referenced
this pull request
Nov 26, 2026
…tes them - Framework schema (D24) in schema `spark` with its own history and hand-written migrations (SparkFrameworkDbContext, SparkFramework0001); SparkDbContext maps the same tables excluded from migrations. - RelationalSchemaMigrator (ISparkSchemaMigrator, D33): the dialect's guard in the maintenance database (sp_getapplock / pg_advisory_lock), database creation, framework then app migrations, a refusal naming --spark-synchronize-model when the model has changes no migration covers, and the idempotent script. PostgreSQL registers AdvisoryLockHistoryRepository (S19, until npgsql/efcore.pg#4163). - RelationalSchema: CreateFromModelAsync / ApplyDifferencesAsync for test databases; tests no longer use EnsureCreated. - SparkMigrationsModelDiffer: a required collection column added to existing rows gets the empty collection as its SQL default (found by D16: PostgreSQL refused the column). D16 now passes on SQL Server and PostgreSQL. - MintPlayer.Spark.Relational.Design (O17): RelationalSchemaScaffolder, found by core through SparkSchemaDesignAssembly + [assembly: SparkSchemaScaffolder]; --spark-synchronize-model writes the migration into Migrations/<Provider>. FreshSnapshotMigrationsAssembly (dotnet/efcore#36016) and the recreated-table refusal; DevelopmentDependency, EF Design PrivateAssets=none, buildTransitive Publish=false + SPARKDESIGN001. - Relational suite 100/100 against SQL Server and PostgreSQL; Spark.Tests command and layering classes green. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4162
Concurrent
Migrate()calls could apply the same migration twice.Cause:
NpgsqlHistoryRepositorytakesLOCK TABLE "__EFMigrationsHistory" IN ACCESS EXCLUSIVE MODEwithLockReleaseBehavior.Transaction, so the lock ends with each migration's transaction.Change:
pg_advisory_lockwithLockReleaseBehavior.Connection, so the migrator takes it once, before reading the history, and holds it for the whole run. This is how SQL Server works withsp_getapplock.pg_advisory_unlockwhile the connection is open. If the connection closes, PostgreSQL releases the lock.AcquiringMigrationLockis now logged on the async path as well.Test:
MigrationsInfrastructureNpgsqlTest.Can_apply_several_migrations_in_parallel(sync and async) runs fourMigrate()calls together over three migrations; the second migration sleeps one second.mainwithout the change, both variants fail:42701: column "Email" of relation "People" already existsand42701: column "Phone" ....MigrationsInfrastructureNpgsqlTestandNpgsqlHistoryRepositoryTest(56 of 56, PostgreSQL 18 Testcontainers image).The existing parallel tests in
MigrationsInfrastructureTestBasemigrate a single migration at a time, so they never reached this race.Standalone repro, plus the same change as a
ReplaceServiceworkaround for 11.0.0: https://github.com/PieterjanDeClippel/efcore-npgsql-migration-lock-raceTrade-off: a session-level lock does not hold through PgBouncer in transaction pooling mode, which
LOCK TABLEinside a transaction does. If you would rather keep the transaction-scoped lock, the alternative is in EF Core's migrator: re-read the history after each reacquired lock, which would fix every provider that usesLockReleaseBehavior.Transaction.🤖 Generated with Claude Code