Re: Remove header lock BufferGetLSNAtomic() on architectures with 64 bit atomic operations - Mailing list pgsql-hackers

From Andres Freund
Subject Re: Remove header lock BufferGetLSNAtomic() on architectures with 64 bit atomic operations
Date
Msg-id 4e3gzrvqdrgw2sds3bnqylgbuq63j3ebtcgxomogkia4sqmhir@xmpndr5ufjmq
Whole thread Raw
In response to Remove header lock BufferGetLSNAtomic() on architectures with 64 bit atomic operations  (Andreas Karlsson <andreas@proxel.se>)
List pgsql-hackers
Hi,

On 2025-11-24 00:10:03 +0100, Andreas Karlsson wrote:
> Andres pointed out this possible optimization on Discord so I hacked up a
> quick patch which avoids taking a lock when reading the LSN from a page on
> architectures where we can be sure to not get a torn value. It is always
> nice to remove a lock from a reasonably hot code path.

Nice.

>  static inline XLogRecPtr
>  PageXLogRecPtrGet(PageXLogRecPtr val)
>  {
> -    return (uint64) val.xlogid << 32 | val.xrecoff;
> +    return val;
>  }
>  
>  #define PageXLogRecPtrSet(ptr, lsn) \
> -    ((ptr).xlogid = (uint32) ((lsn) >> 32), (ptr).xrecoff = (uint32) (lsn))
> +    ((ptr) = (lsn))
> +
> +#else
> +
> +static inline XLogRecPtr
> +PageXLogRecPtrGet(volatile PageXLogRecPtr val)
> +{
> +    return (val << 32) | (val >> 32);
> +}

A volatile on a non-pointer won't do you much good, I'm afraid. You need to
make sure that the underlying value is read as a single 8 byte read, I don't
see how this guarantees that, unfortunately.

Greetings,

Andres Freund



pgsql-hackers by date:

Previous
From: Andres Freund
Date:
Subject: Re: lsyscache: free IndexAmRoutine objects returned by GetIndexAmRoutineByAmId()
Next
From: "Jelte Fennema-Nio"
Date:
Subject: Re: RFC: adding pytest as a supported test framework