public inbox for [email protected]  
help / color / mirror / Atom feed
Re: log_min_messages per backend type
20+ messages / 4 participants
[nested] [flat]

* Re: log_min_messages per backend type
@ 2025-07-31 14:19 Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-08-01 05:53 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  0 siblings, 2 replies; 20+ messages in thread

From: Euler Taveira @ 2025-07-31 14:19 UTC (permalink / raw)
  To: Andres Freund <[email protected]>; +Cc: Alvaro Herrera <[email protected]>; [email protected]

On Thu, Mar 6, 2025, at 10:33 AM, Andres Freund wrote:
> Huh, the startup process is among the most crucial things to monitor?
>

Good point. Fixed.

After collecting some suggestions, I'm attaching a new patch contains the
following changes:

- patch was rebased
- include Alvaro's patch (v2-0001) [1] as a basis for this patch
- add ioworker as new backend type
- add startup as new backend type per Andres suggestion
- small changes into documentation

> I don't know what I think about the whole patch, but I do want to voice
> *strong* opposition to duplicating a list of all backend types into multiple
> places. It's already painfull enough to add a new backend type, without having
> to pointlessly go around and manually add a new backend type to mulltiple
> arrays that have completely predictable content.
>

I'm including Alvaro's patch as is just to make the CF bot happy and to
illustrate how it would be if we adopt his solution to centralize the list of
backend types. I think Alvaro's proposal overcomes the objection [2], right?


[1] https://www.postgresql.org/message-id/[email protected]
[2] https://www.postgresql.org/message-id/y5tgui75jrcj6mm5nmoq4yqwage2432akx4kp2ogtcnim3wskx@2ipmtfi4qvp...


-- 
Euler Taveira
EDB   https://www.enterprisedb.com/

Attachments:

  [text/x-patch] v3-0001-Create-a-separate-file-listing-backend-types.patch (7.0K, ../../[email protected]/2-v3-0001-Create-a-separate-file-listing-backend-types.patch)
  download | inline diff:
From ec409dada269cf54b9bce16cae473a46b2400598 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=81lvaro=20Herrera?= <[email protected]>
Date: Tue, 15 Jul 2025 18:19:27 +0200
Subject: [PATCH v3 1/2] Create a separate file listing backend types

Use our established coding pattern to reduce maintenance pain when
adding other per-process-type characteristics.

Like PG_KEYWORD, PG_CMDTAG, PG_RMGR.
---
 src/backend/postmaster/launch_backend.c | 32 ++------------
 src/backend/utils/init/miscinit.c       | 59 ++-----------------------
 src/include/postmaster/proctypelist.h   | 50 +++++++++++++++++++++
 3 files changed, 58 insertions(+), 83 deletions(-)
 create mode 100644 src/include/postmaster/proctypelist.h

diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index bf6b55ee830..8b2f1a0cf41 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -177,34 +177,10 @@ typedef struct
 } child_process_kind;
 
 static child_process_kind child_process_kinds[] = {
-	[B_INVALID] = {"invalid", NULL, false},
-
-	[B_BACKEND] = {"backend", BackendMain, true},
-	[B_DEAD_END_BACKEND] = {"dead-end backend", BackendMain, true},
-	[B_AUTOVAC_LAUNCHER] = {"autovacuum launcher", AutoVacLauncherMain, true},
-	[B_AUTOVAC_WORKER] = {"autovacuum worker", AutoVacWorkerMain, true},
-	[B_BG_WORKER] = {"bgworker", BackgroundWorkerMain, true},
-
-	/*
-	 * WAL senders start their life as regular backend processes, and change
-	 * their type after authenticating the client for replication.  We list it
-	 * here for PostmasterChildName() but cannot launch them directly.
-	 */
-	[B_WAL_SENDER] = {"wal sender", NULL, true},
-	[B_SLOTSYNC_WORKER] = {"slot sync worker", ReplSlotSyncWorkerMain, true},
-
-	[B_STANDALONE_BACKEND] = {"standalone backend", NULL, false},
-
-	[B_ARCHIVER] = {"archiver", PgArchiverMain, true},
-	[B_BG_WRITER] = {"bgwriter", BackgroundWriterMain, true},
-	[B_CHECKPOINTER] = {"checkpointer", CheckpointerMain, true},
-	[B_IO_WORKER] = {"io_worker", IoWorkerMain, true},
-	[B_STARTUP] = {"startup", StartupProcessMain, true},
-	[B_WAL_RECEIVER] = {"wal_receiver", WalReceiverMain, true},
-	[B_WAL_SUMMARIZER] = {"wal_summarizer", WalSummarizerMain, true},
-	[B_WAL_WRITER] = {"wal_writer", WalWriterMain, true},
-
-	[B_LOGGER] = {"syslogger", SysLoggerMain, false},
+#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
+	[bktype] = {description, main_func, shmem_attach},
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
 };
 
 const char *
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index 43b4dbccc3d..dec220a61f5 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -266,62 +266,11 @@ GetBackendTypeDesc(BackendType backendType)
 
 	switch (backendType)
 	{
-		case B_INVALID:
-			backendDesc = gettext_noop("not initialized");
-			break;
-		case B_ARCHIVER:
-			backendDesc = gettext_noop("archiver");
-			break;
-		case B_AUTOVAC_LAUNCHER:
-			backendDesc = gettext_noop("autovacuum launcher");
-			break;
-		case B_AUTOVAC_WORKER:
-			backendDesc = gettext_noop("autovacuum worker");
-			break;
-		case B_BACKEND:
-			backendDesc = gettext_noop("client backend");
-			break;
-		case B_DEAD_END_BACKEND:
-			backendDesc = gettext_noop("dead-end client backend");
-			break;
-		case B_BG_WORKER:
-			backendDesc = gettext_noop("background worker");
-			break;
-		case B_BG_WRITER:
-			backendDesc = gettext_noop("background writer");
-			break;
-		case B_CHECKPOINTER:
-			backendDesc = gettext_noop("checkpointer");
-			break;
-		case B_IO_WORKER:
-			backendDesc = gettext_noop("io worker");
-			break;
-		case B_LOGGER:
-			backendDesc = gettext_noop("logger");
-			break;
-		case B_SLOTSYNC_WORKER:
-			backendDesc = gettext_noop("slotsync worker");
-			break;
-		case B_STANDALONE_BACKEND:
-			backendDesc = gettext_noop("standalone backend");
-			break;
-		case B_STARTUP:
-			backendDesc = gettext_noop("startup");
-			break;
-		case B_WAL_RECEIVER:
-			backendDesc = gettext_noop("walreceiver");
-			break;
-		case B_WAL_SENDER:
-			backendDesc = gettext_noop("walsender");
-			break;
-		case B_WAL_SUMMARIZER:
-			backendDesc = gettext_noop("walsummarizer");
-			break;
-		case B_WAL_WRITER:
-			backendDesc = gettext_noop("walwriter");
-			break;
+#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
+		case bktype: backendDesc = gettext_noop(description); break;
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
 	}
-
 	return backendDesc;
 }
 
diff --git a/src/include/postmaster/proctypelist.h b/src/include/postmaster/proctypelist.h
new file mode 100644
index 00000000000..919f00bb3a7
--- /dev/null
+++ b/src/include/postmaster/proctypelist.h
@@ -0,0 +1,50 @@
+/*-------------------------------------------------------------------------
+ *
+ * proctypelist.h
+ *
+ * The list of process types is kept on its own source file for use by
+ * automatic tools.  The exact representation of a process type is
+ * determined by the PG_PROCTYPE macro, which is not defined in this
+ * file; it can be defined by the caller for special purposes.
+ *
+ * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1994, Regents of the University of California
+ *
+ * IDENTIFICATION
+ *	  src/include/postmaster/proctypelist.h
+ *
+ *-------------------------------------------------------------------------
+ */
+
+/* there is deliberately not an #ifndef PROCTYPELIST_H here */
+
+/*
+ * WAL senders start their life as regular backend processes, and change their
+ * type after authenticating the client for replication.  We list it here for
+ * PostmasterChildName() but cannot launch them directly.
+ */
+
+/*
+ * List of process types (symbol, description, Main function, shmem_attach)
+ * entries.
+ */
+
+/* bktype, description, main_func, shmem_attach */
+PG_PROCTYPE(B_ARCHIVER, "archiver", PgArchiverMain, true)
+PG_PROCTYPE(B_AUTOVAC_LAUNCHER, "autovacuum launcher", AutoVacLauncherMain, true)
+PG_PROCTYPE(B_AUTOVAC_WORKER, "autovacuum worker", AutoVacWorkerMain, true)
+PG_PROCTYPE(B_BACKEND, "client backend", BackendMain, true)
+PG_PROCTYPE(B_BG_WORKER, "background worker", BackgroundWorkerMain, true)
+PG_PROCTYPE(B_BG_WRITER, "background writer", BackgroundWriterMain, true)
+PG_PROCTYPE(B_CHECKPOINTER, "checkpointer", CheckpointerMain, true)
+PG_PROCTYPE(B_DEAD_END_BACKEND, "dead-end client backend", BackendMain, true)
+PG_PROCTYPE(B_INVALID, "unrecognized", NULL, false)
+PG_PROCTYPE(B_IO_WORKER, "io worker", IoWorkerMain, true)
+PG_PROCTYPE(B_LOGGER, "syslogger", SysLoggerMain, false)
+PG_PROCTYPE(B_SLOTSYNC_WORKER, "slotsync worker", ReplSlotSyncWorkerMain, true)
+PG_PROCTYPE(B_STANDALONE_BACKEND, "standalone backend", NULL, false)
+PG_PROCTYPE(B_STARTUP, "startup", StartupProcessMain, true)
+PG_PROCTYPE(B_WAL_RECEIVER, "walreceiver", WalReceiverMain, true)
+PG_PROCTYPE(B_WAL_SENDER, "walsender", NULL, true)
+PG_PROCTYPE(B_WAL_SUMMARIZER, "walsummarizer", WalSummarizerMain, true)
+PG_PROCTYPE(B_WAL_WRITER, "walwriter", WalWriterMain, true)
-- 
2.39.5



  [text/x-patch] v3-0002-log_min_messages-per-backend-type.patch (24.7K, ../../[email protected]/3-v3-0002-log_min_messages-per-backend-type.patch)
  download | inline diff:
From a36fb59413fc6cca46a87accebebd08e515429a8 Mon Sep 17 00:00:00 2001
From: Euler Taveira <[email protected]>
Date: Mon, 11 Dec 2023 17:24:50 -0300
Subject: [PATCH v3 2/2] log_min_messages per backend type

Change log_min_messages from a single element to a comma-separated list
of elements. Each element is backendtype:loglevel. The backendtype are
archiver, autovacuum (includes launcher and workers), backend, bgworker,
bgwriter, checkpointer, ioworker, logger, slotsyncworker, walreceiver,
walsender, walsummarizer and walwriter. A single log level should be
part of this list that is applied as a final step to the backend types
that are not informed.

The old syntax (a single log level) is still accepted for backward
compatibility. In this case, it means the informed log level is applied
for the backend types that are not informed. The default log level is
the same (WARNING) for all backend types.
---
 doc/src/sgml/config.sgml                      |  32 ++-
 src/backend/commands/extension.c              |   2 +-
 src/backend/commands/variable.c               | 196 ++++++++++++++++++
 src/backend/postmaster/launch_backend.c       |   2 +-
 src/backend/utils/error/elog.c                |   2 +-
 src/backend/utils/init/miscinit.c             |   2 +-
 src/backend/utils/misc/guc_tables.c           |  48 +++--
 src/backend/utils/misc/postgresql.conf.sample |   1 +
 src/include/postmaster/proctypelist.h         |  38 ++--
 src/include/utils/guc.h                       |   2 +-
 src/include/utils/guc_hooks.h                 |   2 +
 src/include/utils/guc_tables.h                |   4 +
 src/test/regress/expected/guc.out             |  51 +++++
 src/test/regress/sql/guc.sql                  |  17 ++
 14 files changed, 353 insertions(+), 46 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 20ccb2d6b54..7c4002f2266 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -7021,7 +7021,7 @@ local0.*    /var/log/postgresql
      <variablelist>
 
      <varlistentry id="guc-log-min-messages" xreflabel="log_min_messages">
-      <term><varname>log_min_messages</varname> (<type>enum</type>)
+      <term><varname>log_min_messages</varname> (<type>string</type>)
       <indexterm>
        <primary><varname>log_min_messages</varname> configuration parameter</primary>
       </indexterm>
@@ -7030,14 +7030,28 @@ local0.*    /var/log/postgresql
        <para>
         Controls which <link linkend="runtime-config-severity-levels">message
         levels</link> are written to the server log.
-        Valid values are <literal>DEBUG5</literal>, <literal>DEBUG4</literal>,
-        <literal>DEBUG3</literal>, <literal>DEBUG2</literal>, <literal>DEBUG1</literal>,
-        <literal>INFO</literal>, <literal>NOTICE</literal>, <literal>WARNING</literal>,
-        <literal>ERROR</literal>, <literal>LOG</literal>, <literal>FATAL</literal>, and
-        <literal>PANIC</literal>.  Each level includes all the levels that
-        follow it.  The later the level, the fewer messages are sent
-        to the log.  The default is <literal>WARNING</literal>.  Note that
-        <literal>LOG</literal> has a different rank here than in
+        Valid values are a comma-separated list of <literal>backendtype:level</literal>
+        and a single <literal>level</literal>. The list allows it to use
+        different levels per backend type.
+        Valid <literal>backendtype</literal> values are <literal>archiver</literal>,
+        <literal>autovacuum</literal>, <literal>backend</literal>,
+        <literal>bgworker</literal>, <literal>bgwriter</literal>,
+        <literal>checkpointer</literal>, <literal>ioworker</literal>,
+        <literal>logger</literal>, <literal>slotsyncworker</literal>,
+        <literal>startup</literal>, <literal>walreceiver</literal>,
+        <literal>walsender</literal>, <literal>walsummarizer</literal>, and
+        <literal>walwriter</literal>.
+        Valid <literal>LEVEL</literal> values are <literal>DEBUG5</literal>,
+        <literal>DEBUG4</literal>, <literal>DEBUG3</literal>, <literal>DEBUG2</literal>,
+        <literal>DEBUG1</literal>, <literal>INFO</literal>, <literal>NOTICE</literal>,
+        <literal>WARNING</literal>, <literal>ERROR</literal>, <literal>LOG</literal>,
+        <literal>FATAL</literal>, and <literal>PANIC</literal>.  Each level includes
+        all the levels that follow it.  The later the level, the fewer messages are sent
+        to the log. A single <literal>LEVEL</literal> is mandatory (order does
+        not matter) and it is assigned to the backend types that are not
+        specified in the list.  The default is <literal>WARNING</literal> for
+        all backend types.
+        Note that <literal>LOG</literal> has a different rank here than in
         <xref linkend="guc-client-min-messages"/>.
         Only superusers and users with the appropriate <literal>SET</literal>
         privilege can change this setting.
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index e6f9ab6dfd6..b6055431d8f 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -1143,7 +1143,7 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
 		(void) set_config_option("client_min_messages", "warning",
 								 PGC_USERSET, PGC_S_SESSION,
 								 GUC_ACTION_SAVE, true, 0, false);
-	if (log_min_messages < WARNING)
+	if (log_min_messages[MyBackendType] < WARNING)
 		(void) set_config_option_ext("log_min_messages", "warning",
 									 PGC_SUSET, PGC_S_SESSION,
 									 BOOTSTRAP_SUPERUSERID,
diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 608f10d9412..5c769dd7bcc 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -35,6 +35,7 @@
 #include "utils/datetime.h"
 #include "utils/fmgrprotos.h"
 #include "utils/guc_hooks.h"
+#include "utils/guc_tables.h"
 #include "utils/snapmgr.h"
 #include "utils/syscache.h"
 #include "utils/timestamp.h"
@@ -1257,3 +1258,198 @@ check_ssl(bool *newval, void **extra, GucSource source)
 #endif
 	return true;
 }
