FG
💻 Software🤖 AI & LLMs

Understanding HNSW + filtering

Freshover 1 year ago
Mar 14, 20260 views
Confidence Score86%
86%

Problem

Hi, I would like to understand how the current implementation handles HNSW + filtering. Imagine you have a table: [code block] And that you even have an index on `category`: [code block] And then you want to do a query like: [code block] To do this efficiently is not straightforward -- ideally we want to do the expensive HNSW ANN on the already pruned subset (https://qdrant.tech/articles/filtrable-hnsw/). Can pgvector do this, or is there plan to enable such optimization in the future? (In this case the condition is simple enough that you might be able to use table partitioning, but that's not always the case)

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
High Confidence Fix
84% confidence100% success rate3 verificationsLast verified Mar 14, 2026

Solution: Understanding HNSW + filtering

Low Risk

@Palmik PostgreSQL supports [conditional indexing][1] through [partial indexes][1] which lets you define an index like: [code block] However, you would have to do this for every single category in the database. To look up from both indexes at the same time, pgvector would have to add support for bitmap scans in `hnsw`. That said, picking an indexing strategy may depend on the actual contents of

84

Trust Score

3 verifications

100% success
  1. 1

    @Palmik PostgreSQL supports [conditional indexing][1] through [partial indexes][

    @Palmik PostgreSQL supports [conditional indexing][1] through [partial indexes][1] which lets you define an index like:

  2. 2

    However, you would have to do this for every single category in the database. To

    However, you would have to do this for every single category in the database. To look up from both indexes at the same time, pgvector would have to add support for bitmap scans in `hnsw`.

  3. 3

    That said, picking an indexing strategy may depend on the actual contents of you

    That said, picking an indexing strategy may depend on the actual contents of your data. For example, if your `category` filter eliminates most rows (e.g. you have a handful of vectors to compare remaining), using the index `embedding` may not make sense. Or based upon your use case, you may want to perform the ANN search first and then filter out the results by category.

  4. 4

    [1]: https://www.postgresql.org/docs/current/indexes-partial.html

    [1]: https://www.postgresql.org/docs/current/indexes-partial.html

Validation

Resolved in pgvector/pgvector GitHub issue #259. Community reactions: 2 upvotes.

Verification Summary

Worked: 3
Last verified Mar 14, 2026

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

pgvectorembeddingsvector-search