Skip to content

Commit fef8f02

Browse files
peterepull[bot]
authored andcommitted
Add GUC backtrace_on_internal_error
When enabled (default off), this logs a backtrace anytime elog() or an equivalent ereport() for internal errors is called. This is not well covered by the existing backtrace_functions, because there are many equally-worded low-level errors in many functions. And if you find out where the error is, then you need to manually rewrite the elog() to ereport() to attach the errbacktrace(), which is annoying. Having a backtrace automatically on every elog() call could be very helpful during development for various kinds of common errors from palloc, syscache, node support, etc. Discussion: https://www.postgresql.org/message-id/flat/ba76c6bc-f03f-4285-bf16-47759cfcab9e@eisentraut.org
1 parent 85bff6b commit fef8f02

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

doc/src/sgml/config.sgml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11086,6 +11086,33 @@ dynamic_library_path = 'C:\tools\postgresql;H:\my_project\lib;$libdir'
1108611086
</listitem>
1108711087
</varlistentry>
1108811088

11089+
<varlistentry id="guc-backtrace-on-internal-error" xreflabel="backtrace_on_internal_error">
11090+
<term><varname>backtrace_on_internal_error</varname> (<type>boolean</type>)
11091+
<indexterm>
11092+
<primary><varname>backtrace_on_internal_error</varname> configuration parameter</primary>
11093+
</indexterm>
11094+
</term>
11095+
<listitem>
11096+
<para>
11097+
If this parameter is on and an error with error code XX000 (internal
11098+
error; see also <xref linkend="errcodes-appendix"/>) is raised, then a
11099+
backtrace is written to the server log together with the error
11100+
message. This can be used to debug such internal errors (which should
11101+
normally not happen in production). The default is off.
11102+
</para>
11103+
11104+
<para>
11105+
Backtrace support is not available on all platforms, and the quality
11106+
of the backtraces depends on compilation options.
11107+
</para>
11108+
11109+
<para>
11110+
Only superusers and users with the appropriate <literal>SET</literal>
11111+
privilege can change this setting.
11112+
</para>
11113+
</listitem>
11114+
</varlistentry>
11115+
1108911116
<varlistentry id="guc-debug-discard-caches" xreflabel="debug_discard_caches">
1109011117
<term><varname>debug_discard_caches</varname> (<type>integer</type>)
1109111118
<indexterm>

src/backend/utils/error/elog.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,11 @@ errfinish(const char *filename, int lineno, const char *funcname)
498498

499499
/* Collect backtrace, if enabled and we didn't already */
500500
if (!edata->backtrace &&
501-
edata->funcname &&
502-
backtrace_functions &&
503-
matches_backtrace_functions(edata->funcname))
501+
((edata->funcname &&
502+
backtrace_functions &&
503+
matches_backtrace_functions(edata->funcname)) ||
504+
(edata->sqlerrcode == ERRCODE_INTERNAL_ERROR &&
505+
backtrace_on_internal_error)))
504506
set_backtrace(edata, 2);
505507

506508
/*

src/backend/utils/misc/guc_tables.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ int log_temp_files = -1;
527527
double log_statement_sample_rate = 1.0;
528528
double log_xact_sample_rate = 0;
529529
char *backtrace_functions;
530+
bool backtrace_on_internal_error = false;
530531

531532
int temp_file_limit = -1;
532533

@@ -812,6 +813,16 @@ StaticAssertDecl(lengthof(config_type_names) == (PGC_ENUM + 1),
812813

813814
struct config_bool ConfigureNamesBool[] =
814815
{
816+
{
817+
{"backtrace_on_internal_error", PGC_SUSET, DEVELOPER_OPTIONS,
818+
gettext_noop("Log backtrace for any error with error code XX000 (internal error)."),
819+
NULL,
820+
GUC_NOT_IN_SAMPLE
821+
},
822+
&backtrace_on_internal_error,
823+
false,
824+
NULL, NULL, NULL
825+
},
815826
{
816827
{"enable_seqscan", PGC_USERSET, QUERY_TUNING_METHOD,
817828
gettext_noop("Enables the planner's use of sequential-scan plans."),

src/include/utils/guc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ extern PGDLLIMPORT int log_temp_files;
266266
extern PGDLLIMPORT double log_statement_sample_rate;
267267
extern PGDLLIMPORT double log_xact_sample_rate;
268268
extern PGDLLIMPORT char *backtrace_functions;
269+
extern PGDLLIMPORT bool backtrace_on_internal_error;
269270

270271
extern PGDLLIMPORT int temp_file_limit;
271272

0 commit comments

Comments
 (0)