pgvector HNSW Index RAM & Sizing Calculator
Model PostgreSQL pgvector HNSW index memory requirements before deployment. Calculate RAM thresholds to prevent disk thrashing, optimize m and ef_construction parameters, and export tuned postgresql.conf and SQL DDL scripts.
1. Dataset & Vector Dimensions
2. Vector Precision & Quantization
3. HNSW Graph Hyperparameters
Higher m increases recall at the cost of index RAM and build time.
Controls graph build quality. Does not affect index RAM size.
4. PostgreSQL Host Server Specs
Memory & Sizing Estimates
Index fits cleanly in RAM
HNSW graph and vector payloads (7.05 GB) fit comfortably inside your 32 GB server memory for sub-15ms search latency.
Key SRE Rule for pgvector
Unlike B-tree indexes where partial cache hits still yield acceptable performance, HNSW vector search relies on non-local random graph walks. If the index spills from RAM to disk, latency degrades from 5ms to 800ms+.
-- 1. Enable pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;
-- 2. Create optimized vector storage table
CREATE TABLE IF NOT EXISTS documents (
id BIGSERIAL PRIMARY KEY,
content TEXT NOT NULL,
metadata JSONB DEFAULT '{}'::jsonb,
embedding vector(1536) NOT NULL
);
-- 3. Set dedicated build memory for the current session (avoids disk spills)
SET maintenance_work_mem = '7943MB';
SET max_parallel_maintenance_workers = 6;
-- 4. Create HNSW index for sub-15ms semantic search
CREATE INDEX IF NOT EXISTS idx_documents_embedding_hnsw
ON documents
USING hnsw (embedding vector_cosine_ops)
WITH (
m = 16,
ef_construction = 64
);How to use
- Step 1: Select your dataset scale (e.g. 100K, 1M, or 10M rows) and choose your embedding model (OpenAI 1536d, BGE 768d, etc.) or input a custom dimension.
- Step 2: Pick your vector quantization: standard float32 vector, float16 halfvec (50% RAM savings with ~99% recall), or binary bit representation.
- Step 3: Configure HNSW graph parameters: m (connections per node) and ef_construction (build candidate list).
- Step 4: Verify that the required RAM fits within 75% of your host server memory to guarantee sub-15ms search latency.
- Step 5: Copy the generated SQL DDL, postgresql.conf parameters, and asyncpg Python queries directly into your codebase.
Frequently asked questions
Why does a pgvector HNSW index need to fit entirely in RAM?
What is the memory difference between vector and halfvec?
How do m and ef_construction affect index size and search speed?
What is the purpose of maintenance_work_mem when building HNSW?
How do I balance query latency vs recall at search time?
The Senior Full-Stack Production Kit (2026)
Checklists, Docker recipes, and SSR error fixes in one dense PDF.