Skip to main content
pgvector is a PostgreSQL extension that adds vector similarity search capabilities. Store vectors alongside your relational data and perform fast similarity queries with SQL.

Installation

Enable the pgvector extension:

Vector Types

Define vector columns with a specific dimension:

Distance Operators

pgvector provides three distance operators:

Indexing

IVFFlat Index

Fast to build, good for most use cases:

HNSW Index

Better recall, slower to build:

Index for Different Distance Types

Functions

Vector Operations

Distance Functions

Advanced Queries

Similarity Search with Threshold

Approximate Nearest Neighbors

Query Performance

Explain Plans

Index Maintenance

Data Types and Limits

Migration Patterns

Add Vector Column to Existing Table

Change Vector Dimensions

Best Practices

Normalize Vectors: For cosine distance, normalize vectors to unit length for consistent results.
Index Size: Indexes can be large. Monitor disk usage and consider partitioning for large datasets.
Bulk Operations: Use copy or batch inserts for better performance when loading vectors.

Troubleshooting

  • Use appropriate index type (IVFFlat vs HNSW)
  • Adjust index parameters (lists, m, ef_construction)
  • Add filters before similarity search
  • Consider materialized views for common queries

Next Steps

Similarity Search

Build semantic search with pgvector

Vector Embeddings

Generate and store embeddings

Database Functions

Create stored procedures with pgvector

AI Examples

Complete RAG application examples