From: ankane (@ankane) <noreply+ankane@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #888: Add optimized implementations of binary_quantize
Date: Sat, 16 Aug 2025 03:56:58 +0000
Message-ID: <gh-pgvector-pgvector-888-c3193358120@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-888@github.com>
References: <gh-pgvector-pgvector-888@github.com>
Hi @lucagiac81, thanks for the PR. The auto-vectorized version seems like a good trade-off, so will do some testing.
The bigger issue is `binary_quantize` should only need to be called a single time for BQ w/ re-ranking queries (for the query vector).
```sql
SELECT * FROM (
SELECT * FROM items ORDER BY binary_quantize(embedding)::bit(3) <~> binary_quantize('[1,-2,3]') LIMIT 20
) ORDER BY embedding <=> '[1,-2,3]' LIMIT 5;
```
However, it's called once for every row in the inner query as well (20 extra times in the example above) due to the [current behavior](https://www.postgresql.org/message-id/flat/2ca5865b-4693-40e5-8f78-f3b45d5378fb%40iki.fi) of the Postgres executor (which impacts all nearest neighbor queries).