I would like to be able to add backtraces to all ERROR logs. This is
useful to me, because during postgres or extension development any
error that I hit is usually unexpected. This avoids me from having to
change backtrace_functions every time I get an error based on the
function name listed in the LOCATION output (added by "\set VERBOSITY
verbose").

Attached is a trivial patch that starts supporting
backtrace_functions='*'. By setting that in postgresql.conf for my dev
environment it starts logging backtraces always.

The main problem it currently has is that it adds backtraces to all
LOG level logs too. So probably we want to make backtrace_functions
only log backtraces for ERROR and up (or maybe WARNING/NOTICE and up),
or add a backtrace_functions_level GUC too control this behaviour. The
docs of backtrace_functions currently heavily suggest that it should
only be logging backtraces for errors, so either we actually start
doing that or we should clarify the docs (emphasis mine):

> This parameter contains a comma-separated list of C function
> names. If an **error** is raised and the name of the internal C function
> where the **error** happens matches a value in the list, then a
> backtrace is written to the server log together with the error
> message. This can be used to debug specific areas of the
> source code.
From dacb18f62e7794d8165de80b811c994d384dc060 Mon Sep 17 00:00:00 2001
From: Jelte Fennema-Nio <jelte.fennema@microsoft.com>
Date: Wed, 20 Dec 2023 11:41:18 +0100
Subject: [PATCH v1] Add wildcard support to backtrace_functions GUC

With this change setting backtrace_functions to '*' will start logging
backtraces for all errors (or more precisely all logs).
---
 doc/src/sgml/config.sgml       | 5 +++++
 src/backend/utils/error/elog.c | 8 +++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 44cada2b403..9b352109a3e 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -11015,6 +11015,11 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
         be used to debug specific areas of the source code.
        </para>
 
+       <para>
+        A single <literal>*</literal> character is interpreted as a wildcard and
+        will cause all errors in the log to contain backtraces.
+       </para>
+
        <para>
         Backtrace support is not available on all platforms, and the quality
         of the backtraces depends on compilation options.
diff --git a/src/backend/utils/error/elog.c b/src/backend/utils/error/elog.c
index 6ea575a53b1..840fbe42ac5 100644
--- a/src/backend/utils/error/elog.c
+++ b/src/backend/utils/error/elog.c
@@ -840,6 +840,8 @@ matches_backtrace_functions(const char *funcname)
 		if (*p == '\0')			/* end of backtrace_symbol_list */
 			break;
 
+		if (strcmp("*", p) == 0)
+			return true;
 		if (strcmp(funcname, p) == 0)
 			return true;
 		p += strlen(p) + 1;
@@ -2128,14 +2130,14 @@ check_backtrace_functions(char **newval, void **extra, GucSource source)
 	int			j;
 
 	/*
-	 * Allow characters that can be C identifiers and commas as separators, as
-	 * well as some whitespace for readability.
+	 * Allow characters that can be C identifiers, commas as separators, the
+	 * wildcard symbol, as well as some whitespace for readability.
 	 */
 	validlen = strspn(*newval,
 					  "0123456789_"
 					  "abcdefghijklmnopqrstuvwxyz"
 					  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
-					  ", \n\t");
+					  ",* \n\t");
 	if (validlen != newvallen)
 	{
 		GUC_check_errdetail("invalid character");

base-commit: 00498b718564cee3530b76d860b328718aed672b
-- 
2.34.1

Reply via email to