pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: jeff-davis (@jeff-davis) <noreply+jeff-davis@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #259: Understanding HNSW + filtering
Date: Mon, 06 Nov 2023 22:45:47 +0000
Message-ID: <gh-pgvector-pgvector-259-c1796976820@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-259@github.com>
References: <gh-pgvector-pgvector-259@github.com>

# Proposal

I'd like to organize a few of the ideas in this thread along with my
own thoughts into a proposal. This is still rough and I need to look
at a lot of code details, but I'd like some feedback.

# Problem Statement

Support queries of the form:

```                                                                                                                                
SELECT ... WHERE category_id = 1234 ORDER BY embedding <-> $1                                                                      
```                                                                                                                                

for an index defined as:

```
CREATE INDEX ON items USING hnsw (embedding vector_l2_ops, category_id)                                                            
```                                                                                                                                

No more than a few scalar attributes are expected along
with the vector.

# Summary

Maintain many HNSW graphs within one index object (i.e. a single
`pg_class` entry). Vector data is shared to avoid unnecessary
duplication. Each HNSW graph would be applicable to the subset of rows
that match a filter condition. (Similar, I believe, to what @jkatz
suggests[^1][^2]).

A scalar index (i.e. btree) would be first used to select the right
HNSW graph based on the filter conditions available from the query,
and then the search follows that graph's entry point.

For common data sets, many of the HNSW graphs would be a single
element or very few elements -- for example, the HNSW graph for the
subset of rows matching a highly selective filter condition. These
would be degenerate HNSW graphs consisting of a single neighborhood,
and we'd need to be sure these simple cases do not have unnecessary
storage overhead. But also, even within the same data set, some HNSW
graphs will be very large -- e.g. the HNSW graph for the subset of
rows matching an unselective filter condition; or the HNSW graph
representing no filter conditions at all.

# Notes

This proposed approach is briefly discussed in section 1.2 of
Filtered-DiskANN[^3] ("a trivial pre-processing method that builds a
separate index for each possible filterable
label..."). Filtered-DiskANN dismisses this approach because it's
"prohibitively expensive in scenarios with a large number of
filters...". But for our purposes we only need to filter on a few
attribtues (e.g. `category_id`) in addition to the vector. I consider
filtering on a large number of possible labels to be a different
problem.

To simplify planning, we introduce two limitations (though these can
be lifted with additional planning work):
1. The vector attribute is listed first among the attributes in the
   index definition, despite logically being secondary in the search
   process.
2. Only a prefix of the attributes defined in the index will be
   usable, similar to a btree (so, an index on `(V, A1, A2)` could be
   queried on `(V, A1, A2)`, `(V, A1)`, or just `V`; but not
   `(V, A2)`). NB: this limitation may negatively impact query recall.

To avoid engineering complexities from introducing a btree, we can
also consider using ideas from HQANN[^4], which allows mixing scalar
and vector types in the same index using a modified distance function.

# Algorithms

Assuming an index on attributes (V, A1, .., AN), where `V` is a vector
attribute and `A1`, ..., `AN` are scalar attributes with btree
opclasses.

`*` is a special token meaning that the attribute in the tuple is
unspecified; i.e. not part of a filter available from the query.

## Insert `tuple T (v, a1, .., aN)`

```                                                                                                                                
# with planner improvements, could generate all combinations of                                                                    
# scalar attributes, but for now just generate all prefixes                                                                        
For i in 0..N:                                                                                                                     
  # fill in attributes 1..i with values from the inserted tuple                                                                    
  # and fill in attributes i..N with '*'                                                                                           
  tuple partialT = (a1, .., ai, *, ...)                                                                                            
  G <- use partialT to search scalar index for HNSW graph                                                                          
  if G == NULL: initialize G and insert into scalar index                                                                          
  Insert T into G                                                                                                                  
```          
                                                                                                                      
## Delete `tuple T (v, a1, .., aN)`

```                                                                                                                                
# with planner improvements, could generate all combinations of                                                                    
# scalar attributes, but for now just generate all prefixes                                                                        
For i in 0..N:                                                                                                                     
  # fill in attributes 1..i with values from the inserted tuple                                                                    
  # and fill in attributes i..N with '*'                                                                                           
  tuple partialT = (a1, .., ai, *, ...)                                                                                            
  G <- use partialT to search scalar index for HNSW graph                                                                          
  Assert G != NULL                                                                                                                 
  Delete T from G                                                                                                                  
  if G is empty:                                                                                                                   
    Delete partialT from scalar index                                                                                              
```                                                         
## Query `WHERE A1 = C1 AND ... AJ = CJ ORDER BY V <-> $1`

```                                                                                                                                
  # ignore attributes that aren't a prefix of the index definition                                                                 
  # until the planner can support them                                                                                             
  M = MIN(J, N)                                                                                                                    
                                                                                                                                   
  # fill in attributes 1..M with filters from the query                                                                            
  # and fill in attributes M+1..N with '*'                                                                                         
  tuple partialT = (a1, .., aM, *, ...)                                                                                            
  G <- use partialT to search scalar index for HNSW graph                                                                          
  Use v to search G and return nearest neighbors                                                                                   
```

[^1]: https://github.com/pgvector/pgvector/issues/259#issuecomment-1719622821
[^2]: https://github.com/pgvector/pgvector/issues/259#issuecomment-1716258158
[^3]: https://harsha-simhadri.org/pubs/Filtered-DiskANN23.pdf
[^4]: https://arxiv.org/pdf/2207.07940.pdf


view thread (90+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-259-c1796976820@github.com>
Permalink:  ../gh-pgvector-pgvector-259-c1796976820@github.com/

 · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox