|
|
pgvector with Spring Boot
Author: Venkata Sudhakar
pgvector is a PostgreSQL extension that adds a vector column type and cosine similarity operators directly to PostgreSQL. Instead of running a separate vector database like Pinecone, you store embeddings right alongside your existing application data. This simplifies your infrastructure - one database, one backup strategy, one connection pool. pgvector supports HNSW and IVFFlat indexes for fast approximate nearest neighbour search across millions of vectors. With Spring Boot and JdbcTemplate you can use pgvector without any special ORM. Store embeddings as float arrays, insert them as PostgreSQL array literals cast to vector, and query with the pgvector operators: the <=> operator for cosine distance (ideal for text embeddings from OpenAI). The result is ordered from most to least similar - you take the top K rows as your RAG context documents. The below example shows enabling pgvector, creating a documents table, and a Spring Boot repository that stores and searches embeddings using JdbcTemplate.
It gives the following output,
id | content | similarity
----+---------------------------------------------+------------
1 | Debezium reads the MySQL binlog for CDC | 0.9812
3 | CDC replication lag measurement | 0.8934
5 | Kafka Connect for CDC pipelines | 0.7621
Usage in a RAG pipeline,
// Embed the user question
float[] queryVec = openAiClient.embed("how does log-based replication work?");
// Retrieve top 3 most relevant docs from PostgreSQL
List context = documentRepository.findSimilar(queryVec, 3);
// Pass context to LLM for generation
// Output: relevant CDC/Debezium documents retrieved correctly
Choose pgvector when your vector data belongs with relational data, your corpus is under a few million documents, and you want minimal infrastructure. Use a dedicated vector database (Pinecone, Qdrant, Weaviate) when you need billion-scale search, multi-tenancy, or managed filtering at very high throughput. For most enterprise RAG pipelines with hundreds of thousands of documents, pgvector on a managed Cloud SQL or RDS instance is fully capable and far simpler to operate.
|
|