Re: Improve LWLock tranche name visibility across backends

2025-08-06 Thread Sami Imseih
Thanks for testing! > Why is it necessary to allocate a new dsa_pointer for tranche names that are > the same size and then > free the old one? > Is there a reason we can't just assign new_ptrs[i] = old_ptrs[i]? Fair point. I will updated in the next rev. We don't need to free the' existing tran

Re: Improve LWLock tranche name visibility across backends

2025-08-06 Thread Sami Imseih
Thanks for testing! > which is what we expect. > > But if I look at LWLockTrancheNames.local (for the process that queried > pg_stat_activity): > > (gdb) p LWLockTrancheNames.local[0] > $1 = 0x0 > (gdb) p LWLockTrancheNames.local[1] > $2 = 0x7b759a81b038 "BDT_Lck" > > It looks like there is an in

Re: Improve LWLock tranche name visibility across backends

2025-08-06 Thread Rahila Syed
Hi, I've begun reviewing this patch and have a few questions listed below: 1. + if (i < LWLockTrancheNames.shmem->allocated && DsaPointerIsValid(old_ptrs[i])) Should an assert be used for the second condition instead? Since for i < LWLockTrancheNames.shmem->allocated, the dsa pointer is expect

Re: Improve LWLock tranche name visibility across backends

2025-08-06 Thread Bertrand Drouvot
Hi, On Tue, Aug 05, 2025 at 11:24:04PM -0500, Sami Imseih wrote: > > I'll fix this in the next rev. > > v5 fixes the above 2 issues found above. Thanks! > For the issue that was throwing " segment that has been freed", > indeed we should be freeing LWLockTrancheNames.shmem->list_ptr, Yeah.

Re: Improve LWLock tranche name visibility across backends

2025-08-05 Thread Sami Imseih
> I'll fix this in the next rev. v5 fixes the above 2 issues found above. For the issue that was throwing " segment that has been freed", indeed we should be freeing LWLockTrancheNames.shmem->list_ptr, list_ptr is a dsa_pointer that stores an array of dsa_pointers, which then made me realize

Re: Improve LWLock tranche name visibility across backends

