pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #1046: HNSW amvalidate accepts operator classes with no required support functions or operators
3+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #1046: HNSW amvalidate accepts operator classes with no required support functions or operators
@ 2026-10-24 01:54 "leemeii (@leemeii)" <noreply+leemeii@github.com>
0 siblings, 0 replies; 3+ messages in thread
From: leemeii (@leemeii) @ 2026-10-24 01:54 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
## 1. Summary
In pgvector, `hnswvalidate()` incorrectly accepts an empty HNSW operator class that contains neither the required ordering operator nor support function.
The affected implementation checks only existing catalog entries in `pg_amproc` and `pg_amop`. When both catalogs contain no entries for the operator family, the validation loops are skipped, causing `amvalidate()` to incorrectly return `true`.
## 2. Reproduction Code
**Environment:**
- PostgreSQL: 18.6
- pgvector: pre-fix and fixed versions
- Access method: HNSW
- Distance metric: L2 (Euclid)
- Data type: `vector(3)`
- Query: ANN Top-K (k = 2)
```sql
\pset pager off
\set ON_ERROR_STOP on
CREATE EXTENSION IF NOT EXISTS vector;
-- Cleanup
DROP TABLE IF EXISTS repro_1046_data;
DROP OPERATOR CLASS IF EXISTS repro_1046_ops USING hnsw;
DROP OPERATOR FAMILY IF EXISTS repro_1046_ops USING hnsw;
-- Phase 1: Create an empty HNSW operator class
CREATE OPERATOR CLASS repro_1046_ops
FOR TYPE vector USING hnsw AS
STORAGE vector;
-- Trigger the validation bug
SELECT
c.opcname,
(
SELECT count(*)
FROM pg_amproc p
WHERE p.amprocfamily = c.opcfamily
) AS support_functions,
(
SELECT count(*)
FROM pg_amop o
WHERE o.amopfamily = c.opcfamily
) AS operators,
amvalidate(c.oid) AS valid
FROM pg_opclass c
JOIN pg_am a ON a.oid = c.opcmethod
WHERE c.opcname = 'repro_1046_ops'
AND a.amname = 'hnsw';
-- Phase 2: Complete the same operator family
ALTER OPERATOR FAMILY repro_1046_ops
USING hnsw
ADD OPERATOR 1 <-> (vector, vector)
FOR ORDER BY float_ops;
ALTER OPERATOR FAMILY repro_1046_ops
USING hnsw
ADD FUNCTION 1 (vector, vector)
vector_l2_squared_distance(vector, vector);
-- Create test data
CREATE TABLE repro_1046_data (
id integer,
v vector(3)
);
INSERT INTO repro_1046_data VALUES
(1, '[1,0,0]'),
(2, '[2,0,0]'),
(3, '[10,0,0]');
-- Validate the completed operator class
SELECT
c.opcname,
(
SELECT count(*)
FROM pg_amproc p
WHERE p.amprocfamily = c.opcfamily
) AS support_functions,
(
SELECT count(*)
FROM pg_amop o
WHERE o.amopfamily = c.opcfamily
) AS operators,
amvalidate(c.oid) AS valid
FROM pg_opclass c
JOIN pg_am a ON a.oid = c.opcmethod
WHERE c.opcname = 'repro_1046_ops'
AND a.amname = 'hnsw';
-- Build an HNSW index using the same operator class
CREATE INDEX repro_1046_idx
ON repro_1046_data
USING hnsw (v repro_1046_ops);
-- Encourage index usage for the small dataset
SET enable_seqscan = off;
-- Verify HNSW index usage
EXPLAIN (COSTS OFF)
SELECT
id,
v <-> '[0,0,0]'::vector AS distance
FROM repro_1046_data
ORDER BY v <-> '[0,0,0]'::vector, id
LIMIT 2;
-- Execute ANN Top-K query
SELECT
id,
v <-> '[0,0,0]'::vector AS distance
FROM repro_1046_data
ORDER BY v <-> '[0,0,0]'::vector, id
LIMIT 2;
-- Cleanup
DROP TABLE repro_1046_data;
DROP OPERATOR CLASS repro_1046_ops USING hnsw;
DROP OPERATOR FAMILY repro_1046_ops USING hnsw;
```
## 3. Expected Behavior
The HNSW validator should reject an empty operator class because both the required ordering operator and support function are missing.
**Phase 1: Validation of the empty operator class**
```text
support_functions | operators | valid
------------------+-----------+-------
0 | 0 | false
```
An empty HNSW operator class must not be considered valid.
**Phase 2: Validation after completing the operator family**
After adding the required L2 ordering operator and support function, the same operator class should pass validation:
```text
support_functions | operators | valid
------------------+-----------+-------
1 | 1 | true
```
HNSW index creation should succeed:
```text
CREATE INDEX
```
The execution plan should contain:
```text
Index Scan using repro_1046_idx
```
The expected L2 Top-K query result is:
```text
id | distance
----+----------
1 | 1
2 | 2
```
**Expected ordered Top-K IDs: `[1, 2]`**
## 4. Actual Behavior
**On the buggy version (before the fix):**
The empty operator class is incorrectly accepted by `hnswvalidate()` even though no required HNSW entries exist.
```text
support_functions | operators | valid
------------------+-----------+-------
0 | 0 | true
```
This is the original #1046 validation failure: `amvalidate()` returns `true` for an operator class that lacks both required entries.
In the original reproducer, attempting index creation at this stage produces:
```text
ERROR: missing support function 1
```
**On the fixed version:**
The empty operator class is correctly rejected:
```text
support_functions | operators | valid
------------------+-----------+-------
0 | 0 | false
```
After completing the operator family, validation succeeds:
```text
support_functions | operators | valid
------------------+-----------+-------
1 | 1 | true
```
Index creation succeeds:
```text
CREATE INDEX
```
The observed execution plan uses the HNSW index:
```text
Limit
-> Incremental Sort
Sort Key: ((v <-> '[0,0,0]'::vector)), id
Presorted Key: ((v <-> '[0,0,0]'::vector))
-> Index Scan using repro_1046_idx
on repro_1046_data
Order By: (v <-> '[0,0,0]'::vector)
```
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] issue #1046: HNSW amvalidate accepts operator classes with no required support functions or operators
@ 2026-10-24 02:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2026-10-24 02:25 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @leemeii, `amvalidate` returns `false` as of 6a0bcc3526b6842fdec3f1d7a5d81103d23ed6e9.
^ permalink raw [nested|flat] 3+ messages in thread
* Re: [pgvector/pgvector] issue #1046: HNSW amvalidate accepts operator classes with no required support functions or operators
@ 2026-10-24 02:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
1 sibling, 0 replies; 3+ messages in thread
From: ankane (@ankane) @ 2026-10-24 02:25 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 3+ messages in thread
end of thread, other threads:[~2026-10-24 02:25 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-24 01:54 [pgvector/pgvector] issue #1046: HNSW amvalidate accepts operator classes with no required support functions or operators "leemeii (@leemeii)" <noreply+leemeii@github.com>
2026-10-24 02:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-10-24 02:25 ` "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