Vector Databases (Pinecone, pgvector)

A vector database stores vectors (embeddings) and finds the closest ones quickly. Exact search compares the question with every vector and is always right…

A library with 4 books needs no catalogue: you just look at each one. A library with a million books needs a catalogue that groups books by shelf. You only check the few shelves closest to the topic you want. That is faster, but sometimes the best book sits on the next shelf, the one you didn't open.

A vector database works like that catalogue, but for vectors (embeddings). It stores vectors and can find "the closest ones" quickly, even among millions of rows. In this picture, the books are vectors, and a shelf is a group of vectors that are close together. The heart of this lesson: exact (check everything) versus approximate (check part of it).

When an in-memory array is enough

Exact search compares the question with every vector, then takes the closest. The result is always right. The work: the dot product multiplies 1,536 numbers for each book, so 4 books × 1,536 = 6,144 multiplications per question, very light work for any server. So the Bookshop doesn't need a vector database yet. An in-memory array or a JSON column, as in the Semantic Search lesson, is enough.

Exact search's work grows in proportion to the number of rows. 10,000 books is already 15.4 million multiplications per question, and a million books 1.5 billion. At that point you need two things: storage that takes part in your database's transactions (changes that are all saved or all cancelled), and an index (an extra structure in the database) that doesn't check every row.

pgvector: vectors inside Postgres

