diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 45912825a123fb0cf0b6bb40231fcceed03923d1..0d9ad2af0450fc4cf9794450c9d262b881a8cb83 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.284 2010/01/29 17:10:05 sriggs Exp $
+ *	  $PostgreSQL: pgsql/src/backend/access/heap/heapam.c,v 1.285 2010/02/03 10:01:29 heikki Exp $
  *
  *
  * INTERFACE ROUTINES
@@ -5074,16 +5074,10 @@ heap2_desc(StringInfo buf, uint8 xl_info, char *rec)
 void
 heap_sync(Relation rel)
 {
-	char reason[NAMEDATALEN + 30];
-
 	/* temp tables never need fsync */
 	if (rel->rd_istemp)
 		return;
 
-	snprintf(reason, sizeof(reason), "heap inserts on \"%s\"",
-			 RelationGetRelationName(rel));
-	XLogReportUnloggedStatement(reason);
-
 	/* main heap */
 	FlushRelationBuffers(rel);
 	/* FlushRelationBuffers will have opened rd_smgr */
diff --git a/src/backend/access/heap/rewriteheap.c b/src/backend/access/heap/rewriteheap.c
index 9f9e3e8007e9ed7fe940fb0b6509f8250970319c..65522f46c147a4b447b45340dcc75139be0c39cd 100644
--- a/src/backend/access/heap/rewriteheap.c
+++ b/src/backend/access/heap/rewriteheap.c
@@ -96,7 +96,7 @@
  * Portions Copyright (c) 1994-5, Regents of the University of California
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/access/heap/rewriteheap.c,v 1.19 2010/01/02 16:57:35 momjian Exp $
+ *	  $PostgreSQL: pgsql/src/backend/access/heap/rewriteheap.c,v 1.20 2010/02/03 10:01:29 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -278,6 +278,15 @@ end_heap_rewrite(RewriteState state)
 				   (char *) state->rs_buffer, true);
 	}
 
+	/* Write an XLOG UNLOGGED record if WAL-logging was skipped */
+	if (!state->rs_use_wal && !state->rs_new_rel->rd_istemp)
+	{
+		char reason[NAMEDATALEN + 30];
+		snprintf(reason, sizeof(reason), "heap rewrite on \"%s\"",
+				 RelationGetRelationName(state->rs_new_rel));
+		XLogReportUnloggedStatement(reason);
+	}
+
 	/*
 	 * If the rel isn't temp, must fsync before commit.  We use heap_sync to
 	 * ensure that the toast table gets fsync'd too.
diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c
index cfaa3927bc7b1240b933ef166fd1a94336f67665..f10ae31ff183eccbf752d9c09485d69e44fc3100 100644
--- a/src/backend/commands/copy.c
+++ b/src/backend/commands/copy.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.322 2010/01/31 18:15:39 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/copy.c,v 1.323 2010/02/03 10:01:29 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2225,7 +2225,13 @@ CopyFrom(CopyState cstate)
 	 * indexes since those use WAL anyway)
 	 */
 	if (hi_options & HEAP_INSERT_SKIP_WAL)
+	{
+		char reason[NAMEDATALEN + 30];
+		snprintf(reason, sizeof(reason), "COPY FROM on \"%s\"",
+				 RelationGetRelationName(cstate->rel));
+		XLogReportUnloggedStatement(reason);
 		heap_sync(cstate->rel);
+	}
 }
 
 
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index cec3b7fc58edca6c4708f36d822e49647ec312b6..a729adb0550ec6fe0aba3f4104ee43b4ba8ac61c 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.322 2010/02/03 01:14:16 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.323 2010/02/03 10:01:29 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -3297,7 +3297,13 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap)
 
 		/* If we skipped writing WAL, then we need to sync the heap. */
 		if (hi_options & HEAP_INSERT_SKIP_WAL)
+		{
+			char reason[NAMEDATALEN + 30];
+			snprintf(reason, sizeof(reason), "table rewrite on \"%s\"",
+					 RelationGetRelationName(newrel));
+			XLogReportUnloggedStatement(reason);
 			heap_sync(newrel);
+		}
 
 		heap_close(newrel, NoLock);
 	}
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index b0698489c41025750acc2f5fe578ceafed311e66..5a8af0b2f75f12240a3ff13a457e0973e72ab1f0 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -26,7 +26,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.343 2010/01/28 23:21:11 petere Exp $
+ *	  $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.344 2010/02/03 10:01:30 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -2240,7 +2240,13 @@ CloseIntoRel(QueryDesc *queryDesc)
 
 		/* If we skipped using WAL, must heap_sync before commit */
 		if (myState->hi_options & HEAP_INSERT_SKIP_WAL)
+		{
+			char reason[NAMEDATALEN + 30];
+			snprintf(reason, sizeof(reason), "SELECT INTO on \"%s\"",
+					 RelationGetRelationName(myState->rel));
+			XLogReportUnloggedStatement(reason);
 			heap_sync(myState->rel);
+		}
 
 		/* close rel, but keep lock until commit */
 		heap_close(myState->rel, NoLock);