> ## Content Index
> Fetch the complete content index at: https://blog.netpy.in/llms.txt
> Use this file to discover other available public pages before exploring further.

# Your pgvector filter runs after the search
- URL: https://blog.netpy.in/pgvector-post-filter-recall/
- Published: 2026-11-30T02:34:41.000Z
- Updated: 2026-11-30T02:34:41.000Z
- Description: pgvector's HNSW index applies your WHERE clause after the nearest-neighbour walk, so a tenant-scoped LIMIT 10 quietly returns four rows. Pinecone's new pg_pinecone extension filters during the search instead. Here is what its benchmarks actually show, and a migration checklist for teams running RAG
- Author: Netpy Technologies
- Tags: PostgreSQL, Vector Search, RAG, Data Engineering, Architecture

# Your pgvector filter runs after the search

A support assistant scoped to one tenant asks Postgres for the ten rows closest to a query embedding. It gets four. No error, no warning, a healthy 200\. The code above renders four citations and the model answers from what it has. Users on that tenant quietly get worse answers than everyone else, and nothing on your dashboard says so.

That is the bug Pinecone is pointing at with `pg_pinecone`, an Apache-2.0 Postgres extension announced on the company's blog in late October 2026 and covered by InfoQ on 29 November. The extension matters less for what it ships than for the failure mode it names: in pgvector's HNSW index, the `WHERE` clause is applied **after** the nearest-neighbour search, not during it.

## What actually happens inside HNSW post-filtering

pgvector's HNSW index is a graph. A query walks toward its nearest neighbours, collects candidates, and only then does Postgres evaluate `tenant_id = $1`. When the filter keeps 1% of rows, roughly 99 of every 100 candidates are discarded after you paid to score them. A query asking for ten rows returns fewer than ten, or pgvector's iterative scan keeps walking to fill the list — more time, and under `relaxed_order`, less accuracy.

That is not a performance footnote. It is a correctness problem that shows up as quality drift in one tenant, one segment, one category — invisible on latency dashboards, visible only to anyone running an exact brute-force comparison.

## What pg\_pinecone does differently

The extension keeps your table, your `vector` column and your SQL — `ORDER BY embedding <=> $1 LIMIT 10` still works — and swaps the index underneath. Each table picks an engine: `pgvector` (the HNSW index you have), `native` (the default, inside your Postgres, no account, no network call) or `remote` (search at Pinecone, rows stay in Postgres).

The native engine replaces the graph with an IVF layout: similar vectors are grouped into clusters, and each vector is stored as a RaBitQ code — about one bit per dimension plus a few correction floats, following Gao and Long, SIGMOD 2024 — packed so a scan reads contiguous pages instead of hopping graph nodes. Search does three things: probe the nearest clusters by centroid, estimate distances from the codes with cheap bit operations, then rescore a shortlist against the full vectors in your table.

Because each row's code carries its filter value and the build counts rows per value, the engine picks its strategy from measured selectivity. At 3% of rows or fewer (the default) it pre-filters and scores every match exactly; above 3% it probes up to half the clusters and checks the filter inside each. On Pinecone's benchmark of one million 768-dimension vectors, a 10%-selective query reached 1.000 recall in 12.3 ms against 0.920 at best for HNSW with iterative scans in 5.9 ms.

The index sizes are the other number to internalise: 136 MB for the native engine against 2.0 GB for HNSW on `halfvec` and 4.1 GB on full-size vectors at 1M-by-768.

## Where the sales pitch ends and engineering begins

Treat the latency figures as a vendor benchmark: InfoQ notes they are Pinecone's own and unverified, and the harness mixes PostgreSQL 17 with 18\. Recall is data-dependent — your vectors are not `wiki_dpr`.

Four structural details matter more than the milliseconds:

- **One filter column per index.** `pinecone.create_index('docs', 'embedding', filter_column => 'tenant_id')` takes a single column. A query filtering on tenant *and* locale *and* document type cannot have all three applied during search. Pick the column carrying your isolation boundary; the rest fall back to ordinary predicates.
- **The 3% threshold is a heuristic, not a law.** It assumes uniform selectivity. Skewed tenant sizes — one enterprise account holding 40% of rows, a thousand small ones holding the rest — is the case a commenter on the InfoQ piece flagged, and the one a fixed threshold handles least gracefully.
- **Centroids are frozen at build time.** New rows join the nearest cluster and are searchable immediately, but after a bulk load that doubles the table, or data unlike the build set, run `REINDEX` or enable automatic maintenance.
- **It is a real extension, with real upgrade tax.** It needs superuser. Version 0.10.0 renamed `pinegres` with no upgrade path, and its RaBitQ index format rejects older indexes until `REINDEX` — about four minutes per million 768-d rows by the project's runbook.

## A migration checklist that survives contact with production

1. Build ground truth first: exact brute-force `ORDER BY` over 5,000 real queries, recording the true top-10.
2. Measure recall at your filter, not at k=10 unfiltered. Slice by tenant size decile — the aggregate hides the tenant that breaks.
3. Assert row counts: every filtered query returns exactly `LIMIT` rows, or is explicitly allowed to under-fill. This catches the bug that opened the article.
4. Set `target_recall` from evidence, pin it in config, and re-run when the corpus doubles.
5. Keep the filter column narrow — an integer tenant id, not a text slug — and resist a second dimension.
6. Benchmark on your hardware and your Postgres minor version; the vendor harness spans two majors.
7. Plan the `REINDEX` window and the on-disk format upgrade path before the first table goes native.
8. Keep the rollback: `pinecone.set_engine()` back to `pgvector` is one call — the strongest argument for trying this on an existing database.

## Why this lands for us

Most of the AI features we ship at [NetPy](https://netpy.in/?ref=blog.netpy.in) already sit on Postgres that holds the customer's own data. The pitch for a second system is recall and filtering; the objection is operational surface. An extension that keeps the data in place and fixes filter order is the first version of that trade we would actually test, and [NetPyCloud](https://cloud.netpy.in/?ref=blog.netpy.in) provisions the Postgres underneath it.

The lesson generalises: if a filter runs after an approximate search, your `LIMIT` is a suggestion and your recall is a rumour. Measure it before you trust it.