mirror of
https://github.com/postgres/postgres.git
synced 2025-08-17 01:02:17 +03:00
Avoid using potentially-under-aligned page buffers.
There's a project policy against using plain "char buf[BLCKSZ]" local or static variables as page buffers; preferred style is to palloc or malloc each buffer to ensure it is MAXALIGN'd. However, that policy's been ignored in an increasing number of places. We've apparently got away with it so far, probably because (a) relatively few people use platforms on which misalignment causes core dumps and/or (b) the variables chance to be sufficiently aligned anyway. But this is not something to rely on. Moreover, even if we don't get a core dump, we might be paying a lot of cycles for misaligned accesses. To fix, invent new union types PGAlignedBlock and PGAlignedXLogBlock that the compiler must allocate with sufficient alignment, and use those in place of plain char arrays. I used these types even for variables where there's no risk of a misaligned access, since ensuring proper alignment should make kernel data transfers faster. I also changed some places where we had been palloc'ing short-lived buffers, for coding style uniformity and to save palloc/pfree overhead. Since this seems to be a live portability hazard (despite the lack of field reports), back-patch to all supported versions. Patch by me; thanks to Michael Paquier for review. Discussion: https://postgr.es/m/1535618100.1286.3.camel@credativ.de
This commit is contained in:
@@ -96,7 +96,7 @@ struct BufFile
|
||||
off_t curOffset; /* offset part of current pos */
|
||||
int pos; /* next read/write position in buffer */
|
||||
int nbytes; /* total # of valid bytes in buffer */
|
||||
char buffer[BLCKSZ];
|
||||
PGAlignedBlock buffer;
|
||||
};
|
||||
|
||||
static BufFile *makeBufFileCommon(int nfiles);
|
||||
@@ -437,7 +437,7 @@ BufFileLoadBuffer(BufFile *file)
|
||||
* Read whatever we can get, up to a full bufferload.
|
||||
*/
|
||||
file->nbytes = FileRead(thisfile,
|
||||
file->buffer,
|
||||
file->buffer.data,
|
||||
sizeof(file->buffer),
|
||||
WAIT_EVENT_BUFFILE_READ);
|
||||
if (file->nbytes < 0)
|
||||
@@ -502,7 +502,7 @@ BufFileDumpBuffer(BufFile *file)
|
||||
file->offsets[file->curFile] = file->curOffset;
|
||||
}
|
||||
bytestowrite = FileWrite(thisfile,
|
||||
file->buffer + wpos,
|
||||
file->buffer.data + wpos,
|
||||
bytestowrite,
|
||||
WAIT_EVENT_BUFFILE_WRITE);
|
||||
if (bytestowrite <= 0)
|
||||
@@ -572,7 +572,7 @@ BufFileRead(BufFile *file, void *ptr, size_t size)
|
||||
nthistime = size;
|
||||
Assert(nthistime > 0);
|
||||
|
||||
memcpy(ptr, file->buffer + file->pos, nthistime);
|
||||
memcpy(ptr, file->buffer.data + file->pos, nthistime);
|
||||
|
||||
file->pos += nthistime;
|
||||
ptr = (void *) ((char *) ptr + nthistime);
|
||||
@@ -621,7 +621,7 @@ BufFileWrite(BufFile *file, void *ptr, size_t size)
|
||||
nthistime = size;
|
||||
Assert(nthistime > 0);
|
||||
|
||||
memcpy(file->buffer + file->pos, ptr, nthistime);
|
||||
memcpy(file->buffer.data + file->pos, ptr, nthistime);
|
||||
|
||||
file->dirty = true;
|
||||
file->pos += nthistime;
|
||||
|
@@ -615,10 +615,9 @@ static void
|
||||
fsm_extend(Relation rel, BlockNumber fsm_nblocks)
|
||||
{
|
||||
BlockNumber fsm_nblocks_now;
|
||||
Page pg;
|
||||
PGAlignedBlock pg;
|
||||
|
||||
pg = (Page) palloc(BLCKSZ);
|
||||
PageInit(pg, BLCKSZ, 0);
|
||||
PageInit((Page) pg.data, BLCKSZ, 0);
|
||||
|
||||
/*
|
||||
* We use the relation extension lock to lock out other backends trying to
|
||||
@@ -648,10 +647,10 @@ fsm_extend(Relation rel, BlockNumber fsm_nblocks)
|
||||
|
||||
while (fsm_nblocks_now < fsm_nblocks)
|
||||
{
|
||||
PageSetChecksumInplace(pg, fsm_nblocks_now);
|
||||
PageSetChecksumInplace((Page) pg.data, fsm_nblocks_now);
|
||||
|
||||
smgrextend(rel->rd_smgr, FSM_FORKNUM, fsm_nblocks_now,
|
||||
(char *) pg, false);
|
||||
pg.data, false);
|
||||
fsm_nblocks_now++;
|
||||
}
|
||||
|
||||
@@ -659,8 +658,6 @@ fsm_extend(Relation rel, BlockNumber fsm_nblocks)
|
||||
rel->rd_smgr->smgr_fsm_nblocks = fsm_nblocks_now;
|
||||
|
||||
UnlockRelationForExtension(rel, ExclusiveLock);
|
||||
|
||||
pfree(pg);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user