6 min read

Vector Search Demystified: A Guide to pgvector, IVFFlat, and HNSW

Tanja Bayer

Tanja Bayer

Introduction: Why Vector Databases Are Hot Right Now

Have you ever noticed how Google can suggest what you’re looking for before you even finish typing? Or how Spotify seems to know your taste in music better than you do? That’s because of a special kind of search—one that finds things that are similar rather than just exact matches.
This is where vector databases come in. Instead of storing data in tables and rows like traditional databases (such as PostgreSQL or MySQL), vector databases store "fingerprints" of data—like a unique way of describing words, images, or videos using numbers. These vector embeddings help AI quickly find things that feel related , even if they don’t exactly match.
This is super important for AI-powered apps , like chatbots, recommendation engines, and image search tools . It also helps large language models (LLMs) like ChatGPT understand context better by remembering what similar conversations have looked like before.
But here’s the fun part: You don’t need a brand-new database to do vector search! If you’re already using PostgreSQL , you can just add pgvector , a powerful extension that brings vector search to Postgres.
In this post, we’ll break down pgvector , explain how it works, and help you choose between its two indexing methods— IVFFlat and HNSW —so you can decide which one is best for your needs. Let’s dive in!

What Are Embeddings and Why Do They Matter?

At its core, an embedding is a way to represent data—whether it’s text, images, or even audio—as a list of numbers ( a vector ). These numbers capture the relationships and meanings behind the data, making it possible to compare similarity rather than relying on exact matches.
For example:
  • The words "cat" and "kitten" will have embeddings that are numerically close, while "airplane" will be much farther away.
  • A search for a beach photo can return other visually similar images, even if they aren't identical.
  • A recommendation system can suggest related products based on what a user has searched for.
Traditional databases excel at structured data (like rows and columns), but they struggle with semantic similarity —which is why vector embeddings are so important for modern AI applications. By storing and searching vectors, we can power smarter search engines, AI assistants, and recommendation systems .
This is where pgvector comes in, bringing vector search capabilities to PostgreSQL . Next, let’s explore how ANN (Approximate Nearest Neighbor) search helps us find similar embeddings efficiently.

What Is Approximate Nearest Neighbor (ANN) Search?

When searching for similar embeddings, we need to find the closest matches in a huge dataset. But if we compare every single vector one by one, the search becomes too slow —especially with millions of embeddings. That’s where Approximate Nearest Neighbor (ANN) search comes in.

How Does ANN Work?

Instead of scanning everything, ANN uses smart shortcuts to quickly find the most relevant matches. It doesn’t guarantee a perfect result, but it’s much faster and usually good enough for real-world applications like AI-powered search and recommendations.

Example: Finding a Similar Song

Imagine you hear a song at a café and want to find something similar.
  • Exact search (slow): You listen to every song ever made until you find a match.
  • ANN search (fast): You skip straight to songs with similar melodies, artists, or genres—finding a good match quickly.

Why ANN Is Essential for Vector Search

Traditional databases look for exact values, like finding "dog" in a table. But ANN allows us to find things that are similar , making it perfect for:
  • AI chatbots (understanding context)
  • Image search (finding similar photos)
  • Recommendations (music, shopping, content)
Now that we understand ANN, let’s look at how PostgreSQL handles vector search with pgvector—and the two indexing methods it offers: IVFFlat and HNSW!

Meet pgvector: Bringing Vector Search to PostgreSQL

Now that we understand vector embeddings and ANN search , the next question is:
How do we actually store and search vectors inside a database?
This is where pgvector comes in! pgvector is an extension for PostgreSQL that allows you to store and efficiently search vector embeddings—meaning you don’t need a brand-new database to handle AI-powered searches.

Why Use pgvector?

  • Works inside PostgreSQL – No need for a separate vector database.
  • Supports different search methods – Exact search & ANN search.
  • Scales well – Efficiently handles large datasets.
With pgvector , you can store embeddings as a vector column and use special operators (like <-> ) to find similar items based on distance metrics like cosine similarity or Euclidean distance .
But searching through vectors efficiently requires indexing —and PostgreSQL offers two key indexing methods in pgvector: IVFFlat and HNSW .

