Re: Use C11 alignas instead of palloc/malloc for alignment

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Peter Eisentraut <peter(at)eisentraut(dot)org>, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Use C11 alignas instead of palloc/malloc for alignment
Date: 2026-09-08 11:55:11
Message-ID: d9e109fd-b406-4833-808c-6d27854df8ae@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 08/09/2026 13:08, Peter Eisentraut wrote:
> There are a number of places where palloc()/malloc()/etc. was used
> solely to obtain an aligned buffer.  We can do these much simpler by
> using alignas with a local variable instead.  See attached patch.

In XLogRecordAssemble, you could use a local variable in XLogInsert()
for the scratch buffer, and pass a pointer to that to
XLogRecordAssemble(). That would avoid the a global variable, which is
nice for multi-threading.

Not new with this patch, but the calculations for HEADER_SCRATCH_SIZE
are a little scary. I'd also suggest adding an assertion somewhere that
you don't overrun the HEADER_SCRATCH_SIZE buffer, and maybe some extra
comments. I don't see any bugs there, but it's very subtle that if you
e.g. add a new "special" block ID similar to XLR_BLOCK_ID_TOPLEVEL_XID,
you need to remember to adjust HEADER_SCRATCH_SIZE accordingly.

For the BLCKSZ-sized buffers, how about using PGAlignedBlock?

- Heikki

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message solai v 2026-09-08 11:55:17 Re: Bug? pg_rewind produces unusable but starting database with standby recovery
Previous Message Ilya Cherdakov 2026-09-08 11:52:58 Assert in test_bms_membership();