On 2026-09-08 Tu 6:08 AM, 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.

One of these revealed a small problem with how pgindent handles alignas (it doesn't know about it and it might or might not work well depending on context), so I added a workaround into pgindent to fix that.  (Or we could try to reshuffle that code to avoid the problem.)


The pgindent recipe should possibly be hardened for things like nested parens. Here's a reworked patch 2.


cheers


andrew


--
Andrew Dunstan
EDB: https://www.enterprisedb.com
From 2e9902b95bea53da9862545cbb2c7d5042db0cd1 Mon Sep 17 00:00:00 2001
From: Andrew Dunstan <[email protected]>
Date: Tue, 8 Sep 2026 09:24:59 -0400
Subject: [PATCH v2 2/2] pgindent: Fix indentation of alignas() in struct
 members

pg_bsd_indent doesn't know about alignas(), so a struct member
declared with it is misindented unless it's the first member.

Disguise each alignas(...) call as a plain identifier while
indenting, restoring it afterward.  Stash the call's original text
by index rather than embedding it in the placeholder: embedding only
works for a single-token argument, and fails to match at all against
nested parens (e.g. alignas(sizeof(x))) or a line break, silently
skipping the disguise.

Also reindent the one affected place in xlogreader.h.

Author: Peter Eisentraut <[email protected]>
Reviewed-by: Andrew Dunstan <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
 src/include/access/xlogreader.h |  2 +-
 src/tools/pgindent/pgindent     | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/src/include/access/xlogreader.h b/src/include/access/xlogreader.h
index a2563458018..6e27b30fb35 100644
--- a/src/include/access/xlogreader.h
+++ b/src/include/access/xlogreader.h
@@ -262,7 +262,7 @@ struct XLogReaderState
 	/*
 	 * Buffer for currently read page (valid up to at least readLen bytes)
 	 */
-				alignas(MAXIMUM_ALIGNOF) char readBuf[XLOG_BLCKSZ];
+	alignas(MAXIMUM_ALIGNOF) char readBuf[XLOG_BLCKSZ];
 	uint32		readLen;
 
 	/* last read XLOG position for data currently in readBuf */
diff --git a/src/tools/pgindent/pgindent b/src/tools/pgindent/pgindent
index 004b8fcab00..eea6c0ad734 100755
--- a/src/tools/pgindent/pgindent
+++ b/src/tools/pgindent/pgindent
@@ -235,6 +235,10 @@ sub write_source
 	return;
 }
 
+# Text of each alignas(...) call, stashed by pre_indent() and restored
+# by post_indent().
+my @alignas_stash;
+
 sub pre_indent
 {
 	my $source = shift;
@@ -263,6 +267,19 @@ sub pre_indent
 	# Protect wrapping in CATALOG()
 	$source =~ s!^(CATALOG\(.*)$!/*$1*/!gm;
 
+	# pg_bsd_indent doesn't know about alignas(), so a non-first struct
+	# member declared with it gets misindented.  Disguise each call as
+	# a plain identifier; stash the original text rather than embed it,
+	# so nested parens or line breaks in the argument aren't a problem.
+	@alignas_stash = ();
+	$source =~ s!
+		\b alignas \s*
+		( \( (?: [^()]++ | (?1) )*+ \) )
+	!
+		push(@alignas_stash, "alignas" . $1);
+		"alignas_" . $#alignas_stash . "_";
+	!gex;
+
 	return $source;
 }
 
@@ -270,6 +287,9 @@ sub post_indent
 {
 	my $source = shift;
 
+	# Restore alignas(...)
+	$source =~ s!\balignas_(\d+)_!$alignas_stash[$1]!g;
+
 	# Restore CATALOG lines
 	$source =~ s!^/\*(CATALOG\(.*)\*/$!$1!gm;
 
-- 
2.43.0

Reply via email to