pgvector — Vector Similarity Search Inside PostgreSQL
A PostgreSQL extension that adds a native `vector` type, HNSW and IVFFlat indexes, and distance operators so semantic search, RAG and recommendation workloads can reuse the same database as the rest of the app.
Instalación lista para agent
Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.
npx -y tokrepo@latest install 121fb0d5-3920-11f1-9bc6-00163e2b0d79 --target codexEjecutar después de confirmar el plan con dry-run.
What it is
pgvector is a PostgreSQL extension that adds a native vector data type, HNSW and IVFFlat indexes, and distance operators for similarity search. It lets you run semantic search, RAG retrieval, and recommendation workloads inside the same database as your application data.
The extension targets teams that already use PostgreSQL and want to add vector search without deploying a separate vector database. It keeps your operational complexity low by reusing your existing database infrastructure.
The project is actively maintained and suitable for both individual developers and teams looking to integrate it into their existing toolchain. Documentation and community support are available for onboarding.
How it saves time or tokens
pgvector eliminates the need to sync data between PostgreSQL and a dedicated vector store. Embeddings live in the same table as your application data, enabling SQL JOINs between vectors and structured data. Transactional consistency means your vector index stays in sync with your data automatically. One backup, one monitoring stack, one connection pool.
How to use
- Install the pgvector extension:
CREATE EXTENSION vector; - Add a vector column to your table:
ALTER TABLE items ADD COLUMN embedding vector(1536); - Insert embeddings generated by your embedding model (OpenAI, Cohere, local models).
- Create an index and query by similarity using distance operators.
Example
-- Enable the extension
CREATE EXTENSION vector;
-- Create a table with embeddings
CREATE TABLE documents (
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT,
embedding vector(1536)
);
-- Create an HNSW index for fast approximate search
CREATE INDEX ON documents
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64);
-- Find the 5 most similar documents
SELECT id, title, 1 - (embedding <=> $1::vector) AS similarity
FROM documents
ORDER BY embedding <=> $1::vector
LIMIT 5;
Related on TokRepo
- AI Tools for RAG — RAG frameworks and retrieval tools that work with pgvector.
- AI Tools for Database — Database tools and extensions for AI workloads.
Common pitfalls
- Not creating an index before querying. Without an HNSW or IVFFlat index, pgvector does exact (brute-force) search, which is slow for tables with more than 10,000 rows.
- Using the wrong distance operator.
<=>is cosine distance,<->is L2 distance,<#>is inner product. Match the operator to how your embeddings were trained. - Storing embeddings with the wrong dimension. The vector column dimension must match your embedding model's output dimension exactly. A mismatch causes insertion errors.
- Not reading the changelog before upgrading. Breaking changes between versions can cause unexpected failures in production. Pin your version and review release notes.
Preguntas frecuentes
pgvector runs inside PostgreSQL, so there is no additional infrastructure. Pinecone and Milvus are purpose-built vector databases with more advanced features (filtering, sharding, hybrid search) at very high scale. pgvector is sufficient for most applications with up to millions of vectors.
HNSW is recommended for most use cases. It offers better query performance with slightly higher memory usage. IVFFlat uses less memory but requires a separate training step and may have lower recall. HNSW is the default choice.
Yes. pgvector with HNSW indexes handles millions of vectors with sub-10ms query latency on typical hardware. For tens of millions of vectors, consider dedicated vector databases or sharding strategies.
Yes. You can combine vector similarity search with standard SQL WHERE clauses. PostgreSQL's query planner uses the HNSW index alongside standard B-tree indexes for filtered queries.
Use an embedding model (OpenAI text-embedding-3-small, Cohere embed, or open-source models via Hugging Face). Generate embeddings in your application code and insert them as vector values. pgvector stores and indexes them; it does not generate embeddings itself.
Referencias (3)
- pgvector GitHub— Native vector type and similarity search for PostgreSQL
- pgvector README— HNSW and IVFFlat index support
- PostgreSQL Documentation— PostgreSQL extension ecosystem
Relacionados en TokRepo
Discusión
Activos relacionados
Faiss — Efficient Similarity Search and Clustering of Dense Vectors
Faiss is a library from Meta AI Research for efficient similarity search and clustering of dense vectors, optimized for billion-scale datasets with GPU acceleration.
PostgreSQL — The Most Advanced Open Source Relational Database
PostgreSQL is the most powerful open-source relational database system. It combines SQL compliance, extensibility, and reliability with advanced features like JSONB, full-text search, vector embeddings (pgvector), and PostGIS — making it the database of choice for modern applications.
Turbopuffer — Serverless Vector DB for AI Search
Serverless vector database built for AI search at scale. Turbopuffer offers sub-millisecond queries, automatic scaling, and pay-per-query pricing with zero infrastructure.
AIMock — Mock LLM, MCP, A2A, Vector APIs
AIMock is a mock server for AI app testing: it simulates LLM providers, MCP tools, A2A agents, and vector/search APIs so tests avoid real paid endpoints.