Index: buffer/bufmgr.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/storage/buffer/bufmgr.c,v
retrieving revision 1.182
diff -d -c -r1.182 bufmgr.c
*** buffer/bufmgr.c	24 Nov 2004 02:56:17 -0000	1.182
--- buffer/bufmgr.c	11 Dec 2004 17:09:31 -0000
***************
*** 681,686 ****
--- 681,687 ----
  {
  	BufferDesc **dirty_buffers;
  	BufferTag  *buftags;
+     int         maxdirty;
  	int			num_buffer_dirty;
  	int			i;
  
***************
*** 688,704 ****
  	if (percent == 0 || maxpages == 0)
  		return 0;
  
  	/*
  	 * Get a list of all currently dirty buffers and how many there are.
  	 * We do not flush buffers that get dirtied after we started. They
  	 * have to wait until the next checkpoint.
  	 */
! 	dirty_buffers = (BufferDesc **) palloc(NBuffers * sizeof(BufferDesc *));
! 	buftags = (BufferTag *) palloc(NBuffers * sizeof(BufferTag));
  
  	LWLockAcquire(BufMgrLock, LW_EXCLUSIVE);
! 	num_buffer_dirty = StrategyDirtyBufferList(dirty_buffers, buftags,
! 											   NBuffers);
  
  	/*
  	 * If called by the background writer, we are usually asked to only
--- 689,714 ----
  	if (percent == 0 || maxpages == 0)
  		return 0;
  
+     /* If we know we will write all dirty buffers, up to the limit of maxpages
+      * then we can make a cheaper call to StrategyDirtyBufferList
+      */
+ 	if (percent = 100)
+     	maxdirty = maxpages;
+     else
+     	maxdirty = NBuffers;
+ 
  	/*
  	 * Get a list of all currently dirty buffers and how many there are.
  	 * We do not flush buffers that get dirtied after we started. They
  	 * have to wait until the next checkpoint.
  	 */
! 	dirty_buffers = (BufferDesc **) palloc(maxdirty * sizeof(BufferDesc *));
! 	buftags = (BufferTag *) palloc(maxdirty * sizeof(BufferTag));
  
  	LWLockAcquire(BufMgrLock, LW_EXCLUSIVE);
!    	num_buffer_dirty = StrategyDirtyBufferList(dirty_buffers, buftags,
! 											   maxdirty);
  
  	/*
  	 * If called by the background writer, we are usually asked to only
