diff -rcN postgresql_with_9fujii_patch/src/backend/access/transam/xlog.c postgresql_with_patch/src/backend/access/transam/xlog.c
*** postgresql_with_9fujii_patch/src/backend/access/transam/xlog.c	2011-10-06 06:06:19.000000000 +0900
--- postgresql_with_patch/src/backend/access/transam/xlog.c	2011-10-13 09:21:26.000000000 +0900
***************
*** 364,369 ****
--- 364,372 ----
  	bool		exclusiveBackup;
  	int			nonExclusiveBackups;
  	XLogRecPtr	lastBackupStart;
+ 
+ 	/* the startup or the walwriter is logged to its own FPW */
+ 	bool		fullPageWrites;
  } XLogCtlInsert;
  
  /*
***************
*** 453,458 ****
--- 456,464 ----
  	bool		recoveryPause;
  
  	slock_t		info_lck;		/* locks shared variables shown above */
+ 
+ 	/* latest LSN that has recovered a WAL which fpw is 'off' */
+ 	XLogRecPtr	lastFpwDisabledLSN;
  } XLogCtlData;
  
  static XLogCtlData *XLogCtl = NULL;
***************
*** 574,579 ****
--- 580,586 ----
  	int			max_prepared_xacts;
  	int			max_locks_per_xact;
  	int			wal_level;
+ 	bool		fullPageWrites;
  } xl_parameter_change;
  
  /* logs restore point */
***************
*** 612,618 ****
  static void SetLatestXTime(TimestampTz xtime);
  static TimestampTz GetLatestXTime(void);
  static void CheckRequiredParameterValues(void);
- static void XLogReportParameters(void);
  static void LocalSetXLogInsertAllowed(void);
  static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags);
  static void KeepLogSeg(XLogRecPtr recptr, uint32 *logId, uint32 *logSeg);
--- 619,624 ----
***************
*** 759,769 ****
  
  	/*
  	 * Decide if we need to do full-page writes in this XLOG record: true if
! 	 * full_page_writes is on or we have a PITR request for it.  Since we
! 	 * don't yet have the insert lock, forcePageWrites could change under us,
! 	 * but we'll recheck it once we have the lock.
  	 */
! 	doPageWrites = fullPageWrites || Insert->forcePageWrites;
  
  	INIT_CRC32(rdata_crc);
  	len = 0;
--- 765,776 ----
  
  	/*
  	 * Decide if we need to do full-page writes in this XLOG record: true if
! 	 * full_page_writes in shared-memory is on or we have a PITR request for
! 	 * it.  Since we don't yet have the insert lock, fullPageWrites or
! 	 * forcePageWrites could change under us, but we'll recheck it once we
! 	 * have the lock.
  	 */
! 	doPageWrites = Insert->fullPageWrites || Insert->forcePageWrites;
  
  	INIT_CRC32(rdata_crc);
  	len = 0;
***************
*** 904,915 ****
  	}
  
  	/*
! 	 * Also check to see if forcePageWrites was just turned on; if we weren't
! 	 * already doing full-page writes then go back and recompute. (If it was
! 	 * just turned off, we could recompute the record without full pages, but
! 	 * we choose not to bother.)
  	 */
! 	if (Insert->forcePageWrites && !doPageWrites)
  	{
  		/* Oops, must redo it with full-page data */
  		LWLockRelease(WALInsertLock);
--- 911,922 ----
  	}
  
  	/*
! 	 * Also check to see if fullPageWrites or forcePageWrites was just
! 	 * turned on; if we weren't already doing full-page writes then go back
! 	 * and recompute. (If it was just turned off, we could recompute the
! 	 * record without full pages, but we choose not to bother.)
  	 */
