From: michaelbautin (@michaelbautin) <noreply+michaelbautin@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #769: Adding a precise formula to the README for estimating the amount of memory for HNSW indexing
Date: Tue, 28 Jan 2025 22:25:56 +0000
Message-ID: <gh-pgvector-pgvector-769-c2620181380@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-769@github.com>
References: <gh-pgvector-pgvector-769@github.com>
@ankane
> Hi @michaelbautin, unfortunately, opening another issue while not taking into account previous feedback isn't productive for the project, so closing this out. You're welcome to create an independent tool to calculate this.
I did attempt to take your feedback in https://github.com/pgvector/pgvector/pull/765#issuecomment-2617244649 into account about potentially adding this formula to the readme, and I opened this issue to hopefully see how exactly the readme could be updated. I am sorry if I misunderstood the meaning of that comment. Please let me know if you have changed your opinion about the usefulness of adding such a formula to the readme.
Here is an attempt to explain why the formula in #765 is very precise. Some preliminary definitions for context.
```
double geometricP = 1 - exp(-1 / ml);
/*
* Average level of a node. We subtract 1 because we number levels starting
* from 0. On average, a node is present at (avgLevel + 1) levels.
*/
double avgLevel = 1 / geometricP - 1;
```
I think this provides the explanation for the most part.
```
/*
* For each element we store:
* - HnswElementData struct
* - An array of pointers to neighbor arrays, as many as the number of
* levels where the element is present.
* - The neighbor arrays themselves. We need to take into account that the
* lowest level has a different number of neighbors per node than higher
* levels. But it also helps that every node is present at the lowest
* level.
*/
```
The actual formula itself is, with extra comments and unnecessary parentheses removed:
```
return MAXALIGN(sizeof(HnswElementData)) + /* HnswElementData struct */
(avgLevel + 1) * sizeof(HnswNeighborArrayPtr) + /* An array of pointers to neighbor arrays */
MAXALIGN(HNSW_NEIGHBOR_ARRAY_SIZE(HnswGetLayerM(m, 0))) + /* Neighbors at lowest level */
avgLevel * MAXALIGN(HNSW_NEIGHBOR_ARRAY_SIZE(m)) /* Neighbors at other levels */
```
The formula exactly retraces all allocations that HnswAlloc is called with, except for the variation in the random level, but the average value of the geometric distribution is estimated very precisely.
Here is the log message that I've obtained by running the modified pgvector code with #765 and indexing the deep-image-96-angular dataset with 9900000 vectors.
```
NOTICE: Tuples indexed in memory: 9990000, bytes used per tuple: 1066.157, estimated bytes per tuple: 1066.133, estimation error: -0.002217%
```
The important part is the estimation error that is less than 0.01% here: `-0.002217%`
If we need a safety margin in tuning of maintenance_work_mem, we could multiply the estimated memory by some factor such as 1.05 and/or add a fixed amount to it.
https://github.com/pgvector/pgvector?tab=readme-ov-file#index-build-time rightly points out that "Indexes build significantly faster when the graph fits into maintenance_work_mem". I am trying to make it easier for users to make sure that it does before spending hours or days trying to build the index.
Here some issues and related responses that amount to "setting maintenance_work_mem" higher. I believe these users would have benefited from knowing that their index build would be slow in advance.
- https://github.com/pgvector/pgvector/issues/300#issuecomment-1760522247 (users waited for 24 hours to build the index)
- https://github.com/pgvector/pgvector/issues/504#issuecomment-2020982889 (4 days or 6 days spent building the index)
- https://github.com/pgvector/pgvector/issues/629 (the user seems to have abandoned their pgvector evaluation due to indexing slowness).
I still believe the approach along the lines of what I am proposing would be a significant improvement in pgvector HNSW user experience with moderate to large datasets. Please let me know if you think if I am wrong. Thank you.