Sub topic 13
Combining Vector Search with `WHERE` Filters
Assume pgvector is enabled and the items table already contains embeddings. The table also has a metadata column:
ALTER TABLE items
ADD COLUMN category_id integer;
CREATE INDEX items_category_idx
ON items (category_id);A metadata filter limits the candidate rows before or during a nearest-neighbor query. For example, this query finds the five items in category 1 closest to the query vector:
SELECT id, content, category_id
FROM items
WHERE category_id = 1
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;The expression embedding <=> '[3,1,2]' calculates cosine distance. Smaller distances mean more similar vectors. The WHERE clause restricts the acceptable rows, while ORDER BY ranks those rows.
How filtering affects the result
With exact search, PostgreSQL can inspect every row that satisfies the filter, calculate its distance, sort the results, and return up to five rows. If the category contains only three rows, the query correctly returns only three rows.
Approximate indexes behave differently. An approximate nearest-neighbor index examines only part of the vector space. The metadata filter may then remove some of the rows found by that scan. The final result can contain fewer rows than the LIMIT requests, even when more matching rows exist elsewhere in the table.
| Search method | Filtering behavior | Typical result |
|---|---|---|
| Exact search | Checks all rows that match `WHERE` | Correct nearest neighbors |
| Approximate search | Filters the candidates found by the index scan | Faster, but may return fewer or less accurate rows |
> callout note > If a filtered approximate query returns too few rows, increasing LIMIT alone may not help. Increase the index search breadth, use a partial index for a common filter, or partition data when appropriate.
Inspecting the planner
Use EXPLAIN to see whether PostgreSQL uses the category index, a vector index, or a sequential scan:
EXPLAIN
SELECT id, content
FROM items
WHERE category_id = 1
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;A sequential scan can be reasonable for a small table or a category containing many rows. For a selective filter, PostgreSQL might first use items_category_idx, then calculate distances and sort the matching rows. The planner chooses based on table statistics and estimated costs.
Run ANALYZE after loading a large amount of data so those estimates are current:
ANALYZE items;Improving filtered approximate search
For an HNSW index, increase hnsw.ef_search for a query that needs better recall:
BEGIN;
SET LOCAL hnsw.ef_search = 100;
SELECT id, content
FROM items
WHERE category_id = 1
ORDER BY embedding <=> '[3,1,2]'
LIMIT 5;
COMMIT;A partial index can help when nearly every query uses the same filter:
CREATE INDEX items_category_1_embedding_hnsw
ON items USING hnsw (embedding vector_cosine_ops)
WHERE category_id = 1;This index contains only category 1 rows. It uses less space than a full-table index, but it does not help queries for other categories.
Exercise
Why can an approximate vector query with WHERE category_id = 1 return fewer rows than its LIMIT requests?
Show answer
The approximate index scans a limited set of candidate vectors. The WHERE clause removes candidates from other categories, and the scan may not find enough category 1 candidates to fill the limit.