pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #785: Continued Issues with HNSW Index Queries Using ORDER BY and LIMIT Despite Parameter Adjustments
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #785: Continued Issues with HNSW Index Queries Using ORDER BY and LIMIT Despite Parameter Adjustments
@ 2025-02-25 09:09  "shikaiwei1 (@shikaiwei1)" <noreply+shikaiwei1@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: shikaiwei1 (@shikaiwei1) @ 2025-02-25 09:09 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

**Description:**  
After following recommendations from the previous issue (#719), I'm still encountering problems with HNSW index queries when combining `ORDER BY` and `LIMIT` clauses, particularly under specific filtering conditions. Adjusting parameters like `hnsw.iterative_scan`, `hnsw.scan_mem_multiplier`, and `hnsw.max_scan_tuples` partially mitigates the issue but introduces significant performance trade-offs.  

### Problem Details  
**Query Structure:**  
```sql  
SELECT  
  (vector <#> '[VECTOR]') AS score  
from modeldata 
            where 1=1
              AND dataset_id in ('67b709f9460b03446665ac40') 
              order by score 
              limit 100;
```  

**Expected Behavior:**  
The query should return the top 100 nearest neighbors efficiently, leveraging the HNSW index while applying filters.  

**Actual Behavior:**  
- **No results or incomplete results** unless specific parameters are adjusted.  
- Even with adjustments, performance degrades significantly (e.g., 229ms → 3228ms).  

---

### Reproduction Steps  
1. **Index Configuration:**  
   ```sql  
   CREATE INDEX vector_index ON public.modeldata USING hnsw (vector vector_ip_ops) WITH (m='32', ef_construction='128')
   ```  
2. **Query Execution:**  
   - Run the above query without parameter adjustments → **0 results**.  
   - Adjust parameters as follows:  
     ```sql  
     SET hnsw.iterative_scan = 'relaxed_order';  
     SET hnsw.scan_mem_multiplier = 2;  -- Higher values increase latency  
     SET hnsw.max_scan_tuples = 20000;  
     ```  
   → Returns **4/100 results** in 229ms.  
   - Further increase `scan_mem_multiplier` (e.g., 2) → Returns more results but with **~3x latency** (3228ms).  

---

### Test Results  
| Parameter                        | Results Returned | Latency  |  
|----------------------------------|-------------------|----------|  
| Default settings                 | 0                 | N/A      |  
| `relaxed_order` only             | 4                 | 229ms   |  
| `scan_mem_multiplier=2`          | Improved results  | 3228ms  |  
| `max_scan_tuples=20000`          | Partial results   | Variable|  

**Key Observations:**  
- `hnsw.iterative_scan=relaxed_order` helps but does not fully resolve recall issues.  
- Increasing memory/scan thresholds trades latency for completeness, but scalability concerns remain (e.g., billion-row datasets).  

---

### Environment  
- **pgvector Version:** 0.8.0  
- **PostgreSQL Version:** 15  .0
- **Index Type:** HNSW  
- **Data Scale:** Millions of rows (testing), targeting billions.  

---

### Request  
1. **Investigate why parameter adjustments are insufficient** to guarantee complete results without prohibitive latency.  
2. **Optimize HNSW index scans** to handle strict `ORDER BY + LIMIT` with filters more efficiently.  
3. Consider enabling iterative scans (`relaxed_order`) by default or improving documentation for such edge cases.  

This issue persists despite following recommendations from #719. Any guidance or optimizations would be greatly appreciated!  

**Related Issues:** #719, #721

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

* Re: [pgvector/pgvector] issue #785: Continued Issues with HNSW Index Queries Using ORDER BY and LIMIT Despite Parameter Adjustments
@ 2025-02-25 10:03  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2025-02-25 10:03 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @shikaiwei1, what's the output of `EXPLAIN (ANALYZE, BUFFERS)` for each of the queries? Also, how many rows are in the table, and how many match the `WHERE` condition?

A few initial suggestions:

1. Add a B-tree index on `dataset_id`
2. Use the default `m` of 16 instead of 32
3. Make sure `shared_buffers` is set correctly ([docs](https://github.com/pgvector/pgvector?tab=readme-ov-file#tuning))

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

* Re: [pgvector/pgvector] issue #785: Continued Issues with HNSW Index Queries Using ORDER BY and LIMIT Despite Parameter Adjustments
@ 2025-03-03 17:05  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2025-03-03 17:05 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-03-03 17:05 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-02-25 09:09 [pgvector/pgvector] issue #785: Continued Issues with HNSW Index Queries Using ORDER BY and LIMIT Despite Parameter Adjustments "shikaiwei1 (@shikaiwei1)" <noreply+shikaiwei1@github.com>
2025-02-25 10:03 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-03 17:05 ` "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