pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: redphosphorus7562 (@redphosphorus7562) <noreply+redphosphorus7562@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
Date: Fri, 20 Jun 2025 08:49:47 +0000
Message-ID: <gh-pgvector-pgvector-862@github.com> (raw)

Hi pgvector team,

I’m using PostgreSQL 17.5 with pgvector 0.8.0, and I’ve observed what seems to be inconsistent behavior when using the HNSW index with hnsw.iterative_scan = relaxed_order.

Here’s a minimal reproducible example:
```sql
CREATE EXTENSION vector;
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
INSERT INTO items (embedding)
SELECT ARRAY[random(), random(), random()]::vector FROM generate_series(1, 20000);
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
SET hnsw.iterative_scan = relaxed_order;
```
Now, when I run the following query:
```sql
EXPLAIN WITH a AS MATERIALIZED (
  SELECT embedding <=> '[3,1,2]' AS distance
  FROM items
  ORDER BY embedding <=> '[3,1,2]'
  LIMIT 40
)
SELECT * FROM a ORDER BY distance;
```
I get the following plan:
```
                                          QUERY PLAN
-------------------------------------------------------------------------------------------
 CTE Scan on a  (cost=103.57..104.37 rows=40 width=8)
   CTE a
     ->  Limit  (cost=95.53..103.57 rows=40 width=8)
           ->  Index Scan using items_embedding_idx on items  (cost=95.53..4112.00 rows=20000 width=8)
                 Order By: (embedding <=> '[3,1,2]'::vector)
(5 rows)
```
I expected PostgreSQL to add a Sort node outside the CTE to enforce the final order, since relaxed_order does not guarantee strict ordering of the index scan results. But instead, no extra sort is added, indicating that the planner believes the result is already fully ordered.
Similarly, for the subquery version:
```sql
EXPLAIN VERBOSE
SELECT * FROM (
  SELECT embedding <=> '[3,1,2]' AS distance
  FROM items
  ORDER BY embedding <=> '[3,1,2]'
  LIMIT 40
) sub
ORDER BY distance;
```
The plan is:
```
                                          QUERY PLAN
-------------------------------------------------------------------------------------------
 Limit  (cost=95.53..103.57 rows=40 width=8)
   Output: ((items.embedding <=> '[3,1,2]'::vector))
   ->  Index Scan using items_embedding_idx on public.items  (cost=95.53..4112.00 rows=20000 width=8)
         Output: (items.embedding <=> '[3,1,2]'::vector)
         Order By: (items.embedding <=> '[3,1,2]'::vector)
(5 rows)
```
Also lacks any external sort.

This implies that PostgreSQL is treating the HNSW index with relaxed_order as providing strictly sorted results — which contradicts the definition of “relaxed” ordering.

So my questions are:
	1.	Is this planner behavior expected?
	2.	Shouldn’t the planner be adding a Sort node when relaxed_order is used, since the returned tuples are not guaranteed to be strictly sorted?
	3.	On a related note: when I set hnsw.iterative_scan = strict_order, the result still doesn’t seem strictly ordered in some edge cases. Could you please clarify the expected guarantees provided by strict_order?

Thanks a lot for your excellent work on pgvector!

view thread (8+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-862@github.com>
Permalink:  ../gh-pgvector-pgvector-862@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