If your data already lives in PostgreSQL, the pgvector extension (an add-on) adds a vector column type and nearest-neighbour search (finding the closest vectors). Its latest version is 0.8.7, released on 1 October 2026 (checked on pgvector's GitHub repo). The code below installs pgvector, adds a vector column to the books table, then finds the 10 books most similar to the question's vector $1:

CREATE EXTENSION vector;

ALTER TABLE books ADD COLUMN embedding vector(1536); -- text-embedding-3-small

UPDATE books SET embedding = '[0.021, -0.013, ...]' WHERE id = 4; -- 1,536 numbers from the API

SELECT id, title, (embedding <#> $1) * -1 AS similarity
FROM books
ORDER BY embedding <#> $1
LIMIT 10;

Distance operators: <->, <=>, <#>

pgvector has three distance operators, which compare two vectors:

Operator Meaning Smaller means
<-> L2 distance (straight line) closer
<=> cosine distance = 1 − cosine similarity more similar
<#> negative inner product (the dot product times −1) more similar

For vectors of length 1, such as OpenAI's embeddings, all three give the same order. The reason: for such vectors, L2² = 2 × (1 − cosine), so the bigger the cosine, the smaller all three numbers. An example from the Lab, for the 3 nearest neighbours of one question: <-> 0.9439 / 0.9848 / 0.9871, <=> 0.4455 / 0.4850 / 0.4871, <#> −0.5545 / −0.5150 / −0.5129.

pgvector's README recommends the inner product (<#>) for normalised vectors because it's the fastest. Be careful with the sign: <#> returns a negative value. So multiply by −1 to get the dot product, and use 1 - (embedding <=> $1) for cosine similarity.

Exact versus approximate: HNSW and IVFFlat

A vector index makes search approximate (ANN, approximate nearest neighbour: the nearest neighbours, roughly). It is far less work, but sometimes the true nearest neighbour is missed. Its accuracy is measured with recall@10: of the 10 true neighbours (the exact search result), what share was found. pgvector has two kinds of index:

Index How it works Settings (default)
HNSW a layered graph (linked points, in levels); better recall for the same speed, but a slower index build and more memory m 16, ef_construction 64; at query time hnsw.ef_search 40
IVFFlat vectors are split into lists groups, then a query checks the probes nearest groups; a fast index build, little memory lists (start at rows / 1000 up to 1 million rows, √rows above that); at query time ivfflat.probes 1 (start at √lists)

This code creates an IVFFlat index with 32 groups, then each query checks 4 groups. vector_ip_ops means the index is for the <#> operator:

CREATE INDEX ON books USING ivfflat (embedding vector_ip_ops) WITH (lists = 32);
SET ivfflat.probes = 4; -- or, with HNSW: SET hnsw.ef_search = 100;

The Lab tests IVFFlat on 2,000 random 32-dimension vectors, synthetic (not real embeddings) and the same each time they are made. There are 20 questions, and the Lab's version of IVFFlat uses 32 lists (groups):

Method Comparisons per question Recall@10
Exact 2,000 1.000
IVFFlat, probes 1 93.5 0.535
IVFFlat, probes 4 292.9 0.850
IVFFlat, probes 8 551.2 0.925
IVFFlat, probes 16 1,075.8 0.990

The more groups checked, the higher the recall, but the more work too. Recall on your own data can differ, so measure it yourself: compare the index's results with the exact search results.

Build an IVFFlat index after the table has data, because its groups are computed from the rows that exist. HNSW has no such step, so CREATE INDEX ON books USING hnsw (embedding vector_ip_ops); can be created while the table is still empty. Pick HNSW for a catalogue that keeps growing and needs high recall. Pick IVFFlat when a fast index build and little memory matter more.

Metadata filters: stock > 0 before or after ANN

Customers only want books that are in stock: WHERE stock > 0. This is called a metadata filter: a filter on an ordinary column next to the vector. pgvector's README says that with approximate indexes, the filter runs after the index is scanned. Its example: an HNSW index with hnsw.ef_search 40 hands over 40 candidates. If the condition matches 10% of rows, only 4 rows are left on average. Here are the results in the Lab (9% of rows have stock, 179 of 2,000):

Method Results from LIMIT 10 Recall@10 Comparisons
IVFFlat probes 1, filter after 5.15 0.235 93.5
IVFFlat probes 1, iterative scan 10 0.420 196.2
Filter first (B-tree index on stock), then exact 10 1.000 179

An iterative index scan (since pgvector 0.8.0: SET ivfflat.iterative_scan = relaxed_order; or SET hnsw.iterative_scan = strict_order;) keeps scanning the next part of the index until there are enough results. For conditions that match only a few rows, the README suggests starting with a plain index (a B-tree, Postgres's default index type) on the filter column: CREATE INDEX ON books (stock);. Postgres filters the rows first, then computes exact distances for the rows that are left. In the Lab that is more accurate and cheaper than the iterative scan.

Dedicated vector databases

Besides pgvector there are databases built just for vectors, for example Pinecone (a managed service: the provider runs the servers), and Weaviate and Chroma (open source). How they differ from an ordinary relational database (a database of tables, such as Postgres): they are optimised for similarity search over high-dimensional vectors (hundreds to thousands of numbers), with indexes such as HNSW. A relational database's B-tree index is built for exact or ordered values.

If you'd rather not run a Postgres server yourself, managed Postgres services such as Supabase also offer pgvector. The order of choice for the Bookshop: an in-memory array while there are few books, pgvector once the book data and its vectors need to live in one place, and a dedicated database if your needs really go beyond that. Measure recall and speed on your own data before moving.

⚠️ Indexes that aren't exact and filters that leave too few results

TL;DR: A vector database stores vectors (embeddings) and finds the closest ones quickly. Exact search compares the question with every vector and is always right. For 4 books (6,144 multiplications) an in-memory array is enough. pgvector (0.8.7, October 2026), a Postgres extension, puts a vector(1536) column in a table, and its indexes handle up to 2,000 dimensions. Its distance operators: <-> L2 (straight-line distance), <=> cosine distance, <#> negative inner product. For length-1 vectors such as OpenAI's embeddings the order of all three is the same, and <#> is the fastest. Approximate indexes check only part of the rows: HNSW (m 16, ef_construction 64, hnsw.ef_search 40) and IVFFlat (lists, ivfflat.probes default 1). Probes is the number of groups checked. In the Lab, probes 1 gives recall 0.535 (the share of true neighbours found) with 93.5 comparisons, probes 16 recall 0.990 with 1,075.8, and exact 2,000. A filter such as stock > 0 runs after the index scan, so results can be fewer than the LIMIT. Use an iterative scan (since 0.8.0), or filter first with a B-tree index. Dedicated vector databases (Pinecone, Weaviate, Chroma) are optimised for similarity search over high-dimensional vectors. Choose by data size and measurements, not trends.

⚡ When an in-memory array is enough

Meaning: Exact search compares the question's vector with every book's vector, so the result is always right. For 4 books with vectors of 1,536 numbers, that is only 4 × 1,536 = 6,144 multiplications per question, so an in-memory array is enough. A million books is 1.5 billion multiplications. That's where you need an index, which doesn't check every row.

⚡ pgvector: vectors inside Postgres

Write:

SELECT id, title, (embedding <#> $1) * -1 AS similarity
FROM books
ORDER BY embedding <#> $1
LIMIT 10;

Meaning: pgvector is a Postgres extension that adds a vector column. This query finds the 10 books whose vectors are most similar to the question's vector $1. <#> is the negative inner product (dot product): smaller means more similar, so multiply by -1 to get the dot product. For vectors of length 1, such as OpenAI's embeddings, the two other operators, <-> (straight-line distance) and <=> (cosine distance), give the same order.

⚡ Approximate: faster, sometimes off

Guess first: an IVFFlat index splits the vectors into groups, then checks only the groups closest to the question. In the Lab, what share of the 10 true nearest neighbours does IVFFlat find when it checks only 1 group (probes 1)?

0.535, just over half. With probes 16, recall@10 (the share found) rises to 0.990, but the number of comparisons also rises from 93.5 to 1,075.8.

Meaning: HNSW and IVFFlat indexes make search approximate: faster because they don't check every row, but sometimes a nearest neighbour is missed. The more groups checked, the higher the recall and the more work.

⚡ Databases built for vectors

Meaning: Pinecone, Weaviate and Chroma are databases built just for vectors. They are optimised for similarity search over high-dimensional vectors. A B-tree index in a relational database (a database of tables, such as Postgres) is built for exact or ordered values.

What you will learn