AI & ML

Vector Database

A specialized database optimized for storing and querying high-dimensional vector embeddings using similarity search algorithms.

A vector database is purpose-built for storing, indexing, and querying vector embeddings at scale. Unlike traditional databases that match exact values, vector databases find the most similar vectors using distance metrics like cosine similarity or Euclidean distance.

When you store a million document embeddings, a vector database can find the 10 most similar documents to a query vector in milliseconds using approximate nearest neighbor (ANN) algorithms like HNSW, IVF, or ScaNN.

Popular vector databases include Pinecone, Weaviate, Qdrant, Milvus, and ChromaDB. PostgreSQL also supports vector search via the pgvector extension. Each has different tradeoffs around scalability, cost, filtering capabilities, and deployment options.

Vector databases are a critical component of RAG systems, recommendation engines, image similarity search, and any application that needs to find semantically similar content quickly.

How it works

A vector database stores embeddings (arrays of numbers) alongside ordinary fields and answers "which stored vectors are closest to this one?". Closeness is a distance function. The pgvector extension for Postgres, for example, offers L2 (Euclidean) distance, negative inner product, cosine distance and L1 distance as separate operators.

Comparing a query with every stored vector is exact but slow at scale, so most systems build an approximate nearest neighbour (ANN) index. HNSW (Hierarchical Navigable Small World), described by Malkov and Yashunin, organises vectors into layered proximity graphs; a search starts in the sparse upper layers and descends, which the authors report gives logarithmic complexity scaling.

Index types trade build cost against query speed. pgvector's documentation says IVFFlat "has faster build times and uses less memory than HNSW, but has lower query performance (in terms of speed-recall tradeoff)". Recall is the share of the true nearest neighbours a search returns.

Approximate means some true matches can be missed. pgvector notes that an approximate index "trades some recall for speed", so results may differ from an exact scan. Index parameters are tuned to hit the recall an application needs at an acceptable latency.

Example

A minimal vector table and nearest-neighbour query in Postgres with pgvector, adapted from the project's README. Real embeddings have hundreds or thousands of dimensions; three keep the example readable.

CREATE EXTENSION vector;
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));

INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');

-- five nearest rows by L2 distance (<->); <=> would use cosine distance
SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;

-- approximate index for large tables
CREATE INDEX ON items USING hnsw (embedding vector_l2_ops);

Without the index, Postgres compares the query with every row and the result is exact. With the HNSW index, queries get much faster on large tables but may occasionally miss a true nearest neighbour.

Common confusion

Do you need a dedicated vector database, or can a regular database do it?

Many general-purpose databases now support vector search, Postgres through pgvector being a common example, and keeping vectors next to the rest of the data simplifies filtering and joins. Dedicated vector databases focus on larger scale and vector-specific features. The right choice depends on data size, query load and operational preferences.

What is the difference between a vector database and vector embeddings?

Embeddings are the numbers an embedding model produces for a piece of text or an image. The vector database is where those numbers are stored, indexed and searched. Changing the embedding model means re-embedding and re-indexing the data.

Further reading

Want to learn more?

Explore more developer terms or read in-depth articles on the blog.

Browse all terms