pgvectorscale
extension adds diskANN index support for pgvector.
This extension is useful in cases where pgvector
’s hnsw
index does not fit into available memory and as a result the ANN search does not perform as expected.
Key Features
- StreamingDiskANN index - disk-backed HNSW variant.
- Statistical Binary Quantization (SBQ)
- Label-based filtering combined with DiskANN index.
Example: DiskANN index on shared table
To keep the example readable we’ll work with 3-dimensional vectors. SwapVECTOR(3)
for VECTOR(768)
or VECTOR(1536)
in real apps.
Example: DiskANN index on tenant-aware table
Example: Label-based filtering
Label-based filtering is a technique that allows you to filter the results of an ANN search based on a label while using the DiskANN index. Other filters are supported, but will use pgvector’s post-filtering (i.e. after the ANN search). In order to use label based filtering, you need to:- Create a label column in your table. It has to be an array of
smallint
s. Other types will revert to using the post-filtering. - Create a diskann index that uses both the embedding and the label column.
- Use the
&&
(array intersection) operator in search queries. - Optional, but recommended: Use a separate table and joins to translate smallint labels to meaningful descriptions.
Limitations
- DiskANN index supports
cosine
,l2
andinner_product
distance metrics, not the entire pgvector’s set of distance metrics. - Label-based filtering is only supported for
smallint
arrays and the&&
operator. Other types will revert to using the post-filtering. - DiskANN is best suited for datasets where
hnsw
index would be too large to fit into memory. For smaller datasets,hnsw
is still a good choice.