2025-08-05 Thread Sami Imseih
Thanks for reviewing! > Issue 1 -- > > If I register enough tranches to go to: > > + /* Resize if needed */ > + if (LWLockTrancheNames.shmem->count >= > LWLockTrancheNames.shmem->allocated) > + { > + newalloc = > pg_nextpower2_32(Ma

Re: Improve LWLock tranche name visibility across backends

2025-08-05 Thread Bertrand Drouvot
Hi, On Mon, Aug 04, 2025 at 10:47:45PM -0500, Sami Imseih wrote: > > > > With a local hash table, I don't think it's necessary to introduce new > > > > code for managing > > > > a DSA based list of tranche names as is done in v3. We can go back to > > > > storing the shared > > > > trance names in

Re: Improve LWLock tranche name visibility across backends

2025-08-04 Thread Sami Imseih
> > > With a local hash table, I don't think it's necessary to introduce new > > > code for managing > > > a DSA based list of tranche names as is done in v3. We can go back to > > > storing the shared > > > trance names in dshash. > > > > > > What do you think? > > > > My first thought is that a p

Re: Improve LWLock tranche name visibility across backends

2025-08-04 Thread Sami Imseih
> On Mon, Aug 04, 2025 at 11:32:19AM -0500, Sami Imseih wrote: > > With a local hash table, I don't think it's necessary to introduce new > > code for managing > > a DSA based list of tranche names as is done in v3. We can go back to > > storing the shared > > trance names in dshash. > > > > What d

Re: Improve LWLock tranche name visibility across backends

2025-08-04 Thread Nathan Bossart
On Mon, Aug 04, 2025 at 11:32:19AM -0500, Sami Imseih wrote: > With a local hash table, I don't think it's necessary to introduce new > code for managing > a DSA based list of tranche names as is done in v3. We can go back to > storing the shared > trance names in dshash. > > What do you think? M

Re: Improve LWLock tranche name visibility across backends

2025-08-04 Thread Sami Imseih
> > > I think we could add a local backend copy that stays up to date with the > > > DSA. One idea would be to use an atomic counter to track the number of > > > entries in the DSA and compare it with a local backend counter whenever > > > the > > > tranche name lookup occurs. If the atomic counte

Re: Improve LWLock tranche name visibility across backends

2025-08-04 Thread Sami Imseih
> > I think we could add a local backend copy that stays up to date with the > > DSA. One idea would be to use an atomic counter to track the number of > > entries in the DSA and compare it with a local backend counter whenever the > > tranche name lookup occurs. If the atomic counter is higher (si

Re: Improve LWLock tranche name visibility across backends

2025-08-04 Thread Bertrand Drouvot
Hi, On Fri, Aug 01, 2025 at 01:15:24PM -0500, Sami Imseih wrote: > I think we could add a local backend copy that stays up to date with the > DSA. One idea would be to use an atomic counter to track the number of > entries in the DSA and compare it with a local backend counter whenever the > tranc

Re: Improve LWLock tranche name visibility across backends

2025-08-01 Thread Sami Imseih
> > Also, I suspect that there might be some concerns about the API changes. > > While it should be a very easy fix, that seems likely to break a lot of > > extensions. > > I don't know if it's possible to make this stuff backward > > compatible, and I also don't know if we really want to, as that'

Re: Improve LWLock tranche name visibility across backends

2025-08-01 Thread Sami Imseih
> > > Unlike the local list of tranche names, appending to and searching the > > > shared memory list requires an LWLock; in exclusive mode to append, and > > > shared mode to search. > > > > The thing that stood out the most to me is how much more expensive looking > > up the lock name is. At the

Re: Improve LWLock tranche name visibility across backends

2025-08-01 Thread Bertrand Drouvot
Hi, On Thu, Jul 31, 2025 at 04:24:38PM -0500, Nathan Bossart wrote: > I've attached a rebased version of the patch. > > On Mon, Jul 21, 2025 at 11:26:44PM -0500, Sami Imseih wrote: > > Unlike the local list of tranche names, appending to and searching the > > shared memory list requires an LWLock

Re: Improve LWLock tranche name visibility across backends

2025-07-31 Thread Nathan Bossart
I've attached a rebased version of the patch. On Mon, Jul 21, 2025 at 11:26:44PM -0500, Sami Imseih wrote: > Unlike the local list of tranche names, appending to and searching the > shared memory list requires an LWLock; in exclusive mode to append, and > shared mode to search. The thing that sto

Re: Improve LWLock tranche name visibility across backends

2025-07-21 Thread Sami Imseih
> >> I was imagining putting the array in one big DSA allocation instead of > >> carting around a pointer for each tranche name. (Sorry, I realize I am > >> hand-waving over some of the details.) > > > > I understood it like this. Here is a sketch: > > > > ``` > > dsa_pointer p; > > > > dsa = dsa_

Re: Improve LWLock tranche name visibility across backends

2025-07-16 Thread Sami Imseih
> > Hi, > > > > If a dshash table is used to store tranche names and IDs, where would the > > tranche name for this table > > be registered? > > I guess it could be a new BuiltinTrancheId for this dsa but not sure what > Nathan > and Sami have in mind. Yes, it will be a BuiltinTrancheId for a sha

Re: Improve LWLock tranche name visibility across backends

2025-07-15 Thread Bertrand Drouvot
Hi, On Tue, Jul 15, 2025 at 12:59:04PM +0530, Rahila Syed wrote: > Hi, > > If a dshash table is used to store tranche names and IDs, where would the > tranche name for this table > be registered? I guess it could be a new BuiltinTrancheId for this dsa but not sure what Nathan and Sami have in mi

Re: Improve LWLock tranche name visibility across backends

2025-07-15 Thread Nathan Bossart
On Tue, Jul 15, 2025 at 12:06:00PM -0500, Sami Imseih wrote: > On Tue, Jul 15, 2025 at 11:57 AM Nathan Bossart > wrote: >> I was imagining putting the array in one big DSA allocation instead of >> carting around a pointer for each tranche name. (Sorry, I realize I am >> hand-waving over some of t

Re: Improve LWLock tranche name visibility across backends

2025-07-15 Thread Sami Imseih
On Tue, Jul 15, 2025 at 11:57 AM Nathan Bossart wrote: > > On Tue, Jul 15, 2025 at 11:52:19AM -0500, Sami Imseih wrote: > >> Another random thought: I worry that the dshash approach might be quite a > >> bit slower, and IIUC we just need to map an integer to a string. Maybe we > >> should just us

Re: Improve LWLock tranche name visibility across backends

2025-07-15 Thread Nathan Bossart
On Tue, Jul 15, 2025 at 11:52:19AM -0500, Sami Imseih wrote: >> Another random thought: I worry that the dshash approach might be quite a >> bit slower, and IIUC we just need to map an integer to a string. Maybe we >> should just use a DSA for LWLockTrancheNames. IOW we'd leave it as a char** >> b

Re: Improve LWLock tranche name visibility across backends

2025-07-15 Thread Sami Imseih
> Another random thought: I worry that the dshash approach might be quite a > bit slower, and IIUC we just need to map an integer to a string. Maybe we > should just use a DSA for LWLockTrancheNames. IOW we'd leave it as a char** > but put it in shared memory. To use DSA just for this purpose, we

Re: Improve LWLock tranche name visibility across backends

2025-07-15 Thread Bertrand Drouvot
Hi, On Fri, Jul 11, 2025 at 04:32:13PM -0500, Sami Imseih wrote: > > > and instead reuse the existing static hash table, which is > > > capped at 128 custom wait events: > > > > > > ``` > > > #define WAIT_EVENT_CUSTOM_HASH_MAX_SIZE 128 > > > ``` > > > > That's probably still high enough, thoughts?

Re: Improve LWLock tranche name visibility across backends

2025-07-15 Thread Rahila Syed
Hi, > > Since DSM is not available during postmaster, if we were to create a DSA > segment in place, similar to what's done in StatsShmemInit(), we would also > need to ensure that the initial shared memory is sized appropriately. This > is > because it would need to be large enough to accommodat

Re: Improve LWLock tranche name visibility across backends

2025-07-14 Thread Nathan Bossart
On Mon, Jul 14, 2025 at 03:45:02PM -0500, Sami Imseih wrote: >> Ah, I missed the problem with postmaster. Could we have the first backend >> that needs to access the table be responsible for creating it and >> populating it with the built-in/requested-at-startup entries? > > We can certainly main

Re: Improve LWLock tranche name visibility across backends

2025-07-14 Thread Sami Imseih
> Ah, I missed the problem with postmaster. Could we have the first backend > that needs to access the table be responsible for creating it and > populating it with the built-in/requested-at-startup entries? We can certainly maintain a flag in the shared state that is set once the first backend l

Re: Improve LWLock tranche name visibility across backends

2025-07-14 Thread Tom Lane
Nathan Bossart writes: > Ah, I missed the problem with postmaster. Could we have the first backend > that needs to access the table be responsible for creating it and > populating it with the built-in/requested-at-startup entries? Also, is > there any chance that postmaster might need to access

Re: Improve LWLock tranche name visibility across backends

2025-07-14 Thread Nathan Bossart
On Mon, Jul 14, 2025 at 02:34:00PM -0500, Sami Imseih wrote: >> Why do we need three different places for the lock names? Is there a >> reason we can't put it all in shared memory? > > The real reason I felt it was better to keep three separate locations is that > it allows for a clear separation

Re: Improve LWLock tranche name visibility across backends

2025-07-14 Thread Sami Imseih
>> Attached is a proof of concept that does not alter the >> LWLockRegisterTranche API. > IMHO we should consider modifying the API, because right now you have to > call LWLockRegisterTranche() in each backend. Why not accept the name as > an argument for LWLockNewTrancheId() and only require it t

Re: Improve LWLock tranche name visibility across backends

2025-07-11 Thread Nathan Bossart
On Fri, Jul 11, 2025 at 04:32:13PM -0500, Sami Imseih wrote: > Now, what I think will be a good API is to provide an alternative to > LWLockRegisterTranche, > which now takes in both a tranche ID and tranche_name. The new API I propose > is > LWLockRegisterTrancheWaitEventCustom which takes only a

Re: Improve LWLock tranche name visibility across backends

2025-07-11 Thread Sami Imseih
> > and instead reuse the existing static hash table, which is > > capped at 128 custom wait events: > > > > ``` > > #define WAIT_EVENT_CUSTOM_HASH_MAX_SIZE 128 > > ``` > > That's probably still high enough, thoughts? I have no reason to believe that this number could be too low. I am not aware of

Re: Improve LWLock tranche name visibility across backends

2025-07-11 Thread Nathan Bossart
On Wed, Jul 09, 2025 at 04:39:48PM -0500, Sami Imseih wrote: > Attached is a proof of concept that does not alter the > LWLockRegisterTranche API. IMHO we should consider modifying the API, because right now you have to call LWLockRegisterTranche() in each backend. Why not accept the name as an a

Re: Improve LWLock tranche name visibility across backends

2025-07-11 Thread Bertrand Drouvot
Hi, On Thu, Jul 10, 2025 at 04:34:34PM -0500, Sami Imseih wrote: > Thanks for the feedback! > > > > Attached is a proof of concept that does not alter the > > > LWLockRegisterTranche API. Instead, it detects when a registration is > > > performed by a normal backend and stores the tranche name in

Re: Improve LWLock tranche name visibility across backends

2025-07-10 Thread Sami Imseih
Thanks for the feedback! > > Attached is a proof of concept that does not alter the > > LWLockRegisterTranche API. Instead, it detects when a registration is > > performed by a normal backend and stores the tranche name in shared memory, > > using a dshash keyed by tranche ID. Tranche name lookup

Re: Improve LWLock tranche name visibility across backends

2025-07-10 Thread Bertrand Drouvot
Hi, On Wed, Jul 09, 2025 at 04:39:48PM -0500, Sami Imseih wrote: > Hi, > > When querying pg_stat_activity, the function pgstat_get_wait_event is > called, which internally uses GetLWLockIdentifier and GetLWTrancheName > to map the LWLock to its tranche name. If the backend does not recognize > th

Improve LWLock tranche name visibility across backends

2025-07-09 Thread Sami Imseih
Hi, This is a follow-up to a discussion started in [0]. LWLocks in PostgreSQL are categorized into tranches, and the tranche name appears as the wait_event in pg_stat_activity. There are both built-in tranche names and tranche names that can be registered by extensions using RequestNamedLWLockTra