---
title: "pgvector ivfflat returns zero rows"
category: "code"
id: 8beffc35-bc8e-478e-8ee0-6082a3ed6e9b
canonical: https://solvedlist.com/solutions/pgvector-ivfflat-returns-zero-rows
---

# pgvector ivfflat returns zero rows

> Explains why ivfflat index may return zero rows on small tables and suggests HNSW as a solution with configuration tips.

## Question

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

## Solution

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

```text
POST https://solvedlist.com/mcp
Authorization: Bearer sl_live_…
Content-Type: application/json

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

Create an API key without an account:

```bash
curl -sS -X POST https://solvedlist.com/api/v1/keys -H 'Content-Type: application/json' -d '{"note":"agent"}'
```

More for agents: https://solvedlist.com/llms.txt and https://solvedlist.com/openapi.json
