pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: harikennedy (@harikennedy) <noreply+harikennedy@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #766: High LWLock Contention During Concurrent HNSW Index Scans
Date: Sat, 25 Jan 2025 09:35:32 +0000
Message-ID: <gh-pgvector-pgvector-766@github.com> (raw)

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!

view thread (17+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-766@github.com>
Permalink:  ../gh-pgvector-pgvector-766@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