---
title: "Understanding vector search and HNSW index with pgvector"
description: "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."
url: "https://swe.quest/articles/understanding-vector-search-and-hnsw-index-with-pgvector-db3498f2e1e5486e"
canonical: "https://neon.com/blog/understanding-vector-search-and-hnsw-index-with-pgvector"
source: "Neon"
published: "2023-09-20"
difficulty: "medium"
kind: "How-to"
reading_minutes: 10
tags: ["vector-search","postgres","llms"]
---

# Understanding vector search and HNSW index with pgvector

A post on [Neon](https://swe.quest/sources/neon.md), published 2023-09-20, 10 min read, How-to. Original: [https://neon.com/blog/understanding-vector-search-and-hnsw-index-with-pgvector](https://neon.com/blog/understanding-vector-search-and-hnsw-index-with-pgvector)

## Summary

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.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Vector search](https://swe.quest/tags/vector-search.md) (Data & databases)
- [PostgreSQL](https://swe.quest/tags/postgres.md) (Data & databases)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- PostgreSQL extensions ([easy posts on PostgreSQL](https://swe.quest/tags/postgres.md?difficulty=easy))
- vector embeddings ([easy posts on RAG & retrieval](https://swe.quest/tags/rag.md?difficulty=easy))

## Keywords

pgvector, HNSW, PostgreSQL, Neon, cosine distance, ef\_search

## Related posts

- [pgvector v0.5.0: Faster semantic search with HNSW indexes](https://swe.quest/articles/pgvector-v0-5-0-faster-semantic-search-with-hnsw-indexes-d1e5c38ac44f551e.md): Supabase · 2023-09-06 · Medium · 6 min read · Vector search, PostgreSQL, Performance engineering, Algorithms
  pgvector 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.
- [20x faster than pgvector: introducing pg\_embedding extension for vector search in Postgres and LangChain](https://swe.quest/articles/20x-faster-than-pgvector-introducing-pg-embedding-extension-for-vector-search-058737fc9388f495.md): Neon · 2023-07-11 · Medium · 7 min read · Vector search, PostgreSQL, Performance engineering
  pg\_embedding brought HNSW graph-based approximate nearest neighbor indexing to Postgres.
- [On-disk HNSW index for Postgres with pg\_embedding](https://swe.quest/articles/on-disk-hnsw-index-for-postgres-with-pg-embedding-aadc4bd81ebf65c4.md): Neon · 2023-08-03 · Medium · 5 min read · Vector search, PostgreSQL
  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.
- [How to Use pgvector for Similarity Search on Heroku Postgres](https://swe.quest/articles/how-to-use-pgvector-for-similarity-search-on-heroku-postgres-c4879269328732cd.md): Heroku Engineering · 2023-11-16 · Medium · 5 min read · Vector search, PostgreSQL, Python
  Heroku Postgres now supports the pgvector extension on Standard-tier and higher databases running Postgres 15, enabled with \`CREATE EXTENSION vector;\`.
- [pgvector: 30x Faster Index Build for your Vector Embeddings](https://swe.quest/articles/pgvector-30x-faster-index-build-for-your-vector-embeddings-cd6dabfa49eb13de.md): Neon · 2024-02-07 · Medium · 6 min read · Vector search, PostgreSQL, Performance engineering
  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.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
