Hi, On 2024-07-29 21:00:35 +0300, Heikki Linnakangas wrote: > On 29/07/2024 20:48, Andres Freund wrote: > > On 2024-07-29 13:25:22 -0400, Tom Lane wrote: > > > Heikki Linnakangas <hlinn...@iki.fi> writes: > > > > Yeah I'm not worried about that at all. Also, the assert is made when > > > > you have already released the spinlock; you are already out of the > > > > critical section. > > > > > > Not in the patch Andres posted. > > > > Which seems fairly fundamental - once outside of the critical section, we > > can't actually assert that the lock isn't acquired, somebody else *validly* > > might have acquired it by then. > > You could do: > > bool was_free = S_LOCK_FREE(lock); > > S_UNLOCK(lock); > Assert(!was_free);
I don't really see the point - we're about to crash with an assertion failure, why would we want to do that outside of the critical section? If anything that will make it harder to debug the issue in a core dump, because other backends might "destroy evidence" due to being able to acquire the spinlock. > Depending on the underlying implementation, you could also use > compare-and-exchange. That'd scale a lot worse, at least on x86-64, as it requires the unlock to be an atomic op, whereas today it's a simple store (+ compiler barrier). I've experimented with replacing all spinlocks with lwlocks, and the fact that you need an atomic op for an rwlock release is one of the two major reasons they have a higher overhead (the remainder is boring stuff like the overhead of external function calls and ownership management). Greetings, Andres Freund