Skip to content

Commit 1f9158b

Browse files
committed
Suppress log spam from multiple reports of SIGQUIT shutdown.
When the postmaster sends SIGQUIT to its children, there's no real need for all the children to log that fact; the postmaster already made a log entry about it, so adding perhaps dozens or hundreds of child-process log entries adds nothing of value. So, let's introduce a new ereport level to specify "WARNING, but never send to log" and use that for these messages. Such a change wouldn't have been desirable before commit 7e784d1, because if someone manually SIGQUIT's a backend, we *do* want to log that. But now we can tell the difference between a signal that was issued by the postmaster and one that was not with reasonable certainty. While we're here, also clear error_context_stack before ereport'ing, to prevent error callbacks from being invoked in the signal-handler context. This should reduce the odds of getting hung up while trying to notify the client. Per a suggestion from Andres Freund. Discussion: https://postgr.es/m/20201225230331.hru3u6obyy6j53tk@alap3.anarazel.de
1 parent db6335b commit 1f9158b

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

src/backend/tcop/postgres.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,18 @@ quickdie(SIGNAL_ARGS)
27892789
* wrong, so there's not much to lose. Assuming the postmaster is still
27902790
* running, it will SIGKILL us soon if we get stuck for some reason.
27912791
*
2792+
* One thing we can do to make this a tad safer is to clear the error
2793+
* context stack, so that context callbacks are not called. That's a lot
2794+
* less code that could be reached here, and the context info is unlikely
2795+
* to be very relevant to a SIGQUIT report anyway.
2796+
*/
2797+
error_context_stack = NULL;
2798+
2799+
/*
2800+
* When responding to a postmaster-issued signal, we send the message only
2801+
* to the client; sending to the server log just creates log spam, plus
2802+
* it's more code that we need to hope will work in a signal handler.
2803+
*
27922804
* Ideally these should be ereport(FATAL), but then we'd not get control
27932805
* back to force the correct type of process exit.
27942806
*/
@@ -2802,7 +2814,7 @@ quickdie(SIGNAL_ARGS)
28022814
break;
28032815
case PMQUIT_FOR_CRASH:
28042816
/* A crash-and-restart cycle is in progress */
2805-
ereport(WARNING,
2817+
ereport(WARNING_CLIENT_ONLY,
28062818
(errcode(ERRCODE_CRASH_SHUTDOWN),
28072819
errmsg("terminating connection because of crash of another server process"),
28082820
errdetail("The postmaster has commanded this server process to roll back"
@@ -2814,7 +2826,7 @@ quickdie(SIGNAL_ARGS)
28142826
break;
28152827
case PMQUIT_FOR_STOP:
28162828
/* Immediate-mode stop */
2817-
ereport(WARNING,
2829+
ereport(WARNING_CLIENT_ONLY,
28182830
(errcode(ERRCODE_ADMIN_SHUTDOWN),
28192831
errmsg("terminating connection due to immediate shutdown command")));
28202832
break;

src/backend/utils/error/elog.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ is_log_level_output(int elevel, int log_min_level)
202202
if (log_min_level == LOG || log_min_level <= ERROR)
203203
return true;
204204
}
205+
else if (elevel == WARNING_CLIENT_ONLY)
206+
{
207+
/* never sent to log, regardless of log_min_level */
208+
return false;
209+
}
205210
else if (log_min_level == LOG)
206211
{
207212
/* elevel != LOG */
@@ -453,7 +458,7 @@ errstart(int elevel, const char *domain)
453458
/* Select default errcode based on elevel */
454459
if (elevel >= ERROR)
455460
edata->sqlerrcode = ERRCODE_INTERNAL_ERROR;
456-
else if (elevel == WARNING)
461+
else if (elevel >= WARNING)
457462
edata->sqlerrcode = ERRCODE_WARNING;
458463
else
459464
edata->sqlerrcode = ERRCODE_SUCCESSFUL_COMPLETION;
@@ -2152,6 +2157,7 @@ write_eventlog(int level, const char *line, int len)
21522157
eventlevel = EVENTLOG_INFORMATION_TYPE;
21532158
break;
21542159
case WARNING:
2160+
case WARNING_CLIENT_ONLY:
21552161
eventlevel = EVENTLOG_WARNING_TYPE;
21562162
break;
21572163
case ERROR:
@@ -3109,6 +3115,7 @@ send_message_to_server_log(ErrorData *edata)
31093115
break;
31103116
case NOTICE:
31113117
case WARNING:
3118+
case WARNING_CLIENT_ONLY:
31123119
syslog_level = LOG_NOTICE;
31133120
break;
31143121
case ERROR:
@@ -3484,6 +3491,7 @@ error_severity(int elevel)
34843491
prefix = gettext_noop("NOTICE");
34853492
break;
34863493
case WARNING:
3494+
case WARNING_CLIENT_ONLY:
34873495
prefix = gettext_noop("WARNING");
34883496
break;
34893497
case ERROR:

src/include/utils/elog.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@
4040
#define WARNING 19 /* Warnings. NOTICE is for expected messages
4141
* like implicit sequence creation by SERIAL.
4242
* WARNING is for unexpected messages. */
43-
#define ERROR 20 /* user error - abort transaction; return to
43+
#define WARNING_CLIENT_ONLY 20 /* Warnings to be sent to client as usual, but
44+
* never to the server log. */
45+
#define ERROR 21 /* user error - abort transaction; return to
4446
* known state */
4547
/* Save ERROR value in PGERROR so it can be restored when Win32 includes
4648
* modify it. We have to use a constant rather than ERROR because macros
4749
* are expanded only when referenced outside macros.
4850
*/
4951
#ifdef WIN32
50-
#define PGERROR 20
52+
#define PGERROR 21
5153
#endif
52-
#define FATAL 21 /* fatal error - abort process */
53-
#define PANIC 22 /* take down the other backends with me */
54-
55-
/* #define DEBUG DEBUG1 */ /* Backward compatibility with pre-7.3 */
54+
#define FATAL 22 /* fatal error - abort process */
55+
#define PANIC 23 /* take down the other backends with me */
5656

5757

5858
/* macros for representing SQLSTATE strings compactly */

0 commit comments

Comments
 (0)