Index: src/backend/libpq/pqcomm.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/libpq/pqcomm.c,v
retrieving revision 1.199
diff -c -r1.199 pqcomm.c
*** src/backend/libpq/pqcomm.c	1 Jan 2009 17:23:42 -0000	1.199
--- src/backend/libpq/pqcomm.c	23 Feb 2009 21:09:45 -0000
***************
*** 124,129 ****
--- 124,130 ----
  static void pq_close(int code, Datum arg);
  static int	internal_putbytes(const char *s, size_t len);
  static int	internal_flush(void);
+ static int	internal_send(const char *bufptr, size_t len);
  
  #ifdef HAVE_UNIX_SOCKETS
  static int	Lock_AF_UNIX(unsigned short portNumber, char *unixSocketName);
***************
*** 1041,1046 ****
--- 1042,1056 ----
  		if (PqSendPointer >= PQ_BUFFER_SIZE)
  			if (internal_flush())
  				return EOF;
+ 
+ 		/*
+ 		 * If buffer is empty and we'd fill it, just push the data immediately
+ 		 * rather than copying it into PqSendBuffer.
+ 		 */
+ 		if (PqSendPointer == 0 && len >= PQ_BUFFER_SIZE)
+ 			return internal_send(s, len);
+ 
+ 		/* Else put (some of) the data into the buffer */
  		amount = PQ_BUFFER_SIZE - PqSendPointer;
  		if (amount > len)
  			amount = len;
***************
*** 1075,1090 ****
  static int
  internal_flush(void)
  {
  	static int	last_reported_send_errno = 0;
  
! 	char	   *bufptr = PqSendBuffer;
! 	char	   *bufend = PqSendBuffer + PqSendPointer;
  
  	while (bufptr < bufend)
  	{
  		int			r;
  
! 		r = secure_write(MyProcPort, bufptr, bufend - bufptr);
  
  		if (r <= 0)
  		{
--- 1085,1115 ----
  static int
  internal_flush(void)
  {
+ 	int			r;
+ 
+ 	r = internal_send(PqSendBuffer, PqSendPointer);
+ 
+ 	/*
+ 	 * On error, we drop the buffered data anyway so that processing can
+ 	 * continue, even though we'll probably quit soon.
+ 	 */
+ 	PqSendPointer = 0;
+ 
+ 	return r;
+ }
+ 
+ static int
+ internal_send(const char *bufptr, size_t len)
+ {
  	static int	last_reported_send_errno = 0;
  
! 	const char *bufend = bufptr + len;
  
  	while (bufptr < bufend)
  	{
  		int			r;
  
! 		r = secure_write(MyProcPort, (void *) bufptr, bufend - bufptr);
  
  		if (r <= 0)
  		{
***************
*** 1108,1118 ****
  						 errmsg("could not send data to client: %m")));
  			}
  
- 			/*
- 			 * We drop the buffered data anyway so that processing can
- 			 * continue, even though we'll probably quit soon.
- 			 */
- 			PqSendPointer = 0;
  			return EOF;
  		}
  
--- 1133,1138 ----
***************
*** 1120,1126 ****
  		bufptr += r;
  	}
  
- 	PqSendPointer = 0;
  	return 0;
  }
  
--- 1140,1145 ----
