fix(v2): take the vector extension advisory lock in ainit_vectorstore_table - #335
Open
Mrxh (x1247897956) wants to merge 1 commit into
Open
Mrxh (x1247897956) wants to merge 1 commit into
Mrxh (x1247897956) wants to merge 1 commit into
Conversation
`PGEngine.ainit_vectorstore_table` ran `CREATE EXTENSION IF NOT EXISTS vector` without the advisory lock that the legacy `PGVector` path takes in `_create_vector_extension`. Postgres lets two concurrent sessions both pass the existence check and then collide on `pg_extension_name_index`, so concurrent initializations could fail with a duplicate key error. Take the same lock key as the legacy path so both code paths serialize against each other.
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.
What
PGEngine.ainit_vectorstore_tablenow takes the same transaction-level advisory lock that the legacyPGVectorpath already uses, before runningCREATE EXTENSION IF NOT EXISTS vector.Why
CREATE EXTENSION IF NOT EXISTSis not safe against concurrent sessions. Two sessions can both pass the existence check and then collide onpg_extension_name_index:The legacy path already guards against this —
_create_vector_extensioninlangchain_postgres/vectorstores.pytakespg_advisory_xact_lock(1573678846307946496)— but the v2 path ran the same statement with no lock at all, so it provided no protection, including against a v2 init racing a legacy init.This can be hit whenever a store is initialized concurrently, for example:
asyncio.gather'dainit_vectorstore_tablecallsPGVectorStoreinit racing a legacyPGVectorinitFixes #329
How
I deliberately reuse the legacy lock key rather than introducing a v2-specific one, so the two code paths serialize against each other instead of only within themselves. The lock is taken as a separate
execute()beforeCREATE EXTENSION: sincepg_advisory_xact_lockis held until the surrounding transaction ends, it covers theCREATE EXTENSIONbelow and thecommit()that follows it.Happy to move the key into a shared constant if you would rather not have it defined in both modules.
Testing
New regression test:
TestEngineAsync::test_init_table_waits_for_vector_extension_lock. It holds the advisory lock on a second session, asserts thatainit_vectorstore_tableblocks until the lock is released, then asserts the table was created.It fails on unpatched
main, i.e. it does reproduce the missing serialization:Commands run locally:
make test→754 passed, 25 skippeduv run pytest --disable-socket --allow-unix-socket tests/unit_tests/v2/test_engine.py→19 passeduv run ruff format --diff/uv run ruff check→ cleanuv run mypy langchain_postgres/v2/engine.py→Success: no issues found in 1 source file