! 	if ((Insert->fullPageWrites || Insert->forcePageWrites) && !doPageWrites)
  	{
  		/* Oops, must redo it with full-page data */
  		LWLockRelease(WALInsertLock);
***************
*** 6865,6870 ****
--- 6872,6885 ----
  	/* Pre-scan prepared transactions to find out the range of XIDs present */
  	oldestActiveXID = PrescanPreparedTransactions(NULL, NULL);
  
+ 	/*
+ 	 * The startup updates FPW in shaerd-memory after REDO. However, it must
+ 	 * perform before writing the WAL of the CHECKPOINT. The reason is that
+ 	 * it uses a value of fpw in shared-memory when it writes a WAL of its
+ 	 * CHECKPOINT.
+ 	 */
+ 	XLogCtl->Insert.fullPageWrites = fullPageWrites;
+ 
  	if (InRecovery)
  	{
  		int			rmid;
***************
*** 6998,7004 ****
  	 * backends to write WAL.
  	 */
  	LocalSetXLogInsertAllowed();
! 	XLogReportParameters();
  
  	/*
  	 * All done.  Allow backends to write WAL.	(Although the bool flag is
--- 7013,7019 ----
  	 * backends to write WAL.
  	 */
  	LocalSetXLogInsertAllowed();
! 	XLogReportParameters(REPORT_ON_STARTUP);
  
  	/*
  	 * All done.  Allow backends to write WAL.	(Although the bool flag is
***************
*** 7856,7862 ****
--- 7871,7880 ----
  	 * Update checkPoint.nextXid since we have a later value
  	 */
  	if (!shutdown && XLogStandbyInfoActive())
+ 	{
  		LogStandbySnapshot(&checkPoint.oldestActiveXid, &checkPoint.nextXid);
+ 		XLogReportParameters(REPORT_ON_BACKEND);
+ 	}
  	else
  		checkPoint.oldestActiveXid = InvalidTransactionId;
  
***************
*** 8380,8393 ****
  /*
   * Check if any of the GUC parameters that are critical for hot standby
   * have changed, and update the value in pg_control file if necessary.
   */
! static void
! XLogReportParameters(void)
  {
  	if (wal_level != ControlFile->wal_level ||
  		MaxConnections != ControlFile->MaxConnections ||
  		max_prepared_xacts != ControlFile->max_prepared_xacts ||
! 		max_locks_per_xact != ControlFile->max_locks_per_xact)
  	{
  		/*
  		 * The change in number of backend slots doesn't need to be WAL-logged
--- 8398,8433 ----
  /*
   * Check if any of the GUC parameters that are critical for hot standby
   * have changed, and update the value in pg_control file if necessary.
+  * This function is called at three timing (backend executes checkpoint,
+  * startup finishes and walwriter receives SIGHUP). The backend and the
+  * startup writes a WAL when FPW is 'off' in addition to when any of the
+  * GUC parameters is changed.
   */
! void
! XLogReportParameters(int updatetiming)
  {
+ 	bool do_fpw_xloginsert = false;
+ 	bool fpw = fullPageWrites;
+ 
+ 	if (updatetiming == REPORT_ON_BACKEND)
+ 	{
+ 		LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
+ 		fpw = XLogCtl->Insert.fullPageWrites;
+ 		LWLockRelease(WALInsertLock);
+ 	}
+ 
+ 	if (!fpw)
+ 	{
+ 		if (updatetiming <= REPORT_ON_STARTUP ||
+ 			(updatetiming == REPORT_ON_WALWRITER && XLogCtl->Insert.fullPageWrites))
+ 			do_fpw_xloginsert = true;
+ 	}
+ 
  	if (wal_level != ControlFile->wal_level ||
  		MaxConnections != ControlFile->MaxConnections ||
  		max_prepared_xacts != ControlFile->max_prepared_xacts ||
! 		max_locks_per_xact != ControlFile->max_locks_per_xact ||
! 		do_fpw_xloginsert)
  	{
  		/*
  		 * The change in number of backend slots doesn't need to be WAL-logged
***************
*** 8396,8402 ****
  		 * values in pg_control either if wal_level=minimal, but seems better
  		 * to keep them up-to-date to avoid confusion.
  		 */
! 		if (wal_level != ControlFile->wal_level || XLogIsNeeded())
  		{
  			XLogRecData rdata;
  			xl_parameter_change xlrec;
--- 8436,8442 ----
  		 * values in pg_control either if wal_level=minimal, but seems better
  		 * to keep them up-to-date to avoid confusion.
  		 */
! 		if (wal_level != ControlFile->wal_level || XLogIsNeeded() || do_fpw_xloginsert)
  		{
  			XLogRecData rdata;
  			xl_parameter_change xlrec;
***************
*** 8405,8410 ****
--- 8445,8451 ----
  			xlrec.max_prepared_xacts = max_prepared_xacts;
  			xlrec.max_locks_per_xact = max_locks_per_xact;
  			xlrec.wal_level = wal_level;
+ 			xlrec.fullPageWrites = fpw;
  
  			rdata.buffer = InvalidBuffer;
  			rdata.data = (char *) &xlrec;
***************
*** 8414,8424 ****
  			XLogInsert(RM_XLOG_ID, XLOG_PARAMETER_CHANGE, &rdata);
  		}
  
! 		ControlFile->MaxConnections = MaxConnections;
! 		ControlFile->max_prepared_xacts = max_prepared_xacts;
! 		ControlFile->max_locks_per_xact = max_locks_per_xact;
! 		ControlFile->wal_level = wal_level;
! 		UpdateControlFile();
  	}
  }
  
--- 8455,8476 ----
  			XLogInsert(RM_XLOG_ID, XLOG_PARAMETER_CHANGE, &rdata);
  		}
  
! 		if (!do_fpw_xloginsert)
! 		{
! 			ControlFile->MaxConnections = MaxConnections;
! 			ControlFile->max_prepared_xacts = max_prepared_xacts;
! 			ControlFile->max_locks_per_xact = max_locks_per_xact;
! 			ControlFile->wal_level = wal_level;
! 			UpdateControlFile();
! 		}
! 	}
! 
! 	/* update own fpw in shared-memory when it has managed fpw */
! 	if (updatetiming >= REPORT_ON_STARTUP)
! 	{
! 		LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
! 		XLogCtl->Insert.fullPageWrites = fullPageWrites;
! 		LWLockRelease(WALInsertLock);
  	}
  }
  
***************
*** 8604,8609 ****
--- 8656,8664 ----
  	}
  	else if (info == XLOG_PARAMETER_CHANGE)
  	{
+ 		/* use volatile pointer to prevent code rearrangement */
+ 		volatile XLogCtlData *xlogctl = XLogCtl;
+ 
  		xl_parameter_change xlrec;
  
  		/* Update our copy of the parameters in pg_control */
***************
*** 8633,8638 ****
--- 8688,8701 ----
  		UpdateControlFile();
  		LWLockRelease(ControlFileLock);
  
+ 		/* record the LSN when FPW is false on master */
+ 		if (!xlrec.fullPageWrites)
+ 		{
+ 			SpinLockAcquire(&xlogctl->info_lck);
+ 			xlogctl->lastFpwDisabledLSN = lsn;
+ 			SpinLockRelease(&xlogctl->info_lck);
+ 		}
+ 
  		/* Check to see if any changes to max_connections give problems */
  		CheckRequiredParameterValues();
  	}
***************
*** 8711,8721 ****
  			}
  		}
  
