Skip to content
Databases for Developers

Lesson 8 of 10 · 18 min

x
8/10

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

Vector Databases & AI Search

Vector databases store embeddings — high-dimensional numerical representations of text, images, or audio produced by machine learning models. Instead of exact keyword matching, vector search finds results that are semantically similar: searching for "heart attack" can return results about "myocardial infarction" because their embeddings are close in vector space.

The core operation is Approximate Nearest Neighbor (ANN) search. You embed the query using the same model that produced the stored embeddings, then find the K vectors in the database closest to the query vector. Pinecone, Weaviate, Qdrant, and pgvector (a Postgres extension) are the most common choices. pgvector is the simplest path if you already run Postgres — add the extension and a vector column, no new infrastructure needed.

Before
Keyword search (misses synonyms)
1SELECT * FROM documents2WHERE content ILIKE '%database performance%';3-- Misses: "query optimization", "slow queries", "index tuning"
After
Vector search (finds semantic matches)
1-- pgvector: find 5 semantically similar documents2SELECT id, title, content,3       embedding <=> $1 AS distance4FROM documents5ORDER BY embedding <=> $16LIMIT 5;7-- $1 = embedding of "database performance"8-- Finds: "query optimization", "slow queries", "index tuning"

Check your understanding

  • What does ANN search return?Show answer

    Answer

    Approximate nearest neighbors — vectors close to the query embedding in the chosen distance metric.
  • Why must query and stored embeddings use the same model?Show answer

    Answer

    Different models live in different vector spaces; mixing them makes distance meaningless.
  • When is pgvector a reasonable first step?Show answer

    Answer

    When you already run Postgres and want semantic search without standing up a separate vector service yet.
Previous

Progress is saved in this browser.

Next Lesson