From: leemeii (@leemeii) <noreply+leemeii@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #1045: HNSW amvalidate accepts operator classes with mismatched pg_amproc support-function types
Date: Thu, 22 Oct 2026 09:27:04 +0000
Message-ID: <gh-pgvector-pgvector-1045@github.com> (raw)
## 1. Summary
In pgvector, `hnswvalidate()` incorrectly accepts malformed HNSW operator-family metadata when the declared input types of a support function in `pg_amproc` do not match its actual SQL function signature.
Specifically, the support function is declared with:
- Declared left type: `halfvec`
- Declared right type: `vector`
- Actual function: `vector_l2_squared_distance(vector, vector)`
## 2. Reproduction Code
**Environment:**
- PostgreSQL: 18.6
- pgvector fixed version: 0.8.7
- Index: HNSW
- Distance metric: L2 (Euclid)
- Vector type: `vector(3)`
- Query: ANN Top-K (k = 2)
```sql
CREATE EXTENSION IF NOT EXISTS vector;
-- Clean up
DROP TABLE IF EXISTS repro_1045_data;
DROP OPERATOR CLASS IF EXISTS repro_1045_ops USING hnsw;
DROP OPERATOR FAMILY IF EXISTS repro_1045_family USING hnsw;
-- Create an HNSW operator family
CREATE OPERATOR FAMILY repro_1045_family
USING hnsw;
-- Add a valid vector/vector support function
CREATE OPERATOR CLASS repro_1045_ops
FOR TYPE vector USING hnsw
FAMILY repro_1045_family AS
OPERATOR 1 <-> (vector, vector)
FOR ORDER BY float_ops,
FUNCTION 1
vector_l2_squared_distance(vector, vector);
-- Add a malformed support-function declaration
-- Declared: (halfvec, vector)
-- Actual: (vector, vector)
ALTER OPERATOR FAMILY repro_1045_family
USING hnsw
ADD FUNCTION 1 (halfvec, vector)
vector_l2_squared_distance(vector, vector);
-- Create test data
CREATE TABLE repro_1045_data (
id integer,
v vector(3)
);
INSERT INTO repro_1045_data VALUES
(1, '[1,0,0]'),
(2, '[2,0,0]'),
(3, '[10,0,0]');
-- Check operator-class validation
SELECT
c.opcname,
p.amproclefttype::regtype AS left_type,
p.amprocrighttype::regtype AS right_type,
p.amproc::regprocedure AS actual_function,
amvalidate(c.oid) AS valid
FROM pg_opclass c
JOIN pg_amproc p
ON p.amprocfamily = c.opcfamily
WHERE c.opcname = 'repro_1045_ops'
ORDER BY left_type, right_type;
-- Create the HNSW index
CREATE INDEX repro_1045_idx
ON repro_1045_data
USING hnsw (v repro_1045_ops);
-- Force index usage for the small test dataset
SET enable_seqscan = off;
-- Check query execution plan
EXPLAIN (COSTS OFF)
SELECT
id,
v <-> '[0,0,0]'::vector AS distance
FROM repro_1045_data
ORDER BY v <-> '[0,0,0]'::vector, id
LIMIT 2;
-- Execute L2 Top-K query
SELECT
id,
v <-> '[0,0,0]'::vector AS distance
FROM repro_1045_data
ORDER BY v <-> '[0,0,0]'::vector, id
LIMIT 2;
```
## 3. Expected Behavior
The HNSW operator-class validator should reject the malformed support-function metadata because the declared `pg_amproc` argument types do not match the actual SQL function signature.
The expected validation result is:
```text
left_type | right_type | valid
----------+------------+------
halfvec | vector | false
vector | vector | false
```
Both rows show `false` because `amvalidate(c.oid)` validates the operator class as a whole, including its associated operator-family entries.
The valid `vector/vector` support function should still allow HNSW index creation and L2 Top-K queries.
Expected index creation:
```text
CREATE INDEX
```
Expected Top-K result:
```text
id | distance
----+----------
1 | 1
2 | 2
```
## 4. Actual Behavior
**On the affected version (before the fix):**
The validator incorrectly accepts the malformed support-function metadata because it does not properly check the declared left/right argument types in `pg_amproc`.
The buggy validation result is:
```text
left_type | right_type | valid
----------+------------+------
halfvec | vector | true
vector | vector | true
```
Because a valid `vector/vector` support function is also available, the HNSW index can be created successfully.
The malformed operator-family metadata is therefore incorrectly accepted without causing index creation failure or an incorrect query result.