+
+/*
+ * GUC check_hook for log_min_messages
+ *
+ * The parsing consists of a comma-separated list of BACKENDTYPENAME:LEVEL
+ * elements. BACKENDTYPENAME is log_min_messages_backend_types.  LEVEL is
+ * server_message_level_options. A single LEVEL element should be part of this
+ * list and it is applied as a final step to the backend types that are not
+ * specified. For backward compatibility, the old syntax is still accepted and
+ * it means to apply this level for all backend types.
+ */
+bool
+check_log_min_messages(char **newval, void **extra, GucSource source)
+{
+	char	   *rawstring;
+	List	   *elemlist;
+	ListCell   *l;
+	int			newlevels[BACKEND_NUM_TYPES];
+	bool		assigned[BACKEND_NUM_TYPES];
+	int			genericlevel = -1;	/* -1 means not assigned */
+
+	/* Initialize the array. */
+	memset(newlevels, WARNING, BACKEND_NUM_TYPES * sizeof(int));
+	memset(assigned, false, BACKEND_NUM_TYPES * sizeof(bool));
+
+	/* Need a modifiable copy of string. */
+	rawstring = pstrdup(*newval);
+
+	/* Parse string into list of identifiers. */
+	if (!SplitGUCList(rawstring, ',', &elemlist))
+	{
+		/* syntax error in list */
+		GUC_check_errdetail("List syntax is invalid.");
+		pfree(rawstring);
+		list_free(elemlist);
+		return false;
+	}
+
+	/* Validate and assign log level and backend type. */
+	foreach(l, elemlist)
+	{
+		char	   *tok = (char *) lfirst(l);
+		char	   *sep;
+		const struct config_enum_entry *entry;
+
+		/*
+		 * Check whether there is a backend type following the log level. If
+		 * there is no separator, it means this log level should be applied
+		 * for all backend types (backward compatibility).
+		 */
+		sep = strchr(tok, ':');
+		if (sep == NULL)
+		{
+			bool		found = false;
+
+			/* Reject duplicates for generic log level. */
+			if (genericlevel != -1)
+			{
+				GUC_check_errdetail("Generic log level was already assigned.");
+				pfree(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, tok) == 0)
+				{
+					genericlevel = entry->val;
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", tok);
+				pfree(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+		}
+		else
+		{
+			char	   *loglevel;
+			char	   *btype;
+			bool		found = false;
+
+			btype = palloc((sep - tok) + 1);
+			memcpy(btype, tok, sep - tok);
+			btype[sep - tok] = '\0';
+			loglevel = pstrdup(sep + 1);
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, loglevel) == 0)
+				{
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", loglevel);
+				pfree(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/*
+			 * Is the backend type name valid? There might be multiple entries
+			 * per backend type, don't bail out when find first occurrence.
+			 */
+			found = false;
+			for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+			{
+				if (pg_strcasecmp(log_min_messages_backend_types[i], btype) == 0)
+				{
+					/* Reject duplicates for a backend type. */
+					if (assigned[i])
+					{
+						GUC_check_errdetail("Backend type \"%s\" was already assigned.", btype);
+						pfree(rawstring);
+						list_free(elemlist);
+						return false;
+					}
+
+					newlevels[i] = entry->val;
+					assigned[i] = true;
+					found = true;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized backend type: \"%s\".", btype);
+				pfree(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+		}
+	}
+
+	/*
+	 * Generic log level must be specified. It is a good idea to specify a
+	 * generic log level to make it clear that it is the fallback value.
+	 * Although, we can document it, it might confuse users that used to
+	 * specify a single log level in prior releases.
+	 */
+	if (genericlevel == -1)
+	{
+		GUC_check_errdetail("Generic log level was not defined.");
+		pfree(rawstring);
+		list_free(elemlist);
+		return false;
+	}
+
+	/*
+	 * Apply the generic log level (the one without a backend type) after all
+	 * of the specific backend type have been assigned. Hence, it doesn't
+	 * matter the order you specify the generic log level, the final result
+	 * will be the same.
+	 */
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+	{
+		if (!assigned[i])
+			newlevels[i] = genericlevel;
+	}
+
+	pfree(rawstring);
+	list_free(elemlist);
+
+	/*
+	 * Pass back data for assign_log_min_messages to use.
+	 */
+	*extra = guc_malloc(LOG, BACKEND_NUM_TYPES * sizeof(int));
+	if (!*extra)
+		return false;
+	memcpy(*extra, newlevels, BACKEND_NUM_TYPES * sizeof(int));
+
+	return true;
+}
+
+/*
+ * GUC assign_hook for log_min_messages
+ */
+void
+assign_log_min_messages(const char *newval, void *extra)
+{
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+		log_min_messages[i] = ((int *) extra)[i];
+}
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index 8b2f1a0cf41..08395dafe04 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -177,7 +177,7 @@ typedef struct
 } child_process_kind;
 
 static child_process_kind child_process_kinds[] = {
-#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
 	[bktype] = {description, main_func, shmem_attach},
 #include "postmaster/proctypelist.h"
 #undef PG_PROCTYPE
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 47af743990f..77cafe6e7b5 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -235,7 +235,7 @@ is_log_level_output(int elevel, int log_min_level)
 static inline bool
 should_output_to_server(int elevel)
 {
-	return is_log_level_output(elevel, log_min_messages);
+	return is_log_level_output(elevel, log_min_messages[MyBackendType]);
 }
 
 /*
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index dec220a61f5..8a919148fd7 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -266,7 +266,7 @@ GetBackendTypeDesc(BackendType backendType)
 
 	switch (backendType)
 	{
-#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
 		case bktype: backendDesc = gettext_noop(description); break;
 #include "postmaster/proctypelist.h"
 #undef PG_PROCTYPE
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index d14b1678e7f..300e172aa82 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -146,7 +146,7 @@ static const struct config_enum_entry client_message_level_options[] = {
 	{NULL, 0, false}
 };
 
-static const struct config_enum_entry server_message_level_options[] = {
+const struct config_enum_entry server_message_level_options[] = {
 	{"debug5", DEBUG5, false},
 	{"debug4", DEBUG4, false},
 	{"debug3", DEBUG3, false},
@@ -536,7 +536,6 @@ static bool default_with_oids = false;
 bool		current_role_is_superuser;
 
 int			log_min_error_statement = ERROR;
-int			log_min_messages = WARNING;
 int			client_min_messages = NOTICE;
 int			log_min_duration_sample = -1;
 int			log_min_duration_statement = -1;
@@ -594,6 +593,7 @@ static char *server_version_string;
 static int	server_version_num;
 static char *debug_io_direct_string;
 static char *restrict_nonsystem_relation_kind_string;
+static char *log_min_messages_string;
 
 #ifdef HAVE_SYSLOG
 #define	DEFAULT_SYSLOG_FACILITY LOG_LOCAL0
@@ -638,6 +638,27 @@ char	   *role_string;
 /* should be static, but guc.c needs to get at this */
 bool		in_hot_standby_guc;
 
+/*
+ * It should be static, but commands/variable.c needs to get at this.
+ */
+int			log_min_messages[] = {
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
+	[bktype] = log_min_messages,
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
+};
+
+/*
+ * It might be in commands/variable.c but for convenience it is near
+ * log_min_messages.
+ */
+const char *const log_min_messages_backend_types[] = {
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
+	[bktype] = bkcategory,
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
+};
+
 
 /*
  * Displayable names for context types (enum GucContext)
@@ -4298,6 +4319,18 @@ struct config_string ConfigureNamesString[] =
 		check_client_encoding, assign_client_encoding, NULL
 	},
 
+	{
+		{"log_min_messages", PGC_SUSET, LOGGING_WHEN,
+			gettext_noop("Sets the message levels that are logged."),
+			gettext_noop("Each level includes all the levels that follow it. The later"
+						 " the level, the fewer messages are sent."),
+			GUC_LIST_INPUT
+		},
+		&log_min_messages_string,
+		"WARNING",
+		check_log_min_messages, assign_log_min_messages, NULL
+	},
+
 	{
 		{"log_line_prefix", PGC_SIGHUP, LOGGING_WHAT,
 			gettext_noop("Controls information prefixed to each log line."),
@@ -5110,17 +5143,6 @@ struct config_enum ConfigureNamesEnum[] =
 		NULL, NULL, NULL
 	},
 
-	{
-		{"log_min_messages", PGC_SUSET, LOGGING_WHEN,
-			gettext_noop("Sets the message levels that are logged."),
-			gettext_noop("Each level includes all the levels that follow it. The later"
-						 " the level, the fewer messages are sent.")
-		},
-		&log_min_messages,
-		WARNING, server_message_level_options,
-		NULL, NULL, NULL
-	},
-
 	{
 		{"log_min_error_statement", PGC_SUSET, LOGGING_WHEN,
 			gettext_noop("Causes all statements generating error at or above this level to be logged."),
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index a9d8293474a..c266effc597 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -539,6 +539,7 @@
 					#   log
 					#   fatal
 					#   panic
+					#   or a comma-separated list of backend_type:level
 
 #log_min_error_statement = error	# values in order of decreasing detail:
 					#   debug5
diff --git a/src/include/postmaster/proctypelist.h b/src/include/postmaster/proctypelist.h
index 919f00bb3a7..356cd45815a 100644
--- a/src/include/postmaster/proctypelist.h
+++ b/src/include/postmaster/proctypelist.h
@@ -29,22 +29,22 @@
  * entries.
  */
 
-/* bktype, description, main_func, shmem_attach */
-PG_PROCTYPE(B_ARCHIVER, "archiver", PgArchiverMain, true)
-PG_PROCTYPE(B_AUTOVAC_LAUNCHER, "autovacuum launcher", AutoVacLauncherMain, true)
-PG_PROCTYPE(B_AUTOVAC_WORKER, "autovacuum worker", AutoVacWorkerMain, true)
-PG_PROCTYPE(B_BACKEND, "client backend", BackendMain, true)
-PG_PROCTYPE(B_BG_WORKER, "background worker", BackgroundWorkerMain, true)
-PG_PROCTYPE(B_BG_WRITER, "background writer", BackgroundWriterMain, true)
-PG_PROCTYPE(B_CHECKPOINTER, "checkpointer", CheckpointerMain, true)
-PG_PROCTYPE(B_DEAD_END_BACKEND, "dead-end client backend", BackendMain, true)
-PG_PROCTYPE(B_INVALID, "unrecognized", NULL, false)
-PG_PROCTYPE(B_IO_WORKER, "io worker", IoWorkerMain, true)
-PG_PROCTYPE(B_LOGGER, "syslogger", SysLoggerMain, false)
-PG_PROCTYPE(B_SLOTSYNC_WORKER, "slotsync worker", ReplSlotSyncWorkerMain, true)
-PG_PROCTYPE(B_STANDALONE_BACKEND, "standalone backend", NULL, false)
-PG_PROCTYPE(B_STARTUP, "startup", StartupProcessMain, true)
-PG_PROCTYPE(B_WAL_RECEIVER, "walreceiver", WalReceiverMain, true)
-PG_PROCTYPE(B_WAL_SENDER, "walsender", NULL, true)
-PG_PROCTYPE(B_WAL_SUMMARIZER, "walsummarizer", WalSummarizerMain, true)
-PG_PROCTYPE(B_WAL_WRITER, "walwriter", WalWriterMain, true)
+/* bktype, bkcategory, description, main_func, shmem_attach, log_min_messages */
+PG_PROCTYPE(B_ARCHIVER, "archiver", "archiver", PgArchiverMain, true, WARNING)
+PG_PROCTYPE(B_AUTOVAC_LAUNCHER, "autovacuum", "autovacuum launcher", AutoVacLauncherMain, true, WARNING)
+PG_PROCTYPE(B_AUTOVAC_WORKER, "autovacuum", "autovacuum worker", AutoVacWorkerMain, true, WARNING)
+PG_PROCTYPE(B_BACKEND, "backend", "client backend", BackendMain, true, WARNING)
+PG_PROCTYPE(B_BG_WORKER, "bgworker", "background worker", BackgroundWorkerMain, true, WARNING)
+PG_PROCTYPE(B_BG_WRITER, "bgwriter", "background writer", BackgroundWriterMain, true, WARNING)
+PG_PROCTYPE(B_CHECKPOINTER, "checkpointer", "checkpointer", CheckpointerMain, true, WARNING)
+PG_PROCTYPE(B_DEAD_END_BACKEND, "backend", "dead-end client backend", BackendMain, true, WARNING)
+PG_PROCTYPE(B_INVALID, "backend", "invalid", NULL, false, WARNING)
+PG_PROCTYPE(B_IO_WORKER, "ioworker", "io worker", IoWorkerMain, true, WARNING)
+PG_PROCTYPE(B_LOGGER, "logger", "syslogger", SysLoggerMain, false, WARNING)
+PG_PROCTYPE(B_SLOTSYNC_WORKER, "slotsyncworker", "slotsync worker", ReplSlotSyncWorkerMain, true, WARNING)
+PG_PROCTYPE(B_STANDALONE_BACKEND, "backend", "standalone backend", NULL, false, WARNING)
+PG_PROCTYPE(B_STARTUP, "startup", "startup", StartupProcessMain, true, WARNING)
+PG_PROCTYPE(B_WAL_RECEIVER, "walreceiver", "walreceiver", WalReceiverMain, true, WARNING)
+PG_PROCTYPE(B_WAL_SENDER, "walsender", "walsender", NULL, true, WARNING)
+PG_PROCTYPE(B_WAL_SUMMARIZER, "walsummarizer", "walsummarizer", WalSummarizerMain, true, WARNING)
+PG_PROCTYPE(B_WAL_WRITER, "walwriter", "walwriter", WalWriterMain, true, WARNING)
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index f619100467d..e10e24940a7 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -271,7 +271,7 @@ extern PGDLLIMPORT bool log_duration;
 extern PGDLLIMPORT int log_parameter_max_length;
 extern PGDLLIMPORT int log_parameter_max_length_on_error;
 extern PGDLLIMPORT int log_min_error_statement;
-extern PGDLLIMPORT int log_min_messages;
+extern PGDLLIMPORT int log_min_messages[];
 extern PGDLLIMPORT int client_min_messages;
 extern PGDLLIMPORT int log_min_duration_sample;
 extern PGDLLIMPORT int log_min_duration_statement;
diff --git a/src/include/utils/guc_hooks.h b/src/include/utils/guc_hooks.h
index 82ac8646a8d..e6ffc96468f 100644
--- a/src/include/utils/guc_hooks.h
+++ b/src/include/utils/guc_hooks.h
@@ -174,5 +174,7 @@ extern void assign_wal_sync_method(int new_wal_sync_method, void *extra);
 extern bool check_synchronized_standby_slots(char **newval, void **extra,
 											 GucSource source);
 extern void assign_synchronized_standby_slots(const char *newval, void *extra);
+extern bool check_log_min_messages(char **newval, void **extra, GucSource source);
+extern void assign_log_min_messages(const char *newval, void *extra);
 
 #endif							/* GUC_HOOKS_H */
diff --git a/src/include/utils/guc_tables.h b/src/include/utils/guc_tables.h
index f72ce944d7f..2aefc653f20 100644
--- a/src/include/utils/guc_tables.h
+++ b/src/include/utils/guc_tables.h
@@ -298,6 +298,10 @@ struct config_enum
 	void	   *reset_extra;
 };
 
+/* log_min_messages */
+extern PGDLLIMPORT const char *const log_min_messages_backend_types[];
+extern PGDLLIMPORT const struct config_enum_entry server_message_level_options[];
+
 /* constant tables corresponding to enums above and in guc.h */
 extern PGDLLIMPORT const char *const config_group_names[];
 extern PGDLLIMPORT const char *const config_type_names[];
diff --git a/src/test/regress/expected/guc.out b/src/test/regress/expected/guc.out
index 7f9e29c765c..741ad6bf062 100644
--- a/src/test/regress/expected/guc.out
+++ b/src/test/regress/expected/guc.out
@@ -913,3 +913,54 @@ SELECT name FROM tab_settings_flags
 (0 rows)
 
 DROP TABLE tab_settings_flags;
+-- Test log_min_messages
+SET log_min_messages TO fatal;
+SHOW log_min_messages;
+ log_min_messages 
+------------------
+ fatal
+(1 row)
+
+SET log_min_messages TO 'fatal';
+SHOW log_min_messages;
+ log_min_messages 
+------------------
+ fatal
+(1 row)
+
+SET log_min_messages TO 'checkpointer:debug2, autovacuum:debug1';  --fail
+ERROR:  invalid value for parameter "log_min_messages": "checkpointer:debug2, autovacuum:debug1"
+DETAIL:  Generic log level was not defined.
+SET log_min_messages TO 'debug1, backend:error, fatal';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "debug1, backend:error, fatal"
+DETAIL:  Generic log level was already assigned.
+SET log_min_messages TO 'backend:error, debug1, backend:warning';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, debug1, backend:warning"
+DETAIL:  Backend type "backend" was already assigned.
+SET log_min_messages TO 'backend:error, foo:fatal, archiver:debug1';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, foo:fatal, archiver:debug1"
+DETAIL:  Unrecognized backend type: "foo".
+SET log_min_messages TO 'backend:error, checkpointer:bar, archiver:debug1';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, checkpointer:bar, archiver:debug1"
+DETAIL:  Unrecognized log level: "bar".
+SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3';
+SHOW log_min_messages;
+                                        log_min_messages                                         
+-------------------------------------------------------------------------------------------------
+ backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3
+(1 row)
+
+SET log_min_messages TO 'warning, autovacuum:debug1';
+SHOW log_min_messages;
+      log_min_messages      
+----------------------------
+ warning, autovacuum:debug1
+(1 row)
+
+SET log_min_messages TO 'autovacuum:debug1, warning';
+SHOW log_min_messages;
+      log_min_messages      
+----------------------------
+ autovacuum:debug1, warning
+(1 row)
+
diff --git a/src/test/regress/sql/guc.sql b/src/test/regress/sql/guc.sql
index f65f84a2632..12fa1812f35 100644
--- a/src/test/regress/sql/guc.sql
+++ b/src/test/regress/sql/guc.sql
@@ -368,3 +368,20 @@ SELECT name FROM tab_settings_flags
   WHERE no_reset AND NOT no_reset_all
   ORDER BY 1;
 DROP TABLE tab_settings_flags;
+
+-- Test log_min_messages
+SET log_min_messages TO fatal;
+SHOW log_min_messages;
+SET log_min_messages TO 'fatal';
+SHOW log_min_messages;
+SET log_min_messages TO 'checkpointer:debug2, autovacuum:debug1';  --fail
+SET log_min_messages TO 'debug1, backend:error, fatal';  -- fail
+SET log_min_messages TO 'backend:error, debug1, backend:warning';  -- fail
+SET log_min_messages TO 'backend:error, foo:fatal, archiver:debug1';  -- fail
+SET log_min_messages TO 'backend:error, checkpointer:bar, archiver:debug1';  -- fail
+SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3';
+SHOW log_min_messages;
+SET log_min_messages TO 'warning, autovacuum:debug1';
+SHOW log_min_messages;
+SET log_min_messages TO 'autovacuum:debug1, warning';
+SHOW log_min_messages;
-- 
2.39.5



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-07-31 16:22 ` Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  1 sibling, 1 reply; 20+ messages in thread

From: Japin Li @ 2025-07-31 16:22 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: Andres Freund <[email protected]>; Alvaro Herrera <[email protected]>; [email protected]

On Thu, 31 Jul 2025 at 11:19, "Euler Taveira" <[email protected]> wrote:
> On Thu, Mar 6, 2025, at 10:33 AM, Andres Freund wrote:
>> Huh, the startup process is among the most crucial things to monitor?
>>
>
> Good point. Fixed.
>
> After collecting some suggestions, I'm attaching a new patch contains the
> following changes:
>
> - patch was rebased
> - include Alvaro's patch (v2-0001) [1] as a basis for this patch
> - add ioworker as new backend type
> - add startup as new backend type per Andres suggestion
> - small changes into documentation
>
>> I don't know what I think about the whole patch, but I do want to voice
>> *strong* opposition to duplicating a list of all backend types into multiple
>> places. It's already painfull enough to add a new backend type, without having
>> to pointlessly go around and manually add a new backend type to mulltiple
>> arrays that have completely predictable content.
>>
>
> I'm including Alvaro's patch as is just to make the CF bot happy and to
> illustrate how it would be if we adopt his solution to centralize the list of
> backend types. I think Alvaro's proposal overcomes the objection [2], right?
>

If we set the log level for all backend types, I don't think a generic log
level is necessary.

-- 
Regards,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
@ 2025-07-31 16:51   ` Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Alvaro Herrera @ 2025-07-31 16:51 UTC (permalink / raw)
  To: Japin Li <[email protected]>; +Cc: Euler Taveira <[email protected]>; Andres Freund <[email protected]>; [email protected]

On 2025-Aug-01, Japin Li wrote:

> If we set the log level for all backend types, I don't think a generic log
> level is necessary.

I don't understand what you mean by this.  Can you elaborate?

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"If it is not right, do not do it.
If it is not true, do not say it." (Marcus Aurelius, Meditations)





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
@ 2025-07-31 23:34     ` Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Japin Li @ 2025-07-31 23:34 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Euler Taveira <[email protected]>; Andres Freund <[email protected]>; [email protected]

On Thu, 31 Jul 2025 at 18:51, Alvaro Herrera <[email protected]> wrote:
> On 2025-Aug-01, Japin Li wrote:
>
>> If we set the log level for all backend types, I don't think a generic log
>> level is necessary.
>
> I don't understand what you mean by this.  Can you elaborate?
>
For example:

ALTER SYSTEM SET log_min_messages TO
'archiver:info, autovacuum:info, backend:info, bgworker:info, bgwriter:info, checkpointer:info, ioworker:info, logger:info, slotsyncworker:info, startup:info, walreceiver:info, walsender:info, walsummarizer:info, walwriter:info';

Given that we've set a log level for every backend type and
assigned[BACKEND_NUM_TYPES] is true, a generic level seems redundant.

-- 
Regards,
Japin Li
ChengDu WenWu Information Technology Co., Ltd.





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
@ 2025-10-05 14:18       ` Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Euler Taveira @ 2025-10-05 14:18 UTC (permalink / raw)
  To: japin <[email protected]>; Alvaro Herrera <[email protected]>; +Cc: Andres Freund <[email protected]>; [email protected]

On Thu, Jul 31, 2025, at 8:34 PM, Japin Li wrote:
> On Thu, 31 Jul 2025 at 18:51, Alvaro Herrera <[email protected]> wrote:
>> On 2025-Aug-01, Japin Li wrote:
>>
>>> If we set the log level for all backend types, I don't think a generic log
>>> level is necessary.
>>
>> I don't understand what you mean by this.  Can you elaborate?
>>
> For example:
>
> ALTER SYSTEM SET log_min_messages TO
> 'archiver:info, autovacuum:info, backend:info, bgworker:info, 
> bgwriter:info, checkpointer:info, ioworker:info, logger:info, 
> slotsyncworker:info, startup:info, walreceiver:info, walsender:info, 
> walsummarizer:info, walwriter:info';
>
> Given that we've set a log level for every backend type and
> assigned[BACKEND_NUM_TYPES] is true, a generic level seems redundant.
>

The main reason I didn't consider this idea was that this GUC assignment will
fail as soon as a new backend type is added. Restore a dump should work
across major versions.

> I think we can avoid memory allocation by using pg_strncasecmp().
>

loglevel does not required a memory allocation but I didn't include the btype
part because it complicates the error message that uses btype a few lines
above.

This new patch contains the following changes:

- patch was rebased
- use commit dbf8cfb4f02
- use some GUC memory allocation functions
- avoid one memory allocation (suggested by Japin Li)
- rename backend type: logger -> syslogger
- adjust tests to increase test coverage
- improve documentation and comments to reflect the current state


-- 
Euler Taveira
EDB   https://www.enterprisedb.com/

Attachments:

  [text/x-patch] v4-0001-log_min_messages-per-backend-type.patch (26.0K, ../../[email protected]/2-v4-0001-log_min_messages-per-backend-type.patch)
  download | inline diff:
From 37a99059c8eaed5bece5d56530892edf348d2e67 Mon Sep 17 00:00:00 2001
From: Euler Taveira <[email protected]>
Date: Tue, 30 Sep 2025 10:59:25 -0300
Subject: [PATCH v4] log_min_messages per backend type

Change log_min_messages from a single element to a comma-separated list
of elements. Each element is backendtype:loglevel. The backendtype are
archiver, autovacuum (includes launcher and workers), backend, bgworker,
bgwriter, checkpointer, ioworker, syslogger, slotsyncworker, startup,
walreceiver, walsender, walsummarizer and walwriter. A single log level
should be part of this list and it is applied as a final step to the
backend types that were not informed.

The old syntax (a single log level) is still accepted for backward
compatibility. In this case, it means the informed log level is applied
for all backend types. The default log level is the same (WARNING) for
all backend types.
---
 doc/src/sgml/config.sgml                      |  31 ++-
 src/backend/commands/extension.c              |   2 +-
 src/backend/commands/variable.c               | 205 ++++++++++++++++++
 src/backend/postmaster/launch_backend.c       |   2 +-
 src/backend/utils/error/elog.c                |   2 +-
 src/backend/utils/init/miscinit.c             |   2 +-
 src/backend/utils/misc/guc_parameters.dat     |  18 +-
 src/backend/utils/misc/guc_tables.c           |  25 ++-
 src/backend/utils/misc/postgresql.conf.sample |   2 +
 src/include/postmaster/proctypelist.h         |  38 ++--
 src/include/utils/guc.h                       |   2 +-
 src/include/utils/guc_hooks.h                 |   2 +
 src/include/utils/guc_tables.h                |   4 +
 src/test/regress/expected/guc.out             |  54 +++++
 src/test/regress/sql/guc.sql                  |  18 ++
 15 files changed, 364 insertions(+), 43 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index e9b420f3ddb..b31d5875838 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -7022,7 +7022,7 @@ local0.*    /var/log/postgresql
      <variablelist>
 
      <varlistentry id="guc-log-min-messages" xreflabel="log_min_messages">
-      <term><varname>log_min_messages</varname> (<type>enum</type>)
+      <term><varname>log_min_messages</varname> (<type>string</type>)
       <indexterm>
        <primary><varname>log_min_messages</varname> configuration parameter</primary>
       </indexterm>
@@ -7031,14 +7031,27 @@ local0.*    /var/log/postgresql
        <para>
         Controls which <link linkend="runtime-config-severity-levels">message
         levels</link> are written to the server log.
-        Valid values are <literal>DEBUG5</literal>, <literal>DEBUG4</literal>,
-        <literal>DEBUG3</literal>, <literal>DEBUG2</literal>, <literal>DEBUG1</literal>,
-        <literal>INFO</literal>, <literal>NOTICE</literal>, <literal>WARNING</literal>,
-        <literal>ERROR</literal>, <literal>LOG</literal>, <literal>FATAL</literal>, and
-        <literal>PANIC</literal>.  Each level includes all the levels that
-        follow it.  The later the level, the fewer messages are sent
-        to the log.  The default is <literal>WARNING</literal>.  Note that
-        <literal>LOG</literal> has a different rank here than in
+        Valid values are a comma-separated list of <literal>backendtype:level</literal>
+        and a single <literal>level</literal>. The list allows it to use
+        different levels per backend type. Only the single <literal>level</literal>
+        is mandatory (order does not matter) and it is assigned to the backend
+        types that are not specified in the list.
+        Valid <literal>backendtype</literal> values are <literal>archiver</literal>,
+        <literal>autovacuum</literal>, <literal>backend</literal>,
+        <literal>bgworker</literal>, <literal>bgwriter</literal>,
+        <literal>checkpointer</literal>, <literal>ioworker</literal>,
+        <literal>syslogger</literal>, <literal>slotsyncworker</literal>,
+        <literal>startup</literal>, <literal>walreceiver</literal>,
+        <literal>walsender</literal>, <literal>walsummarizer</literal>, and
+        <literal>walwriter</literal>.
+        Valid <literal>level</literal> values are <literal>DEBUG5</literal>,
+        <literal>DEBUG4</literal>, <literal>DEBUG3</literal>, <literal>DEBUG2</literal>,
+        <literal>DEBUG1</literal>, <literal>INFO</literal>, <literal>NOTICE</literal>,
+        <literal>WARNING</literal>, <literal>ERROR</literal>, <literal>LOG</literal>,
+        <literal>FATAL</literal>, and <literal>PANIC</literal>.  Each level includes
+        all the levels that follow it.  The later the level, the fewer messages are sent
+        to the log.  The default is <literal>WARNING</literal> for all backend types.
+        Note that <literal>LOG</literal> has a different rank here than in
         <xref linkend="guc-client-min-messages"/>.
         Only superusers and users with the appropriate <literal>SET</literal>
         privilege can change this setting.
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 93ef1ad106f..81f7bc5e567 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -1143,7 +1143,7 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
 		(void) set_config_option("client_min_messages", "warning",
 								 PGC_USERSET, PGC_S_SESSION,
 								 GUC_ACTION_SAVE, true, 0, false);
-	if (log_min_messages < WARNING)
+	if (log_min_messages[MyBackendType] < WARNING)
 		(void) set_config_option_ext("log_min_messages", "warning",
 									 PGC_SUSET, PGC_S_SESSION,
 									 BOOTSTRAP_SUPERUSERID,
diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 608f10d9412..93336fee33b 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -35,6 +35,7 @@
 #include "utils/datetime.h"
 #include "utils/fmgrprotos.h"
 #include "utils/guc_hooks.h"
+#include "utils/guc_tables.h"
 #include "utils/snapmgr.h"
 #include "utils/syscache.h"
 #include "utils/timestamp.h"
@@ -1257,3 +1258,207 @@ check_ssl(bool *newval, void **extra, GucSource source)
 #endif
 	return true;
 }
+
+/*
+ * GUC check_hook for log_min_messages
+ *
+ * The parsing consists of a comma-separated list of BACKENDTYPENAME:LEVEL
+ * elements. BACKENDTYPENAME is log_min_messages_backend_types.  LEVEL is
+ * server_message_level_options. A single LEVEL element should be part of this
+ * list and it is applied as a final step to the backend types that are not
+ * specified. For backward compatibility, the old syntax is still accepted and
+ * it means to apply the LEVEL for all backend types.
+ */
+bool
+check_log_min_messages(char **newval, void **extra, GucSource source)
+{
+	char	   *rawstring;
+	List	   *elemlist;
+	ListCell   *l;
+	int			newlevel[BACKEND_NUM_TYPES];
+	bool		assigned[BACKEND_NUM_TYPES];
+	int			genericlevel = -1;	/* -1 means not assigned */
+
+	/* Initialize the array. */
+	memset(newlevel, WARNING, BACKEND_NUM_TYPES * sizeof(int));
+	memset(assigned, false, BACKEND_NUM_TYPES * sizeof(bool));
+
+	/* Need a modifiable copy of string. */
+	rawstring = guc_strdup(LOG, *newval);
+
+	/* Parse string into list of identifiers. */
+	if (!SplitGUCList(rawstring, ',', &elemlist))
+	{
+		/* syntax error in list */
+		GUC_check_errdetail("List syntax is invalid.");
+		guc_free(rawstring);
+		list_free(elemlist);
+		return false;
+	}
+
+	/* Validate and assign log level and backend type. */
+	foreach(l, elemlist)
+	{
+		char	   *tok = (char *) lfirst(l);
+		char	   *sep;
+		const struct config_enum_entry *entry;
+
+		/*
+		 * Check whether there is a backend type following the log level. If
+		 * there is no separator, it means this is the generic log level. The
+		 * generic log level will be assigned to the backend types that were
+		 * not informed.
+		 */
+		sep = strchr(tok, ':');
+		if (sep == NULL)
+		{
+			bool		found = false;
+
+			/* Reject duplicates for generic log level. */
+			if (genericlevel != -1)
+			{
+				GUC_check_errdetail("Generic log level was already assigned.");
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, tok) == 0)
+				{
+					genericlevel = entry->val;
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", tok);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+		}
+		else
+		{
+			char	   *loglevel;
+			char	   *btype;
+			bool		found = false;
+
+			btype = guc_malloc(LOG, (sep - tok) + 1);
+			if (!btype)
+			{
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+			memcpy(btype, tok, sep - tok);
+			btype[sep - tok] = '\0';
+			loglevel = sep + 1;
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, loglevel) == 0)
+				{
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", loglevel);
+				guc_free(btype);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/*
+			 * Is the backend type name valid? There might be multiple entries
+			 * per backend type, don't bail out because it can assign the
+			 * value for multiple entries.
+			 */
+			found = false;
+			for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+			{
+				if (pg_strcasecmp(log_min_messages_backend_types[i], btype) == 0)
+				{
+					/* Reject duplicates for a backend type. */
+					if (assigned[i])
+					{
+						GUC_check_errdetail("Backend type \"%s\" was already assigned.", btype);
+						guc_free(btype);
+						guc_free(rawstring);
+						list_free(elemlist);
+						return false;
+					}
+
+					newlevel[i] = entry->val;
+					assigned[i] = true;
+					found = true;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized backend type: \"%s\".", btype);
+				guc_free(btype);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			guc_free(btype);
+		}
+	}
+
+	/*
+	 * The generic log level must be specified. It is the fallback value.
+	 */
+	if (genericlevel == -1)
+	{
+		GUC_check_errdetail("Generic log level was not defined.");
+		guc_free(rawstring);
+		list_free(elemlist);
+		return false;
+	}
+
+	/*
+	 * Apply the generic log level after all of the specific backend type have
+	 * been assigned. Hence, it doesn't matter the order you specify the
+	 * generic log level, the final result will be the same.
+	 */
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+	{
+		if (!assigned[i])
+			newlevel[i] = genericlevel;
+	}
+
+	guc_free(rawstring);
+	list_free(elemlist);
+
+	/*
+	 * Pass back data for assign_log_min_messages to use.
+	 */
+	*extra = guc_malloc(LOG, BACKEND_NUM_TYPES * sizeof(int));
+	if (!*extra)
+		return false;
+	memcpy(*extra, newlevel, BACKEND_NUM_TYPES * sizeof(int));
+
+	return true;
+}
+
+/*
+ * GUC assign_hook for log_min_messages
+ */
+void
+assign_log_min_messages(const char *newval, void *extra)
+{
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+		log_min_messages[i] = ((int *) extra)[i];
+}
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index 976638a58ac..c2a044321d1 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -179,7 +179,7 @@ typedef struct
 } child_process_kind;
 
 static child_process_kind child_process_kinds[] = {
-#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
 	[bktype] = {description, main_func, shmem_attach},
 #include "postmaster/proctypelist.h"
 #undef PG_PROCTYPE
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index b7b9692f8c8..616dd0b63d7 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -235,7 +235,7 @@ is_log_level_output(int elevel, int log_min_level)
 static inline bool
 should_output_to_server(int elevel)
 {
-	return is_log_level_output(elevel, log_min_messages);
+	return is_log_level_output(elevel, log_min_messages[MyBackendType]);
 }
 
 /*
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index fec79992c8d..2c3926cbadf 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -266,7 +266,7 @@ GetBackendTypeDesc(BackendType backendType)
 
 	switch (backendType)
 	{
-#define PG_PROCTYPE(bktype, description, main_func, shmem_attach)	\
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages)	\
 		case bktype: backendDesc = description; break;
 #include "postmaster/proctypelist.h"
 #undef PG_PROCTYPE
diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat
index 6bc6be13d2a..e7eb8ab2baf 100644
--- a/src/backend/utils/misc/guc_parameters.dat
+++ b/src/backend/utils/misc/guc_parameters.dat
@@ -2683,6 +2683,16 @@
   boot_val => '"%m [%p] "',
 },
 
+{ name => 'log_min_messages', type => 'string', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
+  short_desc => 'Sets the message levels that are logged.',
+  long_desc => 'Each level includes all the levels that follow it. The later the level, the fewer messages are sent.',
+  flags => 'GUC_LIST_INPUT',
+  variable => 'log_min_messages_string',
+  boot_val => '"WARNING"',
+  check_hook => 'check_log_min_messages',
+  assign_hook => 'assign_log_min_messages',
+},
+
 { name => 'log_timezone', type => 'string', context => 'PGC_SIGHUP', group => 'LOGGING_WHAT',
   short_desc => 'Sets the time zone to use in log messages.',
   variable => 'log_timezone_string',
@@ -3252,14 +3262,6 @@
   options => 'log_error_verbosity_options',
 },
 
-{ name => 'log_min_messages', type => 'enum', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
-  short_desc => 'Sets the message levels that are logged.',
-  long_desc => 'Each level includes all the levels that follow it. The later the level, the fewer messages are sent.',
-  variable => 'log_min_messages',
-  boot_val => 'WARNING',
-  options => 'server_message_level_options',
-},
-
 { name => 'log_min_error_statement', type => 'enum', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
   short_desc => 'Causes all statements generating error at or above this level to be logged.',
   long_desc => 'Each level includes all the levels that follow it. The later the level, the fewer messages are sent.',
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 00c8376cf4d..52b3a902e61 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -146,7 +146,7 @@ static const struct config_enum_entry client_message_level_options[] = {
 	{NULL, 0, false}
 };
 
-static const struct config_enum_entry server_message_level_options[] = {
+const struct config_enum_entry server_message_level_options[] = {
 	{"debug5", DEBUG5, false},
 	{"debug4", DEBUG4, false},
 	{"debug3", DEBUG3, false},
@@ -537,7 +537,6 @@ static bool default_with_oids = false;
 bool		current_role_is_superuser;
 
 int			log_min_error_statement = ERROR;
-int			log_min_messages = WARNING;
 int			client_min_messages = NOTICE;
 int			log_min_duration_sample = -1;
 int			log_min_duration_statement = -1;
@@ -595,6 +594,7 @@ static char *server_version_string;
 static int	server_version_num;
 static char *debug_io_direct_string;
 static char *restrict_nonsystem_relation_kind_string;
+static char *log_min_messages_string;
 
 #ifdef HAVE_SYSLOG
 #define	DEFAULT_SYSLOG_FACILITY LOG_LOCAL0
@@ -639,6 +639,27 @@ char	   *role_string;
 /* should be static, but guc.c needs to get at this */
 bool		in_hot_standby_guc;
 
+/*
+ * It should be static, but commands/variable.c needs to get at this.
+ */
+int			log_min_messages[] = {
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
+	[bktype] = log_min_messages,
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
+};
+
+/*
+ * It might be in commands/variable.c but for convenience it is near
+ * log_min_messages.
+ */
+const char *const log_min_messages_backend_types[] = {
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
+	[bktype] = bkcategory,
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
+};
+
 
 /*
  * Displayable names for context types (enum GucContext)
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index c36fcb9ab61..53ee39098b9 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -539,6 +539,8 @@
 					#   log
 					#   fatal
 					#   panic
+					#   and an optional comma-separated list
+					#   of backendtype:level
 
 #log_min_error_statement = error	# values in order of decreasing detail:
 					#   debug5
diff --git a/src/include/postmaster/proctypelist.h b/src/include/postmaster/proctypelist.h
index 242862451d8..f18e3342285 100644
--- a/src/include/postmaster/proctypelist.h
+++ b/src/include/postmaster/proctypelist.h
@@ -30,22 +30,22 @@
  */
 
 
-/* bktype, description, main_func, shmem_attach */
-PG_PROCTYPE(B_ARCHIVER, gettext_noop("archiver"), PgArchiverMain, true)
-PG_PROCTYPE(B_AUTOVAC_LAUNCHER, gettext_noop("autovacuum launcher"), AutoVacLauncherMain, true)
-PG_PROCTYPE(B_AUTOVAC_WORKER, gettext_noop("autovacuum worker"), AutoVacWorkerMain, true)
-PG_PROCTYPE(B_BACKEND, gettext_noop("client backend"), BackendMain, true)
-PG_PROCTYPE(B_BG_WORKER, gettext_noop("background worker"), BackgroundWorkerMain, true)
-PG_PROCTYPE(B_BG_WRITER, gettext_noop("background writer"), BackgroundWriterMain, true)
-PG_PROCTYPE(B_CHECKPOINTER, gettext_noop("checkpointer"), CheckpointerMain, true)
-PG_PROCTYPE(B_DEAD_END_BACKEND, gettext_noop("dead-end client backend"), BackendMain, true)
-PG_PROCTYPE(B_INVALID, gettext_noop("unrecognized"), NULL, false)
-PG_PROCTYPE(B_IO_WORKER, gettext_noop("io worker"), IoWorkerMain, true)
-PG_PROCTYPE(B_LOGGER, gettext_noop("syslogger"), SysLoggerMain, false)
-PG_PROCTYPE(B_SLOTSYNC_WORKER, gettext_noop("slotsync worker"), ReplSlotSyncWorkerMain, true)
-PG_PROCTYPE(B_STANDALONE_BACKEND, gettext_noop("standalone backend"), NULL, false)
-PG_PROCTYPE(B_STARTUP, gettext_noop("startup"), StartupProcessMain, true)
-PG_PROCTYPE(B_WAL_RECEIVER, gettext_noop("walreceiver"), WalReceiverMain, true)
-PG_PROCTYPE(B_WAL_SENDER, gettext_noop("walsender"), NULL, true)
-PG_PROCTYPE(B_WAL_SUMMARIZER, gettext_noop("walsummarizer"), WalSummarizerMain, true)
-PG_PROCTYPE(B_WAL_WRITER, gettext_noop("walwriter"), WalWriterMain, true)
+/* bktype, bkcategory, description, main_func, shmem_attach, log_min_messages */
+PG_PROCTYPE(B_ARCHIVER, "archiver", gettext_noop("archiver"), PgArchiverMain, true, WARNING)
+PG_PROCTYPE(B_AUTOVAC_LAUNCHER, "autovacuum", gettext_noop("autovacuum launcher"), AutoVacLauncherMain, true, WARNING)
+PG_PROCTYPE(B_AUTOVAC_WORKER, "autovacuum", gettext_noop("autovacuum worker"), AutoVacWorkerMain, true, WARNING)
+PG_PROCTYPE(B_BACKEND, "backend", gettext_noop("client backend"), BackendMain, true, WARNING)
+PG_PROCTYPE(B_BG_WORKER, "bgworker", gettext_noop("background worker"), BackgroundWorkerMain, true, WARNING)
+PG_PROCTYPE(B_BG_WRITER, "bgwriter", gettext_noop("background writer"), BackgroundWriterMain, true, WARNING)
+PG_PROCTYPE(B_CHECKPOINTER, "checkpointer", gettext_noop("checkpointer"), CheckpointerMain, true, WARNING)
+PG_PROCTYPE(B_DEAD_END_BACKEND, "backend", gettext_noop("dead-end client backend"), BackendMain, true, WARNING)
+PG_PROCTYPE(B_INVALID, "backend", gettext_noop("unrecognized"), NULL, false, WARNING)
+PG_PROCTYPE(B_IO_WORKER, "ioworker", gettext_noop("io worker"), IoWorkerMain, true, WARNING)
+PG_PROCTYPE(B_LOGGER, "syslogger", gettext_noop("syslogger"), SysLoggerMain, false, WARNING)
+PG_PROCTYPE(B_SLOTSYNC_WORKER, "slotsyncworker", gettext_noop("slotsync worker"), ReplSlotSyncWorkerMain, true, WARNING)
+PG_PROCTYPE(B_STANDALONE_BACKEND, "backend", gettext_noop("standalone backend"), NULL, false, WARNING)
+PG_PROCTYPE(B_STARTUP, "startup", gettext_noop("startup"), StartupProcessMain, true, WARNING)
+PG_PROCTYPE(B_WAL_RECEIVER, "walreceiver", gettext_noop("walreceiver"), WalReceiverMain, true, WARNING)
+PG_PROCTYPE(B_WAL_SENDER, "walsender", gettext_noop("walsender"), NULL, true, WARNING)
+PG_PROCTYPE(B_WAL_SUMMARIZER, "walsummarizer", gettext_noop("walsummarizer"), WalSummarizerMain, true, WARNING)
+PG_PROCTYPE(B_WAL_WRITER, "walwriter", gettext_noop("walwriter"), WalWriterMain, true, WARNING)
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index f21ec37da89..d68dd350936 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -295,7 +295,7 @@ extern PGDLLIMPORT bool log_duration;
 extern PGDLLIMPORT int log_parameter_max_length;
 extern PGDLLIMPORT int log_parameter_max_length_on_error;
 extern PGDLLIMPORT int log_min_error_statement;
-extern PGDLLIMPORT int log_min_messages;
+extern PGDLLIMPORT int log_min_messages[];
 extern PGDLLIMPORT int client_min_messages;
 extern PGDLLIMPORT int log_min_duration_sample;
 extern PGDLLIMPORT int log_min_duration_statement;
diff --git a/src/include/utils/guc_hooks.h b/src/include/utils/guc_hooks.h
index 82ac8646a8d..e6ffc96468f 100644
--- a/src/include/utils/guc_hooks.h
+++ b/src/include/utils/guc_hooks.h
@@ -174,5 +174,7 @@ extern void assign_wal_sync_method(int new_wal_sync_method, void *extra);
 extern bool check_synchronized_standby_slots(char **newval, void **extra,
 											 GucSource source);
 extern void assign_synchronized_standby_slots(const char *newval, void *extra);
+extern bool check_log_min_messages(char **newval, void **extra, GucSource source);
+extern void assign_log_min_messages(const char *newval, void *extra);
 
 #endif							/* GUC_HOOKS_H */
diff --git a/src/include/utils/guc_tables.h b/src/include/utils/guc_tables.h
index f72ce944d7f..2aefc653f20 100644
--- a/src/include/utils/guc_tables.h
+++ b/src/include/utils/guc_tables.h
@@ -298,6 +298,10 @@ struct config_enum
 	void	   *reset_extra;
 };
 
+/* log_min_messages */
+extern PGDLLIMPORT const char *const log_min_messages_backend_types[];
+extern PGDLLIMPORT const struct config_enum_entry server_message_level_options[];
+
 /* constant tables corresponding to enums above and in guc.h */
 extern PGDLLIMPORT const char *const config_group_names[];
 extern PGDLLIMPORT const char *const config_type_names[];
diff --git a/src/test/regress/expected/guc.out b/src/test/regress/expected/guc.out
index 7f9e29c765c..be25e81140a 100644
--- a/src/test/regress/expected/guc.out
+++ b/src/test/regress/expected/guc.out
@@ -913,3 +913,57 @@ SELECT name FROM tab_settings_flags
 (0 rows)
 
 DROP TABLE tab_settings_flags;
+-- Test log_min_messages
+SET log_min_messages TO foo;  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "foo"
+DETAIL:  Unrecognized log level: "foo".
+SET log_min_messages TO fatal;
+SHOW log_min_messages;
+ log_min_messages 
+------------------
+ fatal
+(1 row)
+
+SET log_min_messages TO 'fatal';
+SHOW log_min_messages;
+ log_min_messages 
+------------------
+ fatal
+(1 row)
+
+SET log_min_messages TO 'checkpointer:debug2, autovacuum:debug1';  --fail
+ERROR:  invalid value for parameter "log_min_messages": "checkpointer:debug2, autovacuum:debug1"
+DETAIL:  Generic log level was not defined.
+SET log_min_messages TO 'debug1, backend:error, fatal';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "debug1, backend:error, fatal"
+DETAIL:  Generic log level was already assigned.
+SET log_min_messages TO 'backend:error, debug1, backend:warning';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, debug1, backend:warning"
+DETAIL:  Backend type "backend" was already assigned.
+SET log_min_messages TO 'backend:error, foo:fatal, archiver:debug1';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, foo:fatal, archiver:debug1"
+DETAIL:  Unrecognized backend type: "foo".
+SET log_min_messages TO 'backend:error, checkpointer:bar, archiver:debug1';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, checkpointer:bar, archiver:debug1"
+DETAIL:  Unrecognized log level: "bar".
+SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3';
+SHOW log_min_messages;
+                                        log_min_messages                                         
+-------------------------------------------------------------------------------------------------
+ backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3
+(1 row)
+
+SET log_min_messages TO 'warning, autovacuum:debug1';
+SHOW log_min_messages;
+      log_min_messages      
+----------------------------
+ warning, autovacuum:debug1
+(1 row)
+
+SET log_min_messages TO 'autovacuum:debug1, warning';
+SHOW log_min_messages;
+      log_min_messages      
+----------------------------
+ autovacuum:debug1, warning
+(1 row)
+
diff --git a/src/test/regress/sql/guc.sql b/src/test/regress/sql/guc.sql
index f65f84a2632..f44c72c532e 100644
--- a/src/test/regress/sql/guc.sql
+++ b/src/test/regress/sql/guc.sql
@@ -368,3 +368,21 @@ SELECT name FROM tab_settings_flags
   WHERE no_reset AND NOT no_reset_all
   ORDER BY 1;
 DROP TABLE tab_settings_flags;
+
+-- Test log_min_messages
+SET log_min_messages TO foo;  -- fail
+SET log_min_messages TO fatal;
+SHOW log_min_messages;
+SET log_min_messages TO 'fatal';
+SHOW log_min_messages;
+SET log_min_messages TO 'checkpointer:debug2, autovacuum:debug1';  --fail
+SET log_min_messages TO 'debug1, backend:error, fatal';  -- fail
+SET log_min_messages TO 'backend:error, debug1, backend:warning';  -- fail
+SET log_min_messages TO 'backend:error, foo:fatal, archiver:debug1';  -- fail
+SET log_min_messages TO 'backend:error, checkpointer:bar, archiver:debug1';  -- fail
+SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3';
+SHOW log_min_messages;
+SET log_min_messages TO 'warning, autovacuum:debug1';
+SHOW log_min_messages;
+SET log_min_messages TO 'autovacuum:debug1, warning';
+SHOW log_min_messages;
-- 
2.39.5



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-11-06 13:09         ` Euler Taveira <[email protected]>
  2025-11-06 14:53           ` Re: log_min_messages per backend type Chao Li <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  0 siblings, 2 replies; 20+ messages in thread

From: Euler Taveira @ 2025-11-06 13:09 UTC (permalink / raw)
  To: japin <[email protected]>; Alvaro Herrera <[email protected]>; +Cc: Andres Freund <[email protected]>; [email protected]

On Sun, Oct 5, 2025, at 11:18 AM, Euler Taveira wrote:
> This new patch contains the following changes:
>
> - patch was rebased
> - use commit dbf8cfb4f02
> - use some GUC memory allocation functions
> - avoid one memory allocation (suggested by Japin Li)
> - rename backend type: logger -> syslogger
> - adjust tests to increase test coverage
> - improve documentation and comments to reflect the current state
>

Patch was rebased since commit fce7c73fba4 broke it. No modifications.


-- 
Euler Taveira
EDB   https://www.enterprisedb.com/

Attachments:

  [text/x-patch] v5-0001-log_min_messages-per-backend-type.patch (25.4K, ../../[email protected]/2-v5-0001-log_min_messages-per-backend-type.patch)
  download | inline diff:
From e457cc99c20f54d8e7e998104402e849ecf83c14 Mon Sep 17 00:00:00 2001
From: Euler Taveira <[email protected]>
Date: Tue, 4 Nov 2025 17:18:20 -0300
Subject: [PATCH v5] log_min_messages per backend type

Change log_min_messages from a single element to a comma-separated list
of elements. Each element is backendtype:loglevel. The backendtype are
archiver, autovacuum (includes launcher and workers), backend, bgworker,
bgwriter, checkpointer, ioworker, syslogger, slotsyncworker, startup,
walreceiver, walsender, walsummarizer and walwriter. A single log level
should be part of this list and it is applied as a final step to the
backend types that were not informed.

The old syntax (a single log level) is still accepted for backward
compatibility. In this case, it means the informed log level is applied
for all backend types. The default log level is the same (WARNING) for
all backend types.
---
 doc/src/sgml/config.sgml                      |  31 ++-
 src/backend/commands/extension.c              |   2 +-
 src/backend/commands/variable.c               | 205 ++++++++++++++++++
 src/backend/postmaster/launch_backend.c       |   2 +-
 src/backend/utils/error/elog.c                |   2 +-
 src/backend/utils/init/miscinit.c             |   2 +-
 src/backend/utils/misc/guc_parameters.dat     |  10 +-
 src/backend/utils/misc/guc_tables.c           |  21 +-
 src/backend/utils/misc/postgresql.conf.sample |   2 +
 src/include/postmaster/proctypelist.h         |  38 ++--
 src/include/utils/guc.h                       |   6 +-
 src/include/utils/guc_hooks.h                 |   2 +
 src/test/regress/expected/guc.out             |  54 +++++
 src/test/regress/sql/guc.sql                  |  18 ++
 14 files changed, 356 insertions(+), 39 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index d8a9f14b618..2ac1e65aa60 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -7053,7 +7053,7 @@ local0.*    /var/log/postgresql
      <variablelist>
 
      <varlistentry id="guc-log-min-messages" xreflabel="log_min_messages">
-      <term><varname>log_min_messages</varname> (<type>enum</type>)
+      <term><varname>log_min_messages</varname> (<type>string</type>)
       <indexterm>
        <primary><varname>log_min_messages</varname> configuration parameter</primary>
       </indexterm>
@@ -7062,14 +7062,27 @@ local0.*    /var/log/postgresql
        <para>
         Controls which <link linkend="runtime-config-severity-levels">message
         levels</link> are written to the server log.
-        Valid values are <literal>DEBUG5</literal>, <literal>DEBUG4</literal>,
-        <literal>DEBUG3</literal>, <literal>DEBUG2</literal>, <literal>DEBUG1</literal>,
-        <literal>INFO</literal>, <literal>NOTICE</literal>, <literal>WARNING</literal>,
-        <literal>ERROR</literal>, <literal>LOG</literal>, <literal>FATAL</literal>, and
-        <literal>PANIC</literal>.  Each level includes all the levels that
-        follow it.  The later the level, the fewer messages are sent
-        to the log.  The default is <literal>WARNING</literal>.  Note that
-        <literal>LOG</literal> has a different rank here than in
+        Valid values are a comma-separated list of <literal>backendtype:level</literal>
+        and a single <literal>level</literal>. The list allows it to use
+        different levels per backend type. Only the single <literal>level</literal>
+        is mandatory (order does not matter) and it is assigned to the backend
+        types that are not specified in the list.
+        Valid <literal>backendtype</literal> values are <literal>archiver</literal>,
+        <literal>autovacuum</literal>, <literal>backend</literal>,
+        <literal>bgworker</literal>, <literal>bgwriter</literal>,
+        <literal>checkpointer</literal>, <literal>ioworker</literal>,
+        <literal>syslogger</literal>, <literal>slotsyncworker</literal>,
+        <literal>startup</literal>, <literal>walreceiver</literal>,
+        <literal>walsender</literal>, <literal>walsummarizer</literal>, and
+        <literal>walwriter</literal>.
+        Valid <literal>level</literal> values are <literal>DEBUG5</literal>,
+        <literal>DEBUG4</literal>, <literal>DEBUG3</literal>, <literal>DEBUG2</literal>,
+        <literal>DEBUG1</literal>, <literal>INFO</literal>, <literal>NOTICE</literal>,
+        <literal>WARNING</literal>, <literal>ERROR</literal>, <literal>LOG</literal>,
+        <literal>FATAL</literal>, and <literal>PANIC</literal>.  Each level includes
+        all the levels that follow it.  The later the level, the fewer messages are sent
+        to the log.  The default is <literal>WARNING</literal> for all backend types.
+        Note that <literal>LOG</literal> has a different rank here than in
         <xref linkend="guc-client-min-messages"/>.
         Only superusers and users with the appropriate <literal>SET</literal>
         privilege can change this setting.
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 93ef1ad106f..81f7bc5e567 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -1143,7 +1143,7 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
 		(void) set_config_option("client_min_messages", "warning",
 								 PGC_USERSET, PGC_S_SESSION,
 								 GUC_ACTION_SAVE, true, 0, false);
-	if (log_min_messages < WARNING)
+	if (log_min_messages[MyBackendType] < WARNING)
 		(void) set_config_option_ext("log_min_messages", "warning",
 									 PGC_SUSET, PGC_S_SESSION,
 									 BOOTSTRAP_SUPERUSERID,
diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 608f10d9412..c86bdf74f41 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -34,6 +34,7 @@
 #include "utils/backend_status.h"
 #include "utils/datetime.h"
 #include "utils/fmgrprotos.h"
+#include "utils/guc.h"
 #include "utils/guc_hooks.h"
 #include "utils/snapmgr.h"
 #include "utils/syscache.h"
@@ -1257,3 +1258,207 @@ check_ssl(bool *newval, void **extra, GucSource source)
 #endif
 	return true;
 }
+
+/*
+ * GUC check_hook for log_min_messages
+ *
+ * The parsing consists of a comma-separated list of BACKENDTYPENAME:LEVEL
+ * elements. BACKENDTYPENAME is log_min_messages_backend_types.  LEVEL is
+ * server_message_level_options. A single LEVEL element should be part of this
+ * list and it is applied as a final step to the backend types that are not
+ * specified. For backward compatibility, the old syntax is still accepted and
+ * it means to apply the LEVEL for all backend types.
+ */
+bool
+check_log_min_messages(char **newval, void **extra, GucSource source)
+{
+	char	   *rawstring;
+	List	   *elemlist;
+	ListCell   *l;
+	int			newlevel[BACKEND_NUM_TYPES];
+	bool		assigned[BACKEND_NUM_TYPES];
+	int			genericlevel = -1;	/* -1 means not assigned */
+
+	/* Initialize the array. */
+	memset(newlevel, WARNING, BACKEND_NUM_TYPES * sizeof(int));
+	memset(assigned, false, BACKEND_NUM_TYPES * sizeof(bool));
+
+	/* Need a modifiable copy of string. */
+	rawstring = guc_strdup(LOG, *newval);
+
+	/* Parse string into list of identifiers. */
+	if (!SplitGUCList(rawstring, ',', &elemlist))
+	{
+		/* syntax error in list */
+		GUC_check_errdetail("List syntax is invalid.");
+		guc_free(rawstring);
+		list_free(elemlist);
+		return false;
+	}
+
+	/* Validate and assign log level and backend type. */
+	foreach(l, elemlist)
+	{
+		char	   *tok = (char *) lfirst(l);
+		char	   *sep;
+		const struct config_enum_entry *entry;
+
+		/*
+		 * Check whether there is a backend type following the log level. If
+		 * there is no separator, it means this is the generic log level. The
+		 * generic log level will be assigned to the backend types that were
+		 * not informed.
+		 */
+		sep = strchr(tok, ':');
+		if (sep == NULL)
+		{
+			bool		found = false;
+
+			/* Reject duplicates for generic log level. */
+			if (genericlevel != -1)
+			{
+				GUC_check_errdetail("Generic log level was already assigned.");
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, tok) == 0)
+				{
+					genericlevel = entry->val;
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", tok);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+		}
+		else
+		{
+			char	   *loglevel;
+			char	   *btype;
+			bool		found = false;
+
+			btype = guc_malloc(LOG, (sep - tok) + 1);
+			if (!btype)
+			{
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+			memcpy(btype, tok, sep - tok);
+			btype[sep - tok] = '\0';
+			loglevel = sep + 1;
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, loglevel) == 0)
+				{
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", loglevel);
+				guc_free(btype);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/*
+			 * Is the backend type name valid? There might be multiple entries
+			 * per backend type, don't bail out because it can assign the
+			 * value for multiple entries.
+			 */
+			found = false;
+			for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+			{
+				if (pg_strcasecmp(log_min_messages_backend_types[i], btype) == 0)
+				{
+					/* Reject duplicates for a backend type. */
+					if (assigned[i])
+					{
+						GUC_check_errdetail("Backend type \"%s\" was already assigned.", btype);
+						guc_free(btype);
+						guc_free(rawstring);
+						list_free(elemlist);
+						return false;
+					}
+
+					newlevel[i] = entry->val;
+					assigned[i] = true;
+					found = true;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized backend type: \"%s\".", btype);
+				guc_free(btype);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			guc_free(btype);
+		}
+	}
+
+	/*
+	 * The generic log level must be specified. It is the fallback value.
+	 */
+	if (genericlevel == -1)
+	{
+		GUC_check_errdetail("Generic log level was not defined.");
+		guc_free(rawstring);
+		list_free(elemlist);
+		return false;
+	}
+
+	/*
+	 * Apply the generic log level after all of the specific backend type have
+	 * been assigned. Hence, it doesn't matter the order you specify the
+	 * generic log level, the final result will be the same.
+	 */
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+	{
+		if (!assigned[i])
+			newlevel[i] = genericlevel;
+	}
+
+	guc_free(rawstring);
+	list_free(elemlist);
+
+	/*
+	 * Pass back data for assign_log_min_messages to use.
+	 */
+	*extra = guc_malloc(LOG, BACKEND_NUM_TYPES * sizeof(int));
+	if (!*extra)
+		return false;
+	memcpy(*extra, newlevel, BACKEND_NUM_TYPES * sizeof(int));
+
+	return true;
+}
+
+/*
+ * GUC assign_hook for log_min_messages
+ */
+void
+assign_log_min_messages(const char *newval, void *extra)
+{
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+		log_min_messages[i] = ((int *) extra)[i];
+}
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index 976638a58ac..c2a044321d1 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -179,7 +179,7 @@ typedef struct
 } child_process_kind;
 
 static child_process_kind child_process_kinds[] = {
-#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
 	[bktype] = {description, main_func, shmem_attach},
 #include "postmaster/proctypelist.h"
 #undef PG_PROCTYPE
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 29643c51439..15d771a16c9 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -235,7 +235,7 @@ is_log_level_output(int elevel, int log_min_level)
 static inline bool
 should_output_to_server(int elevel)
 {
-	return is_log_level_output(elevel, log_min_messages);
+	return is_log_level_output(elevel, log_min_messages[MyBackendType]);
 }
 
 /*
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index fec79992c8d..2c3926cbadf 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -266,7 +266,7 @@ GetBackendTypeDesc(BackendType backendType)
 
 	switch (backendType)
 	{
-#define PG_PROCTYPE(bktype, description, main_func, shmem_attach)	\
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages)	\
 		case bktype: backendDesc = description; break;
 #include "postmaster/proctypelist.h"
 #undef PG_PROCTYPE
diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat
index 1128167c025..55dfc2e6548 100644
--- a/src/backend/utils/misc/guc_parameters.dat
+++ b/src/backend/utils/misc/guc_parameters.dat
@@ -1676,12 +1676,14 @@
   options => 'server_message_level_options',
 },
 
-{ name => 'log_min_messages', type => 'enum', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
+{ name => 'log_min_messages', type => 'string', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
   short_desc => 'Sets the message levels that are logged.',
   long_desc => 'Each level includes all the levels that follow it. The later the level, the fewer messages are sent.',
-  variable => 'log_min_messages',
-  boot_val => 'WARNING',
-  options => 'server_message_level_options',
+  flags => 'GUC_LIST_INPUT',
+  variable => 'log_min_messages_string',
+  boot_val => '"WARNING"',
+  check_hook => 'check_log_min_messages',
+  assign_hook => 'assign_log_min_messages',
 },
 
 { name => 'log_parameter_max_length', type => 'int', context => 'PGC_SUSET', group => 'LOGGING_WHAT',
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 0209b2067a2..8761b371de7 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -146,7 +146,7 @@ static const struct config_enum_entry client_message_level_options[] = {
 	{NULL, 0, false}
 };
 
-static const struct config_enum_entry server_message_level_options[] = {
+const struct config_enum_entry server_message_level_options[] = {
 	{"debug5", DEBUG5, false},
 	{"debug4", DEBUG4, false},
 	{"debug3", DEBUG3, false},
@@ -537,7 +537,6 @@ static bool default_with_oids = false;
 bool		current_role_is_superuser;
 
 int			log_min_error_statement = ERROR;
-int			log_min_messages = WARNING;
 int			client_min_messages = NOTICE;
 int			log_min_duration_sample = -1;
 int			log_min_duration_statement = -1;
@@ -595,6 +594,7 @@ static char *server_version_string;
 static int	server_version_num;
 static char *debug_io_direct_string;
 static char *restrict_nonsystem_relation_kind_string;
+static char *log_min_messages_string;
 
 #ifdef HAVE_SYSLOG
 #define	DEFAULT_SYSLOG_FACILITY LOG_LOCAL0
@@ -639,6 +639,23 @@ char	   *role_string;
 /* should be static, but guc.c needs to get at this */
 bool		in_hot_standby_guc;
 
+/*
+ * log_min_messages
+ */
+int			log_min_messages[] = {
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
+	[bktype] = log_min_messages,
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
+};
+
+const char *const log_min_messages_backend_types[] = {
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
+	[bktype] = bkcategory,
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
+};
+
 
 /*
  * Displayable names for context types (enum GucContext)
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index f62b61967ef..fde1054ae0f 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -541,6 +541,8 @@
 					#   log
 					#   fatal
 					#   panic
+					#   and an optional comma-separated list
+					#   of backendtype:level
 
 #log_min_error_statement = error	# values in order of decreasing detail:
 					#   debug5
diff --git a/src/include/postmaster/proctypelist.h b/src/include/postmaster/proctypelist.h
index 242862451d8..f18e3342285 100644
--- a/src/include/postmaster/proctypelist.h
+++ b/src/include/postmaster/proctypelist.h
@@ -30,22 +30,22 @@
  */
 
 
-/* bktype, description, main_func, shmem_attach */
-PG_PROCTYPE(B_ARCHIVER, gettext_noop("archiver"), PgArchiverMain, true)
-PG_PROCTYPE(B_AUTOVAC_LAUNCHER, gettext_noop("autovacuum launcher"), AutoVacLauncherMain, true)
-PG_PROCTYPE(B_AUTOVAC_WORKER, gettext_noop("autovacuum worker"), AutoVacWorkerMain, true)
-PG_PROCTYPE(B_BACKEND, gettext_noop("client backend"), BackendMain, true)
-PG_PROCTYPE(B_BG_WORKER, gettext_noop("background worker"), BackgroundWorkerMain, true)
-PG_PROCTYPE(B_BG_WRITER, gettext_noop("background writer"), BackgroundWriterMain, true)
-PG_PROCTYPE(B_CHECKPOINTER, gettext_noop("checkpointer"), CheckpointerMain, true)
-PG_PROCTYPE(B_DEAD_END_BACKEND, gettext_noop("dead-end client backend"), BackendMain, true)
-PG_PROCTYPE(B_INVALID, gettext_noop("unrecognized"), NULL, false)
-PG_PROCTYPE(B_IO_WORKER, gettext_noop("io worker"), IoWorkerMain, true)
-PG_PROCTYPE(B_LOGGER, gettext_noop("syslogger"), SysLoggerMain, false)
-PG_PROCTYPE(B_SLOTSYNC_WORKER, gettext_noop("slotsync worker"), ReplSlotSyncWorkerMain, true)
-PG_PROCTYPE(B_STANDALONE_BACKEND, gettext_noop("standalone backend"), NULL, false)
-PG_PROCTYPE(B_STARTUP, gettext_noop("startup"), StartupProcessMain, true)
-PG_PROCTYPE(B_WAL_RECEIVER, gettext_noop("walreceiver"), WalReceiverMain, true)
-PG_PROCTYPE(B_WAL_SENDER, gettext_noop("walsender"), NULL, true)
-PG_PROCTYPE(B_WAL_SUMMARIZER, gettext_noop("walsummarizer"), WalSummarizerMain, true)
-PG_PROCTYPE(B_WAL_WRITER, gettext_noop("walwriter"), WalWriterMain, true)
+/* bktype, bkcategory, description, main_func, shmem_attach, log_min_messages */
+PG_PROCTYPE(B_ARCHIVER, "archiver", gettext_noop("archiver"), PgArchiverMain, true, WARNING)
+PG_PROCTYPE(B_AUTOVAC_LAUNCHER, "autovacuum", gettext_noop("autovacuum launcher"), AutoVacLauncherMain, true, WARNING)
+PG_PROCTYPE(B_AUTOVAC_WORKER, "autovacuum", gettext_noop("autovacuum worker"), AutoVacWorkerMain, true, WARNING)
+PG_PROCTYPE(B_BACKEND, "backend", gettext_noop("client backend"), BackendMain, true, WARNING)
+PG_PROCTYPE(B_BG_WORKER, "bgworker", gettext_noop("background worker"), BackgroundWorkerMain, true, WARNING)
+PG_PROCTYPE(B_BG_WRITER, "bgwriter", gettext_noop("background writer"), BackgroundWriterMain, true, WARNING)
+PG_PROCTYPE(B_CHECKPOINTER, "checkpointer", gettext_noop("checkpointer"), CheckpointerMain, true, WARNING)
+PG_PROCTYPE(B_DEAD_END_BACKEND, "backend", gettext_noop("dead-end client backend"), BackendMain, true, WARNING)
+PG_PROCTYPE(B_INVALID, "backend", gettext_noop("unrecognized"), NULL, false, WARNING)
+PG_PROCTYPE(B_IO_WORKER, "ioworker", gettext_noop("io worker"), IoWorkerMain, true, WARNING)
+PG_PROCTYPE(B_LOGGER, "syslogger", gettext_noop("syslogger"), SysLoggerMain, false, WARNING)
+PG_PROCTYPE(B_SLOTSYNC_WORKER, "slotsyncworker", gettext_noop("slotsync worker"), ReplSlotSyncWorkerMain, true, WARNING)
+PG_PROCTYPE(B_STANDALONE_BACKEND, "backend", gettext_noop("standalone backend"), NULL, false, WARNING)
+PG_PROCTYPE(B_STARTUP, "startup", gettext_noop("startup"), StartupProcessMain, true, WARNING)
+PG_PROCTYPE(B_WAL_RECEIVER, "walreceiver", gettext_noop("walreceiver"), WalReceiverMain, true, WARNING)
+PG_PROCTYPE(B_WAL_SENDER, "walsender", gettext_noop("walsender"), NULL, true, WARNING)
+PG_PROCTYPE(B_WAL_SUMMARIZER, "walsummarizer", gettext_noop("walsummarizer"), WalSummarizerMain, true, WARNING)
+PG_PROCTYPE(B_WAL_WRITER, "walwriter", gettext_noop("walwriter"), WalWriterMain, true, WARNING)
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index f21ec37da89..a4e52433ac0 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -295,7 +295,7 @@ extern PGDLLIMPORT bool log_duration;
 extern PGDLLIMPORT int log_parameter_max_length;
 extern PGDLLIMPORT int log_parameter_max_length_on_error;
 extern PGDLLIMPORT int log_min_error_statement;
-extern PGDLLIMPORT int log_min_messages;
+extern PGDLLIMPORT int log_min_messages[];
 extern PGDLLIMPORT int client_min_messages;
 extern PGDLLIMPORT int log_min_duration_sample;
 extern PGDLLIMPORT int log_min_duration_statement;
@@ -329,6 +329,9 @@ extern PGDLLIMPORT bool trace_sort;
 extern PGDLLIMPORT bool optimize_bounded_sort;
 #endif
 
+/* log_min_messages */
+extern PGDLLIMPORT const char *const log_min_messages_backend_types[];
+
 /*
  * Declarations for options for enum values
  *
@@ -344,6 +347,7 @@ extern PGDLLIMPORT const struct config_enum_entry archive_mode_options[];
 extern PGDLLIMPORT const struct config_enum_entry dynamic_shared_memory_options[];
 extern PGDLLIMPORT const struct config_enum_entry io_method_options[];
 extern PGDLLIMPORT const struct config_enum_entry recovery_target_action_options[];
+extern PGDLLIMPORT const struct config_enum_entry server_message_level_options[];
 extern PGDLLIMPORT const struct config_enum_entry wal_level_options[];
 extern PGDLLIMPORT const struct config_enum_entry wal_sync_method_options[];
 
diff --git a/src/include/utils/guc_hooks.h b/src/include/utils/guc_hooks.h
index 82ac8646a8d..e6ffc96468f 100644
--- a/src/include/utils/guc_hooks.h
+++ b/src/include/utils/guc_hooks.h
@@ -174,5 +174,7 @@ extern void assign_wal_sync_method(int new_wal_sync_method, void *extra);
 extern bool check_synchronized_standby_slots(char **newval, void **extra,
 											 GucSource source);
 extern void assign_synchronized_standby_slots(const char *newval, void *extra);
+extern bool check_log_min_messages(char **newval, void **extra, GucSource source);
+extern void assign_log_min_messages(const char *newval, void *extra);
 
 #endif							/* GUC_HOOKS_H */
diff --git a/src/test/regress/expected/guc.out b/src/test/regress/expected/guc.out
index d6fb879f500..f6ebd27b1a9 100644
--- a/src/test/regress/expected/guc.out
+++ b/src/test/regress/expected/guc.out
@@ -935,3 +935,57 @@ SELECT name FROM tab_settings_flags
 (0 rows)
 
 DROP TABLE tab_settings_flags;
+-- Test log_min_messages
+SET log_min_messages TO foo;  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "foo"
+DETAIL:  Unrecognized log level: "foo".
+SET log_min_messages TO fatal;
+SHOW log_min_messages;
+ log_min_messages 
+------------------
+ fatal
+(1 row)
+
+SET log_min_messages TO 'fatal';
+SHOW log_min_messages;
+ log_min_messages 
+------------------
+ fatal
+(1 row)
+
+SET log_min_messages TO 'checkpointer:debug2, autovacuum:debug1';  --fail
+ERROR:  invalid value for parameter "log_min_messages": "checkpointer:debug2, autovacuum:debug1"
+DETAIL:  Generic log level was not defined.
+SET log_min_messages TO 'debug1, backend:error, fatal';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "debug1, backend:error, fatal"
+DETAIL:  Generic log level was already assigned.
+SET log_min_messages TO 'backend:error, debug1, backend:warning';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, debug1, backend:warning"
+DETAIL:  Backend type "backend" was already assigned.
+SET log_min_messages TO 'backend:error, foo:fatal, archiver:debug1';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, foo:fatal, archiver:debug1"
+DETAIL:  Unrecognized backend type: "foo".
+SET log_min_messages TO 'backend:error, checkpointer:bar, archiver:debug1';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, checkpointer:bar, archiver:debug1"
+DETAIL:  Unrecognized log level: "bar".
+SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3';
+SHOW log_min_messages;
+                                        log_min_messages                                         
+-------------------------------------------------------------------------------------------------
+ backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3
+(1 row)
+
+SET log_min_messages TO 'warning, autovacuum:debug1';
+SHOW log_min_messages;
+      log_min_messages      
+----------------------------
+ warning, autovacuum:debug1
+(1 row)
+
+SET log_min_messages TO 'autovacuum:debug1, warning';
+SHOW log_min_messages;
+      log_min_messages      
+----------------------------
+ autovacuum:debug1, warning
+(1 row)
+
diff --git a/src/test/regress/sql/guc.sql b/src/test/regress/sql/guc.sql
index bafaf067e82..f7dfc909b42 100644
--- a/src/test/regress/sql/guc.sql
+++ b/src/test/regress/sql/guc.sql
@@ -377,3 +377,21 @@ SELECT name FROM tab_settings_flags
   WHERE no_reset AND NOT no_reset_all
   ORDER BY 1;
 DROP TABLE tab_settings_flags;
+
+-- Test log_min_messages
+SET log_min_messages TO foo;  -- fail
+SET log_min_messages TO fatal;
+SHOW log_min_messages;
+SET log_min_messages TO 'fatal';
+SHOW log_min_messages;
+SET log_min_messages TO 'checkpointer:debug2, autovacuum:debug1';  --fail
+SET log_min_messages TO 'debug1, backend:error, fatal';  -- fail
+SET log_min_messages TO 'backend:error, debug1, backend:warning';  -- fail
+SET log_min_messages TO 'backend:error, foo:fatal, archiver:debug1';  -- fail
+SET log_min_messages TO 'backend:error, checkpointer:bar, archiver:debug1';  -- fail
+SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3';
+SHOW log_min_messages;
+SET log_min_messages TO 'warning, autovacuum:debug1';
+SHOW log_min_messages;
+SET log_min_messages TO 'autovacuum:debug1, warning';
+SHOW log_min_messages;
-- 
2.39.5



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-11-06 14:53           ` Chao Li <[email protected]>
  2025-11-26 02:15             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  1 sibling, 1 reply; 20+ messages in thread

From: Chao Li @ 2025-11-06 14:53 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: japin <[email protected]>; Alvaro Herrera <[email protected]>; Andres Freund <[email protected]>; [email protected]

Hi Euler,

I just reviewed the patch and got a few comments.

> On Nov 6, 2025, at 21:09, Euler Taveira <[email protected]> wrote:
> 
> On Sun, Oct 5, 2025, at 11:18 AM, Euler Taveira wrote:
>> This new patch contains the following changes:
>> 
>> - patch was rebased
>> - use commit dbf8cfb4f02
>> - use some GUC memory allocation functions
>> - avoid one memory allocation (suggested by Japin Li)
>> - rename backend type: logger -> syslogger
>> - adjust tests to increase test coverage
>> - improve documentation and comments to reflect the current state
>> 
> 
> Patch was rebased since commit fce7c73fba4 broke it. No modifications.
> 
> 
> -- 
> Euler Taveira
> EDB   https://www.enterprisedb.com/<v5-0001-log_min_messages-per-backend-type.patch;

1 - variable.c
```
+	/* Initialize the array. */
+	memset(newlevel, WARNING, BACKEND_NUM_TYPES * sizeof(int));
```

I think this statement is wrong, because memset() writes bytes but integers, so this statement will set very byte to WARNING, which should not be the intention. You will need to use a loop to initialize every element of newlevel.

2 - variable.c
```
+	/* Parse string into list of identifiers. */
+	if (!SplitGUCList(rawstring, ',', &elemlist))
+	{
+		/* syntax error in list */
+		GUC_check_errdetail("List syntax is invalid.");
+		guc_free(rawstring);
+		list_free(elemlist);
+		return false;
+	}
```

Every element of elemlist points to a position of rawstring, so it’s better to list_free(elemlist) first then guc_free(rawstring).

3 - launch_backend.c
```
 static inline bool
 should_output_to_server(int elevel)
 {
-	return is_log_level_output(elevel, log_min_messages);
+	return is_log_level_output(elevel, log_min_messages[MyBackendType]);
 }
```

Is it possible that when this function is called, MyBackendType has not been initialized? To be safe, maybe we can check if MyBackendType is 0 (B_INVALID), then use the generic log_min_message.

4 - config.sgml
```
+        Valid values are a comma-separated list of <literal>backendtype:level</literal>
+        and a single <literal>level</literal>. The list allows it to use
+        different levels per backend type. Only the single <literal>level</literal>
+        is mandatory (order does not matter) and it is assigned to the backend
+        types that are not specified in the list.
+        Valid <literal>backendtype</literal> values are <literal>archiver</literal>,
+        <literal>autovacuum</literal>, <literal>backend</literal>,
+        <literal>bgworker</literal>, <literal>bgwriter</literal>,
+        <literal>checkpointer</literal>, <literal>ioworker</literal>,
+        <literal>syslogger</literal>, <literal>slotsyncworker</literal>,
+        <literal>startup</literal>, <literal>walreceiver</literal>,
+        <literal>walsender</literal>, <literal>walsummarizer</literal>, and
+        <literal>walwriter</literal>.
+        Valid <literal>level</literal> values are <literal>DEBUG5</literal>,
+        <literal>DEBUG4</literal>, <literal>DEBUG3</literal>, <literal>DEBUG2</literal>,
+        <literal>DEBUG1</literal>, <literal>INFO</literal>, <literal>NOTICE</literal>,
+        <literal>WARNING</literal>, <literal>ERROR</literal>, <literal>LOG</literal>,
+        <literal>FATAL</literal>, and <literal>PANIC</literal>.  Each level includes
+        all the levels that follow it.  The later the level, the fewer messages are sent
+        to the log.  The default is <literal>WARNING</literal> for all backend types.
+        Note that <literal>LOG</literal> has a different rank here than in
```

* “Valid values are …”, here “are” usually mean both are needed, so maybe change “are” to “can be”.
* It says “the single level is mandatory”, then why there is still a default value?

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/









^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 14:53           ` Re: log_min_messages per backend type Chao Li <[email protected]>
@ 2025-11-26 02:15             ` Euler Taveira <[email protected]>
  0 siblings, 0 replies; 20+ messages in thread

From: Euler Taveira @ 2025-11-26 02:15 UTC (permalink / raw)
  To: Chao Li <[email protected]>; +Cc: japin <[email protected]>; Alvaro Herrera <[email protected]>; Andres Freund <[email protected]>; [email protected]

On Thu, Nov 6, 2025, at 11:53 AM, Chao Li wrote:
> I just reviewed the patch and got a few comments.
>

Thanks for your review.

> +	/* Initialize the array. */
> +	memset(newlevel, WARNING, BACKEND_NUM_TYPES * sizeof(int));
> ```
>
> I think this statement is wrong, because memset() writes bytes but 
> integers, so this statement will set very byte to WARNING, which should 
> not be the intention. You will need to use a loop to initialize every 
> element of newlevel.
>

Facepalm. Good catch.

> 2 - variable.c
> ```
> +	/* Parse string into list of identifiers. */
> +	if (!SplitGUCList(rawstring, ',', &elemlist))
> +	{
> +		/* syntax error in list */
> +		GUC_check_errdetail("List syntax is invalid.");
> +		guc_free(rawstring);
> +		list_free(elemlist);
> +		return false;
> +	}
> ```
>
> Every element of elemlist points to a position of rawstring, so it’s 
> better to list_free(elemlist) first then guc_free(rawstring).
>

Fixed.

> 3 - launch_backend.c
> ```
>  static inline bool
>  should_output_to_server(int elevel)
>  {
> -	return is_log_level_output(elevel, log_min_messages);
> +	return is_log_level_output(elevel, log_min_messages[MyBackendType]);
>  }
> ```
>
> Is it possible that when this function is called, MyBackendType has not 
> been initialized? To be safe, maybe we can check if MyBackendType is 0 
> (B_INVALID), then use the generic log_min_message.
>

It uses the generic log level if you don't modify backend type "backend". If
you do specify "backend", that level is used instead. After Alvaro's question
in the other email, I added "postmaster" backend type and assigned it to
B_INVALID. That's exactly the log level that will be used. As I said in that
email, it is ok to consider a process whose backend type is B_INVALID as a
postmaster process.

> * “Valid values are …”, here “are” usually mean both are needed, so 
> maybe change “are” to “can be”.
> * It says “the single level is mandatory”, then why there is still a 
> default value?
>

It seems the current sentence that describe the comma-separated list is
ambiguous. The sentence doesn't make it clear that the list contains 2 elements
(type:level and level) and the "level" element is mandatory. What about the new
sentence?


-- 
Euler Taveira
EDB   https://www.enterprisedb.com/





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-11-06 16:01           ` Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  1 sibling, 1 reply; 20+ messages in thread

From: Alvaro Herrera @ 2025-11-06 16:01 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]

On 2025-Nov-06, Euler Taveira wrote:

> +        Valid values are a comma-separated list of <literal>backendtype:level</literal>
> +        and a single <literal>level</literal>. The list allows it to use
> +        different levels per backend type. Only the single <literal>level</literal>
> +        is mandatory (order does not matter) and it is assigned to the backend
> +        types that are not specified in the list.
> +        Valid <literal>backendtype</literal> values are <literal>archiver</literal>,
> +        <literal>autovacuum</literal>, <literal>backend</literal>,
> +        <literal>bgworker</literal>, <literal>bgwriter</literal>,
> +        <literal>checkpointer</literal>, <literal>ioworker</literal>,
> +        <literal>syslogger</literal>, <literal>slotsyncworker</literal>,
> +        <literal>startup</literal>, <literal>walreceiver</literal>,
> +        <literal>walsender</literal>, <literal>walsummarizer</literal>, and
> +        <literal>walwriter</literal>.
> +        Valid <literal>level</literal> values are <literal>DEBUG5</literal>,
> +        <literal>DEBUG4</literal>, <literal>DEBUG3</literal>, <literal>DEBUG2</literal>,
> +        <literal>DEBUG1</literal>, <literal>INFO</literal>, <literal>NOTICE</literal>,
> +        <literal>WARNING</literal>, <literal>ERROR</literal>, <literal>LOG</literal>,
> +        <literal>FATAL</literal>, and <literal>PANIC</literal>.  Each level includes
> +        all the levels that follow it.  The later the level, the fewer messages are sent
> +        to the log.  The default is <literal>WARNING</literal> for all backend types.

I would use <literal>type:level</literal> rather than "backendtype".
Also, the glossary says we have "auxiliary processes" and "backends", so
from the user perspective, these types are not all backends, but instead
process types.

I think the list of backend types is pretty hard to read.  What do you
think about using 
<simplelist type="vert" columns="4">
to create a friendlier representation?  

So you would say something like "Valid types are listed in the table
below, each corresponding to either postmaster, an auxiliary process
type or a backend".  (Eh, wait, you seem not to have "postmaster" in
your list, what's up with that?)

(I'm not sure about making the log levels also a <simplelist>, because
for that list the order matters, and if we have more than one column
then the order is going to be ambiguous, and if we have just one column
it's going to be too tall.  But maybe it's not all that bad?)

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
@ 2025-11-18 23:38             ` Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Euler Taveira @ 2025-11-18 23:38 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]

On Thu, Nov 6, 2025, at 1:01 PM, Alvaro Herrera wrote:
>
> I would use <literal>type:level</literal> rather than "backendtype".
> Also, the glossary says we have "auxiliary processes" and "backends", so
> from the user perspective, these types are not all backends, but instead
> process types.
>

Hasn't that ship already sailed? If your suggestion is limited to "type:level",
that's ok. I wouldn't like to use 2 terminologies ("process type" and "backend
type") in the documentation.

See miscadmin.h.

/*
 * MyBackendType indicates what kind of a backend this is.
 *
 * If you add entries, please also update the child_process_kinds array in
 * launch_backend.c.
 */
typedef enum BackendType

The "backend type" terminology is exposed. It is even available in the
pg_stat_activity view. I agree that "backend" is not a good name to define a
Postgres process. I don't think we should be inconsistent only in this patch.
Even if the proposal is to rename enum BackendType (I won't propose it as part
of this patch), it would make some extension authors unhappy according to
codesearch.debian.net.

> I think the list of backend types is pretty hard to read.  What do you
> think about using 
> <simplelist type="vert" columns="4">
> to create a friendlier representation?  
>

I tried but didn't like the result. See the attached image. You said table but
it is a multi-column list; it doesn't contain a header or borders. Reading this
message and Chao Li message, I realized the description is not good enough yet.

> So you would say something like "Valid types are listed in the table
> below, each corresponding to either postmaster, an auxiliary process
> type or a backend".  (Eh, wait, you seem not to have "postmaster" in
> your list, what's up with that?)
>

Good question. The current patch uses "backend" to B_INVALID (that's exactly the
MyBackendType for postmaster -- see below). I think it is reasonable to create a
new category "postmaster" and assign it to B_INVALID. If, for some reason, a
process temporarily has MyBackendType equals to B_INVALID, it is ok to still
consider it a postmaster process.

$ ps aux | grep postgres
euler      56968  0.0  0.0 217144 29016 ?        Ss   19:28   0:00 /c/pg/bin/postgres -D /c/pg/pgdata
euler      56969  0.0  0.0 217276  9948 ?        Ss   19:28   0:00 postgres: io worker 0
euler      56970  0.0  0.0 217276  7620 ?        Ss   19:28   0:00 postgres: io worker 1
euler      56971  0.0  0.0 217144  6408 ?        Ss   19:28   0:00 postgres: io worker 2
euler      56972  0.0  0.0 217276  8388 ?        Ss   19:28   0:00 postgres: checkpointer
euler      56973  0.0  0.0 217300  8744 ?        Ss   19:28   0:00 postgres: background writer
euler      56975  0.0  0.0 217276 11292 ?        Ss   19:28   0:00 postgres: walwriter
euler      56976  0.0  0.0 218724  9140 ?        Ss   19:28   0:00 postgres: autovacuum launcher
euler      56977  0.0  0.0 218724  9232 ?        Ss   19:28   0:00 postgres: logical replication launcher
euler      58717  0.0  0.0   6676  2232 pts/1    S+   19:39   0:00 grep --color=auto postgres
$ for p in $(pgrep postgres); do echo "pid: $p" && gdb -q -batch -ex 'set pagination off' -ex "attach $p" -ex 'p MyBackendType' -ex 'quit' 2> /dev/null; done | grep -E '^\$1|pid'
pid: 56968
$1 = B_INVALID
pid: 56969
$1 = B_IO_WORKER
pid: 56970
$1 = B_IO_WORKER
pid: 56971
$1 = B_IO_WORKER
pid: 56972
$1 = B_CHECKPOINTER
pid: 56973
$1 = B_BG_WRITER
pid: 56975
$1 = B_WAL_WRITER
pid: 56976
$1 = B_AUTOVAC_LAUNCHER
pid: 56977
$1 = B_BG_WORKER

> (I'm not sure about making the log levels also a <simplelist>, because
> for that list the order matters, and if we have more than one column
> then the order is going to be ambiguous, and if we have just one column
> it's going to be too tall.  But maybe it's not all that bad?)
>

+1.


-- 
Euler Taveira
EDB   https://www.enterprisedb.com/

Attachments:

  [image/png] lmm.png (90.9K, ../../[email protected]/2-lmm.png)
  download | view image

^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-11-19 10:44               ` Alvaro Herrera <[email protected]>
  2025-11-21 14:13                 ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Alvaro Herrera @ 2025-11-19 10:44 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]

On 2025-Nov-18, Euler Taveira wrote:

> On Thu, Nov 6, 2025, at 1:01 PM, Alvaro Herrera wrote:

> See miscadmin.h.
> 
> /*
>  * MyBackendType indicates what kind of a backend this is.
>  *
>  * If you add entries, please also update the child_process_kinds array in
>  * launch_backend.c.
>  */
> typedef enum BackendType
> 
> The "backend type" terminology is exposed.

Something appearing in the source code does not equate it being exposed
to end users.  Things are exposed when they are in the documentation, or
when the SQL interface requires you to use the term.  So I don't think
the fact that BackendType appears in the code forces us to use that name
in the user-visible interface now.

In the glossary, we talk about "processes"; in the definitions there,
"backend" is one type of process.  So in this list of "backend types"
that you want to introduce, what you really should be talking about is
"process types", where "backend" is one of the options.

> It is even available in the
> pg_stat_activity view. I agree that "backend" is not a good name to define a
> Postgres process. I don't think we should be inconsistent only in this patch.
> Even if the proposal is to rename enum BackendType (I won't propose it as part
> of this patch), it would make some extension authors unhappy according to
> codesearch.debian.net.

I don't think we should rename the BackendType enum.  That's in the
source code, not in the documentation or the SQL interface, so we don't
need to care.

> > I think the list of backend types is pretty hard to read.  What do you
> > think about using 
> > <simplelist type="vert" columns="4">
> > to create a friendlier representation?  
> 
> I tried but didn't like the result. See the attached image.

Well, I like it very much.  The original is a solid wall of text, very
hard to read, where you have to squint hunting commas in order to
distinguish one item from the next.  (Maybe you are young and have good
eyesight, but you won't be young forever.)  In the screenshot you show,
the list of possible process types to use is nicely separated, which
makes it very easy to catch at a glance.  Maybe remove "the table",
which is obviously inappropriate, and just say "Valid process types are
listed below, each corresponding to either postmaster, an auxiliary
process type or a backend".

(Note your original wording says "a backend type, corresponding to [blah
blah] or a backend" which makes no sense -- how is a backend a type of
backend?  It isn't.  It's a type of process.)

> You said table but it is a multi-column list; it doesn't contain a
> header or borders.

Right.  You don't need a full-blown table here: this simple list is
perfectly adequate.

> Good question. The current patch uses "backend" to B_INVALID (that's exactly the
> MyBackendType for postmaster -- see below). I think it is reasonable to create a
> new category "postmaster" and assign it to B_INVALID.

I guess that would work, but I think it's inadequate.  Maybe we could
add a new value B_POSTMASTER and have postmaster switch to that as early
as possible.  Then anything that still has B_INVALID must necessarily be
an improperly identified process.  Users wouldn't assign a value to that
one (the GUC wouldn't let you); instead those would always use the
default value.  Hopefully nobody would see that very often, or at all.

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
@ 2025-11-21 14:13                 ` Euler Taveira <[email protected]>
  2025-11-26 02:11                   ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Euler Taveira @ 2025-11-21 14:13 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]

On Wed, Nov 19, 2025, at 7:44 AM, Alvaro Herrera wrote:
> On 2025-Nov-18, Euler Taveira wrote:
>> The "backend type" terminology is exposed.
>
> Something appearing in the source code does not equate it being exposed
> to end users.  Things are exposed when they are in the documentation, or
> when the SQL interface requires you to use the term.  So I don't think
> the fact that BackendType appears in the code forces us to use that name
> in the user-visible interface now.
>

It is *already* exposed.

$ grep -B 3 -A 3 'backend type' doc/src/sgml/config.sgml 
         </informaltable>

         <para>
          The backend type corresponds to the column
          <structfield>backend_type</structfield> in the view
          <link linkend="monitoring-pg-stat-activity-view">
          <structname>pg_stat_activity</structname></link>,
--
        character count of the error position therein,
        location of the error in the PostgreSQL source code
        (if <varname>log_error_verbosity</varname> is set to <literal>verbose</literal>),
        application name, backend type, process ID of parallel group leader,
        and query id.
        Here is a sample table definition for storing CSV-format log output:

$ grep -B 3 -A 3 'backend_type' doc/src/sgml/config.sgml 

         <para>
          The backend type corresponds to the column
          <structfield>backend_type</structfield> in the view
          <link linkend="monitoring-pg-stat-activity-view">
          <structname>pg_stat_activity</structname></link>,
          but additional types can appear
--
  query_pos integer,
  location text,
  application_name text,
  backend_type text,
  leader_pid integer,
  query_id bigint,
  PRIMARY KEY (session_id, session_line_num)
--
         <entry>Client application name</entry>
        </row>
        <row>
         <entry><literal>backend_type</literal></entry>
         <entry>string</entry>
         <entry>Type of backend</entry>
        </row>

$ psql -c "SELECT backend_type FROM pg_stat_activity" -d postgres
         backend_type         
------------------------------
 client backend
 autovacuum launcher
 logical replication launcher
 io worker
 io worker
 io worker
 checkpointer
 background writer
 walwriter
(9 rows)

> In the glossary, we talk about "processes"; in the definitions there,
> "backend" is one type of process.  So in this list of "backend types"
> that you want to introduce, what you really should be talking about is
> "process types", where "backend" is one of the options.
>
>> It is even available in the
>> pg_stat_activity view. I agree that "backend" is not a good name to define a
>> Postgres process. I don't think we should be inconsistent only in this patch.
>> Even if the proposal is to rename enum BackendType (I won't propose it as part
>> of this patch), it would make some extension authors unhappy according to
>> codesearch.debian.net.
>
> I don't think we should rename the BackendType enum.  That's in the
> source code, not in the documentation or the SQL interface, so we don't
> need to care.
>

I think you missed my point here. As I said if your proposal will let us with 2
terminologies (backend type and process type) as shown above.  We can debate if
we (1) keep the current terminology (backend type), (2) use the proposed one
(process type) or (3) use a mixed variation (keep the SQL interface --
backend_type column but change the description to "process type"). I wouldn't
like to break compatibility (pg_stat_activity changes tend to break a lot of
stuff) so I'm fine with (1) and (3).

>> > I think the list of backend types is pretty hard to read.  What do you
>> > think about using 
>> > <simplelist type="vert" columns="4">
>> > to create a friendlier representation?  
>> 
>> I tried but didn't like the result. See the attached image.
>
> Well, I like it very much.  The original is a solid wall of text, very
> hard to read, where you have to squint hunting commas in order to
> distinguish one item from the next.  (Maybe you are young and have good
> eyesight, but you won't be young forever.)  In the screenshot you show,
> the list of possible process types to use is nicely separated, which
> makes it very easy to catch at a glance.  Maybe remove "the table",
> which is obviously inappropriate, and just say "Valid process types are
> listed below, each corresponding to either postmaster, an auxiliary
> process type or a backend".
>

I don't have a strong preference. One advantage of this suggestion is that it 
is visually easier to identify the types.

> (Note your original wording says "a backend type, corresponding to [blah
> blah] or a backend" which makes no sense -- how is a backend a type of
> backend?  It isn't.  It's a type of process.)
>

I'm rewriting that paragraph.

>> Good question. The current patch uses "backend" to B_INVALID (that's exactly the
>> MyBackendType for postmaster -- see below). I think it is reasonable to create a
>> new category "postmaster" and assign it to B_INVALID.
>
> I guess that would work, but I think it's inadequate.  Maybe we could
> add a new value B_POSTMASTER and have postmaster switch to that as early
> as possible.  Then anything that still has B_INVALID must necessarily be
> an improperly identified process.  Users wouldn't assign a value to that
> one (the GUC wouldn't let you); instead those would always use the
> default value.  Hopefully nobody would see that very often, or at all.
>

I will create a separate patch for this suggestion.


-- 
Euler Taveira
EDB   https://www.enterprisedb.com/





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-21 14:13                 ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-11-26 02:11                   ` Euler Taveira <[email protected]>
  2025-12-09 08:58                     ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Euler Taveira @ 2025-11-26 02:11 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]; Chao Li <[email protected]>

On Fri, Nov 21, 2025, at 11:13 AM, Euler Taveira wrote:
> I think you missed my point here. As I said if your proposal will let us with 2
> terminologies (backend type and process type) as shown above.  We can debate if
> we (1) keep the current terminology (backend type), (2) use the proposed one
> (process type) or (3) use a mixed variation (keep the SQL interface --
> backend_type column but change the description to "process type"). I wouldn't
> like to break compatibility (pg_stat_activity changes tend to break a lot of
> stuff) so I'm fine with (1) and (3).
>

After digesting it for a couple of days, I decided to adopt the terminology in
commit dbf8cfb4f02e (process type) just for this patch. This discussion about
changing "backend type" terminology in other parts belongs to another patch.

>>> Good question. The current patch uses "backend" to B_INVALID (that's exactly the
>>> MyBackendType for postmaster -- see below). I think it is reasonable to create a
>>> new category "postmaster" and assign it to B_INVALID.
>>
>> I guess that would work, but I think it's inadequate.  Maybe we could
>> add a new value B_POSTMASTER and have postmaster switch to that as early
>> as possible.  Then anything that still has B_INVALID must necessarily be
>> an improperly identified process.  Users wouldn't assign a value to that
>> one (the GUC wouldn't let you); instead those would always use the
>> default value.  Hopefully nobody would see that very often, or at all.
>>
>
> I will create a separate patch for this suggestion.
>

After reflection, add B_POSTMASTER into BackendType / proctypelist.h is not a
good idea. These data structures were designed to represent postmaster
children. The child_process_kinds array (launch_backend.c) is assembled with
proctypelist.h which means a function like postmaster_child_launch() could use
one of its elements (e.g. B_POSTMASTER) to start a new child; that's awkward. Do
you envision any issues to use B_INVALID for postmaster? (I wrote 0002 to
minimize the windows that each child process has B_INVALID.)

This new version contains the following changes:

- fix variable assignment (Chao Li)
- fix memory release order (Chao Li)
- change category for B_INVALID (backend -> postmaster) (Alvaro)
- rewrite documentation (Chao Li, Alvaro)
- rename backend type to process type (Alvaro)
- new patch (0002) that assigns MyBackendType as earlier as possible


-- 
Euler Taveira
EDB   https://www.enterprisedb.com/

Attachments:

  [text/x-patch] v6-0001-log_min_messages-per-process-type.patch (26.3K, ../../[email protected]/2-v6-0001-log_min_messages-per-process-type.patch)
  download | inline diff:
From 5a6bfa1c9c6183eba8de5bbdeac43ea4ea5d8229 Mon Sep 17 00:00:00 2001
From: Euler Taveira <[email protected]>
Date: Tue, 4 Nov 2025 17:18:20 -0300
Subject: [PATCH v6 1/2] log_min_messages per process type

Change log_min_messages from a single element to a comma-separated list
of elements. Each element is type:level. The types are archiver,
autovacuum (includes launcher and workers), backend, bgworker, bgwriter,
checkpointer, ioworker, postmaster, syslogger, slotsyncworker, startup,
walreceiver, walsender, walsummarizer and walwriter. A single log level
should be part of this list and it is applied as a final step to the
process types that were not informed.

The old syntax (a single log level) is still accepted for backward
compatibility. In this case, it means the informed log level is applied
for all process types. The default log level is the same (WARNING) for
all process types.
---
 doc/src/sgml/config.sgml                      |  42 +++-
 src/backend/commands/extension.c              |   2 +-
 src/backend/commands/variable.c               | 208 ++++++++++++++++++
 src/backend/postmaster/launch_backend.c       |   2 +-
 src/backend/utils/error/elog.c                |   2 +-
 src/backend/utils/init/miscinit.c             |   2 +-
 src/backend/utils/misc/guc_parameters.dat     |  10 +-
 src/backend/utils/misc/guc_tables.c           |  21 +-
 src/backend/utils/misc/postgresql.conf.sample |   2 +
 src/include/postmaster/proctypelist.h         |  42 ++--
 src/include/utils/guc.h                       |   6 +-
 src/include/utils/guc_hooks.h                 |   2 +
 src/test/regress/expected/guc.out             |  54 +++++
 src/test/regress/sql/guc.sql                  |  18 ++
 14 files changed, 372 insertions(+), 41 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 023b3f03ba9..fba26aa746e 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -7063,7 +7063,7 @@ local0.*    /var/log/postgresql
      <variablelist>
 
      <varlistentry id="guc-log-min-messages" xreflabel="log_min_messages">
-      <term><varname>log_min_messages</varname> (<type>enum</type>)
+      <term><varname>log_min_messages</varname> (<type>string</type>)
       <indexterm>
        <primary><varname>log_min_messages</varname> configuration parameter</primary>
       </indexterm>
@@ -7072,14 +7072,38 @@ local0.*    /var/log/postgresql
        <para>
         Controls which <link linkend="runtime-config-severity-levels">message
         levels</link> are written to the server log.
-        Valid values are <literal>DEBUG5</literal>, <literal>DEBUG4</literal>,
-        <literal>DEBUG3</literal>, <literal>DEBUG2</literal>, <literal>DEBUG1</literal>,
-        <literal>INFO</literal>, <literal>NOTICE</literal>, <literal>WARNING</literal>,
-        <literal>ERROR</literal>, <literal>LOG</literal>, <literal>FATAL</literal>, and
-        <literal>PANIC</literal>.  Each level includes all the levels that
-        follow it.  The later the level, the fewer messages are sent
-        to the log.  The default is <literal>WARNING</literal>.  Note that
-        <literal>LOG</literal> has a different rank here than in
+        Valid values are a comma-separated list of <literal>type:level</literal>
+        and a single <literal>level</literal>. The list allows it to use
+        different levels per process type. Only the single <literal>level</literal>
+        is mandatory (order does not matter) and it is assigned to the process
+        types that are not specified in the list.
+        Valid process types are listed in the table below, each corresponding to
+        either postmaster, an auxiliary process type or a backend.
+        <simplelist type="vert" columns="4">
+         <member><literal>archiver</literal></member>
+         <member><literal>autovacuum</literal></member>
+         <member><literal>backend</literal></member>
+         <member><literal>bgworker</literal></member>
+         <member><literal>bgwriter</literal></member>
+         <member><literal>checkpointer</literal></member>
+         <member><literal>ioworker</literal></member>
+         <member><literal>postmaster</literal></member>
+         <member><literal>syslogger</literal></member>
+         <member><literal>slotsyncworker</literal></member>
+         <member><literal>startup</literal></member>
+         <member><literal>walreceiver</literal></member>
+         <member><literal>walsender</literal></member>
+         <member><literal>walsummarizer</literal></member>
+         <member><literal>walwriter</literal></member>
+        </simplelist>
+        Valid <literal>level</literal> values are <literal>DEBUG5</literal>,
+        <literal>DEBUG4</literal>, <literal>DEBUG3</literal>, <literal>DEBUG2</literal>,
+        <literal>DEBUG1</literal>, <literal>INFO</literal>, <literal>NOTICE</literal>,
+        <literal>WARNING</literal>, <literal>ERROR</literal>, <literal>LOG</literal>,
+        <literal>FATAL</literal>, and <literal>PANIC</literal>.  Each level includes
+        all the levels that follow it.  The later the level, the fewer messages are sent
+        to the log.  The default is <literal>WARNING</literal> for all process types.
+        Note that <literal>LOG</literal> has a different rank here than in
         <xref linkend="guc-client-min-messages"/>.
         Only superusers and users with the appropriate <literal>SET</literal>
         privilege can change this setting.
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index ebc204c4462..d123b7ebb77 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -1143,7 +1143,7 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
 		(void) set_config_option("client_min_messages", "warning",
 								 PGC_USERSET, PGC_S_SESSION,
 								 GUC_ACTION_SAVE, true, 0, false);
-	if (log_min_messages < WARNING)
+	if (log_min_messages[MyBackendType] < WARNING)
 		(void) set_config_option_ext("log_min_messages", "warning",
 									 PGC_SUSET, PGC_S_SESSION,
 									 BOOTSTRAP_SUPERUSERID,
diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 608f10d9412..2b309791ee7 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -34,6 +34,7 @@
 #include "utils/backend_status.h"
 #include "utils/datetime.h"
 #include "utils/fmgrprotos.h"
+#include "utils/guc.h"
 #include "utils/guc_hooks.h"
 #include "utils/snapmgr.h"
 #include "utils/syscache.h"
@@ -1257,3 +1258,210 @@ check_ssl(bool *newval, void **extra, GucSource source)
 #endif
 	return true;
 }
+
+/*
+ * GUC check_hook for log_min_messages
+ *
+ * The parsing consists of a comma-separated list of TYPE:LEVEL elements. TYPE
+ * is log_min_messages_process_types.  LEVEL is server_message_level_options. A
+ * single LEVEL element should be part of this list and it is applied as a
+ * final step to the process types that are not specified. For backward
+ * compatibility, the old syntax is still accepted and it means to apply the
+ * LEVEL for all process types.
+ */
+bool
+check_log_min_messages(char **newval, void **extra, GucSource source)
+{
+	char	   *rawstring;
+	List	   *elemlist;
+	ListCell   *l;
+	int			newlevel[BACKEND_NUM_TYPES];
+	bool		assigned[BACKEND_NUM_TYPES];
+	int			genericlevel = -1;	/* -1 means not assigned */
+
+	/* Initialize the array. */
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+	{
+		newlevel[i] = WARNING;
+		assigned[i] = false;
+	}
+
+	/* Need a modifiable copy of string. */
+	rawstring = guc_strdup(LOG, *newval);
+
+	/* Parse string into list of identifiers. */
+	if (!SplitGUCList(rawstring, ',', &elemlist))
+	{
+		/* syntax error in list */
+		GUC_check_errdetail("List syntax is invalid.");
+		list_free(elemlist);
+		guc_free(rawstring);
+		return false;
+	}
+
+	/* Validate and assign log level and process type. */
+	foreach(l, elemlist)
+	{
+		char	   *tok = (char *) lfirst(l);
+		char	   *sep;
+		const struct config_enum_entry *entry;
+
+		/*
+		 * Check whether there is a process type following the log level. If
+		 * there is no separator, it means this is the generic log level. The
+		 * generic log level will be assigned to the process types that were
+		 * not informed.
+		 */
+		sep = strchr(tok, ':');
+		if (sep == NULL)
+		{
+			bool		found = false;
+
+			/* Reject duplicates for generic log level. */
+			if (genericlevel != -1)
+			{
+				GUC_check_errdetail("Generic log level was already assigned.");
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, tok) == 0)
+				{
+					genericlevel = entry->val;
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", tok);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+		}
+		else
+		{
+			char	   *loglevel;
+			char	   *ptype;
+			bool		found = false;
+
+			ptype = guc_malloc(LOG, (sep - tok) + 1);
+			if (!ptype)
+			{
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+			memcpy(ptype, tok, sep - tok);
+			ptype[sep - tok] = '\0';
+			loglevel = sep + 1;
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, loglevel) == 0)
+				{
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", loglevel);
+				guc_free(ptype);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/*
+			 * Is the process type name valid? There might be multiple entries
+			 * per process type, don't bail out because it can assign the
+			 * value for multiple entries.
+			 */
+			found = false;
+			for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+			{
+				if (pg_strcasecmp(log_min_messages_process_types[i], ptype) == 0)
+				{
+					/* Reject duplicates for a process type. */
+					if (assigned[i])
+					{
+						GUC_check_errdetail("Process type \"%s\" was already assigned.", ptype);
+						guc_free(ptype);
+						guc_free(rawstring);
+						list_free(elemlist);
+						return false;
+					}
+
+					newlevel[i] = entry->val;
+					assigned[i] = true;
+					found = true;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized process type: \"%s\".", ptype);
+				guc_free(ptype);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			guc_free(ptype);
+		}
+	}
+
+	/*
+	 * The generic log level must be specified. It is the fallback value.
+	 */
+	if (genericlevel == -1)
+	{
+		GUC_check_errdetail("Generic log level was not defined.");
+		guc_free(rawstring);
+		list_free(elemlist);
+		return false;
+	}
+
+	/*
+	 * Apply the generic log level after all of the specific process types
+	 * have been assigned. Hence, it doesn't matter the order you specify the
+	 * generic log level, the final result will be the same.
+	 */
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+	{
+		if (!assigned[i])
+			newlevel[i] = genericlevel;
+	}
+
+	guc_free(rawstring);
+	list_free(elemlist);
+
+	/*
+	 * Pass back data for assign_log_min_messages to use.
+	 */
+	*extra = guc_malloc(LOG, BACKEND_NUM_TYPES * sizeof(int));
+	if (!*extra)
+		return false;
+	memcpy(*extra, newlevel, BACKEND_NUM_TYPES * sizeof(int));
+
+	return true;
+}
+
+/*
+ * GUC assign_hook for log_min_messages
+ */
+void
+assign_log_min_messages(const char *newval, void *extra)
+{
+	for (int i = 0; i < BACKEND_NUM_TYPES; i++)
+		log_min_messages[i] = ((int *) extra)[i];
+}
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index 976638a58ac..c2a044321d1 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -179,7 +179,7 @@ typedef struct
 } child_process_kind;
 
 static child_process_kind child_process_kinds[] = {
-#define PG_PROCTYPE(bktype, description, main_func, shmem_attach) \
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
 	[bktype] = {description, main_func, shmem_attach},
 #include "postmaster/proctypelist.h"
 #undef PG_PROCTYPE
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 29643c51439..15d771a16c9 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -235,7 +235,7 @@ is_log_level_output(int elevel, int log_min_level)
 static inline bool
 should_output_to_server(int elevel)
 {
-	return is_log_level_output(elevel, log_min_messages);
+	return is_log_level_output(elevel, log_min_messages[MyBackendType]);
 }
 
 /*
diff --git a/src/backend/utils/init/miscinit.c b/src/backend/utils/init/miscinit.c
index fec79992c8d..2c3926cbadf 100644
--- a/src/backend/utils/init/miscinit.c
+++ b/src/backend/utils/init/miscinit.c
@@ -266,7 +266,7 @@ GetBackendTypeDesc(BackendType backendType)
 
 	switch (backendType)
 	{
-#define PG_PROCTYPE(bktype, description, main_func, shmem_attach)	\
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages)	\
 		case bktype: backendDesc = description; break;
 #include "postmaster/proctypelist.h"
 #undef PG_PROCTYPE
diff --git a/src/backend/utils/misc/guc_parameters.dat b/src/backend/utils/misc/guc_parameters.dat
index 1128167c025..55dfc2e6548 100644
--- a/src/backend/utils/misc/guc_parameters.dat
+++ b/src/backend/utils/misc/guc_parameters.dat
@@ -1676,12 +1676,14 @@
   options => 'server_message_level_options',
 },
 
-{ name => 'log_min_messages', type => 'enum', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
+{ name => 'log_min_messages', type => 'string', context => 'PGC_SUSET', group => 'LOGGING_WHEN',
   short_desc => 'Sets the message levels that are logged.',
   long_desc => 'Each level includes all the levels that follow it. The later the level, the fewer messages are sent.',
-  variable => 'log_min_messages',
-  boot_val => 'WARNING',
-  options => 'server_message_level_options',
+  flags => 'GUC_LIST_INPUT',
+  variable => 'log_min_messages_string',
+  boot_val => '"WARNING"',
+  check_hook => 'check_log_min_messages',
+  assign_hook => 'assign_log_min_messages',
 },
 
 { name => 'log_parameter_max_length', type => 'int', context => 'PGC_SUSET', group => 'LOGGING_WHAT',
diff --git a/src/backend/utils/misc/guc_tables.c b/src/backend/utils/misc/guc_tables.c
index 0209b2067a2..ff9b491327d 100644
--- a/src/backend/utils/misc/guc_tables.c
+++ b/src/backend/utils/misc/guc_tables.c
@@ -146,7 +146,7 @@ static const struct config_enum_entry client_message_level_options[] = {
 	{NULL, 0, false}
 };
 
-static const struct config_enum_entry server_message_level_options[] = {
+const struct config_enum_entry server_message_level_options[] = {
 	{"debug5", DEBUG5, false},
 	{"debug4", DEBUG4, false},
 	{"debug3", DEBUG3, false},
@@ -537,7 +537,6 @@ static bool default_with_oids = false;
 bool		current_role_is_superuser;
 
 int			log_min_error_statement = ERROR;
-int			log_min_messages = WARNING;
 int			client_min_messages = NOTICE;
 int			log_min_duration_sample = -1;
 int			log_min_duration_statement = -1;
@@ -595,6 +594,7 @@ static char *server_version_string;
 static int	server_version_num;
 static char *debug_io_direct_string;
 static char *restrict_nonsystem_relation_kind_string;
+static char *log_min_messages_string;
 
 #ifdef HAVE_SYSLOG
 #define	DEFAULT_SYSLOG_FACILITY LOG_LOCAL0
@@ -639,6 +639,23 @@ char	   *role_string;
 /* should be static, but guc.c needs to get at this */
 bool		in_hot_standby_guc;
 
+/*
+ * log_min_messages
+ */
+int			log_min_messages[] = {
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
+	[bktype] = log_min_messages,
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
+};
+
+const char *const log_min_messages_process_types[] = {
+#define PG_PROCTYPE(bktype, bkcategory, description, main_func, shmem_attach, log_min_messages) \
+	[bktype] = bkcategory,
+#include "postmaster/proctypelist.h"
+#undef PG_PROCTYPE
+};
+
 
 /*
  * Displayable names for context types (enum GucContext)
diff --git a/src/backend/utils/misc/postgresql.conf.sample b/src/backend/utils/misc/postgresql.conf.sample
index dc9e2255f8a..24b5d983590 100644
--- a/src/backend/utils/misc/postgresql.conf.sample
+++ b/src/backend/utils/misc/postgresql.conf.sample
@@ -541,6 +541,8 @@
                                         #   log
                                         #   fatal
                                         #   panic
+                                        #   and an optional comma-separated list
+                                        #   of type:level
 
 #log_min_error_statement = error        # values in order of decreasing detail:
                                         #   debug5
diff --git a/src/include/postmaster/proctypelist.h b/src/include/postmaster/proctypelist.h
index 242862451d8..5fbb8e7ab7c 100644
--- a/src/include/postmaster/proctypelist.h
+++ b/src/include/postmaster/proctypelist.h
@@ -25,27 +25,27 @@
  */
 
 /*
- * List of process types (symbol, description, Main function, shmem_attach)
- * entries.
+ * List of process types (symbol, category, description, Main function,
+ * shmem_attach, message level) entries.
  */
 
 
-/* bktype, description, main_func, shmem_attach */
-PG_PROCTYPE(B_ARCHIVER, gettext_noop("archiver"), PgArchiverMain, true)
-PG_PROCTYPE(B_AUTOVAC_LAUNCHER, gettext_noop("autovacuum launcher"), AutoVacLauncherMain, true)
-PG_PROCTYPE(B_AUTOVAC_WORKER, gettext_noop("autovacuum worker"), AutoVacWorkerMain, true)
-PG_PROCTYPE(B_BACKEND, gettext_noop("client backend"), BackendMain, true)
-PG_PROCTYPE(B_BG_WORKER, gettext_noop("background worker"), BackgroundWorkerMain, true)
-PG_PROCTYPE(B_BG_WRITER, gettext_noop("background writer"), BackgroundWriterMain, true)
-PG_PROCTYPE(B_CHECKPOINTER, gettext_noop("checkpointer"), CheckpointerMain, true)
-PG_PROCTYPE(B_DEAD_END_BACKEND, gettext_noop("dead-end client backend"), BackendMain, true)
-PG_PROCTYPE(B_INVALID, gettext_noop("unrecognized"), NULL, false)
-PG_PROCTYPE(B_IO_WORKER, gettext_noop("io worker"), IoWorkerMain, true)
-PG_PROCTYPE(B_LOGGER, gettext_noop("syslogger"), SysLoggerMain, false)
-PG_PROCTYPE(B_SLOTSYNC_WORKER, gettext_noop("slotsync worker"), ReplSlotSyncWorkerMain, true)
-PG_PROCTYPE(B_STANDALONE_BACKEND, gettext_noop("standalone backend"), NULL, false)
-PG_PROCTYPE(B_STARTUP, gettext_noop("startup"), StartupProcessMain, true)
-PG_PROCTYPE(B_WAL_RECEIVER, gettext_noop("walreceiver"), WalReceiverMain, true)
-PG_PROCTYPE(B_WAL_SENDER, gettext_noop("walsender"), NULL, true)
-PG_PROCTYPE(B_WAL_SUMMARIZER, gettext_noop("walsummarizer"), WalSummarizerMain, true)
-PG_PROCTYPE(B_WAL_WRITER, gettext_noop("walwriter"), WalWriterMain, true)
+/* bktype, bkcategory, description, main_func, shmem_attach, log_min_messages */
+PG_PROCTYPE(B_ARCHIVER, "archiver", gettext_noop("archiver"), PgArchiverMain, true, WARNING)
+PG_PROCTYPE(B_AUTOVAC_LAUNCHER, "autovacuum", gettext_noop("autovacuum launcher"), AutoVacLauncherMain, true, WARNING)
+PG_PROCTYPE(B_AUTOVAC_WORKER, "autovacuum", gettext_noop("autovacuum worker"), AutoVacWorkerMain, true, WARNING)
+PG_PROCTYPE(B_BACKEND, "backend", gettext_noop("client backend"), BackendMain, true, WARNING)
+PG_PROCTYPE(B_BG_WORKER, "bgworker", gettext_noop("background worker"), BackgroundWorkerMain, true, WARNING)
+PG_PROCTYPE(B_BG_WRITER, "bgwriter", gettext_noop("background writer"), BackgroundWriterMain, true, WARNING)
+PG_PROCTYPE(B_CHECKPOINTER, "checkpointer", gettext_noop("checkpointer"), CheckpointerMain, true, WARNING)
+PG_PROCTYPE(B_DEAD_END_BACKEND, "backend", gettext_noop("dead-end client backend"), BackendMain, true, WARNING)
+PG_PROCTYPE(B_INVALID, "postmaster", gettext_noop("unrecognized"), NULL, false, WARNING)
+PG_PROCTYPE(B_IO_WORKER, "ioworker", gettext_noop("io worker"), IoWorkerMain, true, WARNING)
+PG_PROCTYPE(B_LOGGER, "syslogger", gettext_noop("syslogger"), SysLoggerMain, false, WARNING)
+PG_PROCTYPE(B_SLOTSYNC_WORKER, "slotsyncworker", gettext_noop("slotsync worker"), ReplSlotSyncWorkerMain, true, WARNING)
+PG_PROCTYPE(B_STANDALONE_BACKEND, "backend", gettext_noop("standalone backend"), NULL, false, WARNING)
+PG_PROCTYPE(B_STARTUP, "startup", gettext_noop("startup"), StartupProcessMain, true, WARNING)
+PG_PROCTYPE(B_WAL_RECEIVER, "walreceiver", gettext_noop("walreceiver"), WalReceiverMain, true, WARNING)
+PG_PROCTYPE(B_WAL_SENDER, "walsender", gettext_noop("walsender"), NULL, true, WARNING)
+PG_PROCTYPE(B_WAL_SUMMARIZER, "walsummarizer", gettext_noop("walsummarizer"), WalSummarizerMain, true, WARNING)
+PG_PROCTYPE(B_WAL_WRITER, "walwriter", gettext_noop("walwriter"), WalWriterMain, true, WARNING)
diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h
index f21ec37da89..98b0ba08c0c 100644
--- a/src/include/utils/guc.h
+++ b/src/include/utils/guc.h
@@ -295,7 +295,7 @@ extern PGDLLIMPORT bool log_duration;
 extern PGDLLIMPORT int log_parameter_max_length;
 extern PGDLLIMPORT int log_parameter_max_length_on_error;
 extern PGDLLIMPORT int log_min_error_statement;
-extern PGDLLIMPORT int log_min_messages;
+extern PGDLLIMPORT int log_min_messages[];
 extern PGDLLIMPORT int client_min_messages;
 extern PGDLLIMPORT int log_min_duration_sample;
 extern PGDLLIMPORT int log_min_duration_statement;
@@ -329,6 +329,9 @@ extern PGDLLIMPORT bool trace_sort;
 extern PGDLLIMPORT bool optimize_bounded_sort;
 #endif
 
+/* log_min_messages */
+extern PGDLLIMPORT const char *const log_min_messages_process_types[];
+
 /*
  * Declarations for options for enum values
  *
@@ -344,6 +347,7 @@ extern PGDLLIMPORT const struct config_enum_entry archive_mode_options[];
 extern PGDLLIMPORT const struct config_enum_entry dynamic_shared_memory_options[];
 extern PGDLLIMPORT const struct config_enum_entry io_method_options[];
 extern PGDLLIMPORT const struct config_enum_entry recovery_target_action_options[];
+extern PGDLLIMPORT const struct config_enum_entry server_message_level_options[];
 extern PGDLLIMPORT const struct config_enum_entry wal_level_options[];
 extern PGDLLIMPORT const struct config_enum_entry wal_sync_method_options[];
 
diff --git a/src/include/utils/guc_hooks.h b/src/include/utils/guc_hooks.h
index 82ac8646a8d..e6ffc96468f 100644
--- a/src/include/utils/guc_hooks.h
+++ b/src/include/utils/guc_hooks.h
@@ -174,5 +174,7 @@ extern void assign_wal_sync_method(int new_wal_sync_method, void *extra);
 extern bool check_synchronized_standby_slots(char **newval, void **extra,
 											 GucSource source);
 extern void assign_synchronized_standby_slots(const char *newval, void *extra);
+extern bool check_log_min_messages(char **newval, void **extra, GucSource source);
+extern void assign_log_min_messages(const char *newval, void *extra);
 
 #endif							/* GUC_HOOKS_H */
diff --git a/src/test/regress/expected/guc.out b/src/test/regress/expected/guc.out
index d6fb879f500..3b8f44566b2 100644
--- a/src/test/regress/expected/guc.out
+++ b/src/test/regress/expected/guc.out
@@ -935,3 +935,57 @@ SELECT name FROM tab_settings_flags
 (0 rows)
 
 DROP TABLE tab_settings_flags;
+-- Test log_min_messages
+SET log_min_messages TO foo;  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "foo"
+DETAIL:  Unrecognized log level: "foo".
+SET log_min_messages TO fatal;
+SHOW log_min_messages;
+ log_min_messages 
+------------------
+ fatal
+(1 row)
+
+SET log_min_messages TO 'fatal';
+SHOW log_min_messages;
+ log_min_messages 
+------------------
+ fatal
+(1 row)
+
+SET log_min_messages TO 'checkpointer:debug2, autovacuum:debug1';  --fail
+ERROR:  invalid value for parameter "log_min_messages": "checkpointer:debug2, autovacuum:debug1"
+DETAIL:  Generic log level was not defined.
+SET log_min_messages TO 'debug1, backend:error, fatal';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "debug1, backend:error, fatal"
+DETAIL:  Generic log level was already assigned.
+SET log_min_messages TO 'backend:error, debug1, backend:warning';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, debug1, backend:warning"
+DETAIL:  Process type "backend" was already assigned.
+SET log_min_messages TO 'backend:error, foo:fatal, archiver:debug1';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, foo:fatal, archiver:debug1"
+DETAIL:  Unrecognized process type: "foo".
+SET log_min_messages TO 'backend:error, checkpointer:bar, archiver:debug1';  -- fail
+ERROR:  invalid value for parameter "log_min_messages": "backend:error, checkpointer:bar, archiver:debug1"
+DETAIL:  Unrecognized log level: "bar".
+SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3';
+SHOW log_min_messages;
+                                        log_min_messages                                         
+-------------------------------------------------------------------------------------------------
+ backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3
+(1 row)
+
+SET log_min_messages TO 'warning, autovacuum:debug1';
+SHOW log_min_messages;
+      log_min_messages      
+----------------------------
+ warning, autovacuum:debug1
+(1 row)
+
+SET log_min_messages TO 'autovacuum:debug1, warning';
+SHOW log_min_messages;
+      log_min_messages      
+----------------------------
+ autovacuum:debug1, warning
+(1 row)
+
diff --git a/src/test/regress/sql/guc.sql b/src/test/regress/sql/guc.sql
index bafaf067e82..f7dfc909b42 100644
--- a/src/test/regress/sql/guc.sql
+++ b/src/test/regress/sql/guc.sql
@@ -377,3 +377,21 @@ SELECT name FROM tab_settings_flags
   WHERE no_reset AND NOT no_reset_all
   ORDER BY 1;
 DROP TABLE tab_settings_flags;
+
+-- Test log_min_messages
+SET log_min_messages TO foo;  -- fail
+SET log_min_messages TO fatal;
+SHOW log_min_messages;
+SET log_min_messages TO 'fatal';
+SHOW log_min_messages;
+SET log_min_messages TO 'checkpointer:debug2, autovacuum:debug1';  --fail
+SET log_min_messages TO 'debug1, backend:error, fatal';  -- fail
+SET log_min_messages TO 'backend:error, debug1, backend:warning';  -- fail
+SET log_min_messages TO 'backend:error, foo:fatal, archiver:debug1';  -- fail
+SET log_min_messages TO 'backend:error, checkpointer:bar, archiver:debug1';  -- fail
+SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3';
+SHOW log_min_messages;
+SET log_min_messages TO 'warning, autovacuum:debug1';
+SHOW log_min_messages;
+SET log_min_messages TO 'autovacuum:debug1, warning';
+SHOW log_min_messages;
-- 
2.39.5



  [text/x-patch] v6-0002-Assign-backend-type-earlier.patch (7.0K, ../../[email protected]/3-v6-0002-Assign-backend-type-earlier.patch)
  download | inline diff:
From 25204568735906a01004bf5c33096dfddb67a2c0 Mon Sep 17 00:00:00 2001
From: Euler Taveira <[email protected]>
Date: Tue, 25 Nov 2025 21:09:11 -0300
Subject: [PATCH v6 2/2] Assign backend type earlier

Instead of assigning the backend type into the Main function of each
postmaster child, do it earlier (after fork()). The backend type is
already known by postmaster_child_launch() before it calls fork(). This
reduces the time frame that MyBackendType is correct. (This is important
for log_min_messages per backend type that relies on a known
MyBackendType to decide if it writes a log message or not.)
---
 src/backend/postmaster/autovacuum.c        | 2 --
 src/backend/postmaster/bgworker.c          | 1 -
 src/backend/postmaster/bgwriter.c          | 1 -
 src/backend/postmaster/checkpointer.c      | 1 -
 src/backend/postmaster/launch_backend.c    | 2 ++
 src/backend/postmaster/pgarch.c            | 1 -
 src/backend/postmaster/startup.c           | 1 -
 src/backend/postmaster/syslogger.c         | 1 -
 src/backend/postmaster/walsummarizer.c     | 1 -
 src/backend/postmaster/walwriter.c         | 1 -
 src/backend/replication/logical/slotsync.c | 2 --
 src/backend/replication/walreceiver.c      | 1 -
 src/backend/storage/aio/method_worker.c    | 1 -
 13 files changed, 2 insertions(+), 14 deletions(-)

diff --git a/src/backend/postmaster/autovacuum.c b/src/backend/postmaster/autovacuum.c
index 1c38488f2cb..36c1911a3a8 100644
--- a/src/backend/postmaster/autovacuum.c
+++ b/src/backend/postmaster/autovacuum.c
@@ -388,7 +388,6 @@ AutoVacLauncherMain(const void *startup_data, size_t startup_data_len)
 		PostmasterContext = NULL;
 	}
 
-	MyBackendType = B_AUTOVAC_LAUNCHER;
 	init_ps_display(NULL);
 
 	ereport(DEBUG1,
@@ -1401,7 +1400,6 @@ AutoVacWorkerMain(const void *startup_data, size_t startup_data_len)
 		PostmasterContext = NULL;
 	}
 
-	MyBackendType = B_AUTOVAC_WORKER;
 	init_ps_display(NULL);
 
 	Assert(GetProcessingMode() == InitProcessing);
diff --git a/src/backend/postmaster/bgworker.c b/src/backend/postmaster/bgworker.c
index 142a02eb5e9..260f051711e 100644
--- a/src/backend/postmaster/bgworker.c
+++ b/src/backend/postmaster/bgworker.c
@@ -741,7 +741,6 @@ BackgroundWorkerMain(const void *startup_data, size_t startup_data_len)
 	}
 
 	MyBgworkerEntry = worker;
-	MyBackendType = B_BG_WORKER;
 	init_ps_display(worker->bgw_name);
 
 	Assert(GetProcessingMode() == InitProcessing);
diff --git a/src/backend/postmaster/bgwriter.c b/src/backend/postmaster/bgwriter.c
index 72f5acceec7..4119758a72c 100644
--- a/src/backend/postmaster/bgwriter.c
+++ b/src/backend/postmaster/bgwriter.c
@@ -94,7 +94,6 @@ BackgroundWriterMain(const void *startup_data, size_t startup_data_len)
 
 	Assert(startup_data_len == 0);
 
-	MyBackendType = B_BG_WRITER;
 	AuxiliaryProcessMainCommon();
 
 	/*
diff --git a/src/backend/postmaster/checkpointer.c b/src/backend/postmaster/checkpointer.c
index e84e8663e96..32e849bea15 100644
--- a/src/backend/postmaster/checkpointer.c
+++ b/src/backend/postmaster/checkpointer.c
@@ -199,7 +199,6 @@ CheckpointerMain(const void *startup_data, size_t startup_data_len)
 
 	Assert(startup_data_len == 0);
 
-	MyBackendType = B_CHECKPOINTER;
 	AuxiliaryProcessMainCommon();
 
 	CheckpointerShmem->checkpointer_pid = MyProcPid;
diff --git a/src/backend/postmaster/launch_backend.c b/src/backend/postmaster/launch_backend.c
index c2a044321d1..35420b8c9fb 100644
--- a/src/backend/postmaster/launch_backend.c
+++ b/src/backend/postmaster/launch_backend.c
@@ -224,6 +224,8 @@ postmaster_child_launch(BackendType child_type, int child_slot,
 	pid = fork_process();
 	if (pid == 0)				/* child */
 	{
+		MyBackendType = child_type;
+
 		/* Capture and transfer timings that may be needed for logging */
 		if (IsExternalConnectionBackend(child_type))
 		{
diff --git a/src/backend/postmaster/pgarch.c b/src/backend/postmaster/pgarch.c
index ce6b5299324..b760d1aed40 100644
--- a/src/backend/postmaster/pgarch.c
+++ b/src/backend/postmaster/pgarch.c
@@ -222,7 +222,6 @@ PgArchiverMain(const void *startup_data, size_t startup_data_len)
 {
 	Assert(startup_data_len == 0);
 
-	MyBackendType = B_ARCHIVER;
 	AuxiliaryProcessMainCommon();
 
 	/*
diff --git a/src/backend/postmaster/startup.c b/src/backend/postmaster/startup.c
index 27e86cf393f..13e86a2921e 100644
--- a/src/backend/postmaster/startup.c
+++ b/src/backend/postmaster/startup.c
@@ -217,7 +217,6 @@ StartupProcessMain(const void *startup_data, size_t startup_data_len)
 {
 	Assert(startup_data_len == 0);
 
-	MyBackendType = B_STARTUP;
 	AuxiliaryProcessMainCommon();
 
 	/* Arrange to clean up at startup process exit */
diff --git a/src/backend/postmaster/syslogger.c b/src/backend/postmaster/syslogger.c
index 50c2edec1f6..a378b15a6de 100644
--- a/src/backend/postmaster/syslogger.c
+++ b/src/backend/postmaster/syslogger.c
@@ -206,7 +206,6 @@ SysLoggerMain(const void *startup_data, size_t startup_data_len)
 
 	now = MyStartTime;
 
-	MyBackendType = B_LOGGER;
 	init_ps_display(NULL);
 
 	/*
diff --git a/src/backend/postmaster/walsummarizer.c b/src/backend/postmaster/walsummarizer.c
index c4a888a081c..ce497d8ec42 100644
--- a/src/backend/postmaster/walsummarizer.c
+++ b/src/backend/postmaster/walsummarizer.c
@@ -234,7 +234,6 @@ WalSummarizerMain(const void *startup_data, size_t startup_data_len)
 
 	Assert(startup_data_len == 0);
 
-	MyBackendType = B_WAL_SUMMARIZER;
 	AuxiliaryProcessMainCommon();
 
 	ereport(DEBUG1,
diff --git a/src/backend/postmaster/walwriter.c b/src/backend/postmaster/walwriter.c
index fd92c8b7a33..c1b4ddda555 100644
--- a/src/backend/postmaster/walwriter.c
+++ b/src/backend/postmaster/walwriter.c
@@ -94,7 +94,6 @@ WalWriterMain(const void *startup_data, size_t startup_data_len)
 
 	Assert(startup_data_len == 0);
 
-	MyBackendType = B_WAL_WRITER;
 	AuxiliaryProcessMainCommon();
 
 	/*
diff --git a/src/backend/replication/logical/slotsync.c b/src/backend/replication/logical/slotsync.c
index 1f4f06d467b..c9069f299b6 100644
--- a/src/backend/replication/logical/slotsync.c
+++ b/src/backend/replication/logical/slotsync.c
@@ -1403,8 +1403,6 @@ ReplSlotSyncWorkerMain(const void *startup_data, size_t startup_data_len)
 
 	Assert(startup_data_len == 0);
 
-	MyBackendType = B_SLOTSYNC_WORKER;
-
 	init_ps_display(NULL);
 
 	Assert(GetProcessingMode() == InitProcessing);
diff --git a/src/backend/replication/walreceiver.c b/src/backend/replication/walreceiver.c
index 4217fc54e2e..fe35cacb95c 100644
--- a/src/backend/replication/walreceiver.c
+++ b/src/backend/replication/walreceiver.c
@@ -168,7 +168,6 @@ WalReceiverMain(const void *startup_data, size_t startup_data_len)
 
 	Assert(startup_data_len == 0);
 
-	MyBackendType = B_WAL_RECEIVER;
 	AuxiliaryProcessMainCommon();
 
 	/*
diff --git a/src/backend/storage/aio/method_worker.c b/src/backend/storage/aio/method_worker.c
index b5ac073a910..fee1dae7e2d 100644
--- a/src/backend/storage/aio/method_worker.c
+++ b/src/backend/storage/aio/method_worker.c
@@ -391,7 +391,6 @@ IoWorkerMain(const void *startup_data, size_t startup_data_len)
 	volatile int error_errno = 0;
 	char		cmd[128];
 
-	MyBackendType = B_IO_WORKER;
 	AuxiliaryProcessMainCommon();
 
 	pqsignal(SIGHUP, SignalHandlerForConfigReload);
-- 
2.39.5



^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-21 14:13                 ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-26 02:11                   ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-12-09 08:58                     ` Alvaro Herrera <[email protected]>
  2025-12-09 16:30                       ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-12-09 18:24                       ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  0 siblings, 2 replies; 20+ messages in thread

From: Alvaro Herrera @ 2025-12-09 08:58 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]; Chao Li <[email protected]>

Hello

I noticed failures under Windows, and realized that the EXEC_BACKEND
case was busted.  That's easy to fix -- just assign MyBackendType in
SubPostmasterMain() as well.  With that fix I was still getting some
crashes in three test modules, and after some debugging it turned out
that if you have shared_preload_library with a background worker and use
%b in the log_line_prefix, you get a crash trying to expand a name that
hasn't been set up yet.  I figured we can use a constant string in that
case.  Better ideas for that string welcome.

So here's your v6 again with those fixes as 0003 -- let's see what CI
thinks of this.  I haven't looked at your doc changes yet.


BTW with %b in log_line_prefix, the log file looks ... interesting.

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
"Before you were born your parents weren't as boring as they are now. They
got that way paying your bills, cleaning up your room and listening to you
tell them how idealistic you are."  -- Charles J. Sykes' advice to teenagers


^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-21 14:13                 ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-26 02:11                   ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-12-09 08:58                     ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
@ 2025-12-09 16:30                       ` Alvaro Herrera <[email protected]>
  1 sibling, 0 replies; 20+ messages in thread

From: Alvaro Herrera @ 2025-12-09 16:30 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]; Chao Li <[email protected]>

BTW another thing I realized while looking this over, is that we quite
uselessly transform the integer backend type to a string, pass it as a
string using the --forkchild= argument to the child process, then parse
the string back to an int to use as an array index.  It would be much
easier to just use the integer value everywhere, as the attached shows.

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"I love the Postgres community. It's all about doing things _properly_. :-)"
(David Garamond)


^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-21 14:13                 ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-26 02:11                   ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-12-09 08:58                     ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
@ 2025-12-09 18:24                       ` Alvaro Herrera <[email protected]>
  2025-12-10 02:00                         ` Re: log_min_messages per backend type Chao Li <[email protected]>
  1 sibling, 1 reply; 20+ messages in thread

From: Alvaro Herrera @ 2025-12-09 18:24 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]; Chao Li <[email protected]>

On 2025-Dec-09, Alvaro Herrera wrote:

> So here's your v6 again with those fixes as 0003 -- let's see what CI
> thinks of this.  I haven't looked at your doc changes yet.

This passed CI, so I have marked it as Ready for Committer.  Further
comments are still welcome, of course, but if there are none, I intend
to get it committed in a few days.


I'm not really happy with our usage of the translatable description
field for things such as %b in log_line_prefix or the ps display; I
think we should use the shorter names there instead.  Otherwise, you end
up with log lines that look something like this (visible in a server
with %b in log_line_prefix, which the TAP tests as well as pg_regress
have):

  2025-12-08 21:38:04.304 CET autovacuum launcher[2452437] DEBUG: autovacuum launcher started

where the bit before the PID is marked for translation.  I think it
should rather be

  2025-12-08 21:38:04.304 CET autovacuum[2452437] DEBUG: autovacuum launcher started

where that name (the same we'll use in log_min_messages) is not
translated.

However, this issue is rather independent of the patch in this thread,
so I'm going to discuss it in another thread; the name string though is
added by this patch.

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
"Java is clearly an example of money oriented programming"  (A. Stepanov)





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-21 14:13                 ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-26 02:11                   ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-12-09 08:58                     ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-12-09 18:24                       ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
@ 2025-12-10 02:00                         ` Chao Li <[email protected]>
  2025-12-11 01:57                           ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Chao Li @ 2025-12-10 02:00 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: Euler Taveira <[email protected]>; japin <[email protected]>; Andres Freund <[email protected]>; [email protected]



> On Dec 10, 2025, at 02:24, Alvaro Herrera <[email protected]> wrote:
> 
> On 2025-Dec-09, Alvaro Herrera wrote:
> 
>> So here's your v6 again with those fixes as 0003 -- let's see what CI
>> thinks of this.  I haven't looked at your doc changes yet.
> 
> This passed CI, so I have marked it as Ready for Committer.  Further
> comments are still welcome, of course, but if there are none, I intend
> to get it committed in a few days.
> 
> 
> I'm not really happy with our usage of the translatable description
> field for things such as %b in log_line_prefix or the ps display; I
> think we should use the shorter names there instead.  Otherwise, you end
> up with log lines that look something like this (visible in a server
> with %b in log_line_prefix, which the TAP tests as well as pg_regress
> have):
> 
>  2025-12-08 21:38:04.304 CET autovacuum launcher[2452437] DEBUG: autovacuum launcher started
> 
> where the bit before the PID is marked for translation.  I think it
> should rather be
> 
>  2025-12-08 21:38:04.304 CET autovacuum[2452437] DEBUG: autovacuum launcher started
> 
> where that name (the same we'll use in log_min_messages) is not
> translated.
> 
> However, this issue is rather independent of the patch in this thread,
> so I'm going to discuss it in another thread; the name string though is
> added by this patch.
> 
> -- 
> Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
> "Java is clearly an example of money oriented programming"  (A. Stepanov)

Overall the patch is solid, I just have a couple of suggestions:

1 - user experience
```
evantest=# show log_min_messages;
  log_min_messages
---------------------
 warning,backend:log
(1 row)
```

Now “show log_min_messages” prints the raw string the user set, in above example, there is not a white-space between the two log levels, and “show” result doesn’t have a white-space between the two log levels either. IMO, “SHOW log_min_messages” should display a stable result, in other words, say “fatal, backend:log” and “backend:log, fatal” should show the same result as they are actually meaning the same. So, I would suggest normalize the raw string: put the general level in the first place and sort others by process type, then SHOW returns the normalized string.

2 - refactoring for 0001
```
+		sep = strchr(tok, ':');
+		if (sep == NULL)
+		{
+			bool		found = false;
+
+			/* Reject duplicates for generic log level. */
+			if (genericlevel != -1)
+			{
+				GUC_check_errdetail("Generic log level was already assigned.");
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, tok) == 0)
+				{
+					genericlevel = entry->val;
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", tok);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+		}
+		else
+		{
+			char	   *loglevel;
+			char	   *ptype;
+			bool		found = false;
+
+			ptype = guc_malloc(LOG, (sep - tok) + 1);
+			if (!ptype)
+			{
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
+			memcpy(ptype, tok, sep - tok);
+			ptype[sep - tok] = '\0';
+			loglevel = sep + 1;
+
+			/* Is the log level valid? */
+			for (entry = server_message_level_options; entry && entry->name; entry++)
+			{
+				if (pg_strcasecmp(entry->name, loglevel) == 0)
+				{
+					found = true;
+					break;
+				}
+			}
+
+			if (!found)
+			{
+				GUC_check_errdetail("Unrecognized log level: \"%s\".", loglevel);
+				guc_free(ptype);
+				guc_free(rawstring);
+				list_free(elemlist);
+				return false;
+			}
```

In the “if” and “else” clauses, there are duplicate code to valid log levels. We should refactor the code to avoid the duplication. For example, pull up “loglevel” to the “for” loop level, then we can valid it after the “if-else”.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/









^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-21 14:13                 ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-26 02:11                   ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-12-09 08:58                     ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-12-09 18:24                       ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-12-10 02:00                         ` Re: log_min_messages per backend type Chao Li <[email protected]>
@ 2025-12-11 01:57                           ` Euler Taveira <[email protected]>
  2025-12-11 02:11                             ` Re: log_min_messages per backend type Chao Li <[email protected]>
  0 siblings, 1 reply; 20+ messages in thread

From: Euler Taveira @ 2025-12-11 01:57 UTC (permalink / raw)
  To: Chao Li <[email protected]>; Alvaro Herrera <[email protected]>; +Cc: japin <[email protected]>; Andres Freund <[email protected]>; [email protected]

On Tue, Dec 9, 2025, at 11:00 PM, Chao Li wrote:
> Now “show log_min_messages” prints the raw string the user set, in 
> above example, there is not a white-space between the two log levels, 
> and “show” result doesn’t have a white-space between the two log levels 
> either. IMO, “SHOW log_min_messages” should display a stable result, in 
> other words, say “fatal, backend:log” and “backend:log, fatal” should 
> show the same result as they are actually meaning the same. So, I would 
> suggest normalize the raw string: put the general level in the first 
> place and sort others by process type, then SHOW returns the normalized 
> string.
>

I thought about it but leave it alone because (a) it would increase this patch
footprint and (b) the input might be different from the output. I could also be
done in another patch but under reflection an unstable output can break tests
or whatever uses the SHOW log_min_messages output. I thought this change would
require a new show_log_min_messages to manipulate the input again but we can
reassign the GUC value after sorting the existing list and creating a new string
list.

> In the “if” and “else” clauses, there are duplicate code to valid log 
> levels. We should refactor the code to avoid the duplication. For 
> example, pull up “loglevel” to the “for” loop level, then we can valid 
> it after the “if-else”.
>

The for loop is duplicate but if you create a separate function for it but the
result is:

 src/backend/commands/variable.c | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)


I'll post a patch in a couple of hours after spend more time in it.


-- 
Euler Taveira
EDB   https://www.enterprisedb.com/





^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-07-31 16:22 ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-07-31 16:51   ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-07-31 23:34     ` Re: log_min_messages per backend type Japin Li <[email protected]>
  2025-10-05 14:18       ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 13:09         ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-06 16:01           ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-18 23:38             ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-19 10:44               ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-11-21 14:13                 ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-11-26 02:11                   ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
  2025-12-09 08:58                     ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-12-09 18:24                       ` Re: log_min_messages per backend type Alvaro Herrera <[email protected]>
  2025-12-10 02:00                         ` Re: log_min_messages per backend type Chao Li <[email protected]>
  2025-12-11 01:57                           ` Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-12-11 02:11                             ` Chao Li <[email protected]>
  0 siblings, 0 replies; 20+ messages in thread

From: Chao Li @ 2025-12-11 02:11 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: Alvaro Herrera <[email protected]>; japin <[email protected]>; Andres Freund <[email protected]>; [email protected]



> On Dec 11, 2025, at 09:57, Euler Taveira <[email protected]> wrote:
> 
> 
>> In the “if” and “else” clauses, there are duplicate code to valid log 
>> levels. We should refactor the code to avoid the duplication. For 
>> example, pull up “loglevel” to the “for” loop level, then we can valid 
>> it after the “if-else”.
>> 
> 
> The for loop is duplicate but if you create a separate function for it but the
> result is:
> 
> src/backend/commands/variable.c | 43 ++++++++++++++++++++++---------------------
> 1 file changed, 22 insertions(+), 21 deletions(-)
> 
> 

I don’t think we need to add a separate function. We can use ‘if-else” to parse log level, then verify it after “if-else”.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/









^ permalink  raw  reply  [nested|flat] 20+ messages in thread

* Re: log_min_messages per backend type
  2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
@ 2025-08-01 05:53 ` Japin Li <[email protected]>
  1 sibling, 0 replies; 20+ messages in thread

From: Japin Li @ 2025-08-01 05:53 UTC (permalink / raw)
  To: Euler Taveira <[email protected]>; +Cc: Andres Freund <[email protected]>; Alvaro Herrera <[email protected]>; [email protected]

On Thu, Jul 31, 2025 at 11:19:48AM -0300, Euler Taveira wrote:
> On Thu, Mar 6, 2025, at 10:33 AM, Andres Freund wrote:
> > Huh, the startup process is among the most crucial things to monitor?
> >
> 
> Good point. Fixed.
> 
> After collecting some suggestions, I'm attaching a new patch contains the
> following changes:
> 
> - patch was rebased
> - include Alvaro's patch (v2-0001) [1] as a basis for this patch
> - add ioworker as new backend type
> - add startup as new backend type per Andres suggestion
> - small changes into documentation
> 
> > I don't know what I think about the whole patch, but I do want to voice
> > *strong* opposition to duplicating a list of all backend types into multiple
> > places. It's already painfull enough to add a new backend type, without having
> > to pointlessly go around and manually add a new backend type to mulltiple
> > arrays that have completely predictable content.
> >
> 
> I'm including Alvaro's patch as is just to make the CF bot happy and to
> illustrate how it would be if we adopt his solution to centralize the list of
> backend types. I think Alvaro's proposal overcomes the objection [2], right?
> 

I think we can avoid memory allocation by using pg_strncasecmp().

diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 5c769dd7bcc..f854b2fac93 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -1343,14 +1343,10 @@ check_log_min_messages(char **newval, void **extra, GucSource source)
 		}
 		else
 		{
-			char	   *loglevel;
-			char	   *btype;
-			bool		found = false;

-			btype = palloc((sep - tok) + 1);
-			memcpy(btype, tok, sep - tok);
-			btype[sep - tok] = '\0';
-			loglevel = pstrdup(sep + 1);
+			char	   *btype = tok;
+			char	   *loglevel = sep + 1;
+			bool		found = false;

 			/* Is the log level valid? */
 			for (entry = server_message_level_options; entry && entry->name; entry++)
@@ -1377,7 +1373,7 @@ check_log_min_messages(char **newval, void **extra, GucSource source)
 			found = false;
 			for (int i = 0; i < BACKEND_NUM_TYPES; i++)
 			{
-				if (pg_strcasecmp(log_min_messages_backend_types[i], btype) == 0)
+				if (pg_strncasecmp(log_min_messages_backend_types[i], btype, sep - tok) == 0)
 				{
 					/* Reject duplicates for a backend type. */
 					if (assigned[i])

-- 
Best regards,
Japin Li
ChengDu WenWu Information Technology Co., LTD.





^ permalink  raw  reply  [nested|flat] 20+ messages in thread


end of thread, other threads:[~2025-12-11 02:11 UTC | newest]

Thread overview: 20+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-07-31 14:19 Re: log_min_messages per backend type Euler Taveira <[email protected]>
2025-07-31 16:22 ` Japin Li <[email protected]>
2025-07-31 16:51   ` Alvaro Herrera <[email protected]>
2025-07-31 23:34     ` Japin Li <[email protected]>
2025-10-05 14:18       ` Euler Taveira <[email protected]>
2025-11-06 13:09         ` Euler Taveira <[email protected]>
2025-11-06 14:53           ` Chao Li <[email protected]>
2025-11-26 02:15             ` Euler Taveira <[email protected]>
2025-11-06 16:01           ` Alvaro Herrera <[email protected]>
2025-11-18 23:38             ` Euler Taveira <[email protected]>
2025-11-19 10:44               ` Alvaro Herrera <[email protected]>
2025-11-21 14:13                 ` Euler Taveira <[email protected]>
2025-11-26 02:11                   ` Euler Taveira <[email protected]>
2025-12-09 08:58                     ` Alvaro Herrera <[email protected]>
2025-12-09 16:30                       ` Alvaro Herrera <[email protected]>
2025-12-09 18:24                       ` Alvaro Herrera <[email protected]>
2025-12-10 02:00                         ` Chao Li <[email protected]>
2025-12-11 01:57                           ` Euler Taveira <[email protected]>
2025-12-11 02:11                             ` Chao Li <[email protected]>
2025-08-01 05:53 ` Japin Li <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox