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

  1. PostgreSQL 13+ with pgvector extension installed
  2. Python dependencies: psycopg3 (preferred) or psycopg2-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:
Examples:

Usage

Basic Usage

Context Manager

Metadata Filtering

Update and Delete

Retrieve by ID

Index Creation

Index creation is idempotent: calling multiple times is safe.

Statistics

Distance Metrics

Note: Scores returned by search() are normalized to similarity (higher = better) regardless of metric.

Index Types

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

  1. Use indexes for large datasets (>10k vectors)
  2. Tune HNSW parameters: Higher m and ef_construction = better recall, slower build
  3. Connection pool size: Set based on concurrent workload
  4. Batch operations: Use add() with lists instead of individual inserts

Testing

Tests require a running PostgreSQL with pgvector:

See Also