@@ -184,7 +184,94 @@ static void write_pipe_chunks(char *data, int len, int dest);
184184static void send_message_to_frontend (ErrorData * edata );
185185static const char * error_severity (int elevel );
186186static void append_with_tabs (StringInfo buf , const char * str );
187- static bool is_log_level_output (int elevel , int log_min_level );
187+
188+
189+ /*
190+ * is_log_level_output -- is elevel logically >= log_min_level?
191+ *
192+ * We use this for tests that should consider LOG to sort out-of-order,
193+ * between ERROR and FATAL. Generally this is the right thing for testing
194+ * whether a message should go to the postmaster log, whereas a simple >=
195+ * test is correct for testing whether the message should go to the client.
196+ */
197+ static inline bool
198+ is_log_level_output (int elevel , int log_min_level )
199+ {
200+ if (elevel == LOG || elevel == LOG_SERVER_ONLY )
201+ {
202+ if (log_min_level == LOG || log_min_level <= ERROR )
203+ return true;
204+ }
205+ else if (log_min_level == LOG )
206+ {
207+ /* elevel != LOG */
208+ if (elevel >= FATAL )
209+ return true;
210+ }
211+ /* Neither is LOG */
212+ else if (elevel >= log_min_level )
213+ return true;
214+
215+ return false;
216+ }
217+
218+ /*
219+ * Policy-setting subroutines. These are fairly simple, but it seems wise
220+ * to have the code in just one place.
221+ */
222+
223+ /*
224+ * should_output_to_server --- should message of given elevel go to the log?
225+ */
226+ static inline bool
227+ should_output_to_server (int elevel )
228+ {
229+ return is_log_level_output (elevel , log_min_messages );
230+ }
231+
232+ /*
233+ * should_output_to_client --- should message of given elevel go to the client?
234+ */
235+ static inline bool
236+ should_output_to_client (int elevel )
237+ {
238+ if (whereToSendOutput == DestRemote && elevel != LOG_SERVER_ONLY )
239+ {
240+ /*
241+ * client_min_messages is honored only after we complete the
242+ * authentication handshake. This is required both for security
243+ * reasons and because many clients can't handle NOTICE messages
244+ * during authentication.
245+ */
246+ if (ClientAuthInProgress )
247+ return (elevel >= ERROR );
248+ else
249+ return (elevel >= client_min_messages || elevel == INFO );
250+ }
251+ return false;
252+ }
253+
254+
255+ /*
256+ * message_level_is_interesting --- would ereport/elog do anything?
257+ *
258+ * Returns true if ereport/elog with this elevel will not be a no-op.
259+ * This is useful to short-circuit any expensive preparatory work that
260+ * might be needed for a logging message. There is no point in
261+ * prepending this to a bare ereport/elog call, however.
262+ */
263+ bool
264+ message_level_is_interesting (int elevel )
265+ {
266+ /*
267+ * Keep this in sync with the decision-making in errstart().
268+ */
269+ if (elevel >= ERROR ||
270+ should_output_to_server (elevel ) ||
271+ should_output_to_client (elevel ))
272+ return true;
273+ return false;
274+ }
188275
189276
190277/*
@@ -301,27 +388,8 @@ errstart(int elevel, const char *domain)
301388 * warning or less and not enabled for logging, just return false without
302389 * starting up any error logging machinery.
303390 */
304-
305- /* Determine whether message is enabled for server log output */
306- output_to_server = is_log_level_output (elevel , log_min_messages );
307-
308- /* Determine whether message is enabled for client output */
309- if (whereToSendOutput == DestRemote && elevel != LOG_SERVER_ONLY )
310- {
311- /*
312- * client_min_messages is honored only after we complete the
313- * authentication handshake. This is required both for security
314- * reasons and because many clients can't handle NOTICE messages
315- * during authentication.
316- */
317- if (ClientAuthInProgress )
318- output_to_client = (elevel >= ERROR );
319- else
320- output_to_client = (elevel >= client_min_messages ||
321- elevel == INFO );
322- }
323-
324- /* Skip processing effort if non-error message will not be output */
391+ output_to_server = should_output_to_server (elevel );
392+ output_to_client = should_output_to_client (elevel );
325393 if (elevel < ERROR && !output_to_server && !output_to_client )
326394 return false;
327395
@@ -1743,16 +1811,10 @@ pg_re_throw(void)
17431811
17441812 /*
17451813 * At least in principle, the increase in severity could have changed
1746- * where-to-output decisions, so recalculate. This should stay in
1747- * sync with errstart(), which see for comments.
1814+ * where-to-output decisions, so recalculate.
17481815 */
1749- if (IsPostmasterEnvironment )
1750- edata -> output_to_server = is_log_level_output (FATAL ,
1751- log_min_messages );
1752- else
1753- edata -> output_to_server = (FATAL >= log_min_messages );
1754- if (whereToSendOutput == DestRemote )
1755- edata -> output_to_client = true;
1816+ edata -> output_to_server = should_output_to_server (FATAL );
1817+ edata -> output_to_client = should_output_to_client (FATAL );
17561818
17571819 /*
17581820 * We can use errfinish() for the rest, but we don't want it to call
@@ -3505,35 +3567,6 @@ write_stderr(const char *fmt,...)
35053567}
35063568
35073569
3508- /*
3509- * is_log_level_output -- is elevel logically >= log_min_level?
3510- *
3511- * We use this for tests that should consider LOG to sort out-of-order,
3512- * between ERROR and FATAL. Generally this is the right thing for testing
3513- * whether a message should go to the postmaster log, whereas a simple >=
3514- * test is correct for testing whether the message should go to the client.
3515- */
3516- static bool
3517- is_log_level_output (int elevel , int log_min_level )
3518- {
3519- if (elevel == LOG || elevel == LOG_SERVER_ONLY )
3520- {
3521- if (log_min_level == LOG || log_min_level <= ERROR )
3522- return true;
3523- }
3524- else if (log_min_level == LOG )
3525- {
3526- /* elevel != LOG */
3527- if (elevel >= FATAL )
3528- return true;
3529- }
3530- /* Neither is LOG */
3531- else if (elevel >= log_min_level )
3532- return true;
3533-
3534- return false;
3535- }
3536-
35373570/*
35383571 * Adjust the level of a recovery-related message per trace_recovery_messages.
35393572 *
0 commit comments