From ab047556c0bc93bc6f5d28cef1975ae9ccdf46f8 Mon Sep 17 00:00:00 2001
From: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com>
Date: Fri, 2 Dec 2022 10:30:37 +0000
Subject: [PATCH v2] Add fastpath to LWLockUpdateVar()

Add fastpath to LWLockUpdateVar() when there are no waiters. This
avoids unnecessary lwlock's wait list lock acquisition and
release.
---
 src/backend/storage/lmgr/lwlock.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/backend/storage/lmgr/lwlock.c b/src/backend/storage/lmgr/lwlock.c
index f7556dcb7d..9d90cf9adf 100644
--- a/src/backend/storage/lmgr/lwlock.c
+++ b/src/backend/storage/lmgr/lwlock.c
@@ -1744,6 +1744,13 @@ LWLockUpdateVar(LWLock *lock, pg_atomic_uint64 *valptr, uint64 val)
 	/* Update the lock's value atomically first. */
 	pg_atomic_write_u64(valptr, val);
 
+	/*
+	 * Quick exit when there are no waiters. This avoids unnecessary lwlock's
+	 * wait list lock acquisition and release.
+	 */
+	if ((pg_atomic_read_u32(&lock->state) & LW_FLAG_HAS_WAITERS) == 0)
+		return;
+
 	proclist_init(&wakeup);
 
 	LWLockWaitListLock(lock);
-- 
2.34.1

