Vector DBs & RAG (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
What is pgvector and how does it compare to dedicated vector databases?
senior LevelMost AskedCode ExampleDatabases & SQLPostgreSQL
In Simple Words:
pgvector is an open-source PostgreSQL extension that adds vector data types, distance operators (<->, <=>, <#>), and index types (HNSW, IVFFlat) to PostgreSQL:
- Pros: ACID compliance, joins with existing relational data in a single SQL query, no extra infrastructure to manage, familiar tooling.
- Cons: Slower index build times, higher memory footprint per vector, lack of native distributed horizontal sharding compared to Milvus or Pinecone.
- Recommendation: Perfect up to a few million vectors; consider dedicated vector engines for tens of millions of vectors or extreme sub-10ms latency requirements.
sql
-- pgvector SQL Example:
CREATE TABLE documents (
id BIGSERIAL PRIMARY KEY,
content TEXT,
embedding vector(1536)
);
CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
-- Query nearest 5 neighbors with metadata join:
SELECT id, content, 1 - (embedding <=> $1) AS similarity
FROM documents
WHERE tenant_id = 'org_44'
ORDER BY embedding <=> $1
LIMIT 5;