pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index
6+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index
@ 2024-06-04 23:27  "danthedaniel (@danthedaniel)" <noreply+danthedaniel@github.com>
  0 siblings, 0 replies; 6+ messages in thread

From: danthedaniel (@danthedaniel) @ 2024-06-04 23:27 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

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.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index
@ 2024-06-04 23:28  "danthedaniel (@danthedaniel)" <noreply+danthedaniel@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: danthedaniel (@danthedaniel) @ 2024-06-04 23:28 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Oh, and thank you so much to anyone that can help!

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index
@ 2024-06-04 23:32  "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2024-06-04 23:32 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @danthedaniel, it looks like you indexed cosine distance but are querying by L2 distance. Either index with `vector_l2_ops` or query by cosine distance (`<=>`).

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index
@ 2024-06-04 23:32  "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2024-06-04 23:32 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index
@ 2024-06-04 23:40  "danthedaniel (@danthedaniel)" <noreply+danthedaniel@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: danthedaniel (@danthedaniel) @ 2024-06-04 23:40 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

LOL nice. Thanks so much. Should have gotten more sleep last night

^ permalink  raw  [nested|flat] 6+ messages in thread

* Re: [pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index
@ 2025-05-02 23:08  "pearcircuitmike (@pearcircuitmike)" <noreply+pearcircuitmike@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: pearcircuitmike (@pearcircuitmike) @ 2025-05-02 23:08 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane and others,

Thanks for the discussion in this thread. I read through it as I'm facing a similar symptom (planner not using HNSW index) but with a different root cause.

In my case, I've confirmed I am using the correct operator (<=>) matching my index definition (USING hnsw (embedding vector_cosine_ops)).

Despite this, and even after running ANALYZE and successfully completing REINDEX via a direct connection, the query planner consistently ignores the HNSW index and falls back to a slow sequential scan, even for the simplest ORDER BY embedding <=> query_vec LIMIT N queries. Disabling sequential scans (SET enable_seqscan = off) also didn't force the index usage.

We've tested this on Supabase with PostgreSQL 15.6 (and also after upgrading to the latest minor version available) and pgvector 0.8.0. Other index types (B-tree, GIN FTS) on the same table are being used correctly by the planner.

It seems like a planner issue specific to HNSW evaluation in this environment. Has anyone else seen cases where a valid HNSW index with the matching operator is still ignored by the planner?

(We've also opened a support ticket with Supabase detailing these findings).

Any insights would be appreciated! Link to more details: https://github.com/orgs/supabase/discussions/35432

^ permalink  raw  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2025-05-02 23:08 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-06-04 23:27 [pgvector/pgvector] issue #584: Can't get the query planner to use HNSW index "danthedaniel (@danthedaniel)" <noreply+danthedaniel@github.com>
2024-06-04 23:28 ` "danthedaniel (@danthedaniel)" <noreply+danthedaniel@github.com>
2024-06-04 23:32 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-06-04 23:32 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-06-04 23:40 ` "danthedaniel (@danthedaniel)" <noreply+danthedaniel@github.com>
2025-05-02 23:08 ` "pearcircuitmike (@pearcircuitmike)" <noreply+pearcircuitmike@github.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox