diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2018-09-01 15:27:13 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2018-09-01 15:27:13 -0400 |
commit | 083d9ced14e4af4b48e7607bd6cc7567a68ec899 (patch) | |
tree | d10891bda793444d1f9ac2de8def271beac73305 /src/backend/access/gin/ginentrypage.c | |
parent | 20cd88857b3a60d40cf019872cf8a5d40888e3ae (diff) |
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
Diffstat (limited to 'src/backend/access/gin/ginentrypage.c')
-rw-r--r-- | src/backend/access/gin/ginentrypage.c | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/backend/access/gin/ginentrypage.c b/src/backend/access/gin/ginentrypage.c index bdf1f2e5889..f013b9fdeb2 100644 --- a/src/backend/access/gin/ginentrypage.c +++ b/src/backend/access/gin/ginentrypage.c @@ -633,7 +633,7 @@ entrySplitPage(GinBtree btree, Buffer origbuf, /* these must be static so they can be returned to caller */ static ginxlogSplitEntry data; - static char tupstore[2 * BLCKSZ]; + static PGAlignedBlock tupstore[2]; entryPreparePage(btree, lpage, off, insertData, updateblkno); @@ -642,7 +642,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) @@ -667,7 +667,7 @@ entrySplitPage(GinBtree btree, Buffer origbuf, ptr += size; totalsize += size + sizeof(ItemIdData); } - tupstoresize = ptr - tupstore; + tupstoresize = ptr - tupstore[0].data; /* * Initialize the left and right pages, and copy all the tuples back to @@ -676,7 +676,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; @@ -715,7 +715,7 @@ entrySplitPage(GinBtree btree, Buffer origbuf, rdata[0].next = &rdata[1]; rdata[1].buffer = InvalidBuffer; - rdata[1].data = tupstore; + rdata[1].data = tupstore[0].data; rdata[1].len = tupstoresize; rdata[1].next = NULL; |