2929 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
3030 * Portions Copyright (c) 1994, Regents of the University of California
3131 *
32- *$Id: pqcomm.c,v 1.123 2001/11/05 17:46:25 momjian Exp $
32+ *$Id: pqcomm.c,v 1.124 2001/11/12 04:54:08 tgl Exp $
3333 *
3434 *-------------------------------------------------------------------------
3535 */
@@ -503,19 +503,18 @@ pq_recvbuf(void)
503503continue ;/* Ok if interrupted */
504504
505505/*
506- *We would like to use elog()here, but dare not because elog
507- *tries to write tothe client, which will cause problems if
508- *we have a hard communications failure ... So just write the
509- *message to the postmaster log .
506+ *Careful: an elog()that tries to write to the client
507+ *would cause recursion tohere, leading to stack overflow
508+ *and core dump! This message must go *only* to the postmaster
509+ *log. elog(DEBUG) is presently safe .
510510 */
511- fprintf (stderr ,"pq_recvbuf: recv() failed: %s\n" ,
512- strerror (errno ));
511+ elog (DEBUG ,"pq_recvbuf: recv() failed: %m" );
513512return EOF ;
514513}
515514if (r == 0 )
516515{
517- /* as above,elog not safe */
518- fprintf ( stderr ,"pq_recvbuf: unexpected EOF on client connection\n " );
516+ /* as above,only write to postmaster log */
517+ elog ( DEBUG ,"pq_recvbuf: unexpected EOF on client connection" );
519518return EOF ;
520519}
521520/* r contains number of bytes read, so just incr length */
@@ -655,6 +654,8 @@ pq_putbytes(const char *s, size_t len)
655654int
656655pq_flush (void )
657656{
657+ static int last_reported_send_errno = 0 ;
658+
658659unsignedchar * bufptr = PqSendBuffer ;
659660unsignedchar * bufend = PqSendBuffer + PqSendPointer ;
660661
@@ -675,12 +676,20 @@ pq_flush(void)
675676continue ;/* Ok if we were interrupted */
676677
677678/*
678- * We would like to use elog() here, but cannot because elog
679- * tries to write to the client, which would cause a recursive
680- * flush attempt! So just write it out to the postmaster log.
679+ * Careful: an elog() that tries to write to the client
680+ * would cause recursion to here, leading to stack overflow
681+ * and core dump! This message must go *only* to the postmaster
682+ * log. elog(DEBUG) is presently safe.
683+ *
684+ * If a client disconnects while we're in the midst of output,
685+ * we might write quite a bit of data before we get to a safe
686+ * query abort point. So, suppress duplicate log messages.
681687 */
682- fprintf (stderr ,"pq_flush: send() failed: %s\n" ,
683- strerror (errno ));
688+ if (errno != last_reported_send_errno )
689+ {
690+ last_reported_send_errno = errno ;
691+ elog (DEBUG ,"pq_flush: send() failed: %m" );
692+ }
684693
685694/*
686695 * We drop the buffered data anyway so that processing can
@@ -689,8 +698,11 @@ pq_flush(void)
689698PqSendPointer = 0 ;
690699return EOF ;
691700}
701+
702+ last_reported_send_errno = 0 ;/* reset after any successful send */
692703bufptr += r ;
693704}
705+
694706PqSendPointer = 0 ;
695707return 0 ;
696708}
@@ -709,8 +721,8 @@ pq_eof(void)
709721
710722if (res < 0 )
711723{
712- /*don't try toelog here... */
713- fprintf ( stderr ,"pq_eof: recv() failed: %s\n" , strerror ( errno ) );
724+ /*can log topostmaster log only */
725+ elog ( DEBUG ,"pq_eof: recv() failed: %m" );
714726return EOF ;
715727}
716728if (res == 0 )