From 93227c1e979a18d4552b59d463f11d0596d1fb4a Mon Sep 17 00:00:00 2001
From: "Min, Baohong" <baohong.min@intel.com>
Date: Wed, 25 Nov 2026 16:02:04 +0800
Subject: [PATCH v2] Reduce LWLockWaitListLock() cache-line contention with
 adaptive spin reads

Under high lock contention (100+ CPUs, 500+ clients), backends
spinning in LWLockWaitListLock() perform atomic re-reads of lock->state
on every iteration, causing heavy cache-line bouncing that limits
throughput.

Re-read lock->state only periodically, with the interval adapting to the
contention level and tracked separately for each LWLock tranche.  This
optimization significantly improves performance under high contention
while leaving low-contention cases (few cores and clients) unaffected.

Co-authored-by: Min, Baohong <baohong.min@intel.com>
Co-authored-by: Jin, Jun <jun.i.jin@intel.com>
Co-authored-by: Kim, Andrew <andrew.kim@intel.com>
---
 src/backend/storage/lmgr/lwlock.c | 64 ++++++++++++++++++++++++++++---
 src/backend/storage/lmgr/s_lock.c |  1 -
 src/include/storage/s_lock.h      |  2 +
 3 files changed, 60 insertions(+), 7 deletions(-)

diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index 82a1d4d2e26..a304b77b5a7 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -169,6 +169,19 @@ static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS];
 /* Maximum number of LWLock tranches that can be assigned by extensions */
 #define MAX_USER_DEFINED_TRANCHES 256
 
+/*
+ * Per-tranche adaptive read interval for LWLockWaitListLock(). Sized to
+ * cover both built-in and extension tranches so each gets its own slot.
+ */
+#define NUM_ADAPTIVE_TRANCHES (LWTRANCHE_FIRST_USER_DEFINED + MAX_USER_DEFINED_TRANCHES)
+
+/* State re-read threshold for increasing the polling interval */
+#define SPINS_PER_LOCK_READ_THRESHOLD   5
+
+/* Current re-read interval of each tranche, indexed by lock->tranche */
+static int      spins_per_lock_read[NUM_ADAPTIVE_TRANCHES];
+
+
 /*
  * Shared memory structure holding user-defined tranches.
  */
@@ -835,6 +848,8 @@ static void
 LWLockWaitListLock(LWLock *lock)
 {
 	uint32		old_state;
+	int			read_interval;
+	int			idx;
 #ifdef LWLOCK_STATS
 	lwlock_stats *lwstats;
 	uint32		delays = 0;
@@ -842,37 +857,74 @@ LWLockWaitListLock(LWLock *lock)
 	lwstats = get_lwlock_stats_entry(lock);
 #endif
 
+	/*
+	 * Use a backend-local polling interval for each tranche.
+	 * Clamp out-of-range tranche IDs into the last array slot.
+	 */
+	idx = Min(lock->tranche, NUM_ADAPTIVE_TRANCHES - 1);
+	read_interval = Max(spins_per_lock_read[idx], 1);
+
 	while (true)
 	{
 		/*
 		 * Always try once to acquire the lock directly, without setting up
-		 * the spin-delay infrastructure. The work necessary for that shows up
-		 * in profiles and is rarely necessary.
+		 * the spin-delay infrastructure.  The work necessary for that shows
+		 * up in profiles and is rarely necessary.
 		 */
 		old_state = pg_atomic_fetch_or_u32(&lock->state, LW_FLAG_LOCKED);
 		if (likely(!(old_state & LW_FLAG_LOCKED)))
 			break;				/* got lock */
 
-		/* and then spin without atomic operations until lock is released */
+		/* Then spin without atomic operations until lock is released. */
 		{
 			SpinDelayStatus delayStatus;
+			int			spins_since_read = 0;
+			int			lock_read_count = 0;
 
 			init_local_spin_delay(&delayStatus);
 
 			while (old_state & LW_FLAG_LOCKED)
 			{
 				perform_spin_delay(&delayStatus);
-				old_state = pg_atomic_read_u32(&lock->state);
+
+				/*
+				 * Poll lock->state every read_interval active-spin iterations
+				 * and immediately after every sleep.
+				 */
+				if (delayStatus.spins == 0 ||
+					++spins_since_read >= read_interval)
+				{
+					/*
+					 * A high read count means that this wait was long relative to the current interval.
+					 * Widen it gradually, but not beyond the maximum active-spin period.
+					 * Do not widen it for the mandatory poll after a sleep.
+					 */
+					if (delayStatus.spins != 0 && lock_read_count > SPINS_PER_LOCK_READ_THRESHOLD)
+					{
+						read_interval = Min(read_interval + 1, MAX_SPINS_PER_DELAY);
+					}
+
+					old_state = pg_atomic_read_u32(&lock->state);
+					++lock_read_count;
+					spins_since_read = 0;
+				}
 			}
 #ifdef LWLOCK_STATS
 			delays += delayStatus.delays;
 #endif
 			finish_spin_delay(&delayStatus);
+
+			/*
+			 * Bias the interval back toward polling on every spin,
+			 * and save it for subsequent waits on this tranche in this backend.
+			 */
+			read_interval = Max(read_interval - 2, 1);
+			spins_per_lock_read[idx] = read_interval;
 		}
 
 		/*
-		 * Retry. The lock might obviously already be re-acquired by the time
-		 * we're attempting to get it again.
+		 * Retry.  The lock might already have been re-acquired by the time
+		 * we attempt to get it again.
 		 */
 	}
 
diff --git a/src/backend/storage/lmgr/s_lock.c b/src/backend/storage/lmgr/s_lock.c
index 795aeed1028..57a5ccd49aa 100644
--- a/src/backend/storage/lmgr/s_lock.c
+++ b/src/backend/storage/lmgr/s_lock.c
@@ -55,7 +55,6 @@
 #include "utils/wait_event.h"
 
 #define MIN_SPINS_PER_DELAY 10
-#define MAX_SPINS_PER_DELAY 1000
 #define NUM_DELAYS			1000
 #define MIN_DELAY_USEC		1000L
 #define MAX_DELAY_USEC		1000000L
diff --git a/src/include/storage/s_lock.h b/src/include/storage/s_lock.h
index 7e049dfac18..0d1b7f1d33c 100644
--- a/src/include/storage/s_lock.h
+++ b/src/include/storage/s_lock.h
@@ -698,6 +698,8 @@ extern void s_unlock(volatile slock_t *lock);
 /* Support for dynamic adjustment of spins_per_delay */
 #define DEFAULT_SPINS_PER_DELAY  100
 
+#define MAX_SPINS_PER_DELAY 1000
+
 extern void set_spins_per_delay(int shared_spins_per_delay);
 extern int	update_spins_per_delay(int shared_spins_per_delay);
 
-- 
2.43.0

