perf(postgres): use binary parameter for vector similarity queries#2949
Open
perf(postgres): use binary parameter for vector similarity queries#2949
Conversation
Pass the query embedding as positional $4 instead of interpolating it into the SQL string literal. asyncpg transmits the value via the register_vector binary codec.
Collaborator
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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.
Description
Replace string-interpolated embedding literals in vector similarity SQL queries with a positional
$4binary parameter. The query embedding is now transmitted via asyncpg'sregister_vectorbinary codec instead of being serialized to a comma-separated text literal and parsed back by PostgreSQL on every query.Changes Made
lightrag/kg/postgres_impl.pyembedding_stringinterpolation fromSQL_TEMPLATES(relationships,entities,chunks)'[{embedding_string}]'::{vector_cast}with$4::{vector_cast}in all three query templates"embedding": embeddingto the params dict passed todb.query()",".join(map(str, embedding))serialization calle.,r.,c.)Checklist
Additional Notes
With the old approach, a 1024-dim embedding was serialized to ~7 KB of text per query, sent to PostgreSQL, and re-parsed into a vector on every call. With
$4binary parameter +register_vectorcodec, asyncpg transmits the raw float bytes directly — no text serialization, no server-side text→vector parsing.Informal benchmarking showed a measurable latency improvement per query, particularly at higher vector dimensions.
Initial version drafted with Gemini-cli; refined with Claude code.