pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: hlinnaka (@hlinnaka) <noreply+hlinnaka@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #282: Hnsw iterator
Date: Sat, 30 Sep 2023 09:57:17 +0000
Message-ID: <gh-pgvector-pgvector-282-c1741729553@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-282@github.com>
References: <gh-pgvector-pgvector-282@github.com>

I want to bring up this topic that I also wrote in a comment on my branch:

Currently on 'master', 'ef' parameter controls how many candidates to return. In the new continuous or iterator mode, 'ef' controls how many candidates to maintain in the pool at all times. For the same index and parameters, the first candidate to return is the same in both modes, but after that, the continuous mode can yield slightly different results. The results in continuous mode are always at least as good as in the one-shot mode, but because we "top up" the pool of candidates as we go, we might return elements that are closer to the query vector, after we have already returned some farther away elements.

In other words, in one-shot mode, we always return 'ef' elements in the right order, but because this is aNN, they are not necessarily the true nearest elements. In continuous mode, we return all elements if you keep iterating, but **they might not be in the right order**.

That can be surprising, but I think it's fine if you're just doing a simple aNN search. However, the planner doesn't know that the ordering is approximate. That can affect the rest of the plan, for example:

```
prepare surprise(vector) as
select a.id, b.id, a.embedding <=> $1
from items_small a, items_small b
where (a.embedding <=> $1) = (b.embedding <=> $1)
ORDER BY (a.embedding <=> $1)
limit 1000;
postgres=# execute surprise('[-0.31533,0.25417,0.55528,-0.29229,-0.0027537,-1.1689,0.17151,0.060029,0.62897,0.17066,-0.14748,-0.31574,1.0416,-0.017593,-0.053196,0.62312,0.6691,-0.77713,1.4983,0.28086,-0.62815,0.10885,0.74758,-0.24324,0.3955,0.55461,-0.052652,-0.18485,0.51246,0.19892,0.00013484,-0.25888,-0.11083,-0.063943,-0.71447,0.054546,0.96663,0.31343,-0.3033,1.0454,0.82853,0.34762,-0.17433,0.46479,4.1116e-05,0.11956,0.17235,-0.48861,-0.29915,0.7081,0.10253,-0.27391,-1.1292,-0.69647,-0.58459,0.13411,0.27188,-0.47946,0.093771,-0.69395,0.074337,0.20074,0.12972,0.51127,-0.013941,-0.22493,0.80425,0.87931,0.33713,0.22308,-0.62297,-0.4134,0.60944,0.0014924,0.3207,0.26836,0.3099,0.076108,-0.031559,-0.26954,-0.59395,0.098534,0.29471,-0.39031,0.12679,-0.42897,0.20498,-0.60429,-0.27911,-0.44967,0.34115,-0.24527,0.02525,0.54558,0.63618,0.38963,0.10122,0.81177,-0.15689,0.034974]');
ERROR:  mergejoin input data is out of order
```

