Re: Remove header lock BufferGetLSNAtomic() on architectures with 64 bit atomic operations

From: Andres Freund <andres(at)anarazel(dot)de>
To: Andreas Karlsson <andreas(at)proxel(dot)se>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Remove header lock BufferGetLSNAtomic() on architectures with 64 bit atomic operations
Date: 2026-01-02 15:27:42
Message-ID: 4e3gzrvqdrgw2sds3bnqylgbuq63j3ebtcgxomogkia4sqmhir@xmpndr5ufjmq
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Jelte Fennema-Nio 2026-01-02 15:31:36 Re: RFC: adding pytest as a supported test framework
Previous Message Andres Freund 2026-01-02 15:21:41 Re: lsyscache: free IndexAmRoutine objects returned by GetIndexAmRoutineByAmId()