Retrieval Augmented Generation (RAG) is the foundational architecture powering modern enterprise AI. To provide LLMs with relevant private context, text documents are converted into dense mathematical embeddings (high-dimensional floating-point arrays) and indexed in specialized vector storage. As datasets scale from thousands to millions of vectors, choosing between dedicated vector databases (Pinecone, Qdrant) and relational extensions (PostgreSQL with pgvector) dictates infrastructure complexity and query latency.
Vector Indexing Algorithms: HNSW vs. IVFFlat
Raw brute-force vector search (k-Nearest Neighbors - kNN) computes cosine similarity against every single vector in the database, yielding O(N) complexity that becomes unusable past 50,000 vectors. Approximate Nearest Neighbor (ANN) algorithms trade minute accuracy for 100x speedup:
| Indexing Algorithm | How It Works | Memory & Build Tradeoffs |
|---|---|---|
| HNSW (Hierarchical Navigable Small World) | Constructs a multi-layer geometric graph of vector proximities | Blistering query speed (<5ms); high RAM consumption during build phase |
| IVFFlat (Inverted File Flat) | Clusters vectors into Voronoi cells; only searches closest centroids | Fast build time and low RAM footprint; lower recall accuracy under heavy updates |
Comparative Evaluation Matrix
| Vector Engine | Architecture Model | Optimal Deployment Scenario |
|---|---|---|
| PostgreSQL + pgvector | Relational extension | Startups and enterprise teams already running PostgreSQL; zero new infrastructure needed |
| Pinecone | Fully managed proprietary cloud | Teams desiring zero DevOps overhead with automated serverless scaling |
| Qdrant | Open-source Rust standalone engine | High-throughput vector search requiring advanced payload filtering and self-hosting |
| Weaviate | Modular GraphQL-native vector engine | Complex multimodal datasets with built-in vectorization modules |
Vector Quantization & Memory Footprint Optimization
High-dimensional embedding models (such as text-embedding-3-large with 3072 dimensions) require substantial RAM when indexed with HNSW. At 1 million vectors, raw uncompressed 32-bit floating-point arrays consume over 12GB of memory just for index graphs.
Modern vector engines deploy Scalar Quantization (SQ) and Product Quantization (PQ), compressing 32-bit floats into 8-bit integers or binary bitmasks. This slashes memory consumption by 75% with less than a 1% degradation in top-k retrieval accuracy, enabling enterprise search across millions of documents on affordable cloud instances.
Implementing Hybrid Search with Reciprocal Rank Fusion (RRF)
Vector search alone struggles with exact keyword lookups (e.g., serial numbers, SKU codes, legal case IDs). The gold standard in modern enterprise RAG is **Hybrid Search**: combining dense vector semantic similarity with sparse BM25 keyword matching:
-- Hybrid Search in PostgreSQL using pgvector and full-text search
WITH semantic_search AS (
SELECT id, RANK() OVER (ORDER BY embedding <=> $1::vector) as rank_vec
FROM documents
LIMIT 20
),
keyword_search AS (
SELECT id, RANK() OVER (ORDER BY ts_rank_cd(to_tsvector('english', content), plainto_tsquery('english', $2)) DESC) as rank_kw
FROM documents
WHERE to_tsvector('english', content) @@ plainto_tsquery('english', $2)
LIMIT 20
)
SELECT
COALESCE(s.id, k.id) as doc_id,
COALESCE(1.0 / (60 + s.rank_vec), 0.0) + COALESCE(1.0 / (60 + k.rank_kw), 0.0) as rrf_score
FROM semantic_search s
FULL OUTER JOIN keyword_search k ON s.id = k.id
ORDER BY rrf_score DESC
LIMIT 5;
Frequently Asked Questions
Can pgvector scale to 10 million vectors?
Yes. With PostgreSQL 16/17 and the HNSW indexing engine coupled with halfvec (16-bit floating point quantization), pgvector comfortably searches 10 million+ vectors with sub-20ms latency on standard cloud hardware.
What is Hybrid Search?
Hybrid Search combines dense vector semantic search (capturing contextual meaning) with sparse lexical search (traditional BM25 keyword matching), using Reciprocal Rank Fusion (RRF) to deliver the most accurate RAG retrieval results.
Which embedding model is best in 2026?
OpenAI's text-embedding-3-small (1536 dims) offers the best price-to-performance ratio for commercial applications. For self-hosted air-gapped environments, open-weights models like BAAI BGE-M3 and Nomic-Embed lead open leaderboards.
Need Help Implementing This in Production?
Our senior engineering team helps startups and enterprises scale web applications, migrate cloud infrastructure, and build custom AI workflows with guaranteed delivery timelines.