I've also attached a rebased patch that addresses all the latest feedback. A reworked verison of the test patch is also included, but that's mostly intended for CI purposes and is still not intended for commit (yet).
Please see below for some comments regarding v20 patch.
1. Following unused declaration is present in the lwlock.h file.
2. I am struggling to understand the use of LocalLWLockCounter in the patch.
If the intention is to delay acquiring the spin lock when trancheId is less than LocalLWLockCounter, it seems possible that we might read NamedLWLockTrancheNames in the GetLWTrancheName function after LocalLWLockCounter has been updated, but before NamedLWLockTrancheNames has been updated in the LWLockNewTrancheId function. To prevent reading an outdated tranche name, we should acquire the spin lock unconditionally in the GetLWTrancheName function
3. + ereport(ERROR, + (errmsg("maximum number of tranches already registered"), + errdetail("No more than %d tranches may be registered.", + MAX_NAMED_TRANCHES))); + }
Since this patch sets a maximum limit on the number of LW lock tranches that can be registered, would it make sense to offer a configurable option rather than using a hard-coded MAX_NUM_TRANCHES? This will allow users who have reached the maximum limit to register their LW Locks.