pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
10+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-03-29 10:04 "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
  0 siblings, 0 replies; 10+ messages in thread

From: spyderdfx (@spyderdfx) @ 2026-03-29 10:04 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi. So, I have pgvector 0.8.1, postgres 17.7, and a table like this with ~20kk records:
```ruby
create_table :image_hashes do |t|
  t.column :external_id, :integer, null: false
  t.column :vector_binary, :bit, limit: 576

  t.timestamps
end

add_index :image_hashes, :external_id, unique: true
execute 'CREATE INDEX ON image_hashes USING hnsw (vector_binary bit_hamming_ops);'
```

I clearly see that the needed record is in the table:
```
SELECT  (vector_binary <~> '<576 bits omitted>') AS distance FROM  image_hashes WHERE external_id = 608810322 LIMIT 1;
 distance 
----------
        0
(1 row)
```

But querying the nearest neighbors gives unexpected results:
```
SELECT  (vector_binary <~>  '<here are 576 bits>') AS distance FROM  image_hashes ORDER BY  (vector_binary <~>  '<576 bits omitted>') LIMIT 5;
 distance 
----------
      257
      257
      257
      257
      257
(5 rows)
```

Here's query plan:
```
 Limit  (cost=96.28..110.07 rows=5 width=8) (actual time=1.190..1.195 rows=5 loops=1)
   ->  Index Scan using image_hashes_vector_binary_idx on image_hashes  (cost=96.28..55469151.36 rows=20117568 width=8) (actual time=1.185..1.189 rows=5 loops=1)
         Order By: (vector_binary <~>  '<576 bits omitted>'::"bit")
 Planning Time: 1.797 ms
 Execution Time: 1.319 ms
(5 rows)
```

At first I thought that maybe something wrong with the index. I recreated it but nothing changed.
So, what am I doing wrong?

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-03-29 14:44 ` "ankane (@ankane)" <noreply+ankane@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: ankane (@ankane) @ 2026-03-29 14:44 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @spyderdfx, it looks like you're calculating distance with a different column in the second example (`mh_hash_vector_binary` instead of `vector_binary`).

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-03-29 14:44 ` "ankane (@ankane)" <noreply+ankane@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: ankane (@ankane) @ 2026-03-29 14:44 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-03-29 15:00 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: spyderdfx (@spyderdfx) @ 2026-03-29 15:00 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane sorry, it was a typo. It's supposed to be `vector_binary` only.
There are no other vector columns in the table

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-03-29 15:59 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: spyderdfx (@spyderdfx) @ 2026-03-29 15:59 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

So I tried bigger limit
```sql
SELECT  (vector_binary <~> '<576 bits omitted>') AS distance FROM  image_hashes ORDER BY  (vector_binary <~> '<576 bits omitted>') LIMIT 500;
 distance 
----------
      257
      257
..............
      257
      257
      257
(391 rows)
```
It produces only rows with Hamming distance of 257. That looks weird.

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-03-31 12:47 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: spyderdfx (@spyderdfx) @ 2026-03-31 12:47 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I tried to recreate the index with non-default params:
`CREATE INDEX ON image_hashes USING hnsw (vector_binary bit_hamming_ops) WITH (m = 16, ef_construction = 200);`

Results changed but still not good:
```sql
SELECT  (vector_binary <~> '<576 bits omitted>') AS distance FROM  image_hashes ORDER BY  (vector_binary <~> '<576 bits omitted>') LIMIT 10;
 distance 
----------
      241
      241
      245
      245
      246
      247
      247
      248
      248
      249
(10 rows)
```

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-03-31 19:22 ` "ankane (@ankane)" <noreply+ankane@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: ankane (@ankane) @ 2026-03-31 19:22 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

A few questions:

1. What are the results with `SET enable_indexscan = off;`?
2. Are there a lot of duplicate hashes in the table?
3. Which algorithm are you using for hashing?
4. What are the results with `SET hnsw.ef_search = 1000;`?

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-04-01 09:11 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: spyderdfx (@spyderdfx) @ 2026-04-01 09:11 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

1.
 ```sql
SELECT  (vector_binary <~> '<576 bits omitted>') AS distance FROM  image_hashes ORDER BY  (vector_binary <~> '<576 bits omitted>') LIMIT 10;
 distance 
----------
        0
      230
      231
      231
      232
      233
      233
      234
      234
      234
(10 rows)
```
2. There were many dups (like ~15kk out of 20kk). I deleted all of them but the query with index scan doesn't produce the row with distance of 0.
3. Wavelet hashing
4. Yeah, there is the needed row now (but after I deleted dups)
```sql
=> SET hnsw.ef_search = 1000;
SET
=> SELECT  (vector_binary <~> '<576 bits omitted>') AS distance FROM  image_hashes ORDER BY  (vector_binary <~> '<576 bits omitted>') LIMIT 10;
 distance 
----------
        0
      232
      234
      235
      236
      236
      237
      238
      238
      238
(10 rows)
```

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-04-01 18:22 ` "ankane (@ankane)" <noreply+ankane@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: ankane (@ankane) @ 2026-04-01 18:22 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks, it looks like the index is working fine. Approximate indexing won't provide perfect recall. Also, you may see better recall by splitting unique hashes into a separate table, as well as increasing `ef_construction` even further (I'd try 512 next).

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

* Re: [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results
@ 2026-04-01 21:27 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
  8 siblings, 0 replies; 10+ messages in thread

From: spyderdfx (@spyderdfx) @ 2026-04-01 21:27 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

So, that's expected behavior and I need to tune the index further? Thank you for explaining

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


end of thread, other threads:[~2026-04-01 21:27 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-29 10:04 [pgvector/pgvector] issue #972: Binary vectors - querying the nearest neighbors by Hamming distance with HNSW gives wrong results "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
2026-03-29 14:44 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-29 14:44 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-29 15:00 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
2026-03-29 15:59 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
2026-03-31 12:47 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
2026-03-31 19:22 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-04-01 09:11 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@github.com>
2026-04-01 18:22 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-04-01 21:27 ` "spyderdfx (@spyderdfx)" <noreply+spyderdfx@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