Raw 1-bit binary quantization maps every positive float to 1 and negative float to 0, discarding all magnitude information. For 1536-dimensional vectors (especially when not centered around zero mean), ranking strictly by Hamming distance loses fine-grained ordering among the top ~50 candidates.
The fix in both pgvector 0.8+ and Qdrant is Two-Stage Oversampling + Rescoring:
- Stage 1 (In-RAM HNSW on
bit(1536)): Fetch 5x to 10x candidates (50–100 rows instead of 10) using fast Hamming distance (<~>).
- Stage 2 (On-Disk Rescore on
halfvec(1536)): Compute exact cosine distance (<=>) only on those 80 candidate rows! Reading 80 rows from NVMe page cache takes <1.5ms while keeping 95% of your vectors out of hot RAM.
1. Exact SQL Pattern in pgvector 0.8+
Store an expression HNSW index over binary_quantize(embedding)::bit(1536) in RAM, and rescore against halfvec(1536) on disk:
-- Build compact binary HNSW index (~230 MB for 1.2M vectors)
CREATE INDEX memories_bq_hnsw_idx ON agent_memories
USING hnsw ((binary_quantize(embedding)::bit(1536)) bit_hamming_ops)
WITH (m = 16, ef_construction = 64);
-- Query with 8x oversampling + exact halfvec rescoring
WITH candidates AS (
SELECT id, content, embedding
FROM agent_memories
ORDER BY binary_quantize(embedding)::bit(1536) <~> binary_quantize($1::halfvec)::bit(1536)
LIMIT 80
)
SELECT id, content, (embedding <=> $1::halfvec) AS exact_distance
FROM candidates
ORDER BY exact_distance ASC
LIMIT 10;
2. Equivalent Configuration in Qdrant
In Qdrant, enable always_ram=True for the quantized index and rescore=True with oversampling=4.0 at query time:
from qdrant_client.models import SearchParams, QuantizationSearchParams
hits = client.search(
collection_name="agent_memories",
query_vector=query_vec,
limit=10,
search_params=SearchParams(
quantization=QuantizationSearchParams(
rescore=True,
oversampling=4.0, # Fetches 40 binary candidates, rescores exact FP16 on disk
)
),
)
Benchmark Results on 1.2M × 1536d Vectors:
- BQ Only (No Rescore): 230 MB index RAM, 86.1% recall@10.
- BQ + 8x Oversampling +
halfvec Rescore: 230 MB index RAM, 97.1% recall@10, 8.4ms p95 latency.