Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vuqaR-009TRo-1B for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Feb 2026 11:28:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vuqaQ-000q3I-0r for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Feb 2026 11:28:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vuqaP-000q3A-2h for pgsql-hackers@lists.postgresql.org; Tue, 24 Feb 2026 11:28:17 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vuqaM-00000000wvu-2TcD for pgsql-hackers@postgresql.org; Tue, 24 Feb 2026 11:28:16 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-65a43a512b0so6040577a12.3 for ; Tue, 24 Feb 2026 03:28:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1771932494; x=1772537294; darn=postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=WnD7GfhoIoaGh7UapwV3eNiepqUi13LjM2hz+MnxID4=; b=g+FxjLLVbRuwlAIY7KnrXFJfrbYzy+Rlp3W4XkUPBhveNCrz+HgXLphVEZDurw7dXz D9cyuFkJHMW0v39U/68wfSDilTPB+3xq+IEcaD5wSYmgNXEAJu4Zj1RyeRYyqYizjEbr Q4Q1Rc368E2nKb0lSN/GolpkSlX1Cs37GFnmXmOuQwIsEmVOdI7o9e7Beko40Z4RCbU7 0vJ+WABnLRF1e4JJihMa4lD2PQJptYj49i9uUIXbu4JKeUJPMTI0jrtbbwIWvPqpFJX7 Ajb4JwraJZUpc1W26/ES42ZsxQbphrjn50OLrzfUpXGgs9A6aFRnm5iZ/nBqdA8fMA4g z9FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771932494; x=1772537294; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WnD7GfhoIoaGh7UapwV3eNiepqUi13LjM2hz+MnxID4=; b=Y8WLioJpcws6GzeUCBLYGmzxnhICXGrqMo2xh6F1fsZ8nrlQuBKN/pDj38FmEZJiS8 ntGF2aDd1U0OPIu4kNUfCwuEc8LRT/tZrZL8T+oGSx2Rg+yzUjxTka697cs3K1HJwUSt RFZZa3nleBCe4jAXsRQcyz5d/QvXV/wFBFuqxPChvjo/ln9wVZsvMtgSt47uNnxJAK1y 4TBH8eukRw4wKWYDM53CqMyje3EKyGC0LWs4ZhT9ZX8bvxOZnTv2yPavwrKBOm1b+ZRD skTmbMppaSYnlPBEEUsiRt+JbrTS5C8D9WACX3H2UrdMWKtj3XWcBPiB7ftxDiG8ucFD LXdA== X-Forwarded-Encrypted: i=1; AJvYcCXsLpyHO1Q0DokqeAGd59Q050a2cACgmAa8KrMzS9k9Qzkxu0RQvgYy6fD27mrkXnE2fkj5QGiSx1PwwUjC@postgresql.org X-Gm-Message-State: AOJu0YxvZ7lghtux3XQz31F9K25whsdsRAzJnO8unVCVT025BNQ5jwyF /s46JBe9n6JifI1GTbapP9Swze/t1kcWZVLixBFv8UwIBhg/fuf/g6AV X-Gm-Gg: ATEYQzxXLrxwMnWtJ7l1vUAOzbyI/LCjiaax2wOWqFl5+BeOolHTOngHdFuWfN7FM6a NC94bxnQHZguQwpcdNzVOb4Nk7Xa/7cP66W3MUSiGxwCSKbmN382CD8NCusyxMnG4HOVwvsd7Xd vOv6IRHHY6flnSwtoozt0xttSXZ+etVBLwN/Odsmu3BvQivesWxOhXzY4uS2FBY1/3TAk7aNSIM Aow+aZR6Q8iOwWbrSeZxWUXlnXKOwmQQligK1XarCZ/yoK2Gpc8zgDCj7kq+ASAyisdLW0PPtHq LMrNaopxVTCOwXbqbpjbOx2F6bDDXNx427z9UKOYEaC1O0N7iwQ0au3eXWerJmcEi656H27onIu K/LrPVsxDjJ3cPKIf7nfUqlqEBzf2tmA7HLHjtsj+y+gfjYyrqnEhfRAqx+GMTKh67dv86cQHLA H9Tpvvdr7OciIhz9pnKcaCc5ycHQmqCN7cB0RSyvBZdKYELtyNTn92yPv+zEGwbk9OUV+3zjfLG 141yeXD5SXjA1hi+oaojo/5iS7lWDZEA0xFmzoKwQcsxfNFg1KiSfBWaQ== X-Received: by 2002:a17:907:3ccb:b0:b87:206a:a241 with SMTP id a640c23a62f3a-b90819214e8mr719235566b.11.1771932493532; Tue, 24 Feb 2026 03:28:13 -0800 (PST) Received: from ip-10-97-1-34.eu-west-3.compute.internal (ec2-15-237-197-144.eu-west-3.compute.amazonaws.com. [15.237.197.144]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b9084e9fd46sm413478666b.53.2026.02.24.03.28.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Feb 2026 03:28:12 -0800 (PST) Date: Tue, 24 Feb 2026 11:28:11 +0000 From: Bertrand Drouvot To: Peter Eisentraut Cc: Heikki Linnakangas , Andres Freund , "pgsql-hackers@postgresql.org" Subject: Re: PGPROC alignment (was Re: pgsql: Separate RecoveryConflictReasons from procsignals) Message-ID: References: <1cb0d7e9-d6dd-4517-a7cd-0ad98e1207f3@iki.fi> <3dd6f70c-b94d-4428-8e75-74a7136396be@iki.fi> <787be980-0878-4f4a-be01-d042ab5d370e@iki.fi> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="fe1S3SCuJMoalI5t" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --fe1S3SCuJMoalI5t Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, On Mon, Feb 23, 2026 at 05:13:29PM +0100, Peter Eisentraut wrote: > On 13.02.26 09:03, Bertrand Drouvot wrote: > > +/* > > + * If compiler understands aligned pragma, use it to align the struct at cache > > + * line boundaries. This is just for performance, to (a) avoid false sharing > > + * and (b) to make the multiplication / division to convert between PGPROC * > > + * and ProcNumber be a little cheaper. > > + */ > > +#if defined(pg_attribute_aligned) > > + pg_attribute_aligned(PG_CACHE_LINE_SIZE) > > +#endif > > +PGPROC; > > You can/should use C11 standard alignas(), so you don't need to worry about > whether it's supported or not. Oh right, I did not notice 300c8f53247 and following like e7075a3405c, d4c0f91f7d5 and 97e04c74bed. PFA, 0001 doing so for PGPROC and PgAioUringContext. As those are typedef, the patch puts alignas within the struct. For PGPROC at the start of the struct, I think that placing it on the first member is the right location because it ensures the whole struct is aligned to PG_CACHE_LINE_SIZE without adding padding before this member. For example if I set it on backendType, then it adds 100 bytes of padding and the struct is obviously still a multiple of PG_CACHE_LINE_SIZE but is now 1024 bytes (instead of 896). For PgAioUringContext at completion_lock (like suggested by Andres in [1]), which is also the start of the struct. I checked and the padding for those are exactly the same after the changes. 0002, is also making use of alignas in ItemPointerData, but this one is more tricky so I'm not sure that's worth it (given the fact that we still need to keep pg_attribute_aligned() as explained by Peter in [2]). [1]: https://postgr.es/m/lsgnps74ictxtm5karcobenxtt5rvoylvbvx7ja6r5rjcund7v%40owxqgv7gisns [2]: https://postgr.es/m/46f05236-d4d4-4b4e-84d4-faa500f14691%40eisentraut.org Regards, -- Bertrand Drouvot PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com --fe1S3SCuJMoalI5t Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-Use-C11-alignas-in-typedef-definitions.patch" From 3152fd1e4934378823d5e535f876cf1333cb086e Mon Sep 17 00:00:00 2001 From: Bertrand Drouvot Date: Tue, 24 Feb 2026 09:21:19 +0000 Subject: [PATCH v1 1/2] Use C11 alignas in typedef definitions They were already using pg_attribute_aligned. This replaces that with alignas and moves that into the required syntactic position. Suggested-by: Peter Eisentraut Author: Bertrand Drouvot Discussion: https://postgr.es/m/d7a788fa-e609-4894-a8be-2f70e135424f%40eisentraut.org --- src/backend/storage/aio/method_io_uring.c | 16 ++++++++-------- src/include/storage/proc.h | 13 +++++-------- 2 files changed, 13 insertions(+), 16 deletions(-) 58.1% src/backend/storage/aio/ 41.8% src/include/storage/ diff --git a/src/backend/storage/aio/method_io_uring.c b/src/backend/storage/aio/method_io_uring.c index ed6e71bcd46..50ea9f8e51c 100644 --- a/src/backend/storage/aio/method_io_uring.c +++ b/src/backend/storage/aio/method_io_uring.c @@ -77,15 +77,15 @@ const IoMethodOps pgaio_uring_ops = { .wait_one = pgaio_uring_wait_one, }; -/* - * Per-backend state when using io_method=io_uring - * - * Align the whole struct to a cacheline boundary, to prevent false sharing - * between completion_lock and prior backend's io_uring_ring. - */ -typedef struct pg_attribute_aligned (PG_CACHE_LINE_SIZE) -PgAioUringContext +/* Per-backend state when using io_method=io_uring */ +typedef struct PgAioUringContext { + /* + * Align the whole struct to a cacheline boundary, to prevent false + * sharing between completion_lock and prior backend's io_uring_ring. + */ + alignas(PG_CACHE_LINE_SIZE) + /* * Multiple backends can process completions for this backend's io_uring * instance (e.g. when the backend issuing IO is busy doing something diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index a8d2e7db1a1..fe798d350f1 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -180,6 +180,11 @@ typedef enum */ typedef struct PGPROC { + /* + * Align the struct at cache line boundaries. This is just for + * performance, to avoid false sharing. + */ + alignas(PG_CACHE_LINE_SIZE) dlist_head *procgloballist; /* procglobal list that owns this PGPROC */ dlist_node freeProcsLink; /* link in procgloballist, when in recycled * state */ @@ -375,14 +380,6 @@ typedef struct PGPROC uint32 wait_event_info; /* proc's wait information */ } - -/* - * If compiler understands aligned pragma, use it to align the struct at cache - * line boundaries. This is just for performance, to avoid false sharing. - */ -#if defined(pg_attribute_aligned) - pg_attribute_aligned(PG_CACHE_LINE_SIZE) -#endif PGPROC; extern PGDLLIMPORT PGPROC *MyProc; -- 2.34.1 --fe1S3SCuJMoalI5t Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0002-Use-C11-alignas-in-more-tricky-typedef-definition.patch" From e908a2c0e2b0a9d8a6915f60439f75adfe885b66 Mon Sep 17 00:00:00 2001 From: Bertrand Drouvot Date: Tue, 24 Feb 2026 09:55:36 +0000 Subject: [PATCH v1 2/2] Use C11 alignas in more tricky typedef definition Same as commit "xxx" but the alignas has to be guarded because it makes sense if used together with pg_attribute_packed(). Suggested-by: Peter Eisentraut Author: Bertrand Drouvot Discussion: https://postgr.es/m/d7a788fa-e609-4894-a8be-2f70e135424f%40eisentraut.org --- src/include/storage/itemptr.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 100.0% src/include/storage/ diff --git a/src/include/storage/itemptr.h b/src/include/storage/itemptr.h index 8b3392dbefe..a70c543dac3 100644 --- a/src/include/storage/itemptr.h +++ b/src/include/storage/itemptr.h @@ -35,14 +35,17 @@ */ typedef struct ItemPointerData { +/* If compiler understands packed pragma, use alignas with it */ +#if defined(pg_attribute_packed) + alignas(2) +#endif BlockIdData ip_blkid; OffsetNumber ip_posid; } -/* If compiler understands packed and aligned pragmas, use those */ -#if defined(pg_attribute_packed) && defined(pg_attribute_aligned) +/* If compiler understands packed pragma, use it with alignas */ +#if defined(pg_attribute_packed) pg_attribute_packed() - pg_attribute_aligned(2) #endif ItemPointerData; -- 2.34.1 --fe1S3SCuJMoalI5t--