mirror of
https://github.com/postgres/postgres.git
synced 2025-07-11 10:01:57 +03:00
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.munro@gmail.com>
Author: Andres Freund <andres@anarazel.de>
Reviewed-by: Justin Pryzby <pryzby@telsasoft.com>
Discussion: https://postgr.es/m/CA+hUKGK1X532hYqJ_MzFWt0n1zt8trz980D79WbjwnT-yYLZpg@mail.gmail.com
This commit is contained in:
@ -415,7 +415,7 @@ gist_indexsortbuild(GISTBuildState *state)
|
||||
* Write an empty page as a placeholder for the root page. It will be
|
||||
* replaced with the real root page at the end.
|
||||
*/
|
||||
page = palloc0(BLCKSZ);
|
||||
page = palloc_aligned(BLCKSZ, PG_IO_ALIGN_SIZE, MCXT_ALLOC_ZERO);
|
||||
smgrextend(RelationGetSmgr(state->indexrel), MAIN_FORKNUM, GIST_ROOT_BLKNO,
|
||||
page, true);
|
||||
state->pages_allocated++;
|
||||
@ -509,7 +509,8 @@ gist_indexsortbuild_levelstate_add(GISTBuildState *state,
|
||||
levelstate->current_page++;
|
||||
|
||||
if (levelstate->pages[levelstate->current_page] == NULL)
|
||||
levelstate->pages[levelstate->current_page] = palloc(BLCKSZ);
|
||||
levelstate->pages[levelstate->current_page] =
|
||||
palloc_aligned(BLCKSZ, PG_IO_ALIGN_SIZE, 0);
|
||||
|
||||
newPage = levelstate->pages[levelstate->current_page];
|
||||
gistinitpage(newPage, old_page_flags);
|
||||
@ -579,7 +580,7 @@ gist_indexsortbuild_levelstate_flush(GISTBuildState *state,
|
||||
|
||||
/* Create page and copy data */
|
||||
data = (char *) (dist->list);
|
||||
target = palloc0(BLCKSZ);
|
||||
target = palloc_aligned(BLCKSZ, PG_IO_ALIGN_SIZE, MCXT_ALLOC_ZERO);
|
||||
gistinitpage(target, isleaf ? F_LEAF : 0);
|
||||
for (int i = 0; i < dist->block.num; i++)
|
||||
{
|
||||
@ -630,7 +631,7 @@ gist_indexsortbuild_levelstate_flush(GISTBuildState *state,
|
||||
if (parent == NULL)
|
||||
{
|
||||
parent = palloc0(sizeof(GistSortedBuildLevelState));
|
||||
parent->pages[0] = (Page) palloc(BLCKSZ);
|
||||
parent->pages[0] = (Page) palloc_aligned(BLCKSZ, PG_IO_ALIGN_SIZE, 0);
|
||||
parent->parent = NULL;
|
||||
gistinitpage(parent->pages[0], 0);
|
||||
|
||||
|
Reference in New Issue
Block a user