pgvector: Vector Type and Approximate Nearest-Neighbor Search
Last updated: 2026-11-04
pgvector: Vector Type and Approximate Nearest-Neighbor Search
pgvector adds a vector type and a family of distance operators to PostgreSQL, letting you store embeddings in an ordinary table and run approximate nearest-neighbor (ANN) search over an HNSW or IVFFlat index. A query reads as ORDER BY embedding <=> '[...]' LIMIT k, where <=> is cosine distance. Vector recall and business filters can be combined in a single statement, so you need not bolt on a separate vector database. Note that pgvector is a third-party extension rather than an official PostgreSQL contrib module.
| Kind | extension |
|---|---|
| Example Language | SQL |
📝 Syntax
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE t_docs (
id bigserial PRIMARY KEY,
content text,
embedding vector(3)
);
CREATE INDEX idx_docs_embedding
ON t_docs USING hnsw (embedding vector_cosine_ops);
SELECT id, embedding <=> '[0.1,0.2,0.3]' AS distance
FROM t_docs
ORDER BY embedding <=> '[0.1,0.2,0.3]'
LIMIT 3;
Returns:CREATE EXTENSION returns success on completion. A vector column stores an array of float4 values; <=> returns cosine distance, <-> returns L2 (Euclidean) distance and <#> returns negative inner product — all double precision, smaller meaning more similar.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SELECT indexname, indexdef FROM pg_indexes WHERE tablename = 't_docs' ORDER BY indexname
indexname | indexdef
---------------------+-----------------------------------------------------------------------
idx_docs_embedding | CREATE INDEX idx_docs_embedding ON public.t_docs USING hnsw (embedding vector_cosine_ops)
t_docs_pkey | CREATE UNIQUE INDEX t_docs_pkey ON public.t_docs USING btree (id)
(2 rows)
SELECT id, content,
round((embedding <=> '[0.1,0.2,0.3]')::numeric, 6) AS cosine_distance
FROM t_docs ORDER BY embedding <=> '[0.1,0.2,0.3]' LIMIT 3
id | content | cosine_distance
----+--------------------+-----------------
1 | PostgreSQL extension | 0.000000
2 | MySQL replication | 0.285714
3 | vector search | 0.337151
(3 rows)
📌 Key Points
- In browser PGlite it must be loaded on demand: pgvector is not in the main package's contrib set, so install @electric-sql/pglite-pgvector separately, then import { vector } from '@electric-sql/pglite-pgvector' and new PGlite({ extensions: { vector } }).
- Size: the pgvector WASM bundle is about 50 KB — larger than pg_trgm (~15 KB), btree_gist (~22 KB) and uuid-ossp (~17 KB), but still far smaller than pgcrypto at ~1.1 MB.
- Distance operators: <=> cosine distance, <-> L2 Euclidean distance, <#> negative inner product. The index operator class must match the operator you query with (vector_cosine_ops / vector_l2_ops / vector_ip_ops); a mismatch means the index is never used.
- HNSW is a graph index: fast queries and high recall, but slow to build and memory-hungry. IVFFlat needs a decent amount of training data (a few thousand rows is a reasonable floor) before it is created, otherwise the cluster centroids are poor.
- The 3 in vector(3) is the dimension and a mismatch is a hard error; the dimension must be fixed before you can index the column. In practice use something like vector(768) to match your embedding model.
- HNSW recall is not 100%; raise hnsw.ef_search to trade speed for accuracy, and when you need exact results use SET LOCAL enable_indexscan = off to force a sequential scan.