pgsql: Introduce PG_IO_ALIGN_SIZE and align all I/O buffers.

From: Thomas Munro <tmunro(at)postgresql(dot)org>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Introduce PG_IO_ALIGN_SIZE and align all I/O buffers.
Date: 2023-04-08 04:35:13
Message-ID: E1pl0IC-001z10-0L@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Introduce PG_IO_ALIGN_SIZE and align all I/O buffers.

In order to have the option to use O_DIRECT/FILE_FLAG_NO_BUFFERING in a
later commit, we need the addresses of user space buffers to be well
aligned. The exact requirements vary by OS and file system (typically
sectors and/or memory pages). The address alignment size is set to
4096, which is enough for currently known systems: it matches modern
sectors and common memory page size. There is no standard governing
O_DIRECT's requirements so we might eventually have to reconsider this
with more information from the field or future systems.

Aligning I/O buffers on memory pages is also known to improve regular
buffered I/O performance.

Three classes of I/O buffers for regular data pages are adjusted:
(1) Heap buffers are now allocated with the new palloc_aligned() or
MemoryContextAllocAligned() functions introduced by commit 439f6175.
(2) Stack buffers now use a new struct PGIOAlignedBlock to respect
PG_IO_ALIGN_SIZE, if possible with this compiler. (3) The buffer
pool is also aligned in shared memory.

WAL buffers were already aligned on XLOG_BLCKSZ. It's possible for
XLOG_BLCKSZ to be configured smaller than PG_IO_ALIGNED_SIZE and thus
for O_DIRECT WAL writes to fail to be well aligned, but that's a
pre-existing condition and will be addressed by a later commit.

BufFiles are not yet addressed (there's no current plan to use O_DIRECT
for those, but they could potentially get some incidental speedup even
in plain buffered I/O operations through better alignment).

If we can't align stack objects suitably using the compiler extensions
we know about, we disable the use of O_DIRECT by setting PG_O_DIRECT to
0. This avoids the need to consider systems that have O_DIRECT but
can't align stack objects the way we want; such systems could in theory
be supported with more work but we don't currently know of any such
machines, so it's easier to pretend there is no O_DIRECT support
instead. That's an existing and tested class of system.

Add assertions that all buffers passed into smgrread(), smgrwrite() and
smgrextend() are correctly aligned, unless PG_O_DIRECT is 0 (= stack
alignment tricks may be unavailable) or the block size has been set too
small to allow arrays of buffers to be all aligned.

Author: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
Author: Andres Freund <andres(at)anarazel(dot)de>
Reviewed-by: Justin Pryzby <pryzby(at)telsasoft(dot)com>
Discussion: https://postgr.es/m/CA+hUKGK1X532hYqJ_MzFWt0n1zt8trz980D79WbjwnT-yYLZpg@mail.gmail.com

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/faeedbcefd40bfdf314e048c425b6d9208896d90

Modified Files
--------------
contrib/bloom/blinsert.c | 2 +-
contrib/pg_prewarm/pg_prewarm.c | 2 +-
src/backend/access/gist/gistbuild.c | 9 ++++----
src/backend/access/hash/hashpage.c | 2 +-
src/backend/access/heap/rewriteheap.c | 2 +-
src/backend/access/nbtree/nbtree.c | 2 +-
src/backend/access/nbtree/nbtsort.c | 8 +++++---
src/backend/access/spgist/spginsert.c | 2 +-
src/backend/access/transam/generic_xlog.c | 13 ++++++++----
src/backend/access/transam/xlog.c | 2 +-
src/backend/catalog/storage.c | 2 +-
src/backend/storage/buffer/buf_init.c | 10 ++++++---
src/backend/storage/buffer/bufmgr.c | 2 +-
src/backend/storage/buffer/localbuf.c | 7 +++++--
src/backend/storage/file/buffile.c | 6 ++++++
src/backend/storage/page/bufpage.c | 5 ++++-
src/backend/storage/smgr/md.c | 15 +++++++++++++-
src/backend/utils/sort/logtape.c | 2 +-
src/bin/pg_checksums/pg_checksums.c | 2 +-
src/bin/pg_rewind/local_source.c | 4 ++--
src/bin/pg_upgrade/file.c | 4 ++--
src/common/file_utils.c | 4 ++--
src/include/c.h | 34 +++++++++++++++++++++++--------
src/include/pg_config_manual.h | 6 ++++++
src/include/storage/fd.h | 5 +++--
src/tools/pgindent/typedefs.list | 1 +
26 files changed, 108 insertions(+), 45 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Andres Freund 2023-04-08 05:08:10 pgsql: Replace replication slot's invalidated_at LSN with an enum
Previous Message Amit Kapila 2023-04-08 04:24:00 pgsql: Doc: Fix the datatype of the newly added SUBSCRIPTION options.