pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
13+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-26 04:12 "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
0 siblings, 0 replies; 13+ messages in thread
From: Wulfsta (@Wulfsta) @ 2025-09-26 04:12 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I am interested in experimenting with a metric that is not included in pgvector on HNSW indices, can you provide a high level overview of what has to be done to support a new distance metric? I may be missing it, but a developer FAQ would be very helpful!
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-26 04:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2025-09-26 04:19 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-26 04:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2025-09-26 04:19 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @Wulfsta, check out #234. There's an example of how to add one in SQL (although it isn't officially supported), as well as a branch with how to add one in C.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-26 04:49 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: Wulfsta (@Wulfsta) @ 2025-09-26 04:49 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks, I’ll check it out!
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-26 05:32 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: Wulfsta (@Wulfsta) @ 2025-09-26 05:32 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane it's probably worth asking as well - must these functions be unary? What I want to experiment with is a sort of filtering directly in the vectors (I have read the past filtering discussions here). I don't know if this is a common approach, so perhaps this is a naive proposal, but I want to sum two distance metrics, where the first applies to the first 16 bytes and the second applies to the rest of the vector. To demonstrate in SQL, what I am visualizing is as follows
```
CREATE INDEX idx_compound_hnsw ON uuid_embedding_table USING hnsw (uuid, embedding vector_cosine_ops)
```
to accelerate
```
SELECT * FROM uuid_embedding_table WHERE uuid = $1 ORDER BY embedding <=> $2
```
where the compound index is defined as:
- a HNSW index using a single distance metric that is the sum of
- the Hamming distance of `uuid` and `$1`
- half the cosine distance of the `embedding` column and `$2`.
Note that given distance metrics `d_1(a, b)` where `a`, `b` are in `M` and `d_2(c, d)` where `c`, `d` are in `N`, then `d((a, c), (b, d)) = d_1(a, b) + d_2(c, d)` still satisfies the constraints of a metric. That is, the sum of the Hamming distance of two UUIDs converted into bits and the cosine distance of two embeddings can be used as a distance metric. In particular, note that the Hamming distance is larger than half the cosine distance unless the UUIDs are equal (this idea should work for other distance metrics, as long as the Hamming distance of the UUIDs dominates the sum, or is scaled to ensure it does). This should make it such that a single HNSW index can support this style of filtering directly?
Anyhow, I could probably hack this into place by storing the UUIDs as part of the vectors and doing some byte array slicing and type conversion? This thought experiment is starting to look like it would require [a lot](https://github.com/pgvector/pgvector/blob/4cef9213ec81b8609bd06cb8ca02fee7c3c3f05d/src/hnsw.c#L277) of modification to pgvector, and I'm curious what your thoughts are about this?
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-26 05:42 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2025-09-26 05:42 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
It sounds like you're trying to implement something similar to HQANN, which requires a lot of changes ([hqann2 branch](https://github.com/pgvector/pgvector/compare/hqann2)).
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-26 05:44 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: Wulfsta (@Wulfsta) @ 2025-09-26 05:44 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Ah, I will have to give that paper a read to see how that algorithm works. I think this was mentioned in another filtering discussion but I didn't investigate the specifics.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-26 05:52 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: Wulfsta (@Wulfsta) @ 2025-09-26 05:52 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Oh, section 3.1 of [HQANN: Efficient and Robust Similarity Search for Hybrid Queries with Structured and Unstructured Constraints](https://arxiv.org/pdf/2207.07940.pdf) is exactly the same idea! May I ask if you have plans to merge that branch in the future? I am also curious about the results you have seen from your implementation - does distorting the metric space like this work for these types of filtered searches?
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-30 20:07 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: Wulfsta (@Wulfsta) @ 2025-09-30 20:07 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane, if I want to hack something like this in, am I on the right track with the following?
```
CREATE FUNCTION hqann_like_metric(a halfvec, b halfvec) RETURNS float8
LANGUAGE SQL IMMUTABLE STRICT PARALLEL SAFE
-- embed each byte of the uuid as an integer in fp16 (fp16 can handle integers up to 2^11)
RETURN ln(exp(1.0) * l1_distance(subvector(a, 1, 16), subvector(b, 1, 16)) + 1) + cosine_distance(subvector(a, 17, vector_dims(a)), subvector(b, 17, vector_dims(b)));
CREATE OPERATOR <@> (
LEFTARG = halfvec, RIGHTARG = halfvec, PROCEDURE = hqann_like_metric,
COMMUTATOR = '<@>'
);
CREATE OPERATOR CLASS vector_hqann_like_metric_ops
FOR TYPE halfvec USING hnsw AS
OPERATOR 1 <@> (halfvec, halfvec) FOR ORDER BY float_ops,
FUNCTION 1 hqann_like_metric(halfvec, halfvec),
CREATE INDEX ON denormalized_search USING hnsw (combined_embedding vector_hqann_like_metric_ops);
```
I assume this is not ideal due to the slow speed at which this function will execute, and really should be added closer to the metal, but I am trying to understand #234. I am not quite following the support functions `FUNCTION 1 ...` - in fact, the whole linking system for PostgreSQL is quite opaque to me...
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-30 20:36 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2025-09-30 20:36 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @Wulfsta, you'll need to use a language other than `SQL` for the function (like `plpgsql`), but the rest looks reasonable (support function 1 is the distance function for HNSW). Also, need a semi-colon instead of a comma at the end of `CREATE OPERATOR CLASS`.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-30 20:43 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: Wulfsta (@Wulfsta) @ 2025-09-30 20:43 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Do I also need to add function 3 for halfvec support? Additionally, what is function 2?
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-30 20:48 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2025-09-30 20:48 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Support function 2 is normalization (which isn't needed here), and support function 3 is type support, which is needed for halfvec.
```sql
FUNCTION 3 hnsw_halfvec_support(internal);
```
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
@ 2025-09-30 20:57 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: Wulfsta (@Wulfsta) @ 2025-09-30 20:57 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Great, thanks! I'll give it a try.
^ permalink raw [nested|flat] 13+ messages in thread
end of thread, other threads:[~2025-09-30 20:57 UTC | newest]
Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-09-26 04:12 [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric? "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
2025-09-26 04:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-09-26 04:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-09-26 04:49 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
2025-09-26 05:32 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
2025-09-26 05:42 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-09-26 05:44 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
2025-09-26 05:52 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
2025-09-30 20:07 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
2025-09-30 20:36 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-09-30 20:43 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@github.com>
2025-09-30 20:48 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-09-30 20:57 ` "Wulfsta (@Wulfsta)" <noreply+Wulfsta@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