*** xlog.c	Tue Jun  7 18:50:50 2005
--- xlog.gw.c	Mon Jun 27 14:59:31 2005
*************** static void ReadControlFile(void);
*** 464,469 ****
--- 464,480 ----
  static char *str_time(time_t tnow);
  static void issue_xlog_fsync(void);
  
+ /* XLog gather-write staffs */
+ typedef struct XLogPages
+ {
+ 	char	*head;		/* Head of first page */
+ 	int		 size;		/* Total bytes of pages == count(pages) * BLCKSZ */
+ 	int		 offset;	/* Offset in xlog segment file  */
+ } XLogPages;
+ static void XLogPageReset(XLogPages *pages);
+ static void XLogPageWrite(XLogPages *pages, int index);
+ static void XLogPageFlush(XLogPages *pages, int index);
+ 
  #ifdef WAL_DEBUG
  static void xlog_outrec(char *buf, XLogRecord *record);
  #endif
*************** static void
*** 1251,1259 ****
  XLogWrite(XLogwrtRqst WriteRqst)
  {
  	XLogCtlWrite *Write = &XLogCtl->Write;
- 	char	   *from;
  	bool		ispartialpage;
  	bool		use_existent;
  
  	/* We should always be inside a critical section here */
  	Assert(CritSectionCount > 0);
--- 1262,1271 ----
  XLogWrite(XLogwrtRqst WriteRqst)
  {
  	XLogCtlWrite *Write = &XLogCtl->Write;
  	bool		ispartialpage;
  	bool		use_existent;
+ 	int			currentIndex = Write->curridx;
+ 	XLogPages	pages;
  
  	/* We should always be inside a critical section here */
  	Assert(CritSectionCount > 0);
*************** XLogWrite(XLogwrtRqst WriteRqst)
*** 1264,1269 ****
--- 1276,1283 ----
  	 */
  	LogwrtResult = Write->LogwrtResult;
  
+ 	XLogPageReset(&pages);
+ 
  	while (XLByteLT(LogwrtResult.Write, WriteRqst.Write))
  	{
  		/*
*************** XLogWrite(XLogwrtRqst WriteRqst)
*** 1272,1285 ****
  		 * end of the last page that's been initialized by
  		 * AdvanceXLInsertBuffer.
  		 */
! 		if (!XLByteLT(LogwrtResult.Write, XLogCtl->xlblocks[Write->curridx]))
  			elog(PANIC, "xlog write request %X/%X is past end of log %X/%X",
  				 LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
! 				 XLogCtl->xlblocks[Write->curridx].xlogid,
! 				 XLogCtl->xlblocks[Write->curridx].xrecoff);
  
  		/* Advance LogwrtResult.Write to end of current buffer page */
! 		LogwrtResult.Write = XLogCtl->xlblocks[Write->curridx];
  		ispartialpage = XLByteLT(WriteRqst.Write, LogwrtResult.Write);
  
  		if (!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
--- 1286,1299 ----
  		 * end of the last page that's been initialized by
  		 * AdvanceXLInsertBuffer.
  		 */
! 		if (!XLByteLT(LogwrtResult.Write, XLogCtl->xlblocks[currentIndex]))
  			elog(PANIC, "xlog write request %X/%X is past end of log %X/%X",
  				 LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
! 				 XLogCtl->xlblocks[currentIndex].xlogid,
! 				 XLogCtl->xlblocks[currentIndex].xrecoff);
  
  		/* Advance LogwrtResult.Write to end of current buffer page */
! 		LogwrtResult.Write = XLogCtl->xlblocks[currentIndex];
  		ispartialpage = XLByteLT(WriteRqst.Write, LogwrtResult.Write);
  
  		if (!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
*************** XLogWrite(XLogwrtRqst WriteRqst)
*** 1287,1292 ****
--- 1301,1307 ----
  			/*
  			 * Switch to new logfile segment.
  			 */
+ 			XLogPageFlush(&pages, currentIndex);
  			if (openLogFile >= 0)
  			{
  				if (close(openLogFile))
*************** XLogWrite(XLogwrtRqst WriteRqst)
*** 1360,1390 ****
  			openLogOff = 0;
  		}
  
! 		/* Need to seek in the file? */
! 		if (openLogOff != (LogwrtResult.Write.xrecoff - BLCKSZ) % XLogSegSize)
! 		{
! 			openLogOff = (LogwrtResult.Write.xrecoff - BLCKSZ) % XLogSegSize;
! 			if (lseek(openLogFile, (off_t) openLogOff, SEEK_SET) < 0)
! 				ereport(PANIC,
! 						(errcode_for_file_access(),
! 						 errmsg("could not seek in log file %u, segment %u to offset %u: %m",
! 								openLogId, openLogSeg, openLogOff)));
! 		}
! 
! 		/* OK to write the page */
! 		from = XLogCtl->pages + Write->curridx * BLCKSZ;
! 		errno = 0;
! 		if (write(openLogFile, from, BLCKSZ) != BLCKSZ)
! 		{
! 			/* if write didn't set errno, assume problem is no disk space */
! 			if (errno == 0)
! 				errno = ENOSPC;
! 			ereport(PANIC,
! 					(errcode_for_file_access(),
! 					 errmsg("could not write to log file %u, segment %u at offset %u: %m",
! 							openLogId, openLogSeg, openLogOff)));
! 		}
! 		openLogOff += BLCKSZ;
  
  		/*
  		 * If we just wrote the whole last page of a logfile segment,
--- 1375,1382 ----
  			openLogOff = 0;
  		}
  
! 		/* Add a page to buffer */
! 		XLogPageWrite(&pages, currentIndex);
  
  		/*
  		 * If we just wrote the whole last page of a logfile segment,
*************** XLogWrite(XLogwrtRqst WriteRqst)
*** 1396,1403 ****
  		 * This is also the right place to notify the Archiver that the
  		 * segment is ready to copy to archival storage.
  		 */
! 		if (openLogOff >= XLogSegSize && !ispartialpage)
  		{
  			issue_xlog_fsync();
  			LogwrtResult.Flush = LogwrtResult.Write;	/* end of current page */
  
--- 1388,1396 ----
  		 * This is also the right place to notify the Archiver that the
  		 * segment is ready to copy to archival storage.
  		 */
! 		if (openLogOff + pages.size >= XLogSegSize && !ispartialpage)
  		{
+ 			XLogPageFlush(&pages, currentIndex);
  			issue_xlog_fsync();
  			LogwrtResult.Flush = LogwrtResult.Write;	/* end of current page */
  
*************** XLogWrite(XLogwrtRqst WriteRqst)
*** 1411,1418 ****
  			LogwrtResult.Write = WriteRqst.Write;
  			break;
  		}
! 		Write->curridx = NextBufIdx(Write->curridx);
  	}
  
  	/*
  	 * If asked to flush, do so
--- 1404,1412 ----
  			LogwrtResult.Write = WriteRqst.Write;
  			break;
  		}
! 		currentIndex = NextBufIdx(currentIndex);
  	}
+ 	XLogPageFlush(&pages, currentIndex);
  
  	/*
  	 * If asked to flush, do so
*************** remove_backup_label(void)
*** 5916,5918 ****
--- 5910,5979 ----
  					 errmsg("could not remove file \"%s\": %m",
  							labelfilepath)));
  }
+ 
+ /* XLog gather-write staffs */
+ 
+ static void
+ XLogPageReset(XLogPages *pages)
+ {
+ 	memset(pages, 0, sizeof(*pages));
+ }
+ 
+ static void
+ XLogPageWrite(XLogPages *pages, int index)
+ {
+ 	char *page = XLogCtl->pages + index * BLCKSZ;
+ 	int size = BLCKSZ;
+ 	int offset = (LogwrtResult.Write.xrecoff - BLCKSZ) % XLogSegSize;
+ 
+ 	if (pages->head + pages->size == page
+ 		&& pages->offset + pages->size == offset)
+ 	{	/* Pages are continuous. Append new page. */
+ 		pages->size += size;
+ 	}
+ 	else
+ 	{	/* Pages are not continuous. Flush and clear. */
+ 		XLogPageFlush(pages, PrevBufIdx(index));
+ 		pages->head = page;
+ 		pages->size = size;
+ 		pages->offset = offset;
+ 	}
+ }
+ 
+ static void
+ XLogPageFlush(XLogPages *pages, int index)
+ {
+ 	if (!pages->head)
+ 	{	/* No needs to write pages. */
+ 		XLogCtl->Write.curridx = index;
+ 		return;
+ 	}
+ 	
+ 	/* Need to seek in the file? */
+ 	if (openLogOff != pages->offset)
+ 	{
+ 		openLogOff = pages->offset;
+ 		if (lseek(openLogFile, (off_t) openLogOff, SEEK_SET) < 0)
+ 			ereport(PANIC,
+ 					(errcode_for_file_access(),
+ 					 errmsg("could not seek in log file %u, segment %u to offset %u: %m",
+ 							openLogId, openLogSeg, openLogOff)));
+ 	}
+ 
+ 	/* OK to write the page */
+ 	errno = 0;
+ 	if (write(openLogFile, pages->head, pages->size) != pages->size)
+ 	{
+ 		/* if write didn't set errno, assume problem is no disk space */
+ 		if (errno == 0)
+ 			errno = ENOSPC;
+ 		ereport(PANIC,
+ 				(errcode_for_file_access(),
+ 				 errmsg("could not write to log file %u, segment %u at offset %u: %m",
+ 						openLogId, openLogSeg, openLogOff)));
+ 	}
+ 
+ 	openLogOff += pages->size;
+ 	XLogCtl->Write.curridx = index;
+ 	XLogPageReset(pages);
+ }