From: Wulfsta (@Wulfsta) <noreply+Wulfsta@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #901: How do I extend pgvector with a custom distance metric?
Date: Tue, 30 Sep 2025 20:07:52 +0000
Message-ID: <gh-pgvector-pgvector-901-c3353629875@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-901@github.com>
References: <gh-pgvector-pgvector-901@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...