PostgreSQL Enterprise (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
AI Vector Embeddings with `pgvector`.
senior LevelMost AskedCode ExamplePostgreSQL
In Simple Words:
The pgvector extension turns PostgreSQL into a high-performance Vector Database for AI and Retrieval-Augmented Generation (RAG):
- Stores embeddings up to 16,000 dimensions using the
vector(n)type. - Supports Cosine Distance (
<=>), L2 Euclidean Distance (<->), and Inner Product (<#>). - Accelerates Approximate Nearest Neighbor (ANN) search using HNSW (Hierarchical Navigable Small World) and IVFFlat indexes.
sql
CREATE EXTENSION vector;
CREATE TABLE document_embeddings (id BIGSERIAL PRIMARY KEY, content TEXT, embedding vector(1536));
CREATE INDEX ON document_embeddings USING hnsw (embedding vector_cosine_ops);
-- Find top 5 most semantically similar documents
SELECT content FROM document_embeddings ORDER BY embedding <=> '[0.012, -0.045, ...]' LIMIT 5;