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.

Kindextension
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

Qpgvector or a dedicated vector database?
ABelow a few million vectors, and if you do not want to run another system, pgvector is a strong choice: vectors and business data live in the same database and the same transaction, which keeps JOINs and filtering simple. At larger scale, or when you need multi-tenant isolation and specialised retrieval algorithms, a dedicated vector database fits better.
QHNSW index creation is slow — what can I do?
AHNSW build cost grows with m and ef_construction, so start at something like m=16 and ef_construction=64, and once the index exists you can rebuild it with CREATE INDEX CONCURRENTLY to avoid locking the table for long. On small datasets IVFFlat builds faster and uses less memory.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