From: danthedaniel (@danthedaniel) <noreply+danthedaniel@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index
Date: Tue, 04 Jun 2024 23:27:57 +0000
Message-ID: <gh-pgvector-pgvector-584@github.com> (raw)
I want to perform clustering. Part of our clustering operation requires iterative comparison of embeddings across a subset of our global `AnnotationEmbedding` table. This will be implemented as a `N * top(3, N)` operation. Without a nearest-neighbours index this is `O(N * N)`, but it can be brought down to `O(N * 3 * log(N))` with an index. Given that pgvector doesn't support indexing combined with filters (in our case filtering is done through an `INNER JOIN`) I'm planning to take the subset of data and index it in a temp table. This is slow but still dramatically faster than `O(N * N)` embedding cosign distance calculations.
Here's my table/index DDL:
```sql
CREATE TEMP TABLE embeddings (
id INT,
embedding vector(1024),
annotation_id INT
);
CREATE INDEX temp_embeddings_idx ON embeddings USING hnsw (embedding vector_cosine_ops);
```
Then I throw in some data:
```sql
INSERT INTO embeddings (id, embedding, annotation_id)
SELECT
"AnnotationEmbedding"."id" AS "id",
"AnnotationEmbedding"."embedding" AS "embedding",
"ImageAnnotation"."id" AS "annotation_id"
FROM "AnnotationEmbedding"
-- ... filters, etc.
```
And execute these for good measure while debugging:
```sql
ANALYZE embeddings;
SET enable_seqscan = off;
```
But the query planner doesn't seem to have any idea that there is an index available:
```sql
EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS)
SELECT
(SELECT "embedding" FROM "embeddings" "inner" LIMIT 1) <-> "outer"."embedding" AS "distance"
FROM "embeddings" "outer"
ORDER BY "distance" DESC
LIMIT 1;
```
Output:
```
Limit (cost=20000000195.50..20000000195.50 rows=1 width=8) (actual time=75.143..75.145 rows=1 loops=1)
Output: (($0 <-> "outer".embedding))
Buffers: local hit=44456 read=5358
InitPlan 1 (returns $0)
-> Limit (cost=10000000000.00..10000000000.02 rows=1 width=18) (actual time=0.005..0.006 rows=1 loops=1)
Output: "inner".embedding
Buffers: local hit=1
-> Seq Scan on pg_temp.embeddings "inner" (cost=10000000000.00..10000000136.56 rows=7856 width=18) (actual time=0.003..0.004 rows=1 loops=1)
Output: "inner".embedding
Buffers: local hit=1
-> Sort (cost=10000000195.48..10000000215.12 rows=7856 width=8) (actual time=38.548..38.548 rows=1 loops=1)
Output: (($0 <-> "outer".embedding))
Sort Key: (($0 <-> "outer".embedding)) DESC
Sort Method: top-N heapsort Memory: 25kB
Buffers: local hit=44456 read=5358
-> Seq Scan on pg_temp.embeddings "outer" (cost=10000000000.00..10000000156.20 rows=7856 width=8) (actual time=0.038..38.094 rows=7856 loops=1)
Output: ($0 <-> "outer".embedding)
Buffers: local hit=44456 read=5358
Planning Time: 0.086 ms
JIT:
Functions: 6
Options: Inlining true, Optimization true, Expressions true, Deforming true
Timing: Generation 0.376 ms, Inlining 5.042 ms, Optimization 20.151 ms, Emission 11.403 ms, Total 36.974 ms
Execution Time: 75.580 ms
```
I understand that maybe in this small example a sequential scan makes more sense. But in a real-world scale test (~7,500 embeddings) the index is still ignored and the query can take minutes to complete.