]> granicus.if.org Git - postgresql/commitdiff
Fix another longstanding problem in copy_relation_data: it was blithely
authorTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 Jul 2010 19:23:58 +0000 (19:23 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Thu, 29 Jul 2010 19:23:58 +0000 (19:23 +0000)
assuming that a local char[] array would be aligned on at least a word
boundary.  There are architectures on which that is pretty much guaranteed to
NOT be the case ... and those arches also don't like non-aligned memory
accesses, meaning that log_newpage() would crash if it ever got invoked.
Even on Intel-ish machines there's a potential for a large performance penalty
from doing I/O to an inadequately aligned buffer.  So palloc it instead.

Backpatch to 8.0 --- 7.4 doesn't have this code.

src/backend/commands/tablecmds.c

index 8f8d28e825c9ecca3e7576f4a7d1b988ce7cf20e..3d2e385b08890fbb223d65087a77f74530a78f10 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.174.2.11 2010/07/29 16:15:33 rhaas Exp $
+ *       $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.174.2.12 2010/07/29 19:23:58 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -5837,11 +5837,11 @@ static void
 copy_relation_data(Relation rel, SMgrRelation dst)
 {
        SMgrRelation src;
+       char       *buf;
+       Page            page;
        bool            use_wal;
        BlockNumber nblocks;
        BlockNumber blkno;
-       char            buf[BLCKSZ];
-       Page            page = (Page) buf;
 
        /*
         * Since we copy the file directly without looking at the shared buffers,
@@ -5851,6 +5851,15 @@ copy_relation_data(Relation rel, SMgrRelation dst)
         */
        FlushRelationBuffers(rel);
 
+       /*
+        * palloc the buffer so that it's MAXALIGN'd.  If it were just a local
+        * char[] array, the compiler might align it on any byte boundary, which
+        * can seriously hurt transfer speed to and from the kernel; not to
+        * mention possibly making PageSetLSN fail.
+        */
+       buf = (char *) palloc(BLCKSZ);
+       page = (Page) buf;
+
        /*
         * We need to log the copied data in WAL iff WAL archiving is enabled AND
         * it's not a temp rel.
@@ -5914,6 +5923,8 @@ copy_relation_data(Relation rel, SMgrRelation dst)
                smgrwrite(dst, blkno, buf, true);
        }
 
+       pfree(buf);
+
        /*
         * If the rel isn't temp, we must fsync it down to disk before it's safe
         * to commit the transaction.  (For a temp rel we don't care since the rel