mirror of
https://github.com/postgres/postgres.git
synced 2025-10-18 04:29:09 +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:
@@ -132,8 +132,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,
|
||||
/* Save old file size */
|
||||
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
|
||||
@@ -181,7 +174,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",
|
||||
@@ -195,7 +188,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
|
||||
@@ -203,8 +196,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)
|
||||
@@ -214,12 +207,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)
|
||||
@@ -253,11 +246,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)
|
||||
@@ -273,8 +266,6 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile,
|
||||
}
|
||||
|
||||
/* Clean up */
|
||||
pg_free(buffer);
|
||||
pg_free(new_vmbuf);
|
||||
close(dst_fd);
|
||||
close(src_fd);
|
||||
}
|
||||
|
Reference in New Issue
Block a user