pgvector Guide: Vector Search Inside PostgreSQL

Use pgvector for semantic search in PostgreSQL: installing, vector columns, distance operators, HNSW and IVFFlat indexes, filtering and tuning recall.

Published December 1, 2026

If you already run PostgreSQL, you may not need a separate vector database at all. The pgvector extension adds a vector type, distance operators and approximate nearest-neighbour indexes, so embeddings live next to the rows they describe, with transactions, joins, backups and access control you already have.

The concept

pgvector stores fixed-dimension vectors in a column, such as vector(1536). Three main distance operators cover common needs: <-> for Euclidean (L2) distance, <=> for cosine distance and <#> for negative inner product. Queries order by distance and take the top k, exactly like ordinary SQL.

Without an index, every query scans all vectors, which is exact but slow beyond tens or hundreds of thousands of rows. pgvector offers two approximate indexes. HNSW builds a multi-layer graph with excellent speed and recall, at the cost of slower builds and more memory. IVFFlat clusters vectors into lists and searches the closest lists; it builds faster and uses less memory but needs data present before building and tuning of probes.

Index options

IndexBuildQuery speed / recallTune with
None (exact)n/aExact, slow at scale-
HNSWSlower, more memoryFast, high recallm, ef_construction, hnsw.ef_search
IVFFlatFaster, needs data firstGood, depends on probeslists, ivfflat.probes

How HNSW searches

Layer 2: few long linksLayer 1Layer 1dense layer 0dense layer 0dense layer 0
HNSW searches from sparse upper layers down to dense lower layers, narrowing to the nearest neighbours.

Why indexes matter

Exact scan (1M rows)≈900 msIVFFlat probes=10≈25 msHNSW ef_search=40≈8 ms
Illustrative query latency for 1M 768-d vectors; depends on hardware and settings.

Practical examples

Set up a table and HNSW index

Cosine distance is common for text embeddings.

CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE documents (
  id        bigserial PRIMARY KEY,
  tenant_id int NOT NULL,
  content   text NOT NULL,
  embedding vector(768) NOT NULL
);
CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64);

Query with a filter

Combine vector search with ordinary WHERE clauses.

SET hnsw.ef_search = 100;            -- higher = better recall, slower
SELECT id, content, 1 - (embedding <=> $1) AS similarity
FROM documents
WHERE tenant_id = $2
ORDER BY embedding <=> $1
LIMIT 10;

Insert from Python

pgvector has client helpers for psycopg and others.

import psycopg
from pgvector.psycopg import register_vector
conn = psycopg.connect(DSN); register_vector(conn)
conn.execute("INSERT INTO documents (tenant_id, content, embedding) VALUES (%s, %s, %s)",
             (42, text, embedding))            # embedding: numpy array or list

Common mistakes

  • Using the wrong operator class for the distance you query with (index ignored).
  • Building IVFFlat on an empty table, producing poor clusters.
  • Very selective filters with HNSW returning fewer than k rows; consider iterative scans or partial indexes per tenant.
  • Forgetting maintenance_work_mem for faster index builds on large tables.

Key takeaways

  • pgvector adds vectors, distance operators and ANN indexes to PostgreSQL.
  • HNSW: best default for speed and recall; IVFFlat: faster builds, less memory.
  • Match the index operator class to your query operator.
  • Tune ef_search or probes to trade recall for speed.

FAQ

How many vectors can pgvector handle?

Millions comfortably on a well-sized server; beyond that, consider partitioning, quantized vector types, or a dedicated vector database.

Does pgvector support half-precision?

Recent versions include halfvec and binary/sparse vector types that reduce storage and memory.

Can I use it on managed Postgres?

Most major managed PostgreSQL services offer pgvector as an available extension.

More AI articles