pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #844: Questions on HNSW graph memory usage and size estimation
6+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] issue #844: Questions on HNSW graph memory usage and size estimation
@ 2025-05-25 05:50 "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
  0 siblings, 0 replies; 6+ messages in thread

From: developerayuva (@developerayuva) @ 2025-05-25 05:50 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi team,

Thanks for the great work on `pgvector`! I had a couple of questions related to HNSW index builds:

- When building an HNSW index, I saw the following notice:
```
NOTICE:  hnsw graph no longer fits into maintenance_work_mem after 5908085 tuples
DETAIL:  Building will take significantly more time.
HINT:  Increase maintenance_work_mem to speed up builds.
```
Is there a way to **estimate in advance** how much `maintenance_work_mem` is required so that the HNSW graph can fully fit into memory during index creation? This would help avoid performance hits and tune the setting properly.

- Once the index build is complete, is there a way to **check the size of the HNSW graph** that was built? Is it equivalent to the index size shown by `pg_relation_size()`, or is the actual graph size stored separately?

Thanks

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

* Re: [pgvector/pgvector] issue #844: Questions on HNSW graph memory usage and size estimation
@ 2025-05-25 12:03 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: jkatz (@jkatz) @ 2025-05-25 12:03 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> Is there a way to estimate in advance how much maintenance_work_mem is required so that the HNSW graph can fully fit into memory during index creation? 

I typically do `sizeof(# vectors) * 1.3` - where the `sizeof(vector)` is `8 + dims * 4`.

` Is it equivalent to the index size shown by pg_relation_size()`

You have to call `pg_relation_size` on the name of the index.

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

* Re: [pgvector/pgvector] issue #844: Questions on HNSW graph memory usage and size estimation
@ 2025-05-25 17:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

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

@ankane closed this.

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

* Re: [pgvector/pgvector] issue #844: Questions on HNSW graph memory usage and size estimation
@ 2025-05-25 18:07 ` "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: developerayuva (@developerayuva) @ 2025-05-25 18:07 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> I typically do sizeof(# vectors) * 1.3 - where the sizeof(vector) is 8 + dims * 4.

Thanks for the explanation!
I tried estimating the HNSW index size using the formula you shared
Here's my setup:
```
vector10m=> \d+ vector_items
                                                          Table "public.vector_items"
  Column   |   Type    | Collation | Nullable |                 Default                  | Storage  | Compression | Stats target | Description 
-----------+-----------+-----------+----------+------------------------------------------+----------+-------------+--------------+-------------
 id        | integer   |           | not null | nextval('vector_items_id_seq'::regclass) | plain    |             |              | 
 embedding | vector(5) |           |          |                                          | external |             |              | 
Indexes:
    "vector_items_pkey" PRIMARY KEY, btree (id)
    "vector_items_embedding_idx" hnsw (embedding vector_l2_ops) WITH (m='16', ef_construction='64')
Access method: heap

vector10m=> select count(*) from vector_items;
  count   
----------
 10000000
(1 row)

vector10m=> SELECT pg_size_pretty(pg_relation_size('vector_items_embedding_idx'));
 pg_size_pretty 
----------------
 3114 MB
(1 row)

vector10m=> show maintenance_work_mem ;
 maintenance_work_mem 
----------------------
 4028MB
(1 row)
```
Based on the formula, I expected:
`10000000 * (8 + (5 * 4)) * 1.3 = 364000000 Bytes = 364 MB ` 
However, the actual index size is 3114 MB, which is much higher.
Am I missing some additional components or overhead in the index that contribute to this size?


> You have to call pg_relation_size on the name of the index.

So, is the final index size (from `pg_relation_size`) the same as the size of the HNSW graph built in memory during index creation?

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

* Re: [pgvector/pgvector] issue #844: Questions on HNSW graph memory usage and size estimation
@ 2025-05-25 19:50 ` "ankane (@ankane)" <noreply+ankane@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: ankane (@ankane) @ 2025-05-25 19:50 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

There are two sizes:

1. The size of the in-memory graph during the index build
2. The size of the final index

A quick way to estimate 1 is to set `maintenance_work_mem` to 100MB, see how many tuples fit (from the notice), and extrapolate.

A quick way to estimate 2 is to create an index with the first 10k rows, check the size with `pg_relation_size`, and extrapolate.


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

* Re: [pgvector/pgvector] issue #844: Questions on HNSW graph memory usage and size estimation
@ 2025-05-25 20:21 ` "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
  4 siblings, 0 replies; 6+ messages in thread

From: developerayuva (@developerayuva) @ 2025-05-25 20:21 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks! Yes, extrapolation sounds like a good practical approach—I think that will help me moving forward.

Just a thought—do you think it might be useful to add a SQL function that helps estimate how much `maintenance_work_mem` would be needed for a given number of vectors and dimensions? Not sure how feasible it would be, but maybe it’s something worth exploring. 
I’d be happy to give it a try and contribute if it seems doable!

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


end of thread, other threads:[~2025-05-25 20:21 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-05-25 05:50 [pgvector/pgvector] issue #844: Questions on HNSW graph memory usage and size estimation "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
2025-05-25 12:03 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-05-25 17:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-25 18:07 ` "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
2025-05-25 19:50 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-25 20:21 ` "developerayuva (@developerayuva)" <noreply+developerayuva@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