]> granicus.if.org Git - postgresql/commit - contrib/bloom/blinsert.c
Avoid using potentially-under-aligned page buffers.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 1 Sep 2018 19:27:12 +0000 (15:27 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 1 Sep 2018 19:27:17 +0000 (15:27 -0400)
commit44cac9346479d4b0cc9195b0267fd13eb4e7442c
treed90876e13f78977dc571be5b70592c82fc33e3fe
parent5e8d670c313531c0dca245943fb84c94a477ddc4
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
24 files changed:
contrib/bloom/blinsert.c
contrib/pg_prewarm/pg_prewarm.c
contrib/pg_standby/pg_standby.c
src/backend/access/gin/ginentrypage.c
src/backend/access/gin/ginfast.c
src/backend/access/hash/hashpage.c
src/backend/access/heap/heapam.c
src/backend/access/heap/visibilitymap.c
src/backend/access/transam/generic_xlog.c
src/backend/access/transam/xlog.c
src/backend/access/transam/xloginsert.c
src/backend/access/transam/xlogreader.c
src/backend/commands/tablecmds.c
src/backend/replication/walsender.c
src/backend/storage/file/buffile.c
src/backend/storage/freespace/freespace.c
src/backend/utils/sort/logtape.c
src/bin/pg_basebackup/walmethods.c
src/bin/pg_resetwal/pg_resetwal.c
src/bin/pg_rewind/copy_fetch.c
src/bin/pg_upgrade/file.c
src/bin/pg_verify_checksums/pg_verify_checksums.c
src/bin/pg_waldump/pg_waldump.c
src/include/c.h