AI Fundamentals — How Modern AI Works›06 · Vector databases
Learning Hub / AI Fundamentals & AI-Assisted Engineering / AI Fundamentals — How Modern AI Works

Lesson 06 of 13 · Giving AI your knowledge

Vector databases

A vector database stores embeddings and finds the nearest ones to a question very fast, even among millions. What it stores, why it finds 'approximately' nearest results, why metadata filters and permissions matter, how it differs from a normal database, and the common options from a Postgres extension to managed services.

Foundation → Practitioner
Key wordsvector databasenearest neighbourapproximate nearest neighbourANNHNSWmetadata filteringhybrid searchpgvectorOpenSearchQdrantWeaviateMilvusPineconeChroma

What it stores

A vector database is a store for embeddings (lesson 5) plus what they came from:

id vector (embedding) text metadata
1 [0.12, -0.53, …] "To reset MFA, open Settings → Security…" source: help/mfa.md, team: support, updated: 2026-10-02
2 [0.40, 0.08, …] "VPN clients must use version 5 or later…" source: it/vpn.md, team: it, updated: 2026-06-11

Its main job is one question: "Which stored items are nearest to this query vector?"

Why "approximately" nearest

Comparing the question against every stored vector works for thousands of items but is too slow for millions. Vector databases build an index (a common one is HNSW, a graph of shortcuts between nearby points) that jumps quickly to the right neighbourhood. This is approximate nearest-neighbour (ANN) search: it returns the true nearest results almost every time, in milliseconds.

Finding the nearest café in a huge city by measuring the distance to every café would take forever. Instead you go to the right district, then the right street, then look around. You'll almost always find the nearest one, and it takes seconds. That's what the index does.

A search, step by step

"How do I reset two-factor?"
  │ 1. embed the question with the SAME embedding model used for the documents
  ▼
query vector
  │ 2. ANN search, top-k = 5
  │    + metadata filter: team IN user's groups, language = en
  ▼
5 nearest chunks (with scores and source links)
  │ 3. (optional) re-rank: a second model re-orders the 5 by relevance
  ▼
give them to the LLM to answer (RAG, lesson 7)

Metadata filters are not optional

  • Permissions. If a user cannot open a document, its chunks must not be retrieved for them. Filter by the user's groups at search time. This is the most common security mistake in AI search projects.
  • Freshness and scope. Filter by product, version, region or date so that old or irrelevant text does not crowd out the right answer.

Vector database or normal database?

Relational database Vector database
Typical question "Orders where status = 'open'" "Text most similar in meaning to this"
Matching Exact By similarity score
Index B-tree on columns ANN index on vectors

They are often used together, and many normal databases and search engines now include vector search. A sensible path:

  • Already on PostgreSQL and up to a few million chunks → pgvector.
  • Already run OpenSearch or Elasticsearch and want keyword plus vector (hybrid) search → use their vector features.
  • Very large scale or special features → a purpose-built database (Qdrant, Weaviate, Milvus) or a managed service.

What to ask about any vector store

  • How are permissions enforced, at query time?
  • How is the index kept up to date when documents change or are deleted?
  • Can it do hybrid search for exact codes and names?
  • What happens when we change embedding model (everything is re-embedded)?
  • Backups, encryption, and where the data lives.

Recap

  • A vector database stores embeddings, text and metadata, and finds the nearest items fast using an ANN index.
  • Always search with the same embedding model, and filter by permissions and scope.
  • Start with what you already run (pgvector, OpenSearch); move to a dedicated store when you need to.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.