pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
8+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
@ 2025-06-20 08:49  "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
  0 siblings, 0 replies; 8+ messages in thread

From: redphosphorus7562 (@redphosphorus7562) @ 2025-06-20 08:49 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

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!

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

* Re: [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
@ 2025-06-20 12:48  "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2025-06-20 12:48 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @redphosphorus7562, for 1 and 2, the docs mention using a materialized CTE due to this behavior (the Postgres planner expects the index output to always be ordered, so it won't perform an additional sort for the 2nd query). For 3, can you share a minimal reproducible example?

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

* Re: [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
@ 2025-06-23 02:03  "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: redphosphorus7562 (@redphosphorus7562) @ 2025-06-23 02:03 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thank you for your reply,you can use this sql to reproduce :
```sql
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 = strict_order;
select embedding <=> '[3,2,1]' as distance from items order by distance limit 5000;
```
I sorted the results and compared them to each other, and found that some of the positions were not strictly ordered       

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

* Re: [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
@ 2025-06-23 05:55  "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: redphosphorus7562 (@redphosphorus7562) @ 2025-06-23 05:55 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Also, I have another question about relaxed_order.  @ankane 
<img width="848" alt="Image" src="https://github.com/user-attachments/assets/10296032-5c12-4640-9e9a-c5500bf46bfb"; />
As shown in the image above,the documentation states that using a CTE will ensure strict ordering, but I’m curious how that’s actually achieved — because from the plan, it doesn’t appear to do any explicit sorting (in PG17). In some older versions of PostgreSQL (e.g., 13), there _is_ an explicit sort.  
  
```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;
```

**In PG17 ,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)
```

**In PG13:**
```
                                              QUERY PLAN                                               
-------------------------------------------------------------------------------------------------------
 Sort  (cost=105.31..105.41 rows=40 width=8)
   Sort Key: a.distance
   CTE a
     ->  Limit  (cost=95.42..103.44 rows=40 width=8)
           ->  Index Scan using items_embedding_idx on items  (cost=95.42..4108.00 rows=20000 width=8)
                 Order By: (embedding <=> '[3,1,2]'::vector)
   ->  CTE Scan on a  (cost=0.00..0.80 rows=40 width=8)
(7 rows)
```

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

* Re: [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
@ 2025-07-01 17:01  "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2025-07-01 17:01 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks @redphosphorus7562.

The out-of-order results are due to the normalization with cosine distance, which can cause the index ordering to be slightly different than the heap (since floating point has limited precision). This can likely be addressed by storing the original vectors and L2 norm separately in the index (rather than storing normalized vectors).

For the materialized CTE, it looks like Postgres no longer adds a sort node with 17+, so users will need to do `ORDER BY distance + 0`. Will update the docs to reflect this.

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

* Re: [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
@ 2025-07-01 17:28  "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2025-07-01 17:28 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

For reference, here's the commit that changed the materialized CTE behavior: https://github.com/postgres/postgres/commit/a65724dfa73db8b451d0c874a9161935a34a914e

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

* Re: [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
@ 2025-07-02 02:15  "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: redphosphorus7562 (@redphosphorus7562) @ 2025-07-02 02:15 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks for the detailed explanation! That really helped clarify the issue.

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

* Re: [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index
@ 2025-07-02 02:15  "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: redphosphorus7562 (@redphosphorus7562) @ 2025-07-02 02:15 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@redphosphorus7562 closed this.

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


end of thread, other threads:[~2025-07-02 02:15 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-06-20 08:49 [pgvector/pgvector] issue #862: Inconsistent behavior with relaxed_order: planner assumes strict ordering from HNSW index "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
2025-06-20 12:48 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-06-23 02:03 ` "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
2025-06-23 05:55 ` "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
2025-07-01 17:01 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-07-01 17:28 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-07-02 02:15 ` "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@github.com>
2025-07-02 02:15 ` "redphosphorus7562 (@redphosphorus7562)" <noreply+redphosphorus7562@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