Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7be1807

Browse files
committed
Suppress duplicate error messages in pq_flush. Write error messages to
postmaster log with elog(DEBUG) so that they will be timestamped etc. Once upon a time I think elog() was unsafe here, but it shouldn't be anymore.
1 parent 1131ba3 commit 7be1807

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

src/backend/libpq/pqcomm.c

+28-16
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
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)
503503
continue; /* Ok if interrupted */
504504

505505
/*
506-
* We would like to use elog() here, but dare not because elog
507-
* tries to write to the 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 to here, 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");
513512
return EOF;
514513
}
515514
if (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");
519518
return 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)
655654
int
656655
pq_flush(void)
657656
{
657+
static int last_reported_send_errno = 0;
658+
658659
unsigned char *bufptr = PqSendBuffer;
659660
unsigned char *bufend = PqSendBuffer + PqSendPointer;
660661

@@ -675,12 +676,20 @@ pq_flush(void)
675676
continue; /* 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)
689698
PqSendPointer = 0;
690699
return EOF;
691700
}
701+
702+
last_reported_send_errno = 0; /* reset after any successful send */
692703
bufptr += r;
693704
}
705+
694706
PqSendPointer = 0;
695707
return 0;
696708
}
@@ -709,8 +721,8 @@ pq_eof(void)
709721

710722
if (res < 0)
711723
{
712-
/* don't try to elog here... */
713-
fprintf(stderr, "pq_eof: recv() failed: %s\n", strerror(errno));
724+
/* can log to postmaster log only */
725+
elog(DEBUG, "pq_eof: recv() failed: %m");
714726
return EOF;
715727
}
716728
if (res == 0)

0 commit comments

Comments
 (0)