On Thu, Mar 24, 2022 at 01:31:08PM -0700, Nathan Bossart wrote:
> A couple of other options to consider:
> 
> 1) Always set log_min_messages to WARNING/ERROR/FATAL for 'postgres -C'.
> We might need some special logic for handling the case where the user is
> inspecting the log_min_messages parameter.  With this approach, you'd
> probably never get extra output unless something was wrong (e.g., database
> already running when inspecting a runtime-computed GUC).  Also, this would
> silence any extra output that you might see today with non-runtime-computed
> GUCs.
> 
> 2) Add some way to skip just the shutdown message (e.g., a variable set
> when output_config_variable is true).  With this approach, you wouldn't get
> extra output by default, but you still might if log_min_messages is set to
> something like DEBUG3.  This wouldn't impact any extra output that you see
> today with non-runtime-computed GUCs.

I've attached a first attempt at option 1.

-- 
Nathan Bossart
Amazon Web Services: https://aws.amazon.com
>From f36d04f5d19c673a7be788d6d4955b148f580095 Mon Sep 17 00:00:00 2001
From: Nathan Bossart <nathandboss...@gmail.com>
Date: Mon, 28 Mar 2022 10:12:23 -0700
Subject: [PATCH v3 1/1] Silence extra logging with 'postgres -C'.

Presently, the server may emit a variety of extra log messages when inspecting
GUC values.  For example, when inspecting a runtime-computed GUC, the server
will always emit a "database system is shut down" LOG (unless the user has set
log_min_messages higher than LOG).  To avoid these extra log messages, this
change sets log_min_messages to FATAL when -C is used (unless the user has set
it to PANIC).  At FATAL, the user will still receive messages explaining why a
GUC's value cannot be inspected.
---
 src/backend/postmaster/postmaster.c | 34 +++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index 80bb269599..7ee20d7c69 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -897,6 +897,26 @@ PostmasterMain(int argc, char *argv[])
 
 	if (output_config_variable != NULL)
 	{
+		const char *logging_setting;
+		int			flags;
+
+		/*
+		 * Silence any extra log messages when -C is used.  Before adjusting
+		 * log_min_messages, we save its value in case the user is inspecting
+		 * it.  If we are inspecting a runtime-computed GUC, it is possible that
+		 * a preloaded library's _PG_init() will override our custom setting for
+		 * log_min_messages, but trying to deal with that is probably more
+		 * trouble than it's worth.
+		 *
+		 * If the user has set log_min_messages to PANIC, we leave it at PANIC.
+		 * This has the side effect of silencing helpful FATAL messages (e.g.,
+		 * when trying to inspect a runtime-computed GUC on a running server),
+		 * but presumably the user has set it there for good reason.
+		 */
+		logging_setting = GetConfigOption("log_min_messages", false, false);
+		if (log_min_messages < FATAL)
+			SetConfigOption("log_min_messages", "FATAL", PGC_INTERNAL, PGC_S_OVERRIDE);
+
 		/*
 		 * If this is a runtime-computed GUC, it hasn't yet been initialized,
 		 * and the present value is not useful.  However, this is a convenient
@@ -908,17 +928,23 @@ PostmasterMain(int argc, char *argv[])
 		 * on running servers for those GUCs, but using this option now would
 		 * lead to incorrect results for them.
 		 */
-		int			flags = GetConfigOptionFlags(output_config_variable, true);
+		flags = GetConfigOptionFlags(output_config_variable, true);
 
 		if ((flags & GUC_RUNTIME_COMPUTED) == 0)
 		{
+			const char *config_val;
+
 			/*
 			 * "-C guc" was specified, so print GUC's value and exit.  No
 			 * extra permission check is needed because the user is reading
-			 * inside the data dir.
+			 * inside the data dir.  If we are inspecting log_min_messages,
+			 * print the saved value.
 			 */
-			const char *config_val = GetConfigOption(output_config_variable,
-													 false, false);
+			if (pg_strcasecmp(output_config_variable, "log_min_messages") == 0)
+				config_val = logging_setting;
+			else
+				config_val = GetConfigOption(output_config_variable,
+											 false, false);
 
 			puts(config_val ? config_val : "");
 			ExitPostmaster(0);
-- 
2.25.1

Reply via email to