{{ message }}
Repository navigation
Serialize vector index builds so two concurrent CREATE INDEX CONCURRENTLY cannot deadlock - #887
Merged
Merged
Conversation
…NTLY cannot deadlock Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: WaylandYang <145302500+WaylandYang@users.noreply.github.com>
WaylandYang
force-pushed
the
fix/serial-vector-index-builds
branch
from
September 23, 2026 10:10
0f4a429 to
772b1fa
Compare
This was referenced Sep 24, 2026
Merged
Merged
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.

Why
utopia-serverrunsbuild_vector_indexjobs with no serialization and worker concurrency defaults to 64. When two builds for the same table are claimed together (two embedding dimensions arriving at once), both issueCREATE INDEX CONCURRENTLYonchunks. Each CIC has to wait for every other transaction on the table to finish and each is such a transaction for the other; with ingest writing chunks alongside, Postgres reportsdeadlock detected(40P01), one build fails and is retried.vector_index::build's own comment says builds are meant to run one at a time, but nothing enforced it.Found while merging the store tests into one process (#886), where the same pair of statements deadlocked 3/3 times; reproduced in isolation at roughly a 50 % rate per attempt when a concurrent transaction touches
chunks, and never when the two builds start more than ~10 ms apart.What
buildtakes a session-level advisory lock (pg_try_advisory_lockpolled every 250 ms) on its held connection before the CIC work and releases it afterwards; if the release fails the connection is closed instead of returned to the pool so a stale lock cannot block future builds. Session level because CIC cannot run inside a transaction, so a transaction-scoped lock has nowhere to live. Cross-worker and cross-instance.Why try-lock and not the blocking
pg_advisory_lock: the blocking call is itself a statement holding a snapshot, and CIC's last phase waits for every older snapshot, so the queued build waited on the running build's lock while the running build waited on the queued build's snapshot: the same deadlock in a new place. Verified locally: the blocking variant failed 10/10, the polled try-lock passes 10/10.Test
two_dimensions_built_at_once_take_turnsintests/a_vector_index_is_built_by_a_job.rs: builds dims 11 and 13 concurrently under a concurrent transaction onchunks, eight rounds. Against the unfixed library it failed 5/5 runs with 40P01; with the fix 10/10 pass. The file stays a standalone test binary (see #886) because it does DDL on a shared table.Also re-ran
a_vector_build_uses_one_connection(pool of one connection: the lock is taken on the same held connection, so it still fits),the_nearest_chunk_is_found_however_it_is_reachedanda_batch_gathers_its_neighbours_in_order: 3/3 clean, andpg_locksshows no advisory lock left behind.cargo fmt --all --checkandcargo clippy -p utopia-store --all-targets -D warningsclean.🤖 Generated with Claude Code