Skip to content

Frameworks & SDKsGuide

pgvector in Practice: Embeddings and Vector Search in Postgres

Everything you need to run vector search inside Postgres: the vector type, distance operators, HNSW vs IVFFlat indexes, query-time recall tuning and filtered search with iterative scans.

AI Dev Hub Editorial

Automated quality gate

Published
Updated
Reading time
2 min read
  • 2 sources cited
  • 15 facts quote-verified

Overview

pgvector adds vector similarity search to Postgres, so embeddings can live next to the rows they describe and be queried with ordinary SQL, joins and transactions 1. For many retrieval-augmented generation (RAG) and recommendation workloads, that removes an entire system from the architecture. Managed Postgres providers such as Neon offer it as an extension you can enable per database 2.

Key concepts

Enable the extension and store vectors

sql
CREATE EXTENSION vector;

CREATE TABLE documents (
  id bigserial PRIMARY KEY,
  content text NOT NULL,
  embedding vector(1536)
);

The dimension in vector(1536) must match the output size of the embedding model you use.

Distance operators

pgvector provides these distance operators 1:

  • <-> — L2 (Euclidean) distance
  • <#> — negative inner product
  • <=> — cosine distance
  • <+> — L1 distance
  • <~> and <%> — Hamming and Jaccard distance for binary vectors

<#> returns the negative inner product because Postgres only supports ascending-order index scans on operators 1. For cosine similarity, subtract the distance from one:

sql
SELECT id, content, 1 - (embedding <=> $1) AS cosine_similarity
FROM documents
ORDER BY embedding <=> $1
LIMIT 5;

Practical walkthrough: indexing

By default pgvector performs exact nearest-neighbor search, which gives perfect recall 1. That is often fine for small tables. As data grows, add an approximate index. There are two types 1.

HNSW builds a multilayer graph. It has better query performance than IVFFlat in terms of the speed–recall trade-off, but slower build times and higher memory use. Because there is no training step, you can create it before the table has any data 1.

sql
CREATE INDEX ON documents USING hnsw (embedding vector_cosine_ops);

IVFFlat divides vectors into lists and searches only a subset of them. It builds faster and uses less memory than HNSW, but has lower query performance 1. Create it after the table has some data, and choose the number of lists with care — the README suggests starting at rows / 1000 for up to one million rows 1.

sql
CREATE INDEX ON documents USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);

Match the operator class to the operator in your query — vector_cosine_ops for <=>, vector_l2_ops for <-> and vector_ip_ops for <#> 1 — otherwise the planner cannot use the index.

Tune recall at query time

sql
SET hnsw.ef_search = 100; -- 40 by default; higher improves recall, lowers speed
SET ivfflat.probes = 10;  -- 1 by default; more probes improve recall

Both defaults come from the pgvector README 1.

Best practices

  • Filter with iterative scans. With approximate indexes, filtering is applied after the index is scanned, so a selective WHERE clause can return fewer rows than your LIMIT 1. Starting with version 0.8.0, iterative index scans keep scanning more of the index until enough results are found 1:
sql
SET hnsw.iterative_scan = strict_order;
  • Know the dimension limits. Indexed vector columns support up to 2,000 dimensions; the half-precision halfvec type supports up to 4,000 and halves storage 1.
  • Keep vectors next to the data. Store embeddings in the same table, or a one-to-one side table, so updates and deletes stay transactional.
  • Measure recall. Compare approximate results with exact search on a sample of real queries before and after tuning.

Pitfalls

  • Ordering by <=> while the index uses vector_l2_ops — the index is silently skipped.
  • Building an IVFFlat index on an empty table — its lists are poorly clustered; build after loading data 1.
  • Forgetting that <#> is a negative inner product — results sort in the opposite order to what you expect.

Related reading: before you embed content fetched from the web, make sure the fetcher cannot be abused — see SSRF-Safe Fetching in Node.js.

Frequently asked questions

Should I use HNSW or IVFFlat?

Default to HNSW for its better speed-recall trade-off. Choose IVFFlat when index build time or memory is the tighter constraint.

Do I need a vector index at all?

Not for small tables. Without an index pgvector runs exact nearest-neighbor search with perfect recall; add an approximate index when query latency starts to matter.

How many dimensions can pgvector index?

Indexed vector columns support up to 2,000 dimensions; the half-precision halfvec type supports up to 4,000.

Sources

Every factual claim above cites one of these documents, retrieved and snapshotted by our research pipeline.

  1. S1
    pgvector: Open-source vector similarity search for Postgres
    pgvectorOfficialRetrieved Sep 30, 2026
  2. S2
    The pgvector extension
    NeonHighRetrieved Sep 30, 2026

Frameworks & SDKsNews

OpenAI Python SDK v3.13.0 Introduces Agents API

OpenAI has released version 3.13.0 of its Python SDK, introducing a new Agents API. This update allows developers to integrate agent capabilities directly into their Python applications.

2 min readListen · 5 min

Frameworks & SDKsNews

Anthropic TypeScript SDK v0.131.0 Adds Spend Limits API

Anthropic has released version 0.131.0 of its TypeScript SDK, introducing a new API endpoint for listing spend limits and deprecating the 'sonnet 4.5' model from the client.

2 min readListen · 5 min

Frameworks & SDKsNews

OpenAI Node SDK v7.28.0 Adds Custom Voice, Agent Events

The OpenAI Node SDK v7.28.0, released on October 2, 2026, introduces new API features for custom voice creation and agent session events. This update expands the SDK's capabilities for developers building AI-powered applications.

1 min read