short · 3 min
pgvector
A Postgres extension that stores vectors in a normal column and searches them with SQL, and where it runs out.
updated 27 Sept 2026
If we already run Postgres, do we need a separate vector database to search embeddings? Often not. pgvector is an open-source Postgres extension that adds a vector column type and nearest-neighbour search. The embeddings sit in the same table as the text and ids they belong to, and we query them with SQL.
It's just a column
Say we're storing article chunks:
CREATE EXTENSION vector;
CREATE TABLE chunks (
id bigserial PRIMARY KEY,
node_id text,
body text,
embedding vector(1024)
);
To find the five chunks closest to a question, we embed the question and sort by distance:
SELECT id, node_id, body
FROM chunks
ORDER BY embedding <=> $1
LIMIT 5;
<=> is cosine distance (<-> is straight-line L2, <#> negative inner
product). If your vectors are normalized to length 1, inner product ranks
the same as cosine and is the fastest.
With no index, that query is exact. Postgres compares the question with every row and returns the true five nearest. For a small table, a few thousand chunks say, that's the right place to start, and you can speed it up by letting Postgres use more parallel workers.
Adding an index
When exact gets too slow, we add an approximate HNSW index:
CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops);
Two things catch people here. The index has to match the operator we query
with (vector_cosine_ops for <=>), one index per distance. And the query
has to keep its shape: ORDER BY the distance operator, ascending, with a
LIMIT. Change that and Postgres quietly skips the index.
The defaults are 16 links per vector (m), 64 build candidates
(ef_construction) and 40 search candidates (hnsw.ef_search). We can
raise ef_search for one query with SET LOCAL inside a transaction. In
one 2024 benchmark on a million 1,536-number vectors, ef_search at 40
found 96.8% of the true nearest neighbours; at 200 it found 99.6%, at a
bit over a quarter of the speed.
Where it runs out
Big vectors. A column can hold up to 16,000 numbers, but an index only
covers 2,000. Past that, index them as halfvec, 2-byte floats, up to
4,000. That's nearly free: in the same benchmark it halved the index (from
about 7.7 GB to 3.9 GB) with the same recall. Indexing as bits goes up to
64,000 and saves far more, but recall can collapse unless you re-sort the
candidates with the full vectors afterwards. On one dataset it fell to
about 2%.
Filters. This one surprises people. Add a WHERE clause and the
approximate index filters after it scans. If the filter matches 10% of
rows and the index hands back its default 40 candidates, about 4 survive.
We asked for 10 and got 4.
Since version 0.8.0, iterative scans fix this by scanning more of the
index until enough rows pass, up to a limit (hnsw.max_scan_tuples).
A normal index on the filter column often works too, giving fast exact
search over just the matching rows.
Many customers in one index. One tenant's vectors can change recall and speed for another, so give them separate partitions or tables.
The surprise in your tests
A normal Postgres index only makes a query faster. An HNSW index can
return different rows than the same query without it, which is the recall
trade from vector indexes. So tests that check exact
outputs can start failing the day you add it. The useful habit is to check
recall yourself: run the same queries with
SET LOCAL enable_indexscan = off to get the exact answer, and compare.
(The benchmark numbers above came from pgvector 0.7.0 on one big server in 2024. The latest release is 0.8.6, from 2026-07-29. Your numbers will differ.) For keyword search next to the vectors, see hybrid search.
Further reading
- pgvector, Andrew Kane and contributors, 0.8.6 (2026). The README is the reference for types, operators, defaults and filtering. Keep it open while you build.
- Scalar and Binary Quantization for Pgvector Vector Search and Storage,
Jonathan Katz, 2024. Real recall, speed and size numbers for full,
half-size and bit indexes. Read it before you pick
halfvecor bits.