Skip to content

perf(postgres): use binary parameter for vector similarity queries#2949

Open
wkpark wants to merge 1 commit intoHKUDS:devfrom
wkpark:perf-postgres-safe-param-dev
Open

perf(postgres): use binary parameter for vector similarity queries#2949
wkpark wants to merge 1 commit intoHKUDS:devfrom
wkpark:perf-postgres-safe-param-dev

Conversation

@wkpark
Copy link
Copy Markdown
Contributor

@wkpark wkpark commented Apr 18, 2026

Description

Replace string-interpolated embedding literals in vector similarity SQL queries with a positional $4 binary parameter. The query embedding is now transmitted via asyncpg's register_vector binary 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.py
    • Removed embedding_string interpolation from SQL_TEMPLATES (relationships, entities, chunks)
    • Replaced '[{embedding_string}]'::{vector_cast} with $4::{vector_cast} in all three query templates
    • Added "embedding": embedding to the params dict passed to db.query()
    • Removed per-query ",".join(map(str, embedding)) serialization call
    • Cleaned up unnecessary table alias prefixes (e., r., c.)

Checklist

  • Changes tested locally
  • Code reviewed
  • Documentation updated (if necessary)
  • Unit tests added (if applicable)

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 $4 binary parameter + register_vector codec, 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.

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.
@danielaskdd
Copy link
Copy Markdown
Collaborator

@codex review

@danielaskdd danielaskdd added the enhancement New feature or request label Apr 18, 2026
@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants