pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: RakshitKhajuria (@RakshitKhajuria) <noreply+RakshitKhajuria@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #663:  Issue with HNSW Index Utilization for Cosine Similarity Searches Using <=> Operator
Date: Wed, 28 Aug 2024 07:22:35 +0000
Message-ID: <gh-pgvector-pgvector-663@github.com> (raw)

## Issue with Index Utilization for Cosine Similarity Searches Using `<=>` Operator

### Description
I am currently working with PostgreSQL and using the HNSW index to optimize vector similarity searches. My aim is to leverage the `<=>` operator for cosine similarity measures. However, I've encountered an issue where this operator does not utilize the HNSW index as expected, resulting in a full sequential scan rather than an indexed scan.

### Details
- **Index used**: HNSW
- **Data type**: vector 

### Observations
I conducted several tests using `EXPLAIN ANALYZE` to better understand the query planner's behavior. Here's what I found:
- When using the `<->` operator (designed for Euclidean distance), the query planner successfully uses the HNSW index, resulting in significantly faster query times.
- Conversely, when I switch to the `<=>` operator for cosine similarity, PostgreSQL reverts to a full sequential scan. This seems to indicate a lack of support or optimization for cosine similarity in conjunction with the HNSW index.

#### Query Details
Here are the queries I executed:

1. **Euclidean Distance (Indexed)**:

```sql
   EXPLAIN ANALYZE SELECT * FROM my_table ORDER BY vector_column <-> query_vector LIMIT 10;
```

Result: Index Scan using my_table_vector_idx on my_table

2. Cosine Similarity (Sequential Scan):

```sql
EXPLAIN ANALYZE SELECT * FROM my_table ORDER BY vector_column <=> query_vector LIMIT 10;
```
Result: Sequential Scan on my_table

## Request
Could you please provide insights into:

- Whether the <=> operator is intended to support HNSW index utilization for cosine similarity in PostgreSQL?
- Any potential configuration or implementation steps I might have missed that could enable indexed searches with cosine similarity?
- If this functionality is not currently supported, could it be considered for future development?

view thread (4+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-663@github.com>
Permalink:  ../gh-pgvector-pgvector-663@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