Re: Improve LWLock tranche name visibility across backends
Nathan Bossart <nathandbossart@gmail.com>
From: Nathan Bossart <nathandbossart@gmail.com>
To: Rahila Syed <rahilasyed90@gmail.com>
Cc: Sami Imseih <samimseih@gmail.com>, Bertrand Drouvot <bertranddrouvot.pg@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>, Andres Freund <andres@anarazel.de>, pgsql-hackers <pgsql-hackers@postgresql.org>
Date: 2025-09-04T13:51:21Z
Lists: pgsql-hackers
On Thu, Sep 04, 2025 at 03:29:52PM +0530, Rahila Syed wrote: > Since updates to LocalLWLockCounter and LWLockTrancheNames are performed > while holding the lock, but reading LocalLWLockCounter and then > LWLockTrancheNames in GetLWTrancheName can occur without acquiring the > same lock in case trancheID < LocalLWLockCounter, There is a small window > between updating LocalLWLockCounter and adding the name to > LWLockTrancheNames. During this window, if GetLWTrancheNames is called, > it might attempt to access a name in LWLockTrancheNames that hasn't been > added yet. We hold the lock when reading/writing the shared-memory LWLockCounter. There's no need to take a lock when reading the backend-local LocalLWLockCounter, as it can't be modified by any other process. Additionally, when adding a new tranche name, we hold the lock the entire time while we bump the shared-memory counter and copy the name, so there's no chance another backend will see the counter update but not the name or attempt to use the same LWLockTrancheNames slot. -- nathan
Commits
-
test_dsa: Avoid leaking LWLock tranches.
- c5c74282f2ea 19 (unreleased) landed
-
Teach DSM registry to ERROR if attaching to an uninitialized entry.
- b26d76f64327 18.2 landed
- ac2800ddc185 17.8 landed
- 1165a933aab1 19 (unreleased) landed
-
Add a test harness for the LWLock tranche code.
- 16607718c010 19 (unreleased) landed
-
Revert recent change to RequestNamedLWLockTranche().
- d814d7fc3d52 19 (unreleased) landed
-
Move dynamically-allocated LWLock tranche names to shared memory.
- 38b602b0289f 19 (unreleased) landed
-
Prepare DSM registry for upcoming changes to LWLock tranche names.
- 5487058b56e0 19 (unreleased) landed
-
Add GetNamedDSA() and GetNamedDSHash().
- fe07100e82b0 19 (unreleased) cited