pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #690: Questions Regarding PGVector Index Size and Storage Structure
4+ messages / 3 participants
[nested] [flat]
* [pgvector/pgvector] issue #690: Questions Regarding PGVector Index Size and Storage Structure
@ 2024-10-03 09:13 "kwunhang (@kwunhang)" <noreply+kwunhang@github.com>
0 siblings, 0 replies; 4+ messages in thread
From: kwunhang (@kwunhang) @ 2024-10-03 09:13 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
1. I am testing PGVector using both ivfflat and HNSW, and I have a few questions. The testing data consists of 768-dimensional vectors using `Vector` and 2024-dimensional vectors using `halfvec`.
I checked the index size with the following code:
```sql
SELECT pg_size_pretty(pg_relation_size('index_name'));
```
I found that the index sizes of HNSW and ivfflat are very similar. For example, with 1 million data points of 768 dimensions, setting `nlist=1000` for ivfflat and `m=24`, `efConstruction=80` for HNSW, the index sizes are 3912MB and 3904MB, respectively.
May I ask why the index sizes of these two indexing methods are so similar?
2. I also have another question regarding how the index is stored in PGVector. I found issue #688 and [this article](https://lantern.dev/blog/pgvector-storage) for reference. I tried to read the source code to understand the structure, but I still have some misunderstandings.
<img width="832" alt="螢幕截圖 2024-10-03 下午5 10 53" src="https://github.com/user-attachments/assets/1e80e536-3121-4606-b745-f687dd19d44b";
When more data is inserted into the database, a HNSW graph is created, and elements are located at different levels. For example, consider an HNSW graph with 3 levels (level 0, 1, and 2) and `m=3`. For an element in level 1, my understanding is that the element would have 3 neighbors in level 1 and 6 neighbors in level 0.
In this case, how is the neighbor information stored in the page? Is there a computation of 2 neighbor arrays that are stored on the page, or is it handled in another way?
Thank you for your assistance!
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #690: Questions Regarding PGVector Index Size and Storage Structure
@ 2024-10-03 13:54 "jkatz (@jkatz)" <noreply+jkatz@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: jkatz (@jkatz) @ 2024-10-03 13:54 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
For 1, IVFFlat index sizes are effectively `2 * sizeof(vectors in table) + (nlists * sizeof(vector))`. HNSW sizes are impacted by `m`, as `m` probabilistically dictates the number of levels and the rough distribution of how many vectors are in each level.
Consider [this script][1] that emulates how level placement occurs (though note that L0 is an underestimate as it should include all vectors):
```python
from collections import defaultdict
import math
import random
def generate_hnsw_levels(n, m):
ml = 1 / math.log(m)
levels = defaultdict(int)
for _ in range(n):
level = math.floor(-math.log(random.random()) * ml);
levels[level] += 1
return levels
def print_levels(levels):
for k, v in sorted(levels.items()):
print(k, v)
levels = generate_hnsw_levels(1_000_000, 24)
print_levels(levels)
```
Here is the output for the 1M vectors and m=16/24 cases:
```
m=16
0 937392
1 58649
2 3705
3 240
4 13
5 1
```
```
m=24
0 958595
1 39709
2 1619
3 74
4 2
5 1
```
For m=16, there are ~62K vectors in the upper layers of the graph, whereas m=24 has 41K vectors. Given all vectors are in L0, this means m=24 will have a smaller index overall, and could have a size that's comparable to an IVFFlat index. However, under the default pgvector settings of m=16, using the full, unquantized vectors typically yields an index size that's ~1.3x larger than the vectors stored in the table.
[1]: https://gist.github.com/jkatz/5c70179064bc4df92e10bfdbf9701ece
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #690: Questions Regarding PGVector Index Size and Storage Structure
@ 2024-10-03 18:41 "ankane (@ankane)" <noreply+ankane@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: ankane (@ankane) @ 2024-10-03 18:41 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @kwunhang, they're similar sizes for the same reason as #688. Postgres uses 8KB pages, so in the 768 dimension case, two vectors will fit on a page (3KB each). That leaves around 2KB of space for other data. For IVFFlat, it's empty, and for HNSW, it stores the neighbors.
Neighbors for all levels (for a single element) are stored in a single tuple (in the `indextids` array), starting with the highest level.
```c
typedef struct HnswNeighborTupleData
{
uint8 type;
uint8 unused;
uint16 count;
ItemPointerData indextids[FLEXIBLE_ARRAY_MEMBER];
} HnswNeighborTupleData;
```
In your example, `count` and the length of `indextids` would be 9 (3 for level 1, 6 for level 0).
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #690: Questions Regarding PGVector Index Size and Storage Structure
@ 2024-10-03 18:41 "ankane (@ankane)" <noreply+ankane@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: ankane (@ankane) @ 2024-10-03 18:41 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 4+ messages in thread
end of thread, other threads:[~2024-10-03 18:41 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-10-03 09:13 [pgvector/pgvector] issue #690: Questions Regarding PGVector Index Size and Storage Structure "kwunhang (@kwunhang)" <noreply+kwunhang@github.com>
2024-10-03 13:54 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-10-03 18:41 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-03 18:41 ` "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