On-disk HNSW index for Postgres with pg_embedding
pg_embedding's new version builds its HNSW vector index on disk rather than in memory and adds cosine, Manhattan and Euclidean (L2) distance support. Benchmarks on an 8 vCPU / 32 GB machine show the in-memory index is roughly 2x faster, but persisting the index lets it work with Neon's autoscaling, scale-to-zero and read replicas for read-heavy similarity search. The post walks through enabling the extension, storing embeddings as real[] and tuning the index parameters dims, m, efConstruction and efSearch, where efSearch should be at least the query's LIMIT k. Neon has since discontinued pg_embedding and recommends pgvector instead.
Related posts
- 20x faster than pgvector: introducing pg_embedding extension for vector search in Postgres and LangChain
Neon mediumAdept
pg_embedding brought HNSW graph-based approximate nearest neighbor indexing to Postgres. On the GIST-960 dataset (1M 960-dim vectors, k=100, 4 vCPU/16 GB), it ran 5–30x faster than pgvector's IVFFlat at equal recall. IVFFlat needs more probes to raise recall and drifts toward a sequential scan, while HNSW's layered graph gives roughly logarithmic search. HNSW trades higher memory use, slower index builds (driven by efConstruction) and L2-only distance for that speed. Neon has since deprecated pg_embedding in favor of pgvector, which now supports HNSW natively.
- pgvector v0.5.0: Faster semantic search with HNSW indexes
Supabase mediumAdeptpgvector v0.5.0 adds HNSW indexes, which combine skip-list-style layering with navigable small world proximity graphs to do approximate nearest-neighbor search over embeddings. In Supabase benchmarks on 1536-dimension OpenAI vectors, HNSW delivered about 3x the QPS of IVFFlat on 224K vectors and over 6x on 1M vectors at equal accuracy. Tuning ef_search, m and ef_construction trades speed against recall; raising ef_construction to 80 reached 0.99 accuracy@10 with ef_search 100 and 35% more QPS. HNSW can be built on an empty table and stays optimal as data arrives, but it builds more slowly and uses more memory than IVFFlat, whose build times dropped over 50% thanks to parallel list assignment and float-based FMA distance math.
- Understanding vector search and HNSW index with pgvector
Neon mediumAdept
pgvector 0.5.0 adds an HNSW (Hierarchical Navigable Small Worlds) index for approximate nearest neighbor search in Postgres, replacing O(N) sequential exact scans with a multi-layer graph traversal that trades a little accuracy for much faster queries. A walkthrough covers vector embeddings, L2 and cosine distance with worked examples, and pgvector SQL for storing and querying vectors with the <=> operator. Index tuning hinges on m (graph degree), ef_construction (build-time candidate list) and ef_search (query-time beam size): higher values improve recall at the cost of build time, memory or query latency. HNSW is faster and higher-recall than IVF but resource-intensive to build, often requiring more RAM and a larger maintenance_work_mem.
- How to Use pgvector for Similarity Search on Heroku Postgres
Heroku Engineering mediumAdeptHeroku Postgres now supports the pgvector extension on Standard-tier and higher databases running Postgres 15, enabled with `CREATE EXTENSION vector;`. A worked example loads 100-dimensional Wikipedia2Vec word embeddings for a list of animals into a VECTOR(100) column using Python, psycopg and pgvector-python. It then runs nearest-neighbour queries with the Euclidean (`<->`), cosine (`<=>`) and inner product (`<#>`) operators. On indexing, IVFFlat and HNSW indexes switch queries to approximate search, should match the distance function you query with most, and IVFFlat should be built after data is loaded for better recall.
- pgvector: 30x Faster Index Build for your Vector Embeddings
Neon mediumAdept
pgvector 0.6.0 adds parallel builds for HNSW indexes, cutting build time by up to 30x in benchmarks on 10M 1,536-dimension vectors using a 64 vCPU, 512GB instance. Recall is essentially unchanged, with most results shifting slightly upward. Fast builds depend on giving the graph enough memory: raise maintenance_work_mem (for example to 8GB) so the graph fits, and raise max_parallel_maintenance_workers above the default of 2. On Neon, which currently supports pgvector 0.5.1 with 0.6.0 coming, compute can be scaled up for the build and back down afterward, avoiding permanent over-provisioning just for indexing.