! 		appendStringInfo(buf, "parameter change: max_connections=%d max_prepared_xacts=%d max_locks_per_xact=%d wal_level=%s",
  						 xlrec.MaxConnections,
  						 xlrec.max_prepared_xacts,
  						 xlrec.max_locks_per_xact,
! 						 wal_level_str);
  	}
  	else
  		appendStringInfo(buf, "UNKNOWN");
--- 8774,8785 ----
  			}
  		}
  
! 		appendStringInfo(buf, "parameter change: max_connections=%d max_prepared_xacts=%d max_locks_per_xact=%d wal_level=%s full_page_writes=%s",
  						 xlrec.MaxConnections,
  						 xlrec.max_prepared_xacts,
  						 xlrec.max_locks_per_xact,
! 						 wal_level_str,
! 						 xlrec.fullPageWrites ? "true" : "false");
  	}
  	else
  		appendStringInfo(buf, "UNKNOWN");
***************
*** 8933,8938 ****
--- 8997,9003 ----
  	bool		recovery_in_progress = false;
  	XLogRecPtr	checkpointloc;
  	XLogRecPtr	startpoint;
+ 	XLogRecPtr	lastFpwDisabledLSN;
  	pg_time_t	stamp_time;
  	char		strfbuf[128];
  	char		xlogfilename[MAXFNAMELEN];
***************
*** 9089,9094 ****
--- 9154,9177 ----
  				gotUniqueStartpoint = true;
  		} while (!gotUniqueStartpoint);
  
+ 		/*
+ 		 * check whether the master's FPW is 'off' since latest CHECKPOINT.
+ 		 */
+ 		if (recovery_in_progress)
+ 		{
+ 			/* use volatile pointer to prevent code rearrangement */
+ 			volatile XLogCtlData *xlogctl = XLogCtl;
+ 
+ 			SpinLockAcquire(&xlogctl->info_lck);
+ 			lastFpwDisabledLSN = xlogctl->lastFpwDisabledLSN;
+ 			SpinLockRelease(&xlogctl->info_lck);
+ 
+ 			if (XLByteLE(startpoint, lastFpwDisabledLSN))
+ 				ereport(ERROR,
+ 						(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ 						 errmsg("WAL generated with full_page_writes=off was replayed since latest checkpoint")));
+ 		}
+ 
  		XLByteToSeg(startpoint, _logId, _logSeg);
  		XLogFileName(xlogfilename, ThisTimeLineID, _logId, _logSeg);
  
