summaryrefslogtreecommitdiff
path: root/src/bin/pg_upgrade/file.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2018-09-01 15:27:13 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2018-09-01 15:27:13 -0400
commit10b9af3ebbedfcbe696bf93d4db861db8de0de32 (patch)
treec228ee552c4d266b23d508d07b9c33cc8d545ab2 /src/bin/pg_upgrade/file.c
parent1664c8b300e345158b007eba2c6879e7bde74cc8 (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/bin/pg_upgrade/file.c')
-rw-r--r--src/bin/pg_upgrade/file.c31
1 files changed, 11 insertions, 20 deletions
diff --git a/src/bin/pg_upgrade/file.c b/src/bin/pg_upgrade/file.c
index b8be9a5bda0..c9fd0316565 100644
--- a/src/bin/pg_upgrade/file.c
+++ b/src/bin/pg_upgrade/file.c
@@ -131,8 +131,8 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
{
int src_fd;
int dst_fd;
- char *buffer;
- char *new_vmbuf;
+ PGAlignedBlock buffer;
+ PGAlignedBlock new_vmbuf;
ssize_t totalBytesRead = 0;
ssize_t src_filesize;
int rewriteVmBytesPerPage;
@@ -159,13 +159,6 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
src_filesize = statbuf.st_size;
/*
- * Malloc the work buffers, rather than making them local arrays, to
- * ensure adequate alignment.
- */
- buffer = (char *) pg_malloc(BLCKSZ);
- new_vmbuf = (char *) pg_malloc(BLCKSZ);
-
- /*
* Turn each visibility map page into 2 pages one by one. Each new page
* has the same page header as the old one. If the last section of the
* last page is empty, we skip it, mostly to avoid turning one-page
@@ -180,7 +173,7 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
PageHeaderData pageheader;
bool old_lastblk;
- if ((bytesRead = read(src_fd, buffer, BLCKSZ)) != BLCKSZ)
+ if ((bytesRead = read(src_fd, buffer.data, BLCKSZ)) != BLCKSZ)
{
if (bytesRead < 0)
pg_fatal("error while copying relation \"%s.%s\": could not read file \"%s\": %s\n",
@@ -194,7 +187,7 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
old_lastblk = (totalBytesRead == src_filesize);
/* Save the page header data */
- memcpy(&pageheader, buffer, SizeOfPageHeaderData);
+ memcpy(&pageheader, buffer.data, SizeOfPageHeaderData);
/*
* These old_* variables point to old visibility map page. old_cur
@@ -202,8 +195,8 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
* old block. old_break is the end+1 position on the old page for the
* data that will be transferred to the current new page.
*/
- old_cur = buffer + SizeOfPageHeaderData;
- old_blkend = buffer + bytesRead;
+ old_cur = buffer.data + SizeOfPageHeaderData;
+ old_blkend = buffer.data + bytesRead;
old_break = old_cur + rewriteVmBytesPerPage;
while (old_break <= old_blkend)
@@ -213,12 +206,12 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
bool old_lastpart;
/* First, copy old page header to new page */
- memcpy(new_vmbuf, &pageheader, SizeOfPageHeaderData);
+ memcpy(new_vmbuf.data, &pageheader, SizeOfPageHeaderData);
/* Rewriting the last part of the last old page? */
old_lastpart = old_lastblk && (old_break == old_blkend);
- new_cur = new_vmbuf + SizeOfPageHeaderData;
+ new_cur = new_vmbuf.data + SizeOfPageHeaderData;
/* Process old page bytes one by one, and turn it into new page. */
while (old_cur < old_break)
@@ -252,11 +245,11 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
/* Set new checksum for visibility map page, if enabled */
if (new_cluster.controldata.data_checksum_version != 0)
- ((PageHeader) new_vmbuf)->pd_checksum =
- pg_checksum_page(new_vmbuf, new_blkno);
+ ((PageHeader) new_vmbuf.data)->pd_checksum =
+ pg_checksum_page(new_vmbuf.data, new_blkno);
errno = 0;
- if (write(dst_fd, new_vmbuf, BLCKSZ) != BLCKSZ)
+ if (write(dst_fd, new_vmbuf.data, BLCKSZ) != BLCKSZ)
{
/* if write didn't set errno, assume problem is no disk space */
if (errno == 0)
@@ -272,8 +265,6 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
}
/* Clean up */
- pg_free(buffer);
- pg_free(new_vmbuf);
close(dst_fd);
close(src_fd);
}