46,69d45
< /*-------------------------------------------------------------------------*/
< 
< #ifdef O_DIRECT
< #	define OPEN_DIRECT_FLAG		O_DIRECT
< #endif
< 
< #define XLOG_MULTIPAGE_WRITER_DEBUG
< 
< /*-------------------------------------------------------------------------*/
< 
< /* O_DIRECT : BEGIN */
< 
< /* TODO: Aligment depends on OS and filesystem. */
< #define O_DIRECT_BUFFER_ALIGN	4096
< 
< #ifdef OPEN_DIRECT_FLAG
< #	define XLOG_EXTRA_BUFFERS		O_DIRECT_BUFFER_ALIGN
< #	define XLOG_BUFFERS_ALIGN(LEN)	TYPEALIGN(XLOG_EXTRA_BUFFERS, (LEN))
< #else
< #	define XLOG_EXTRA_BUFFERS		0
< #	define XLOG_BUFFERS_ALIGN(LEN)	MAXALIGN(LEN)
< #endif
< 
< /* O_DIRECT : END */
492,556d467
< /* BEGIN : XLOG_MULTIPAGE_WRITER */
< 
< static struct XLogMultipageData
< {
< 	char	*pages;		/* Head of first page */
< 	int		 size;		/* Total bytes of pages == count(pages) * BLCKSZ */
< 	int		 offset;	/* Offset in xlog segment file  */
< } XLogMultipage;
< 
< static void XLogMultipageFlush(void)
< {
< 	if (!XLogMultipage.pages)
< 	{	/* No needs to write pages. */
< 		return;
< 	}
< 	
< 	/* Need to seek in the file? */
< 	if (openLogOff != XLogMultipage.offset)
< 	{
< 		openLogOff = XLogMultipage.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, XLogMultipage.pages, XLogMultipage.size) != XLogMultipage.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)));
< 	}
< 
< #ifdef XLOG_MULTIPAGE_WRITER_DEBUG
< 	elog(LOG, "XLogMultipageFlush writes %d pages.", XLogMultipage.size / BLCKSZ);
< #endif
< 
< 	openLogOff += XLogMultipage.size;
< 	memset(&XLogMultipage, 0, sizeof(XLogMultipage));
< }
< 
< static void XLogMultipageWrite(char *page, int size, int offset)
< {
< 	if (XLogMultipage.pages + XLogMultipage.size == page
< 		&& XLogMultipage.offset + XLogMultipage.size == offset)
< 	{	/* Pages are continuous. Append new page. */
< 		XLogMultipage.size += size;
< 	}
< 	else
< 	{	/* Pages are not continuous. Flush and clear. */
< 		XLogMultipageFlush();
< 		XLogMultipage.pages = page;
< 		XLogMultipage.size = size;
< 		XLogMultipage.offset = offset;
< 	}
< }
< 
< /* END : XLOG_MULTIPAGE_WRITER */
1230a1142
> 	char	   *from;
1233,1238d1144
< 	int			currentIndex = Write->curridx;
< 
< #ifdef XLOG_MULTIPAGE_WRITER_DEBUG
< 	if (XLogMultipage.pages)
< 		elog(PANIC, "XLogMultipage.pages not null (%d) : size=%d", __LINE__, XLogMultipage.size);
< #endif
1254c1160
< 		if (!XLByteLT(LogwrtResult.Write, XLogCtl->xlblocks[currentIndex]))
---
> 		if (!XLByteLT(LogwrtResult.Write, XLogCtl->xlblocks[Write->curridx]))
1257,1258c1163,1164
< 				 XLogCtl->xlblocks[currentIndex].xlogid,
< 				 XLogCtl->xlblocks[currentIndex].xrecoff);
---
> 				 XLogCtl->xlblocks[Write->curridx].xlogid,
> 				 XLogCtl->xlblocks[Write->curridx].xrecoff);
1261c1167
< 		LogwrtResult.Write = XLogCtl->xlblocks[currentIndex];
---
> 		LogwrtResult.Write = XLogCtl->xlblocks[Write->curridx];
1269d1174
< 			XLogMultipageFlush();
1339a1245,1256
> 			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)));
1342,1346c1259,1272
< 		/* Add a page to buffer */
< 		XLogMultipageWrite(
< 			XLogCtl->pages + currentIndex * BLCKSZ,
< 			BLCKSZ,
< 			(LogwrtResult.Write.xrecoff - BLCKSZ) % XLogSegSize);
---
> 		/* 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;
1358c1284
< 		if (openLogOff + XLogMultipage.size >= XLogSegSize && !ispartialpage)
---
> 		if (openLogOff >= XLogSegSize && !ispartialpage)
1360d1285
< 			XLogMultipageFlush();
1374c1299
< 		currentIndex = NextBufIdx(currentIndex);
---
> 		Write->curridx = NextBufIdx(Write->curridx);
1376d1300
< 	XLogMultipageFlush();
1412,1413d1335
< 	Write->curridx = currentIndex;
< 
1435,1439d1356
< 
< #ifdef XLOG_MULTIPAGE_WRITER_DEBUG
< 	if (XLogMultipage.pages)
< 		elog(PANIC, "XLogMultipage.pages not null (%d) : size=%d", __LINE__, XLogMultipage.size);
< #endif
1562,1564d1478
< 
< 	if (XLogMultipage.pages)
< 		elog(PANIC, "xlog multipage-write usage error at XLogFlush");
3469c3383
< 		+ XLOG_EXTRA_BUFFERS + BLCKSZ * XLOGbuffers +
---
> 		+ BLCKSZ * XLOGbuffers +
3487c3401
< 						+ XLOG_EXTRA_BUFFERS + BLCKSZ * XLOGbuffers,
---
> 						+ BLCKSZ * XLOGbuffers,
3515,3516c3429,3430
< 		(char*)XLOG_BUFFERS_ALIGN(((char *) XLogCtl)
< 		+ sizeof(XLogCtlData) + sizeof(XLogRecPtr) * XLOGbuffers);
---
> 		((char *) XLogCtl) + MAXALIGN(sizeof(XLogCtlData) +
> 									  sizeof(XLogRecPtr) * XLOGbuffers);
3574,3577d3487
< #if 1
< 	buffer = (char *) XLOG_BUFFERS_ALIGN(
< 		malloc(BLCKSZ + XLOG_EXTRA_BUFFERS) );
< #else
3580,3582d3489
< #endif
< 	/* XXX: Does buffer memory-leak? */
< 
5276,5282d5182
< #ifdef OPEN_DIRECT_FLAG
< 	else if (pg_strcasecmp(method, "open_direct") == 0)
< 	{
< 		new_sync_method = SYNC_METHOD_OPEN;
< 		new_sync_bit = OPEN_DIRECT_FLAG;
< 	}
< #endif
