Skip to content

Updated

Vector indexes

Knowing which index type and distance operator fit your queries tells you how to keep vector search fast as your table grows. For how to create an index, see HNSW indexes.

Index types

A vector index speeds up queries as your vector table grows. Without an index, each query performs a sequential scan, which can be resource-intensive when you have many records.

pgvector supports two types of indexes:

In general we recommend using HNSW because of its performance and robustness against changing data. If queries are slow on an existing table, see Increase vector lookup speeds with HNSW.

Distance operators

An index speeds up nearest neighbor search for a given distance measure. pgvector includes three distance operators:

OperatorDescriptionOperator class
<->Euclidean distancevector_l2_ops
<#>negative inner productvector_ip_ops
<=>cosine distancevector_cosine_ops

For more on operator classes, see the Postgres documentation on operator classes.

Limits

For pgvector versions 0.7.0 and later, you can index vectors with the following maximum dimensions:

  • vector: up to 2,000 dimensions
  • halfvec: up to 4,000 dimensions
  • bit: up to 64,000 dimensions

To check your pgvector version, run select * from pg_extension where extname = 'vector';, or open the Extensions page in the Supabase Dashboard.

If your pgvector version is earlier than 0.7.0, upgrade your project in the Supabase Dashboard.

Trade-offs

Nearest neighbor search with either index type is approximate, because exact search on high-dimensional data can't be indexed efficiently. Similarity results can change slightly after you add an index, trading some accuracy for speed.

When to use each index type

HNSW is the default choice. The two index types differ in when you can build them:

Index typeWhen to build it
HNSWRight after you create the table. As new data is added, the index fills automatically and its structure stays optimal.
IVFFlatOnly after the table has enough data, so its cell clusters match your data's distribution. Consider rebuilding the index when the distribution changes significantly.
  • HNSW indexes: create the recommended index type.
  • IVFFlat indexes: create the alternative index type.
  • pgvector: enable and use the extension.
  • The pgvector documentation on indexing on GitHub: the full indexing reference.