mirror of
https://github.com/postgres/postgres.git
synced 2025-07-18 17:42:25 +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:
@ -616,7 +616,7 @@ entrySplitPage(GinBtree btree, Buffer origbuf,
|
||||
Page lpage = PageGetTempPageCopy(BufferGetPage(origbuf));
|
||||
Page rpage = PageGetTempPageCopy(BufferGetPage(origbuf));
|
||||
Size pageSize = PageGetPageSize(lpage);
|
||||
char tupstore[2 * BLCKSZ];
|
||||
PGAlignedBlock tupstore[2]; /* could need 2 pages' worth of tuples */
|
||||
|
||||
entryPreparePage(btree, lpage, off, insertData, updateblkno);
|
||||
|
||||
@ -625,7 +625,7 @@ entrySplitPage(GinBtree btree, Buffer origbuf,
|
||||
* one after another in a temporary workspace.
|
||||
*/
|
||||
maxoff = PageGetMaxOffsetNumber(lpage);
|
||||
ptr = tupstore;
|
||||
ptr = tupstore[0].data;
|
||||
for (i = FirstOffsetNumber; i <= maxoff; i++)
|
||||
{
|
||||
if (i == off)
|
||||
@ -658,7 +658,7 @@ entrySplitPage(GinBtree btree, Buffer origbuf,
|
||||
GinInitPage(rpage, GinPageGetOpaque(lpage)->flags, pageSize);
|
||||
GinInitPage(lpage, GinPageGetOpaque(rpage)->flags, pageSize);
|
||||
|
||||
ptr = tupstore;
|
||||
ptr = tupstore[0].data;
|
||||
maxoff++;
|
||||
lsize = 0;
|
||||
|
||||
|
@ -64,18 +64,15 @@ writeListPage(Relation index, Buffer buffer,
|
||||
size = 0;
|
||||
OffsetNumber l,
|
||||
off;
|
||||
char *workspace;
|
||||
PGAlignedBlock workspace;
|
||||
char *ptr;
|
||||
|
||||
/* workspace could be a local array; we use palloc for alignment */
|
||||
workspace = palloc(BLCKSZ);
|
||||
|
||||
START_CRIT_SECTION();
|
||||
|
||||
GinInitBuffer(buffer, GIN_LIST);
|
||||
|
||||
off = FirstOffsetNumber;
|
||||
ptr = workspace;
|
||||
ptr = workspace.data;
|
||||
|
||||
for (i = 0; i < ntuples; i++)
|
||||
{
|
||||
@ -127,7 +124,7 @@ writeListPage(Relation index, Buffer buffer,
|
||||
XLogRegisterData((char *) &data, sizeof(ginxlogInsertListPage));
|
||||
|
||||
XLogRegisterBuffer(0, buffer, REGBUF_WILL_INIT);
|
||||
XLogRegisterBufData(0, workspace, size);
|
||||
XLogRegisterBufData(0, workspace.data, size);
|
||||
|
||||
recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_INSERT_LISTPAGE);
|
||||
PageSetLSN(page, recptr);
|
||||
@ -140,8 +137,6 @@ writeListPage(Relation index, Buffer buffer,
|
||||
|
||||
END_CRIT_SECTION();
|
||||
|
||||
pfree(workspace);
|
||||
|
||||
return freesize;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user