Indexing Methods: IVFFlat vs. HNSW

Indexes are like shortcuts that speed up searches. Without them, every query would scan all vectors—making large-scale searches painfully slow.
pgvector supports two main index types:
  • IVFFlat (Inverted File Flat) – Fast but less accurate
  • HNSW (Hierarchical Navigable Small World) – More accurate but uses more memory
Let’s break them down.

IVFFlat: Fast and Efficient for Large Datasets

IVFFlat works by grouping vectors into clusters (called “centroids”) and only searching within the most relevant clusters instead of scanning everything.
Pros:
Cons:
Best for: Massive datasets where speed is more important than perfect accuracy—like recommendation systems or real-time searches.

HNSW: High Accuracy with a Memory Trade-Off

HNSW uses a graph-based structure where vectors are connected to their nearest neighbors, making searches extremely accurate.
Pros:
Very accurate results (better recall than IVFFlat)
Great for small-to-medium datasets
Cons:
Higher memory usage (stores a lot of extra connections)
Indexing is slower compared to IVFFlat
Best for: Use cases where accuracy is critical, such as semantic search, AI chatbots, and image retrieval .

Which One Should You Use?

Use IVFFlat if… you have a huge dataset and need fast search speeds (even if some results aren’t perfect).
Use HNSW if… you need high accuracy and can afford the extra memory usage.

Performance Considerations & Tuning

Choosing the right indexing method in pgvector depends on speed, accuracy, and memory usage . Here’s how IVFFlat and HNSW compare in real-world performance.

Index Build Times

  • IVFFlat requires training before it can be used. The number of clusters ( lists parameter) impacts both indexing speed and query performance.
  • HNSW takes longer to build because it constructs a graph of nearest neighbors, but once built, searches are highly efficient.

Query Speed

  • IVFFlat is fast because it searches only a subset of vectors, but its speed depends on how well the centroids are chosen.
  • HNSW is generally faster for smaller datasets but may slow down as the dataset grows due to its graph structure.

Memory Usage

  • IVFFlat is memory-efficient since it doesn’t store extra relationships between vectors.
  • HNSW requires more memory because it maintains multiple layers of connections between data points.

Accuracy vs. Recall

  • IVFFlat trades accuracy for speed—if the clustering isn't optimal, similar items may be missed .
  • HNSW achieves higher recall and is preferred when accuracy is a priority (e.g., AI-powered assistants or semantic search).

Tuning for Best Performance

  • For IVFFlat, adjusting the lists parameter is key. More lists mean faster queries but slower insertions .
  • For HNSW, tweaking ef_construction (index build quality) and ef_search (query accuracy) helps balance speed and precision.
In general, IVFFlat is ideal for scaling to millions of vectors , while HNSW provides better accuracy for AI-driven applications where precision matters. The best choice depends on your dataset size, memory constraints, and search accuracy needs.

Conclusion

Vector search is becoming a must-have technology for AI applications, allowing databases to retrieve similar items instead of just exact matches. With the rise of large language models (LLMs) and generative AI , the ability to store and search embeddings efficiently is more important than ever.
pgvector brings this power to PostgreSQL , making it easy to work with vector embeddings inside a familiar database. Choosing the right indexing method depends on your needs:
  • IVFFlat is best for large datasets where speed is more important than perfect accuracy .
  • HNSW is ideal when accuracy is a priority and you can afford higher memory usage .
By tuning parameters like lists (IVFFlat) or ef_search (HNSW) , you can optimize performance for your specific use case. Whether you’re building an AI-powered chatbot, a recommendation system, or an image search engine , vector databases are here to stay , and pgvector is a great way to get started!

Sources & Further Reading

pgvector GitHub Repository – Official documentation and setup instructions.
PostgreSQL Documentation – Learn more about indexing and query optimization.
HNSW Algorithm Paper – In-depth look at HNSW indexing.
Write You a Vector Database – A good explanation how the algorithm works
Want to test in real live get started with our AI Workflows with Postgres Datasources.
Sign Up Here