v9-0002-sort-log_min_messages-elements-per-process-type.patch

text/x-patch

Filename: v9-0002-sort-log_min_messages-elements-per-process-type.patch
Type: text/x-patch
Part: 1
Message: Re: log_min_messages per backend type

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v9-0002
Subject: sort log_min_messages elements per process type
File+
src/backend/commands/variable.c 50 0
src/test/regress/expected/guc.out 2 2
From b1e7dc57578cd07fb96678ace2911676f43b6339 Mon Sep 17 00:00:00 2001
From: Euler Taveira <euler@eulerto.com>
Date: Wed, 14 Jan 2026 17:29:09 -0300
Subject: [PATCH v9 2/3] sort log_min_messages elements per process type

The generic log level is always the first element.
---
 src/backend/commands/variable.c   | 50 +++++++++++++++++++++++++++++++
 src/test/regress/expected/guc.out |  4 +--
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/src/backend/commands/variable.c b/src/backend/commands/variable.c
index 99f40186533..a450254d1fa 100644
--- a/src/backend/commands/variable.c
+++ b/src/backend/commands/variable.c
@@ -42,6 +42,8 @@
 #include "utils/tzparser.h"
 #include "utils/varlena.h"
 
+static int	log_min_messages_cmp(const ListCell *a, const ListCell *b);
+
 /*
  * DATESTYLE
  */
@@ -1289,6 +1291,9 @@ check_log_min_messages(char **newval, void **extra, GucSource source)
 {
 	char	   *rawstring;
 	List	   *elemlist;
+	StringInfoData buf;
+	char	   *result;
+	bool		first = true;
 	int			newlevel[BACKEND_NUM_TYPES];
 	bool		assigned[BACKEND_NUM_TYPES];
 	int			genericlevel = -1;	/* -1 means not assigned */
@@ -1455,8 +1460,39 @@ check_log_min_messages(char **newval, void **extra, GucSource source)
 			newlevel[i] = genericlevel;
 	}
 
+	/*
+	 * Use a stable representation of log_min_messages. The generic level is
+	 * always the first element and the other elements (type:level) are sorted
+	 * by process type. See log_min_messages_cmp for details.
+	 */
+	list_sort(elemlist, log_min_messages_cmp);
+
+	initStringInfo(&buf);
+	foreach_ptr(char, tok, elemlist)
+	{
+		if (first)
+		{
+			appendStringInfoString(&buf, tok);
+			first = false;
+		}
+		else
+		{
+			appendStringInfo(&buf, ", %s", tok);
+		}
+	}
+
+	result = (char *) guc_malloc(LOG, buf.len + 1);
+	if (!result)
+		return false;
+	memcpy(result, buf.data, buf.len);
+	result[buf.len] = '\0';
+
+	guc_free(*newval);
+	*newval = result;
+
 	guc_free(rawstring);
 	list_free(elemlist);
+	pfree(buf.data);
 
 	/*
 	 * Pass back data for assign_log_min_messages to use.
@@ -1469,6 +1505,20 @@ check_log_min_messages(char **newval, void **extra, GucSource source)
 	return true;
 }
 
+static int
+log_min_messages_cmp(const ListCell *a, const ListCell *b)
+{
+	const char *s = lfirst(a);
+	const char *t = lfirst(b);
+
+	if (strchr(s, ':') == NULL)
+		return -1;
+	else if (strchr(t, ':') == NULL)
+		return 1;
+	else
+		return strcmp(s, t);
+}
+
 /*
  * GUC assign_hook for log_min_messages
  */
diff --git a/src/test/regress/expected/guc.out b/src/test/regress/expected/guc.out
index 3b8f44566b2..9a6fd503009 100644
--- a/src/test/regress/expected/guc.out
+++ b/src/test/regress/expected/guc.out
@@ -972,7 +972,7 @@ SET log_min_messages TO 'backend:error, checkpointer:debug3, fatal, archiver:deb
 SHOW log_min_messages;
                                         log_min_messages                                         
 -------------------------------------------------------------------------------------------------
- backend:error, checkpointer:debug3, fatal, archiver:debug2, autovacuum:debug1, walsender:debug3
+ fatal, archiver:debug2, autovacuum:debug1, backend:error, checkpointer:debug3, walsender:debug3
 (1 row)
 
 SET log_min_messages TO 'warning, autovacuum:debug1';
@@ -986,6 +986,6 @@ SET log_min_messages TO 'autovacuum:debug1, warning';
 SHOW log_min_messages;
       log_min_messages      
 ----------------------------
- autovacuum:debug1, warning
+ warning, autovacuum:debug1
 (1 row)
 
-- 
2.39.5