Throwing away unnecessary spin-locks
Throwing away unnecessary spin-locks
Story
=====
A common pattern that I have been repeatedly warned against is using
int new_done;
new_done = CheckpointerShmem->ckpt_done;
SpinLockRelease(&CheckpointerShmem->ckpt_lck);
I think could certainly be replaced by something like
Grepping the codebase we get
Re: Throwing away unnecessary spin-locks
Hi, On 2026-10-01 20:50:35 +0100, Alexandre Felipe wrote: > Please try, if you want > $ grep -A 2 -rn SpinLockAcquire src/backend | grep SpinLockRelease -B 2 > > Story > ===== > A common pattern that I have been repeatedly warned against is using > spin-locks unnecessarily. I was surprised to see in checkpointer.c > FirstCallSinceLastCheckpoint. > > int new_done; > SpinLockAcquire(&CheckpointerShmem->ckpt_lck); > new_done = CheckpointerShmem->ckpt_done; > SpinLockRelease(&CheckpointerShmem->ckpt_lck); > > I think could certainly be replaced by something like > > + pg_compiler_barrier() > + new_done = CheckpointerShmem->ckpt_done; > + pg_compiler_barrier() That's not a correct transformation. A compiler barrier does not guarantee cache coherency. You actually have to use correct memory barrier pairings. It might suffice to use a read memory barrier in this case, but obviously the easiest transformation is to use full memory barriers on both sides. The main reason for not having changed all of these over is that you actually have to carefully think about the changes... > Grepping the codebase we get 105 matches, in 26 files. > > One interesting case is xlog.c that uses the lock to protect 64-bit > assignments I saw conversations about pg_atomic_u64 for that, but then in > 32-bit platforms we get this weird 3-field structure everywhere. What are you referring to with "weird 3-field structure"? Greetings, Andres Freund
Re: Throwing away unnecessary spin-locks
Hi,
On 2026-10-01 20:50:35 +0100, Alexandre Felipe wrote:
> Please try, if you want
> $ grep -A 2 -rn SpinLockAcquire src/backend | grep SpinLockRelease -B 2
>
> Story
> =====
> A common pattern that I have been repeatedly warned against is using
> spin-locks unnecessarily. I was surprised to see in checkpointer.c
> FirstCallSinceLastCheckpoint.
>
> int new_done;
> SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
> new_done = CheckpointerShmem->ckpt_done;
> SpinLockRelease(&CheckpointerShmem->ckpt_lck);
>
> I think could certainly be replaced by something like
>
> + pg_compiler_barrier()
> + new_done = CheckpointerShmem->ckpt_done;
> + pg_compiler_barrier()
That's not a correct transformation. A compiler barrier does not guarantee
cache coherency. You actually have to use correct memory barrier pairings. It
might suffice to use a read memory barrier in this case, but obviously the
easiest transformation is to use full memory barriers on both sides.
#define SLOCK_DEFINE_SCALAR_ACCESSORS(typename) \
static inline typename \
slock_read_barrier_##typename(volatile slock_t *lock, volatile typename *p) \
{ \
typename val; \
\
(void) lock; \
AssertPointerAlignment(p, alignof(typename)); \
val = *p; \
pg_read_barrier(); \
return val; \
} \
static inline void \
slock_write_barrier_##typename(volatile slock_t *lock, volatile typename *p, typename v) \
{ \
(void) lock; \
AssertPointerAlignment(p, alignof(typename)); \
*p = v; \
pg_write_barrier(); \
}
The main reason for not having changed all of these over is that you actually
have to carefully think about the changes...
> Grepping the codebase we get 105 matches, in 26 files.
>
> One interesting case is xlog.c that uses the lock to protect 64-bit
> assignments I saw conversations about pg_atomic_u64 for that, but then in
> 32-bit platforms we get this weird 3-field structure everywhere.
What are you referring to with "weird 3-field structure"?
typedef struct pg_atomic_uint64
{
int sema;
volatile uint64 value;
} pg_atomic_uint64;
Re: Throwing away unnecessary spin-locks
On Thu, Oct 1, 2026 at 9:08 PM Andres Freund <andres@anarazel.de> wrote:Hi,
On 2026-10-01 20:50:35 +0100, Alexandre Felipe wrote:
> Please try, if you want
> $ grep -A 2 -rn SpinLockAcquire src/backend | grep SpinLockRelease -B 2
>
> Story
> =====
> A common pattern that I have been repeatedly warned against is using
> spin-locks unnecessarily. I was surprised to see in checkpointer.c
> FirstCallSinceLastCheckpoint.
>
> int new_done;
> SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
> new_done = CheckpointerShmem->ckpt_done;
> SpinLockRelease(&CheckpointerShmem->ckpt_lck);
>
> I think could certainly be replaced by something like
>
> + pg_compiler_barrier()
> + new_done = CheckpointerShmem->ckpt_done;
> + pg_compiler_barrier()
That's not a correct transformation. A compiler barrier does not guarantee
cache coherency. You actually have to use correct memory barrier pairings. It
might suffice to use a read memory barrier in this case, but obviously the
easiest transformation is to use full memory barriers on both sides.Like this (sometimes I miss C++ templates)#define SLOCK_DEFINE_SCALAR_ACCESSORS(typename) \
static inline typename \
slock_read_barrier_##typename(volatile slock_t *lock, volatile typename *p) \
{ \
typename val; \
\
(void) lock; \
AssertPointerAlignment(p, alignof(typename)); \
val = *p; \
pg_read_barrier(); \
return val; \
} \
static inline void \
slock_write_barrier_##typename(volatile slock_t *lock, volatile typename *p, typename v) \
{ \
(void) lock; \
AssertPointerAlignment(p, alignof(typename)); \
*p = v; \
pg_write_barrier(); \
}
Or on both sides?The main reason for not having changed all of these over is that you actually
have to carefully think about the changes...Or thinking once about conditions that make them possible and apply sometransformation.> Grepping the codebase we get 105 matches, in 26 files.
>
> One interesting case is xlog.c that uses the lock to protect 64-bit
> assignments I saw conversations about pg_atomic_u64 for that, but then in
> 32-bit platforms we get this weird 3-field structure everywhere.
What are you referring to with "weird 3-field structure"?
typedef struct pg_atomic_uint64
{
int sema;
volatile uint64 value;
} pg_atomic_uint64;Sorry it is not declared as 3 field, I was thinkingstruct {int sema; volatile uint32 v1; volatile uint32 v2; }Regards,Alexandre
Re: Throwing away unnecessary spin-locks
> #define SLOCK_DEFINE_SCALAR_ACCESSORS(typename) \
> static inline typename \
> slock_read_barrier_##typename(volatile slock_t *lock, volatile typename *p) \
> { \
> typename val; \
> \
> (void) lock; \
> AssertPointerAlignment(p, alignof(typename)); \
> val = *p; \
> pg_read_barrier(); \
> return val; \
> } \
> static inline void \
> slock_write_barrier_##typename(volatile slock_t *lock, volatile typename *p, typename v) \
> { \
> (void) lock; \
> AssertPointerAlignment(p, alignof(typename)); \
> *p = v; \
> pg_write_barrier(); \
>
> }
On x86 pg_read_barrier() and pg_write_barrier() are only compiler
barriers, so v1 ends up as a plain load and a plain store there. That
hides problems. ARM, RISC-V and POWER are weakly ordered, and on those
a write barrier placed after the store does not order it against the
stores before it. The spinlock version does not have that problem.
convert the field to pg_atomic and use pg_atomic_read_membarrier_u32()
and pg_atomic_write_membarrier_u32().
Thanks,
Shihao
Re: Throwing away unnecessary spin-locks
Hi Alexandre,> #define SLOCK_DEFINE_SCALAR_ACCESSORS(typename) \
> static inline typename \
> slock_read_barrier_##typename(volatile slock_t *lock, volatile typename *p) \
> { \
> typename val; \
> \
> (void) lock; \
> AssertPointerAlignment(p, alignof(typename)); \
> val = *p; \
> pg_read_barrier(); \
> return val; \
> } \
> static inline void \
> slock_write_barrier_##typename(volatile slock_t *lock, volatile typename *p, typename v) \
> { \
> (void) lock; \
> AssertPointerAlignment(p, alignof(typename)); \
> *p = v; \
> pg_write_barrier(); \
>
> }
On x86 pg_read_barrier() and pg_write_barrier() are only compiler
barriers, so v1 ends up as a plain load and a plain store there. That
hides problems. ARM, RISC-V and POWER are weakly ordered, and on those
a write barrier placed after the store does not order it against the
stores before it. The spinlock version does not have that problem.
I think the safer route is what recent commits like df3978c2340 did,
convert the field to pg_atomic and use pg_atomic_read_membarrier_u32()
and pg_atomic_write_membarrier_u32().
I see that pg_atomic_write_membarrier_u32 uses an exchange,
Re: Throwing away unnecessary spin-locks
I think the safer route is what recent commits like df3978c2340 did,
convert the field to pg_atomic and use pg_atomic_read_membarrier_u32()
and pg_atomic_write_membarrier_u32().
Actually, v1 conflicted with that commit, rebasing.
Re: Throwing away unnecessary spin-locks
Hi,
I'm sorry to be blunt, but this dangerous stuff. You're just doing mechanical
replacements, without any analysis. That's a recipe for hard to encounter and
find bugs.
It also doesn't make any sort of sense to me to use slock_* or whatnot. This
has nothing to do with spinlocks.
> @@ -2748,9 +2746,7 @@ XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
> void
> XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
> {
> - SpinLockAcquire(&XLogCtl->info_lck);
> - XLogCtl->replicationSlotMinLSN = lsn;
> - SpinLockRelease(&XLogCtl->info_lck);
> + slock_write_uint64(&XLogCtl->info_lck, &XLogCtl->replicationSlotMinLSN, lsn);
> }
Just about all the write side ones are completely wrong. This allows the
field to be set while someone else holds the spinlock. As I said before, this
needs careful, documented, analysis FOR EACH AND EVERY SINGLE CHANGE.
This isn't to say that the read sides are right either. Some of them very well
might depend on the read not happening while multiple fields were updated with
the lock held.
Greetings,
Andres Freund
Re: Throwing away unnecessary spin-locks
Hi,
I'm sorry to be blunt, but this dangerous stuff. You're just doing mechanical
replacements, without any analysis. That's a recipe for hard to encounter and
find bugs.
It also doesn't make any sort of sense to me to use slock_* or whatnot. This
has nothing to do with spinlocks.
> @@ -2748,9 +2746,7 @@ XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
> void
> XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
> {
> - SpinLockAcquire(&XLogCtl->info_lck);
> - XLogCtl->replicationSlotMinLSN = lsn;
> - SpinLockRelease(&XLogCtl->info_lck);
> + slock_write_uint64(&XLogCtl->info_lck, &XLogCtl->replicationSlotMinLSN, lsn);
> }
Just about all the write side ones are completely wrong. This allows the
field to be set while someone else holds the spinlock. As I said before, this
needs careful, documented, analysis FOR EACH AND EVERY SINGLE CHANGE.
- current_bytepos = Insert->CurrBytePos;
- SpinLockRelease(&Insert->insertpos_lck);
startbytepos = Insert->C
startbytepos = Insert->CurrBytePos;
endbytepos = startbytepos + size;
prevbytepos = Insert->PrevBytePos;
Insert->CurrBytePos = endbytepos;
Insert->PrevBytePos = startbytepos;
urrBytePos;
endbytepos = startbytepos + size;
prevbytepos = Insert->PrevBytePos;
Insert->CurrBytePos = endbytepos;
Insert->PrevBytePos = startbytepos;
SpinLockRelease(&Insert->insertpos_lck);
Re: Throwing away unnecessary spin-locks
Hi,
I'm sorry to be blunt, but this dangerous stuff. You're just doing mechanical
replacements, without any analysis. That's a recipe for hard to encounter and
find bugs.
It also doesn't make any sort of sense to me to use slock_* or whatnot. This
has nothing to do with spinlocks.
> @@ -2748,9 +2746,7 @@ XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
> void
> XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
> {
> - SpinLockAcquire(&XLogCtl->info_lck);
> - XLogCtl->replicationSlotMinLSN = lsn;
> - SpinLockRelease(&XLogCtl->info_lck);
> + slock_write_uint64(&XLogCtl->info_lck, &XLogCtl->replicationSlotMinLSN, lsn);
> }
Just about all the write side ones are completely wrong. This allows the
field to be set while someone else holds the spinlock. As I said before, this
needs careful, documented, analysis FOR EACH AND EVERY SINGLE CHANGE.
Here is one that looks really broken:- SpinLockAcquire(&Insert->insertpos_lck);
- current_bytepos = Insert->CurrBytePos;
- SpinLockRelease(&Insert->insertpos_lck);+ current_bytepos = slock_read_uint64(&Insert->insertpos_lck, &Insert->CurrBytePos);The insert itself is guarded via:SpinLockAcquire(&Insert->insertpos_lck);
startbytepos = Insert->C
startbytepos = Insert->CurrBytePos;
endbytepos = startbytepos + size;
prevbytepos = Insert->PrevBytePos;
Insert->CurrBytePos = endbytepos;
Insert->PrevBytePos = startbytepos;
urrBytePos;
endbytepos = startbytepos + size;
prevbytepos = Insert->PrevBytePos;
Insert->CurrBytePos = endbytepos;
Insert->PrevBytePos = startbytepos;
SpinLockRelease(&Insert->insertpos_lck);,
Trying to find your snippet, is this the one?
1262 ReserveXLogSwitch(XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
1263 {
1278 SpinLockAcquire(&Insert->insertpos_lck);
1280 startbytepos = Insert->CurrBytePos;
1282 ptr = XLogBytePosToEndRecPtr(startbytepos);
1290 endbytepos = startbytepos + size;
1306 SpinLockRelease(&Insert->insertpos_lck);
You can't sneak under the lock in this case. The insert lock seems very dangerous in general. You might have better luck with the info_lock. Have you measured contention?
Re: Throwing away unnecessary spin-locks
Trying to find your snippet, is this the one?
1262 ReserveXLogSwitch(XLogRecPtr *StartPos, XLogRecPtr *EndPos, XLogRecPtr *PrevPtr)
1263 {
1278 SpinLockAcquire(&Insert->insertpos_lck);
1280 startbytepos = Insert->CurrBytePos;
1282 ptr = XLogBytePosToEndRecPtr(startbytepos);
1290 endbytepos = startbytepos + size;1303 Insert->CurrBytePos = endbytepos;
1306 SpinLockRelease(&Insert->insertpos_lck);This illustrates well the cases where things can't be written without a lock.atomically. Because changes between line 1280 and line 1303 are rolled back.However, this particular example doesn't stop us from reading because it will eitherCurBytePos before line 1303, and that is the same as reading under a lock before thatblock, or after the line 1303 and that is equivalent to reading under a lock after that block.
Re: Throwing away unnecessary spin-locks
Hi,
On 2026-10-02 18:01:59 +0100, Alexandre Felipe wrote:
> On Fri, Oct 2, 2026 at 7:20 AM Andres Freund wrote:
> > I'm sorry to be blunt, but this dangerous stuff. You're just doing
> > mechanical
> > replacements, without any analysis. That's a recipe for hard to encounter
> > and
> > find bugs.
> That is a fair point, it doesn't mean that we won't analyse them.
>
> I want to have a high level view and see if this is something we can
> mechanise.
Shrug. Then you would not have had to post this, since you could just have
analyzed it before doing so.
> And I appreciate all your feedback, this is how I am getting more familiar
> with the hidden rules :)
Not rather fundamentally mechanically changing locking code isn't a
particularly hidden rule...
> > It also doesn't make any sort of sense to me to use slock_* or whatnot.
> > This
> > has nothing to do with spinlocks.
> >
>
> It doesn't use the spin lock, but that is a way to say to the future
> readers: look, this variable is guarded by a spin lock, and we know what we
> are doing.
> If we simply remove the spin lock in the future someone might look and
> wonder "I think they forgot a lock here! I saw the same variable guarded by
> a lock elsewhere."
I don't buy this. I think for most of these the answer would be to move the
relevant state entirely outside of the spinlock protected rule. The whole
point here is to make them *independent* of the spinlock. On both the read
*and* the write side (or have the spinlock on the write side just protect
against independent dangers).
> > > @@ -2748,9 +2746,7 @@ XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
> > > void
> > > XLogSetReplicationSlotMinimumLSN(XLogRecPtr lsn)
> > > {
> > > - SpinLockAcquire(&XLogCtl->info_lck);
> > > - XLogCtl->replicationSlotMinLSN = lsn;
> > > - SpinLockRelease(&XLogCtl->info_lck);
> > > + slock_write_uint64(&XLogCtl->info_lck, &XLogCtl->replicationSlotMinLSN,
> > lsn);
> > > }
> >
>
> > Just about all the write side ones are completely wrong. This allows the
> > field to be set while someone else holds the spinlock. As I said before,
> > this
> > needs careful, documented, analysis FOR EACH AND EVERY SINGLE CHANGE.
> >
>
> Noted,
> Can I count on you to look at the details?
I can't promise that. The code quality and payoff need to be high enough to
make that worthwhile. I'm willing to look some, but I certainly won't have
time to review many rounds of dozens of patches.
> Is breaking one commit per block and do the analysis at the patch preamble a
> good way to share the analysis?
I'd not do many of these at once. Do one simple conversion, with the relevant
analysis, and see whether it goes anywhere and whether you need to adjust your
approach. After that maybe 2 at once.
I don't think one commit per block is quite right either. You'll typically
have to add comments to the read and write side of some specific state
together, splitting that into separate commits doesn't make sense.
And the code needs to document the rules, not just the commit message.
> The analysis will be way longer than the changes themselves.
Of course.
Greetings,
Andres Freund
Re: Throwing away unnecessary spin-locks
I'd not do many of these at once. Do one simple conversion, with the relevant
analysis, and see whether it goes anywhere and whether you need to adjust your
approach. After that maybe 2 at once.
I don't think one commit per block is quite right either. You'll typically
have to add comments to the read and write side of some specific state
together, splitting that into separate commits doesn't make sense.
And the code needs to document the rules, not just the commit message.
> The analysis will be way longer than the changes themselves.
Of course.