Add HNSW indexes for scalable vector similarity search - #304
Open
dhruvi-16-me wants to merge 2 commits into
Open
Add HNSW indexes for scalable vector similarity search#304dhruvi-16-me wants to merge 2 commits into
dhruvi-16-me wants to merge 2 commits into
Conversation
Contributor
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. 🗂️ Base branches to auto review (2)
Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Overview
This PR improves the performance of semantic retrieval using HNSW (Hierarchical Navigable Small World) indexes provided by
pgvector.Previous PR established the embedding infrastructure by generating and storing embeddings for meetings, tasks, and tickets. While semantic search was already functional, vector similarity queries would become increasingly expensive as the dataset grows because PostgreSQL would need to compare the query embedding against every stored embedding.
This PR introduces HNSW indexes to enable efficient Approximate Nearest Neighbor (ANN) search without changing the existing retrieval logic or application behavior.
Motivation
Before this change, vector similarity queries relied on brute-force comparisons over all available embeddings.
Although this approach is acceptable for small datasets, it does not scale well as more meetings, tasks, and tickets are added.
By introducing HNSW indexes, PostgreSQL can efficiently locate embeddings that are close to the query vector instead of comparing against every row.
This optimization is purely infrastructural and prepares the semantic search layer for larger datasets while preserving the current API and retrieval behavior.
Changes
1. Added HNSW vector indexes
Created HNSW indexes for embedding columns using cosine similarity (
vector_cosine_ops).Indexed columns:
meetings.summary_embeddingtasks.description_embeddingtickets.description_embeddingThese indexes optimize existing vector similarity queries without modifying query semantics.
2. Benchmark script
Added:
This script contains reusable benchmark queries using:
to inspect execution plans and measure retrieval performance for:
The benchmark allows developers to verify query planning and evaluate vector search performance as datasets grow.
3. Verification script
Added:
This script verifies:
pgvectorextension is installedThis provides a repeatable post-deployment validation process.
Screenshots
Verified creation of HNSW indexes. The query confirms that HNSW indexes were successfully created on the embedding columns used for semantic retrieval (summary_embedding for meetings and description_embedding for tasks and tickets). These indexes enable PostgreSQL to perform efficient Approximate Nearest Neighbor (ANN) searches as the dataset grows.
Verified pgvector installation. This confirms that the pgvector extension is available in the database, which is required for storing embedding vectors and creating HNSW indexes.
Benchmark executed successfully. The benchmark queries completed successfully, confirming that vector similarity searches execute correctly after the migration. On the current development dataset, PostgreSQL selected a Sequential Scan because the tables contain very few embedded rows. This is expected planner behavior; HNSW indexes become beneficial as the dataset grows.
What did NOT change
This PR intentionally does not modify retrieval behavior
The only change is how PostgreSQL internally executes existing vector similarity queries.
Validation
The following validation steps were performed:
pgvectorextension is available.EXPLAIN (ANALYZE, BUFFERS).During benchmarking, PostgreSQL may choose a Sequential Scan on very small datasets. This is expected planner behavior and does not indicate a problem with the indexes. As the number of embedded rows increases, PostgreSQL will begin favoring HNSW index scans where they provide a lower estimated execution cost.
Impact
This PR improves the scalability of semantic retrieval while keeping existing functionality unchanged.
Before
After
From the application's perspective, the returned results remain the same; only the database execution strategy becomes more efficient.
✅ Checklist