***************
*** 9233,9238 ****
--- 9316,9322 ----
  	bool		recovery_in_progress = false;
  	XLogRecPtr	startpoint;
  	XLogRecPtr	stoppoint;
+ 	XLogRecPtr	lastFpwDisabledLSN;
  	XLogRecData rdata;
  	pg_time_t	stamp_time;
  	char		strfbuf[128];
***************
*** 9372,9377 ****
--- 9456,9477 ----
  						"though pg_start_backup() was executed during recovery"),
  				 errhint("The database backup will not be usable.")));
  
+ 	/* check whether the master's FPW is 'off' since pg_start_backup. */
+ 	if (recovery_in_progress)
+ 	{
+ 		/* use volatile pointer to prevent code rearrangement */
+ 		volatile XLogCtlData *xlogctl = XLogCtl;
+ 
+ 		SpinLockAcquire(&xlogctl->info_lck);
+ 		lastFpwDisabledLSN = xlogctl->lastFpwDisabledLSN;
+ 		SpinLockRelease(&xlogctl->info_lck);
+ 
+ 		if (XLByteLE(startpoint, lastFpwDisabledLSN))
+ 			ereport(ERROR,
+ 					(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+ 					  errmsg("WAL generated with full_page_writes=off was replayed during online backup")));
+ 	}
+ 
  	/*
  	 * During recovery, we don't write an end-of-backup record. We can
  	 * assume that pg_control was backed up just before pg_stop_backup()
diff -rcN postgresql_with_9fujii_patch/src/backend/postmaster/walwriter.c postgresql_with_patch/src/backend/postmaster/walwriter.c
*** postgresql_with_9fujii_patch/src/backend/postmaster/walwriter.c	2011-10-06 06:05:45.000000000 +0900
--- postgresql_with_patch/src/backend/postmaster/walwriter.c	2011-10-13 09:21:26.000000000 +0900
***************
*** 216,221 ****
--- 216,229 ----
  	PG_SETMASK(&UnBlockSig);
  
  	/*
+ 	 * After the startup process, the walwriter manages the FPW. Because
+ 	 * the walwriter may have not received a SIGHUP then, it updates the FPW
+ 	 * when wal_level is hotstandby.
+ 	 */
+ 	if (XLogStandbyInfoActive())
+ 		XLogReportParameters(REPORT_ON_WALWRITER);
+ 
+ 	/*
  	 * Loop forever
  	 */
  	for (;;)
***************
*** 236,241 ****
--- 244,256 ----
  		{
  			got_SIGHUP = false;
  			ProcessConfigFile(PGC_SIGHUP);
+ 
+ 			/*
+ 			 * The walwriter manages the FPW. When the walwriter has received
+ 			 * a SIGHUP, when wal_level is hotstandby, it updates the FPW.
+ 			 */
+ 			if (XLogStandbyInfoActive())
+ 				XLogReportParameters(REPORT_ON_WALWRITER);
  		}
  		if (shutdown_requested)
  		{
diff -rcN postgresql_with_9fujii_patch/src/include/access/xlog.h postgresql_with_patch/src/include/access/xlog.h
*** postgresql_with_9fujii_patch/src/include/access/xlog.h	2011-10-06 06:05:45.000000000 +0900
--- postgresql_with_patch/src/include/access/xlog.h	2011-10-13 09:21:26.000000000 +0900
***************
*** 208,213 ****
--- 208,224 ----
  } WalLevel;
  extern int	wal_level;
  
+ /*
+  * The place of updating xlog parameter.
+  * If it is backend then this means a timing for CHECKPOINT.
+  */
+ typedef enum
+ {
+ 	REPORT_ON_BACKEND = 0,
+ 	REPORT_ON_STARTUP,
+ 	REPORT_ON_WALWRITER
+ } XLogParemeterUpdate;
+ 
  #define XLogArchivingActive()	(XLogArchiveMode && wal_level >= WAL_LEVEL_ARCHIVE)
  #define XLogArchiveCommandSet() (XLogArchiveCommand[0] != '\0')
  
***************
*** 306,311 ****
--- 317,323 ----
  extern bool CreateRestartPoint(int flags);
  extern void XLogPutNextOid(Oid nextOid);
  extern XLogRecPtr XLogRestorePoint(const char *rpName);
+ extern void XLogReportParameters(int updatetiming);
  extern XLogRecPtr GetRedoRecPtr(void);
  extern XLogRecPtr GetInsertRecPtr(void);
  extern XLogRecPtr GetFlushRecPtr(void);
