Repository navigation
fix: serialize InitializeDb across concurrent instances - #47
Merged
Merged
Conversation
Run the initialization DDL in a transaction that first takes a database-wide lock (sp_getapplock on SQL Server, pg_advisory_xact_lock on PostgreSQL), so instances starting at the same time wait for each other instead of failing with "object already exists" or a unique violation in the PostgreSQL system catalogs. Add a concurrency test per provider (10 parallel calls on a new table, 5 rounds) and document the behavior in the README. Closes #20
…rrent-initializedb
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.
Summary
InitializeDb()failed when several instances started at the same time: concurrent existence checks all passed and the laterCREATEstatements failed.23505 duplicate key value violates unique constraint "pg_class_relname_nsp_index".The DDL now runs in a transaction that first takes a database-wide lock, so concurrent calls wait for each other:
sp_getapplock(exclusive, owned by the transaction); a negative result raises a clear error.pg_advisory_xact_lock(hashtext(...)).The lock name is a private constant shared by all tables in the database; initialization is short, so serializing different tables is harmless. A blocked call waits up to the command timeout (30 s by default).
Tests
New
InitializeDb_ConcurrentCallsOnNewTable_AllSucceedper provider: 10 repositories callInitializeDb()together (released by aBarrieron dedicated threads) on a new table, 5 rounds. The connection pool is warmed first; with cold connections the calls are staggered and the race rarely shows.Docs
README Configuration section notes that concurrent startup is safe.
Closes #20