PgVectorStore adds PostgreSQL-native vector storage and similarity search to Semantica: no dedicated vector database required.
Overview
PgVectorStore provides native PostgreSQL vector storage using the pgvector extension. It supports multiple distance metrics (cosine similarity, L2/Euclidean, inner product), index types (IVFFlat, HNSW), and JSONB metadata storage with filtering.
Features
Distance metrics: cosine, L2 (Euclidean), inner product
Index types: IVFFlat and HNSW for approximate nearest-neighbor search
JSONB metadata storage with filtering support
Connection pooling via psycopg3/psycopg2
Batch insert, update, and delete
Idempotent index creation: safe to call multiple times
Setup
Prerequisites
- PostgreSQL 13+ with pgvector extension installed
- Python dependencies:
psycopg3(preferred) orpsycopg2-binary,pgvector
Installing Dependencies
PostgreSQL Setup
1
Install the pgvector extension
2
Create the extension in your database
3
Verify installation
Docker Quickstart
Connection String Format
Standard PostgreSQL connection string:Usage
Basic Usage
Context Manager
Metadata Filtering
Update and Delete
Retrieve by ID
Index Creation
Statistics
Distance Metrics
Note: Scores returned by
search() are normalized to similarity (higher = better) regardless of metric.
Index Types
- HNSW
- IVFFlat
Hierarchical Navigable Small World: best for high-dimensional vectors with high recall requirements.
Schema
The vector table schema:Migration Notes
From Other Vector Stores
Backup and Restore
Use PostgreSQL native backup tools:Configuration
Connection Pool Settings
Environment Variables
Error Handling
Common errors and solutions:Performance Tuning
- Use indexes for large datasets (>10k vectors)
- Tune HNSW parameters: Higher
mandef_construction= better recall, slower build - Connection pool size: Set based on concurrent workload
- Batch operations: Use
add()with lists instead of individual inserts