The plan is:
```
postgres=# explain execute surprise('[-0.31533,0.25417,0.55528,-0.29229,-0.0027537,-1.1689,0.17151,0.060029,0.62897,0.17066,-0.14748,-0.31574,1.0416,-0.017593,-0.053196,0.62312,0.6691,-0.77713,1.4983,0.28086,-0.62815,0.10885,0.74758,-0.24324,0.3955,0.55461,-0.052652,-0.18485,0.51246,0.19892,0.00013484,-0.25888,-0.11083,-0.063943,-0.71447,0.054546,0.96663,0.31343,-0.3033,1.0454,0.82853,0.34762,-0.17433,0.46479,4.1116e-05,0.11956,0.17235,-0.48861,-0.29915,0.7081,0.10253,-0.27391,-1.1292,-0.69647,-0.58459,0.13411,0.27188,-0.47946,0.093771,-0.69395,0.074337,0.20074,0.12972,0.51127,-0.013941,-0.22493,0.80425,0.87931,0.33713,0.22308,-0.62297,-0.4134,0.60944,0.0014924,0.3207,0.26836,0.3099,0.076108,-0.031559,-0.26954,-0.59395,0.098534,0.29471,-0.39031,0.12679,-0.42897,0.20498,-0.60429,-0.27911,-0.44967,0.34115,-0.24527,0.02525,0.54558,0.63618,0.38963,0.10122,0.81177,-0.15689,0.034974]');
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         QUERY PLAN                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=65.20..89.29 rows=1000 width=16)
   ->  Merge Join  (cost=65.20..424656.30 rows=17625172 width=16)
         Merge Cond: ((a.embedding <=> '[-0.31533,0.25417,0.55528,-0.29229,-0.0027537,-1.1689,0.17151,0.060029,0.62897,0.17066,-0.14748,-0.31574,1.0416,-0.017593,-0.053196,0.62312,0.6691,-0.77713,1.4983,0.28086,-0.62815,0.10885,0.74758,-0.24324,0.3955,0.55461,-0.052652,-0.18485,0.51246,0.19892,0.00013484,-0.25888,-0.11083,-0.063943,-0.71447,0.054546,0.96663,0.31343,-0.3033,1.0454,0.82853,0.34762,-0.17433,0.46479,4.1116e-05,0.11956,0.17235,-0.48861,-0.29915,0.7081,0.10253,-0.27391,-1.1292,-0.69647,-0.58459,0.13411,0.27188,-0.47946,0.093771,-0.69395,0.074337,0.20074,0.12972,0.51127,-0.013941,-0.22493,0.80425,0.87931,0.33713,0.22308,-0.62297,-0.4134,0.60944,0.0014924,0.3207,0.26836,0.3099,0.076108,-0.031559,-0.26954,-0.59395,0.098534,0.29471,-0.39031,0.12679,-0.42897,0.20498,-0.60429,-0.27911,-0.44967,0.34115,-0.24527,0.02525,0.54558,0.63618,0.38963,0.10122,0.81177,-0.15689,0.034974]'::vector) = (b.embedding <=> '[-0.31533,0.25417,0.55528,-0.29229,-0.0027537,-1.1689,0.17151,0.060029,0.62897,0.17066,-0.14748,-0.31574,1.0416,-0.017593,-0.053196,0.62312,0.6691,-0.77713,1.4983,0.28086,-0.62815,0.10885,0.74758,-0.24324,0.3955,0.55461,-0.052652,-0.18485,0.51246,0.19892,0.00013484,-0.25888,-0.11083,-0.063943,-0.71447,0.054546,0.96663,0.31343,-0.3033,1.0454,0.82853,0.34762,-0.17433,0.46479,4.1116e-05,0.11956,0.17235,-0.48861,-0.29915,0.7081,0.10253,-0.27391,-1.1292,-0.69647,-0.58459,0.13411,0.27188,-0.47946,0.093771,-0.69395,0.074337,0.20074,0.12972,0.51127,-0.013941,-0.22493,0.80425,0.87931,0.33713,0.22308,-0.62297,-0.4134,0.60944,0.0014924,0.3207,0.26836,0.3099,0.076108,-0.031559,-0.26954,-0.59395,0.098534,0.29471,-0.39031,0.12679,-0.42897,0.20498,-0.60429,-0.27911,-0.44967,0.34115,-0.24527,0.02525,0.54558,0.63618,0.38963,0.10122,0.81177,-0.15689,0.034974]'::vector))
         ->  Index Scan using items_small_embedding_idx on items_small a  (cost=32.60..13822.32 rows=59372 width=412)
               Order By: (embedding <=> '[-0.31533,0.25417,0.55528,-0.29229,-0.0027537,-1.1689,0.17151,0.060029,0.62897,0.17066,-0.14748,-0.31574,1.0416,-0.017593,-0.053196,0.62312,0.6691,-0.77713,1.4983,0.28086,-0.62815,0.10885,0.74758,-0.24324,0.3955,0.55461,-0.052652,-0.18485,0.51246,0.19892,0.00013484,-0.25888,-0.11083,-0.063943,-0.71447,0.054546,0.96663,0.31343,-0.3033,1.0454,0.82853,0.34762,-0.17433,0.46479,4.1116e-05,0.11956,0.17235,-0.48861,-0.29915,0.7081,0.10253,-0.27391,-1.1292,-0.69647,-0.58459,0.13411,0.27188,-0.47946,0.093771,-0.69395,0.074337,0.20074,0.12972,0.51127,-0.013941,-0.22493,0.80425,0.87931,0.33713,0.22308,-0.62297,-0.4134,0.60944,0.0014924,0.3207,0.26836,0.3099,0.076108,-0.031559,-0.26954,-0.59395,0.098534,0.29471,-0.39031,0.12679,-0.42897,0.20498,-0.60429,-0.27911,-0.44967,0.34115,-0.24527,0.02525,0.54558,0.63618,0.38963,0.10122,0.81177,-0.15689,0.034974]'::vector)
         ->  Materialize  (cost=32.60..13970.75 rows=59372 width=412)
               ->  Index Scan using items_small_embedding_idx on items_small b  (cost=32.60..13822.32 rows=59372 width=412)
                     Order By: (embedding <=> '[-0.31533,0.25417,0.55528,-0.29229,-0.0027537,-1.1689,0.17151,0.060029,0.62897,0.17066,-0.14748,-0.31574,1.0416,-0.017593,-0.053196,0.62312,0.6691,-0.77713,1.4983,0.28086,-0.62815,0.10885,0.74758,-0.24324,0.3955,0.55461,-0.052652,-0.18485,0.51246,0.19892,0.00013484,-0.25888,-0.11083,-0.063943,-0.71447,0.054546,0.96663,0.31343,-0.3033,1.0454,0.82853,0.34762,-0.17433,0.46479,4.1116e-05,0.11956,0.17235,-0.48861,-0.29915,0.7081,0.10253,-0.27391,-1.1292,-0.69647,-0.58459,0.13411,0.27188,-0.47946,0.093771,-0.69395,0.074337,0.20074,0.12972,0.51127,-0.013941,-0.22493,0.80425,0.87931,0.33713,0.22308,-0.62297,-0.4134,0.60944,0.0014924,0.3207,0.26836,0.3099,0.076108,-0.031559,-0.26954,-0.59395,0.098534,0.29471,-0.39031,0.12679,-0.42897,0.20498,-0.60429,-0.27911,-0.44967,0.34115,-0.24527,0.02525,0.54558,0.63618,0.38963,0.10122,0.81177,-0.15689,0.034974]'::vector)
(8 rows)
```

I'm not sure what to think of this. The planner doesn't have a concept of an approximate sort order. Perhaps we could drop the information that a path is sorted after filtering has been done, but I'm afraid that would prevent the index scan from being chosen in a lot of scenarios that would be fine. The current situation, without this PR, where the index scan doesn't return all the rows in the table also violates the planner's assumptions, just differently, and it's perhaps less likely to throw errors :shrug:.

view thread (33+ messages)  latest in thread

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