| From: | "Min, Baohong" <baohong(dot)min(at)intel(dot)com> |
|---|---|
| To: | wenhui qiu <qiuwenhuifx(at)gmail(dot)com> |
| Cc: | "Okanovic, Haris" <harisokn(at)amazon(dot)com>, Jakub Wartak <jakub(dot)wartak(at)enterprisedb(dot)com>, "Jin, Jun I" <jun(dot)i(dot)jin(at)intel(dot)com>, "Kim, Andrew" <andrew(dot)kim(at)intel(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
| Subject: | Re: [PATCH v2] Reduce LWLockWaitListLock() cache-line contention with adaptive spin reads |
| Date: | 2026-11-26 04:24:39 |
| Message-ID: | SA1PR11MB832836DD001A27CC76B33C08E9422@SA1PR11MB8328.namprd11.prod.outlook.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi all,
Thanks for the detailed review.
Attached is v2 of the patch. The main changes since v1 are:
* Replaced the independently chosen polling limit of 256 with
MAX_SPINS_PER_DELAY.
* Changed the interval adjustment from +5/-1 to a more gradual
+1/-2 policy.
* Tracked the polling interval separately for each LWLock tranche
in each backend.
* Added an immediate lock->state reread after every sleep.
* Removed the global MIN_DELAY_USEC change.
* Added a high-to-low contention transition test.
V2 benchmark:
To reduce benchmark variability caused by storage I/O, I placed the
PostgreSQL data directory on /dev/shm, and started the server with:
/usr/local/pgsql/bin/pg_ctl -D /dev/shm/data -l logfile start
This kept the database files and WAL on an in-memory filesystem and
minimized physical storage I/O during the test.
The Intel-512vCPU throughput results for v2 are:
Clients Base TPS v2 TPS Improvement
------- -------- --------- --------------------
8 43421 43548 +0.3%
16 72836 74090 +1.7%
32 98180 99553 +1.4%
64 109105 111922 +2.6%
128 96461 102385 +6.1%
256 75903 92828 +22.3%
512 59096 86933 +47.1%
800 52056 83800 +61.0%
1024 48689 81992 +68.4%
The low-client results remain close to the baseline, while the benefit
increases with contention.
Comments inline.
> 1. Why is 256 an appropriate maximum polling interval?
>
> A fixed limit seems reasonable, but the patch does not explain why 256
> is preferable to a smaller value. This trades fewer state reads for
> slower detection of lock release, and the duration of 256 iterations
> varies across processors.
In v2, I removed that limit and instead use MAX_SPINS_PER_DELAY as the
upper bound. This is intended as a structural bound rather than a
tuned value. perform_spin_delay() cannot actively spin for more than
MAX_SPINS_PER_DELAY iterations, and v2 rereads lock->state immediately
after every sleep.
I moved the existing definition of MAX_SPINS_PER_DELAY to s_lock.h so
that LWLockWaitListLock() can use the same value rather than duplicating
its numeric value. This does not change the behavior of existing
spin-delay callers.
> Similarly, how were the threshold of 5 and the +5/-1 adjustment chosen?
> Since lock_read_count decreases as the polling interval increases, what
> behavior is the controller intended to converge to?
The threshold was selected empirically. I agree that the +5/-1
adjustment in v1 was difficult to explain, so v2 changes
it to +1/-2.
During active spinning, the interval is increased by one after the
number of reads in a wait exceeds SPINS_PER_LOCK_READ_THRESHOLD. After
the wait completes, the interval is reduced by two.
Ignoring mandatory reads after sleeps, a threshold of 5 gives the
following approximate update:
interval change = max(reads - 6, 0) - 2
The interval therefore tends toward approximately eight state reads per
wait: fewer reads reduce the interval, while more reads increase it
gradually. This is the intended steady-state behavior of the
controller.
> 2. Could the polling interval cause unnecessary sleeps?
>
> perform_spin_delay() also calls pg_usleep(). If the polling interval
> exceeds spins_per_delay, the loop can sleep repeatedly without checking
> whether the lock has already been released.
>
> For example, with an interval of 256 and spins_per_delay of 10, there
> are 25 sleeps before the first reread.
Yes, this was a problem in v1. Thank you for pointing it out.
In v2, the loop checks delayStatus.spins after every call to
perform_spin_delay(). perform_spin_delay() resets spins to zero after
sleeping, so v2 rereads lock->state immediately after every sleep.
> 3. How does the adaptation recover when contention subsides?
>
> The interval is backend-local and applies to unrelated LWLocks.
> Successful uncontended acquisitions do not decrease it, so a value
> learned from one heavily contended lock can persist into later
> operations.
In v2, the polling interval is backend-local and tracked separately for
each LWLock tranche. Therefore, a value learned for one tranche no
longer affects unrelated LWLock tranches.
After each completed wait, the interval is reduced by two, down to a
minimum of one. Shorter waits therefore move it back toward polling on
every spin. A stale interval only matters if the backend enters the
spin loop again; if acquisitions are uncontended, the interval is not
used.
> Could you include a workload transition from sustained contention to
> brief contention? This would help assess whether the +5/-1 adjustment
> recovers quickly enough.
I first started a pgbench instance with either 8 or 64 clients and kept
those clients connected throughout the test. I then started a second
pgbench instance with 800 additional clients and ran it for 10 seconds.
After the second instance completed, the original 8 or 64 clients
continued running so that their post-contention throughput could be
measured.
The workload transitions were therefore:
8 persistent clients -> 808 total clients -> 8 persistent clients
64 persistent clients -> 864 total clients -> 64 persistent clients
The original backend processes remained connected after the additional
clients exited, so they retained the backend-local intervals learned
during the high-contention phase.
I collected per-transaction pgbench logs in 10 ms buckets.
8 clients
time TPS
1795587125.61 8 800
1795587125.62 9 900
1795587125.63 8 800
1795587125.64 11 1100
1795587125.65 1 100
1795587125.66 11 1100
1795587125.67 105 10500
1795587125.68 105 10500
1795587125.69 113 11300
1795587125.70 182 18200
1795587125.71 370 37000
1795587125.72 414 41400
1795587125.73 469 46900
1795587125.74 428 42800
64 clients
time TPS
1795587841.90 56 5600
1795587841.91 51 5100
1795587841.92 9 900
1795587841.93 2 200
1795587841.94 78 7800
1795587841.95 123 12300
1795587841.96 168 16800
1795587841.97 803 80300
1795587841.98 841 84100
1795587841.99 1124 112400
1795587842.00 997 99700
1795587842.01 1072 107200
1795587842.02 1062 106200
1795587842.03 1109 110900
1795587842.04 1145 114500
With 8 clients, throughput recovered to 41400 TPS, within ~60 ms.
With 64 clients, throughput recovered to 112400 TPS, within ~60 ms.
> 4. Why does this optimization need to change the global sleep minimum?
>
> Changing MIN_DELAY_USEC affects every caller of perform_spin_delay(),
> including ordinary spinlocks and buffer-header waits.
Agreed. The LWLock optimization does not need to change the global
sleep minimum.
V2 removes the MIN_DELAY_USEC change entirely.
Thanks again for the review.
Regards,
Baohong
| Attachment | Content-Type | Size |
|---|---|---|
| LWLockWaitListLock-adaptive-spin-reads-v2.patch | application/octet-stream | 5.7 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | shveta malik | 2026-11-26 04:26:49 | Re: Proposal: Conflict log history table for Logical Replication |
| Previous Message | Peter Smith | 2026-11-26 04:18:25 | DOC: Minor updates for logical replication conflicts |