pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
17+ messages / 6 participants
[nested] [flat]

* [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-01-25 09:35  "harikennedy (@harikennedy)" <noreply+harikennedy@github.com>
  0 siblings, 0 replies; 17+ messages in thread

From: harikennedy (@harikennedy) @ 2025-01-25 09:35 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I’ve been running into LWLock contention issues with HNSW indexes during concurrent workloads, and I wanted to see if anyone has insights or suggestions for improving this.

The problem becomes noticeable when we have 32+ DB connections. At this level, the database load is dominated by LWLock:LockManager wait events. At lower concurrency levels, the QPS scales pretty well, and there’s minimal lock contention. But as we increase the number of connections, contention spikes, and throughput saturates.

Observations
Lock Behavior - Based on the code in hnswscan.c, it looks like the search process uses `LockPage(..., HNSW_SCAN_LOCK, ShareLock)` to protect access to the adjacency graph during traversal. These locks are brief but can pile up when multiple queries hit the same graph structures.

Scaling Issue- With ~32 workers (1 connection per worker), QPS is great and lock contention is low. When we push to 32+ to 100 or more workers, contention grows exponentially, leading to LWLock:LockManager dominating database load.

What I’ve Tried:
Concurrency Tuning: Sticking to ~32 workers seems to work best, but we’d like to scale further if possible.
Instance Scaling: Larger instances don’t seem to help much because the bottleneck is lock contention, not compute or I/O.
Yet to try: Having prepared statements. 

Ideas:
1. Finer grained Locking: Is there a way to reduce the granularity of the HNSW_SCAN_LOCK to avoid so much contention when multiple queries traverse the graph?
2. Asynchronous/Cooperative Traversal: Would it be feasible to rework the traversal logic to reduce the need for locking during searches?
3. Parallel Index Scans: Could enabling parallel index scans (amcanparallel) help distribute the workload better across multiple processes?

Setup:
Dataset sizes: 12M-100M rows (with plans for larger datasets).
Instance type: db.r5.8xlarge (32 vCPUs, 256G).

Would love to hear if others have run into this and how you’ve approached it. Thanks in advance for any suggestions or insights!

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-01-25 20:19  "ankane (@ankane)" <noreply+ankane@github.com>
  15 siblings, 0 replies; 17+ messages in thread

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

@ankane closed this.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-01-25 20:19  "ankane (@ankane)" <noreply+ankane@github.com>
  15 siblings, 0 replies; 17+ messages in thread

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

Hi @harikennedy, since it's a shared lock, it shouldn't cause contention. It sounds like you're saturating the CPU.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-01-25 20:33  "harikennedy (@harikennedy)" <noreply+harikennedy@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: harikennedy (@harikennedy) @ 2025-01-25 20:33 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks for the response! 

I’d like to clarify that the issue doesn’t appear to be CPU saturation. During high concurrency workloads on a db.r5.8xlarge instance, CPU usage remains around 30–50%, while AWS Performance Insights shows LWLock:LockManager wait events dominating the load.

![Image](https://github.com/user-attachments/assets/d11e5fa0-8161-4dc0-b180-812591f8c5d0)

Shared locks like LockPage(..., HNSW_SCAN_LOCK, ShareLock) allow concurrent reads, but at high concurrency, the overhead of acquiring and releasing these locks seems to cause contention. This is evident as query throughput saturates at ~32 sessions and increasing concurrency beyond this only increases LWLock contention.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-01-25 21:00  "ankane (@ankane)" <noreply+ankane@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: ankane (@ankane) @ 2025-01-25 21:00 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane reopened this.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-01-25 21:00  "ankane (@ankane)" <noreply+ankane@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: ankane (@ankane) @ 2025-01-25 21:00 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Interesting, thanks for the additional context. Will investigate this more.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-02-05 07:49  "Light-V (@Light-V)" <noreply+Light-V@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: Light-V (@Light-V) @ 2025-02-05 07:49 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@harikennedy Could you provide information on the QPS level at which the contention occurs?

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-02-05 20:07  "greenhal (@greenhal)" <noreply+greenhal@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: greenhal (@greenhal) @ 2025-02-05 20:07 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> [@harikennedy](https://github.com/harikennedy) Could you provide information on the QPS level at which the contention occurs?

Per the graph, it's looks like he's testing at 32, 100, 400 users.  I've seen similar results, but at ~40 users, the cpu is at >= 95% utilization. 

A few questions..
How many dimensions?
pg version?
pgvector version?
m , ef, ef_search, n settings ?



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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-02-06 21:57  "harikennedy (@harikennedy)" <noreply+harikennedy@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: harikennedy (@harikennedy) @ 2025-02-06 21:57 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

For reference, here were the settings in my earlier post:

- Hardware: 32 vCPUs, 256GB RAM
- Dimension: <100
- PostgreSQL Version: 16.4
- pgVector Version: 0.7.3
- HNSW Params: m=16, efc=512, efs=1 to 20

@ankane  @greenhal I made a few changes since then, including upgrading the hardware and pgvector version. Here’s the new setup:

- Hardware: 64 vCPUs, 256GB RAM
- Dimension: <100
- PostgreSQL Version: 16.5
- pgVector Version: 0.8.0
- HNSW Params: m=16, efc=max

After the upgrade, I noticed saturation occurring around ~40 sessions, even though CPU utilization remained below 30% and sessions were underutilized. This behavior aligns with what @greenhal mentioned earlier except for CPU util.

However, I also observed that saturation depends on ef_search: lower values result in session underutilization, while increasing ef_search shifts the saturation point.

For efs=1
![Image](https://github.com/user-attachments/assets/f1860b09-7884-47c7-a2e9-bce076ed4a19)

For efs=10 

![Image](https://github.com/user-attachments/assets/86b34b4a-b849-4060-9e6e-d62850024fc8)

QPS remained constant from ef_search = 1 to ef_search = 10 (this changes with the dataset size), which is different from what I observed in pgVector 0.7.3, where QPS was higher for lower ef_search values and gradually decreased as expected. In the new setup, QPS only starts decreasing beyond a certain point (eg: ef_search = 10).

Would be curious to hear if others have noticed similar behavior or have any pointers into why this change might have occurred.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-02-21 13:56  "crbsram (@crbsram)" <noreply+crbsram@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: crbsram (@crbsram) @ 2025-02-21 13:56 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> For reference, here were the settings in my earlier post:
> 
> * Hardware: 32 vCPUs, 256GB RAM
> * Dimension: <100
> * PostgreSQL Version: 16.4
> * pgVector Version: 0.7.3
> * HNSW Params: m=16, efc=512, efs=1 to 20
> 
> [@ankane](https://github.com/ankane) [@greenhal](https://github.com/greenhal) I made a few changes since then, including upgrading the hardware and pgvector version. Here’s the new setup:
> 
> * Hardware: 64 vCPUs, 256GB RAM
> * Dimension: <100
> * PostgreSQL Version: 16.5
> * pgVector Version: 0.8.0
> * HNSW Params: m=16, efc=max
> 
> After the upgrade, I noticed saturation occurring around ~40 sessions, even though CPU utilization remained below 30% and sessions were underutilized. This behavior aligns with what [@greenhal](https://github.com/greenhal) mentioned earlier except for CPU util.
> 
> However, I also observed that saturation depends on ef_search: lower values result in session underutilization, while increasing ef_search shifts the saturation point.
> 
> For efs=1 ![Image](https://github.com/user-attachments/assets/f1860b09-7884-47c7-a2e9-bce076ed4a19)
> 
> For efs=10
> 
> ![Image](https://github.com/user-attachments/assets/86b34b4a-b849-4060-9e6e-d62850024fc8)
> 
> QPS remained constant from ef_search = 1 to ef_search = 10 (this changes with the dataset size), which is different from what I observed in pgVector 0.7.3, where QPS was higher for lower ef_search values and gradually decreased as expected. In the new setup, QPS only starts decreasing beyond a certain point (eg: ef_search = 10).
> 
> Would be curious to hear if others have noticed similar behavior or have any pointers into why this change might have occurred.

HI Hari

Is it reproducible with postgres 17 version with SIMD advantages.

Thanks
C.R.Bala

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-02-21 16:12  "greenhal (@greenhal)" <noreply+greenhal@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: greenhal (@greenhal) @ 2025-02-21 16:12 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

I've made a few attempts to reproduce this.   I can replicate the high LWLock:LockManager waits, but only with CPU Saturation.   LWLock waits are worse when you have high efc, low efs and a small dimension (I tested with the bigann dataset, 10m records, dimensions 128).   I curious why are you using such a large efc when you are only using efs of >= 10.    A smaller efc would provide a faster index build and should provide close to the same recall. 

FYI, my most reset test was on a r5.8xlarge, pg 17.3.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-02-22 05:05  "crbsram (@crbsram)" <noreply+crbsram@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: crbsram (@crbsram) @ 2025-02-22 05:05 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

> I've made a few attempts to reproduce this. I can replicate the high LWLock:LockManager waits, but only with CPU Saturation. LWLock waits are worse when you have high efc, low efs and a small dimension (I tested with the bigann dataset, 10m records, dimensions 128). I curious why are you using such a large efc when you are only using efs of >= 10. A smaller efc would provide a faster index build and should provide close to the same recall.
> 
> FYI, my most reset test was on a r5.8xlarge, pg 17.3.

It is interesting to see the same reproducible in pg17.3

We are using pgvector with 512 dimension with default settings. we have not experienced it.
Ours running in a bare metal machine with 512GBRAM with NVMeSSD disks and 64 core processor on RHEL 9.
Indexing using HNSW.
We are not observing the same with 30 concurrent sessions. (the sessions are managed by connection pooler by the App layer)

The original writers of this library has to check on this, whether this is should be fixed and a new patching to be released or not to mitigate this shared lock.

Thanks
C.R.Bala

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-03-07 14:53  "jkatz (@jkatz)" <noreply+jkatz@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: jkatz (@jkatz) @ 2025-03-07 14:53 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

How are you transferring the data to the server - i.e., are you sending the vectors over in their binary format, or using their text format?

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-03-09 08:17  "crbsram (@crbsram)" <noreply+crbsram@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: crbsram (@crbsram) @ 2025-03-09 08:17 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

We are using pgvector library for python.

Hope it should internally convert the vector to binary format.

We convert the image to vector using the auraface opensource model using python.

Thanks
C.R.Bala

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-03-16 19:25  "jkatz (@jkatz)" <noreply+jkatz@github.com>
  15 siblings, 0 replies; 17+ messages in thread

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

> Hope it should internally convert the vector to binary format.

It does if you're using a `numpy.ndarray` type. If you're just using a `list`, it there will be a binary/text/binary conversation that's fairly costly, which could potentially manifest as a lock contention on the server-side due to the additional CPU utilization.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-03-22 18:53  "ankane (@ankane)" <noreply+ankane@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: ankane (@ankane) @ 2025-03-22 18:53 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

At this point, I don't think there's anything for pgvector to do here. It seems like this behavior may be limited to low-dimensional vectors with a very low `ef_search`. Also, removing `HNSW_SCAN_LOCK` does not seem to increase performance (per @greenhal's testing).

If someone does find a way to improve performance, feel free to submit a PR.

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

* Re: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
@ 2025-03-22 18:53  "ankane (@ankane)" <noreply+ankane@github.com>
  15 siblings, 0 replies; 17+ messages in thread

From: ankane (@ankane) @ 2025-03-22 18:53 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

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


end of thread, other threads:[~2025-03-22 18:53 UTC | newest]

Thread overview: 17+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-01-25 09:35 [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans "harikennedy (@harikennedy)" <noreply+harikennedy@github.com>
2025-01-25 20:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-25 20:19 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-25 20:33 ` "harikennedy (@harikennedy)" <noreply+harikennedy@github.com>
2025-01-25 21:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-01-25 21:00 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-02-05 07:49 ` "Light-V (@Light-V)" <noreply+Light-V@github.com>
2025-02-05 20:07 ` "greenhal (@greenhal)" <noreply+greenhal@github.com>
2025-02-06 21:57 ` "harikennedy (@harikennedy)" <noreply+harikennedy@github.com>
2025-02-21 13:56 ` "crbsram (@crbsram)" <noreply+crbsram@github.com>
2025-02-21 16:12 ` "greenhal (@greenhal)" <noreply+greenhal@github.com>
2025-02-22 05:05 ` "crbsram (@crbsram)" <noreply+crbsram@github.com>
2025-03-07 14:53 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-03-09 08:17 ` "crbsram (@crbsram)" <noreply+crbsram@github.com>
2025-03-16 19:25 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2025-03-22 18:53 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-03-22 18:53 ` "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