pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
8+ messages / 3 participants
[nested] [flat]

* [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
@ 2026-03-15 08:48 "R3gardless (@R3gardless)" <noreply+R3gardless@github.com>
  0 siblings, 0 replies; 8+ messages in thread

From: R3gardless (@R3gardless) @ 2026-03-15 08:48 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi,

I'm currently testing HNSW index build performance while varying `maintenance_work_mem`.

**Environment**
- pgvector 0.8.2 compiled with GCC 14.2.1
- HNSW params : m = 16, ef_construction = 64
- max_parallel_workers = 16
- Table Quantization type : vector

In my environment it is difficult to set `maintenance_work_mem` large enough to fit the entire index in memory due to memory limits and potential OOM risks.

I tested with `maintenance_work_mem` values of 1GB, 10GB, 20GB and 30GB (each run repeated 3 times, values shown are averages of index build time)

<img width="1935" height="886" alt="Image" src="https://github.com/user-attachments/assets/5bd7d361-f34e-4644-97f6-2f7d1b919f74"; />

My understanding is that increasing `maintenance_work_mem` allows more of the index build process to be performed in memory, and if the memory limit is exceeded, the build may spill to disk. (https://github.com/pgvector/pgvector?tab=readme-ov-file#index-build-time)

But for the vector (no quantization) index increasing `maintenance_work_mem` from 1GB to 30GB did not significantly reduce build time. Besides, halfvec and bit quantized indexes showed a clear decrease in build time as `maintenance_work_mem` increased.

Is there still a performance benefit to increasing `maintenance_work_mem` for full-precision vector indexes when it is not large enough to hold the entire index in memory? Or does the benefit only become meaningful once the index fits entirely in memory?

Thank you.

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

* Re: [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
@ 2026-03-15 17:42 ` "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2026-03-15 17:42 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @R3gardless, there should still be a benefit. At what time and how many tuples are you seeing the `hnsw graph no longer fits into maintenance_work_mem after N tuples` message for each value of `maintenance_work_mem` for the vector case?

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

* Re: [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
@ 2026-03-16 02:41 ` "R3gardless (@R3gardless)" <noreply+R3gardless@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: R3gardless (@R3gardless) @ 2026-03-16 02:41 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane thank you for response!

I attached logs from tests with varying `maintenance_work_mem` values (1GB, 10GB and 20GB.)

**`maintenance_work_mem` 1GB**
```
2026-03-13 10:27:16 KST [00000] [3475327 (12)] vectordb@127.0.0.1(60648)/vectordb LOG:  statement: ALTER USER "vectordb" SET maintenance_work_mem TO '1GB';
2026-03-13 10:27:16 KST [00000] [3475327 (13)] vectordb@127.0.0.1(60648)/vectordb LOG:  statement: ALTER USER "vectordb" SET max_parallel_maintenance_workers TO '16';
2026-03-13 10:27:16 KST [00000] [3475327 (14)] vectordb@127.0.0.1(60648)/vectordb LOG:  statement: ALTER USER "vectordb" SET max_parallel_workers TO '16';
2026-03-13 10:27:16 KST [00000] [3475327 (15)] vectordb@127.0.0.1(60648)/vectordb LOG:  statement: ALTER TABLE "vdbbench_table_test" SET (parallel_workers = 16);
2026-03-13 10:27:17 KST [00000] [3475327 (16)] vectordb@127.0.0.1(60648)/vectordb DEBUG:  sending cancel to blocking autovacuum PID 3469924
2026-03-13 10:27:17 KST [00000] [3475327 (17)] vectordb@127.0.0.1(60648)/vectordb DETAIL:  Process 3475327 waits for ShareUpdateExclusiveLock on relation 25373 of database 16385.
2026-03-13 10:27:17 KST [57014] [3469924 (3)] @/ ERROR:  canceling autovacuum task
2026-03-13 10:27:17 KST [57014] [3469924 (4)] @/ CONTEXT:  while scanning block 1207948 of relation "public.vdbbench_table_test"
  automatic vacuum of table "vectordb.public.vdbbench_table_test"
2026-03-13 10:27:17 KST [00000] [3475327 (18)] vectordb@127.0.0.1(60648)/vectordb LOG:  duration: 1001.047 ms
2026-03-13 10:27:17 KST [00000] [3475327 (19)] vectordb@127.0.0.1(60648)/vectordb LOG:  statement: 
                  CREATE INDEX IF NOT EXISTS  "pgvector_index"  ON public. "vdbbench_table_test" 
                  USING  "hnsw"  (embedding  "vector_cosine_ops" )
                   WITH ( "m" = "16", "ef_construction" = "64" );
2026-03-13 10:27:17 KST [00000] [3475327 (20)] vectordb@127.0.0.1(60648)/vectordb DEBUG:  building index "pgvector_index" on table "vdbbench_table_test" serially
2026-03-13 10:28:23 KST [00000] [3475333 (1)] @/ NOTICE:  hnsw graph no longer fits into maintenance_work_mem after 284896 tuples
2026-03-13 10:28:23 KST [00000] [3475333 (2)] @/ DETAIL:  Building will take significantly more time.
2026-03-13 10:28:23 KST [00000] [3475333 (3)] @/ HINT:  Increase maintenance_work_mem to speed up builds.
2026-03-13 10:28:25 KST [00000] [3475327 (22)] vectordb@127.0.0.1(60648)/vectordb NOTICE:  hnsw graph no longer fits into maintenance_work_mem after 284896 tuples
2026-03-13 10:28:25 KST [00000] [3475327 (23)] vectordb@127.0.0.1(60648)/vectordb DETAIL:  Building will take significantly more time.
2026-03-13 10:28:25 KST [00000] [3475327 (24)] vectordb@127.0.0.1(60648)/vectordb HINT:  Increase maintenance_work_mem to speed up builds.
2026-03-13 12:19:24 KST [00000] [3475327 (1045)] vectordb@127.0.0.1(60648)/vectordb LOG:  duration: 6726699.766 ms
```

**`maintenance_work_mem` 10GB**
```
2026-03-13 17:18:39 KST [00000] [3594164 (12)] vectordb@127.0.0.1(35526)/vectordb LOG:  statement: ALTER USER "vectordb" SET maintenance_work_mem TO '10GB';
2026-03-13 17:18:39 KST [00000] [3594164 (13)] vectordb@127.0.0.1(35526)/vectordb LOG:  statement: ALTER USER "vectordb" SET max_parallel_maintenance_workers TO '16';
2026-03-13 17:18:39 KST [00000] [3594164 (14)] vectordb@127.0.0.1(35526)/vectordb LOG:  statement: ALTER USER "vectordb" SET max_parallel_workers TO '16';
2026-03-13 17:18:39 KST [00000] [3594164 (15)] vectordb@127.0.0.1(35526)/vectordb LOG:  statement: ALTER TABLE "vdbbench_table_test" SET (parallel_workers = 16);
2026-03-13 17:18:40 KST [00000] [3594164 (16)] vectordb@127.0.0.1(35526)/vectordb DEBUG:  sending cancel to blocking autovacuum PID 3589517
2026-03-13 17:18:40 KST [00000] [3594164 (17)] vectordb@127.0.0.1(35526)/vectordb DETAIL:  Process 3594164 waits for ShareUpdateExclusiveLock on relation 25397 of database 16385.
2026-03-13 17:18:40 KST [57014] [3589517 (3)] @/ ERROR:  canceling autovacuum task
2026-03-13 17:18:40 KST [57014] [3589517 (4)] @/ CONTEXT:  while scanning block 1089328 of relation "public.vdbbench_table_test"
  automatic vacuum of table "vectordb.public.vdbbench_table_test"
2026-03-13 17:18:40 KST [00000] [3594164 (18)] vectordb@127.0.0.1(35526)/vectordb LOG:  duration: 1000.895 ms
2026-03-13 17:18:40 KST [00000] [3594164 (19)] vectordb@127.0.0.1(35526)/vectordb LOG:  statement: 
                  CREATE INDEX IF NOT EXISTS  "pgvector_index"  ON public. "vdbbench_table_test" 
                  USING  "hnsw"  (embedding  "vector_cosine_ops" )
                   WITH ( "m" = "16", "ef_construction" = "64" );
2026-03-13 17:18:40 KST [00000] [3594164 (20)] vectordb@127.0.0.1(35526)/vectordb DEBUG:  building index "pgvector_index" on table "vdbbench_table_test" seriall
2026-03-13 17:32:55 KST [00000] [3594174 (1)] @/ NOTICE:  hnsw graph no longer fits into maintenance_work_mem after 2859018 tuples
2026-03-13 17:32:55 KST [00000] [3594174 (2)] @/ DETAIL:  Building will take significantly more time.
2026-03-13 17:32:55 KST [00000] [3594174 (3)] @/ HINT:  Increase maintenance_work_mem to speed up builds.
2026-03-13 19:11:01 KST [00000] [3594164 (1129)] vectordb@127.0.0.1(35526)/vectordb LOG:  duration: 6741619.730 ms
```

**`maintenance_work_mem` 30GB**
```
026-03-14 00:07:08 KST [00000] [3712285 (12)] vectordb@127.0.0.1(56168)/vectordb LOG:  statement: ALTER USER "vectordb" SET maintenance_work_mem TO '20GB';
2026-03-14 00:07:08 KST [00000] [3712285 (13)] vectordb@127.0.0.1(56168)/vectordb LOG:  statement: ALTER USER "vectordb" SET max_parallel_maintenance_workers TO '16';
2026-03-14 00:07:08 KST [00000] [3712285 (14)] vectordb@127.0.0.1(56168)/vectordb LOG:  statement: ALTER USER "vectordb" SET max_parallel_workers TO '16';
2026-03-14 00:07:08 KST [00000] [3712285 (15)] vectordb@127.0.0.1(56168)/vectordb LOG:  statement: ALTER TABLE "vdbbench_table_test" SET (parallel_workers = 16);
2026-03-14 00:07:09 KST [00000] [3712285 (16)] vectordb@127.0.0.1(56168)/vectordb DEBUG:  sending cancel to blocking autovacuum PID 3707543
2026-03-14 00:07:09 KST [00000] [3712285 (17)] vectordb@127.0.0.1(56168)/vectordb DETAIL:  Process 3712285 waits for ShareUpdateExclusiveLock on relation 25421 of database 16385.
2026-03-14 00:07:09 KST [57014] [3707543 (3)] @/ ERROR:  canceling autovacuum task
2026-03-14 00:07:09 KST [57014] [3707543 (4)] @/ CONTEXT:  while scanning block 1238902 of relation "public.vdbbench_table_test"
  automatic vacuum of table "vectordb.public.vdbbench_table_test"
2026-03-14 00:07:09 KST [00000] [3712285 (18)] vectordb@127.0.0.1(56168)/vectordb LOG:  duration: 1000.780 ms
2026-03-14 00:07:09 KST [00000] [3712285 (19)] vectordb@127.0.0.1(56168)/vectordb LOG:  statement: 
                  CREATE INDEX IF NOT EXISTS  "pgvector_index"  ON public. "vdbbench_table_test" 
                  USING  "hnsw"  (embedding  "vector_cosine_ops" )
                   WITH ( "m" = "16", "ef_construction" = "64" );
2026-03-14 00:07:09 KST [00000] [3712285 (20)] vectordb@127.0.0.1(56168)/vectordb DEBUG:  building index "pgvector_index" on table "vdbbench_table_test" serially
2026-03-14 00:41:58 KST [00000] [3712285 (22)] vectordb@127.0.0.1(56168)/vectordb NOTICE:  hnsw graph no longer fits into maintenance_work_mem after 5719284 tuples
2026-03-14 00:41:58 KST [00000] [3712285 (23)] vectordb@127.0.0.1(56168)/vectordb DETAIL:  Building will take significantly more time.
2026-03-14 00:41:58 KST [00000] [3712285 (24)] vectordb@127.0.0.1(56168)/vectordb HINT:  Increase maintenance_work_mem to speed up builds.
2026-03-14 02:24:50 KST [00000] [3712285 (1261)] vectordb@127.0.0.1(56168)/vectordb LOG:  duration: 8260413.418 ms
```

In the logs, the following message appears:
```
hnsw graph no longer fits into maintenance_work_mem after <# of tuples> tuples
```

The number of tuples increases as `maintenance_work_mem` increases. However, the overall index build duration did not improve significantly.

Also, there were no other active sessions running while the index was being built.

So it seems that increasing `maintenance_work_mem` allows more tuples to fit into memory during the HNSW graph construction phase, but it does not significantly reduce the total build time in this case.

Thanks a lot!

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

* Re: [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
@ 2026-03-16 03:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2026-03-16 03:25 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I'm not sure why it's not helping the total build time here (and haven't seen this behavior in my testing).

It'd be good to plot [indexing progress](https://github.com/pgvector/pgvector?tab=readme-ov-file#indexing-progress), CPU load, and I/O load over time to see what's going on.

Also, what hardware are you running this on? (CPU, disk, etc)

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

* Re: [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
@ 2026-03-16 11:24 ` "CraigMilhiser (@CraigMilhiser)" <noreply+CraigMilhiser@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: CraigMilhiser (@CraigMilhiser) @ 2026-03-16 11:24 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I just started learning pgvector so I may be very off here but the below research may align. It is different index types and algorithms but the fundamentals of CPU, OS, I/O, and RAM still apply.  Perhaps - I am new to this package.

https://vondra.me/posts/dont-give-postgres-too-much-memory-even-on-busy-systems/
https://vondra.me/posts/dont-give-postgres-too-much-memory/

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

* Re: [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
@ 2026-03-18 10:38 ` "R3gardless (@R3gardless)" <noreply+R3gardless@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: R3gardless (@R3gardless) @ 2026-03-18 10:38 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @ankane @CraigMilhiser,
Thank you for the suggestion. I’ll test it and share the results soon. Thanks!

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

* Re: [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
@ 2026-04-24 22:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2026-04-24 22:18 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Cleaning up issues, but happy to revisit when there's data + hardware info.

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

* Re: [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time
@ 2026-04-24 22:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
  6 siblings, 0 replies; 8+ messages in thread

From: ankane (@ankane) @ 2026-04-24 22:18 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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


end of thread, other threads:[~2026-04-24 22:18 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-03-15 08:48 [pgvector/pgvector] issue #969: Tuning `maintencne_work_mem` to improve  HNSW index build time "R3gardless (@R3gardless)" <noreply+R3gardless@github.com>
2026-03-15 17:42 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-16 02:41 ` "R3gardless (@R3gardless)" <noreply+R3gardless@github.com>
2026-03-16 03:25 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-03-16 11:24 ` "CraigMilhiser (@CraigMilhiser)" <noreply+CraigMilhiser@github.com>
2026-03-18 10:38 ` "R3gardless (@R3gardless)" <noreply+R3gardless@github.com>
2026-04-24 22:18 ` "ankane (@ankane)" <noreply+ankane@github.com>
2026-04-24 22:18 ` "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