Installation
Enable the pgvector extension:Vector Types
Define vector columns with a specific dimension:Distance Operators
pgvector provides three distance operators:- Cosine Distance (<=>)
- L2 Distance (<->)
- Inner Product (<#>)
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
Filtered Similarity Search
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.
Troubleshooting
Index not being used
Index not being used
Slow queries
Slow queries
- 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
Out of memory
Out of memory
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
