pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #1045: HNSW amvalidate accepts operator classes with mismatched pg_amproc support-function types
3+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #1045: HNSW amvalidate accepts operator classes with mismatched pg_amproc support-function types
@ 2026-10-22 09:27  "leemeii (@leemeii)" <noreply+leemeii@github.com>
  0 siblings, 0 replies; 3+ messages in thread

From: leemeii (@leemeii) @ 2026-10-22 09:27 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

## 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.


^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] issue #1045: HNSW amvalidate accepts operator classes with mismatched pg_amproc support-function types
@ 2026-10-22 20:48  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-10-22 20:48 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 3+ messages in thread

* Re: [pgvector/pgvector] issue #1045: HNSW amvalidate accepts operator classes with mismatched pg_amproc support-function types
@ 2026-10-22 20:49  "ankane (@ankane)" <noreply+ankane@github.com>
  1 sibling, 0 replies; 3+ messages in thread

From: ankane (@ankane) @ 2026-10-22 20:49 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @leemeii, thanks for reporting. Fixed in the commit above. There are likely more improvements that can be made to these functions.

^ permalink  raw  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2026-10-22 20:49 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-22 09:27 [pgvector/pgvector] issue #1045: HNSW amvalidate accepts operator classes with mismatched pg_amproc support-function types "leemeii (@leemeii)" <noreply+leemeii@github.com>
2026-10-22 20:48 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-22 20:49 ` "ankane (@ankane)" <noreply+ankane@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