pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #835: HNSW Index Not Used for KNN Queries (PG17.4, pgvector 0.8.0, halfvec) - Planner Prefers Seq Scan
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #835: HNSW Index Not Used for KNN Queries (PG17.4, pgvector 0.8.0, halfvec) - Planner Prefers Seq Scan
@ 2025-05-05 07:18  "issahammoud (@issahammoud)" <noreply+issahammoud@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: issahammoud (@issahammoud) @ 2025-05-05 07:18 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

**Environment:**

- PostgreSQL Version: 17.4 (Debian 17.4-1.pgdg120+2)
- pgvector Version: 0.8.0 - Running official image via Docker (pgvector/pgvector:pg17)
- Table: articles (~1.05 million rows)
- Vector Column: embedding halfvec(1024)
- Index: embedding_index created with USING hnsw (embedding halfvec_ip_ops) WITH (m = 32, ef_construction = 128) (matches <#> operator)

**Problem Description:**

The PostgreSQL query planner consistently fails to utilize the embedding_index (HNSW, halfvec_ip_ops) for basic KNN searches (ORDER BY embedding <#> vector DESC LIMIT N). It instead defaults to a slow Parallel Seq Scan followed by a Sort, even for queries with LIMIT 10. This results in execution times of 30-70 seconds.

**Basic KNN Query & Result:**

```
-- SET app.vector = '[...]' -- Session variable set before running
EXPLAIN (ANALYZE, BUFFERS)
SELECT rowid
FROM articles
ORDER BY embedding::halfvec <#> current_setting('app.vector')::halfvec DESC
LIMIT 10;
```

**Result:** The plan consistently shows Parallel Seq Scan + Sort, not Index Scan. Execution time ~24-55 seconds.

```
                                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=151604.17..151605.34 rows=10 width=16) (actual time=29277.370..29324.927 rows=10 loops=1)
   Buffers: shared hit=3808544 read=553100
   ->  Gather Merge  (cost=151604.17..254322.34 rows=880380 width=16) (actual time=29207.001..29254.547 rows=10 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         Buffers: shared hit=3808544 read=553100
         ->  Sort  (cost=150604.15..151704.62 rows=440190 width=16) (actual time=29103.808..29103.831 rows=7 loops=3)
               Sort Key: (((embedding)::halfvec <#> (current_setting('app.vector'::text))::halfvec)) DESC
               Sort Method: top-N heapsort  Memory: 25kB
               Buffers: shared hit=3808544 read=553100
               Worker 0:  Sort Method: top-N heapsort  Memory: 25kB
               Worker 1:  Sort Method: top-N heapsort  Memory: 25kB
               ->  Parallel Seq Scan on articles  (cost=0.00..141091.80 rows=440190 width=16) (actual time=6.963..29028.874 rows=352235 loops=3)
                     Buffers: shared hit=3808479 read=553093
 Planning Time: 2.522 ms
 JIT:
   Functions: 7
   Options: Inlining false, Optimization false, Expressions true, Deforming true
   Timing: Generation 8.866 ms (Deform 1.187 ms), Inlining 0.000 ms, Optimization 14.263 ms, Emission 70.331 ms, Total 93.460 ms
 Execution Time: 29332.419 ms
(20 rows)
```

**Troubleshooting Steps Performed (Index Still Not Used):**

- Verified pgvector extension (0.8.0) is active.
- Confirmed PostgreSQL version is 17.4.
- Confirmed index exists, uses hnsw, and was created with the correct operator class (halfvec_ip_ops) matching the query operator (<#>).
- Executed REINDEX INDEX embedding_index; successfully.
- Executed VACUUM (ANALYZE, VERBOSE) articles; successfully after reindexing.
- Verified planner settings (enable_seqscan, enable_indexscan) are on.
- Tested with explicit casting (embedding::halfvec) in ORDER BY.
- Installed pg_hint_plan (1.7.0) and used /*+ IndexScan(articles embedding_index) */ hint; the hint was ignored, and the plan remained Seq Scan.
- Attempted to influence planner costs by significantly lowering random_page_cost; this had no effect on the chosen plan.
- A minimal test case on a small, new table using vector also failed to use the HNSW index scan.

Why is the query planner failing to select the HNSW index scan plan in this PG17.4 / pgvector 0.8.0 environment, even when hinted? Is this a known issue or regression, potentially related to halfvec or planner cost estimation in this version?

Thank you

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

* Re: [pgvector/pgvector] issue #835: HNSW Index Not Used for KNN Queries (PG17.4, pgvector 0.8.0, halfvec) - Planner Prefers Seq Scan
@ 2025-05-05 08:08  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

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

Hi @issahammoud, the index won't be used for `DESC` order ([docs](https://github.com/pgvector/pgvector?tab=readme-ov-file#why-isnt-a-query-using-an-index)).

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

* Re: [pgvector/pgvector] issue #835: HNSW Index Not Used for KNN Queries (PG17.4, pgvector 0.8.0, halfvec) - Planner Prefers Seq Scan
@ 2025-05-05 08:08  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

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

@ankane closed this.

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


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

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-05-05 07:18 [pgvector/pgvector] issue #835: HNSW Index Not Used for KNN Queries (PG17.4, pgvector 0.8.0, halfvec) - Planner Prefers Seq Scan "issahammoud (@issahammoud)" <noreply+issahammoud@github.com>
2025-05-05 08:08 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-05 08:08 ` "ankane (@ankane)" <noreply+ankane@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