88 *
99 *
1010 * IDENTIFICATION
11- * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.304 2009/10/14 22:14:21 tgl Exp $
11+ * $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.305 2009/11/04 12:24:23 heikki Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
@@ -3037,6 +3037,9 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
30373037int i ;
30383038ListCell * l ;
30393039EState * estate ;
3040+ CommandId mycid ;
3041+ BulkInsertState bistate ;
3042+ int hi_options ;
30403043
30413044/*
30423045 * Open the relation(s). We have surely already locked the existing
@@ -3051,6 +3054,29 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
30513054else
30523055newrel = NULL ;
30533056
3057+ /*
3058+ * Prepare a BulkInsertState and options for heap_insert. Because
3059+ * we're building a new heap, we can skip WAL-logging and fsync it
3060+ * to disk at the end instead (unless WAL-logging is required for
3061+ * archiving). The FSM is empty too, so don't bother using it.
3062+ */
3063+ if (newrel )
3064+ {
3065+ mycid = GetCurrentCommandId (true);
3066+ bistate = GetBulkInsertState ();
3067+
3068+ hi_options = HEAP_INSERT_SKIP_FSM ;
3069+ if (!XLogArchivingActive ())
3070+ hi_options |=HEAP_INSERT_SKIP_WAL ;
3071+ }
3072+ else
3073+ {
3074+ /* keep compiler quiet about using these uninitialized */
3075+ mycid = 0 ;
3076+ bistate = NULL ;
3077+ hi_options = 0 ;
3078+ }
3079+
30543080/*
30553081 * If we need to rewrite the table, the operation has to be propagated to
30563082 * tables that use this table's rowtype as a column type.
@@ -3252,7 +3278,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
32523278
32533279/* Write the tuple out to the new relation */
32543280if (newrel )
3255- simple_heap_insert (newrel ,tuple );
3281+ heap_insert (newrel ,tuple , mycid , hi_options , bistate );
32563282
32573283ResetExprContext (econtext );
32583284
@@ -3270,7 +3296,15 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
32703296
32713297heap_close (oldrel ,NoLock );
32723298if (newrel )
3299+ {
3300+ FreeBulkInsertState (bistate );
3301+
3302+ /* If we skipped writing WAL, then we need to sync the heap. */
3303+ if (hi_options & HEAP_INSERT_SKIP_WAL )
3304+ heap_sync (newrel );
3305+
32733306heap_close (newrel ,NoLock );
3307+ }
32743308}
32753309
32763310/*