code · Nov 12, 2026

pgvector ivfflat returns zero rows

PostgreSQL pgvector ivfflat index returns zero rows for similarity query on small table, but works without index. Why and is HNSW the fix?

Part of PostgreSQL constraints, triggers and vector search

Symptom: `ORDER BY embedding <=> $1 LIMIT 5` returns rows without an index, but after creating an `ivfflat` index the same query returns 0 rows. Cause: ivfflat runs k-means once, at CREATE INDEX time. With few rows and the default `lists = 100`, many lists stay empty, and a search with the default `ivfflat.probes = 1` visits exactly one of them. Neighbours in unvisited lists are missed. Row count, index build and search parameters decide whether this happens; it is not automatic on every small table. Fix: HNSW builds a graph incrementally, needs no training phase and works from the first row: ``` DROP INDEX IF EXISTS items_embedding_ivfflat; CREATE INDEX items_embedding_hnsw ON items USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64); ANALYZE items; ``` Query-time recall is set with `SET hnsw.ef_search = 40;`. If you keep ivfflat: create the index after the table is populated, choose `lists` from the row count (roughly rows divided by 1000 below a million rows), and raise `SET ivfflat.probes = 10`. Verify: HNSW is approximate, so do not expect identical rows. Compare recall against exact search on a sample (`SET enable_indexscan = off`), and use `EXPLAIN (ANALYZE)` to confirm the index is used at a realistic table size; on a small table PostgreSQL may legitimately choose a sequential scan.

Query directly via MCP

This blueprint is also machine-readable: Markdown. Create an API key without an account:

curl -sS -X POST https://solvedlist.com/api/v1/keys \
  -H 'Content-Type: application/json' -d '{"note":"agent"}'
POST https://solvedlist.com/mcp
Authorization: Bearer ***
Content-Type: application/json

{"jsonrpc":"2.0","id":1,"method":"tools/call",
 "params":{"name":"solvedlist_lookup","arguments":{"question":"…"}}}

More: /llms.txt · /openapi.json