Skip to content

Commit 662ffa8

Browse files
committed
Handle errors in utility statements
1 parent 1413536 commit 662ffa8

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

pg_backtrace.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "postgres.h"
66
#include "miscadmin.h"
77
#include "utils/guc.h"
8+
#include "tcop/utility.h"
89
#include "executor/executor.h"
910

1011
#ifdef PG_MODULE_MAGIC
@@ -24,6 +25,7 @@ PG_FUNCTION_INFO_V1(pg_backtrace_sigsegv);
2425
static int backtrace_level = ERROR;
2526
static ErrorContextCallback backtrace_callback;
2627
static ExecutorRun_hook_type prev_executor_run_hook;
28+
static ProcessUtility_hook_type prev_utility_hook;
2729
static bool inside_signal_handler;
2830
static pqsigfunc signal_handlers[_NSIG];
2931

@@ -52,11 +54,7 @@ backtrace_callback_function(void* arg)
5254
}
5355
}
5456

55-
static void
56-
backtrace_executor_run_hook(QueryDesc *queryDesc,
57-
ScanDirection direction,
58-
uint64 count,
59-
bool execute_once)
57+
static void backtrace_register_error_callback(void)
6058
{
6159
ErrorContextCallback* esp;
6260
for (esp = error_context_stack; esp != NULL && esp != &backtrace_callback; esp = esp->previous);
@@ -66,13 +64,37 @@ backtrace_executor_run_hook(QueryDesc *queryDesc,
6664
backtrace_callback.previous = error_context_stack;
6765
error_context_stack = &backtrace_callback;
6866
}
67+
}
68+
69+
static void
70+
backtrace_executor_run_hook(QueryDesc *queryDesc,
71+
ScanDirection direction,
72+
uint64 count,
73+
bool execute_once)
74+
{
75+
backtrace_register_error_callback();
6976
if (prev_executor_run_hook)
7077
(*prev_executor_run_hook)(queryDesc, direction, count, execute_once);
7178
else
7279
standard_ExecutorRun(queryDesc, direction, count, execute_once);
7380
}
7481

75-
82+
static void backtrace_utility_hook(PlannedStmt *pstmt,
83+
const char *queryString, ProcessUtilityContext context,
84+
ParamListInfo params,
85+
QueryEnvironment *queryEnv,
86+
DestReceiver *dest, char *completionTag)
87+
{
88+
backtrace_register_error_callback();
89+
if (prev_utility_hook)
90+
(*prev_utility_hook)(pstmt, queryString,
91+
context, params, queryEnv,
92+
dest, completionTag);
93+
else
94+
standard_ProcessUtility(pstmt, queryString,
95+
context, params, queryEnv,
96+
dest, completionTag);
97+
}
7698

7799
static void
78100
backtrace_handler(SIGNAL_ARGS)
@@ -111,6 +133,9 @@ void _PG_init(void)
111133
prev_executor_run_hook = ExecutorRun_hook;
112134
ExecutorRun_hook = backtrace_executor_run_hook;
113135

136+
prev_utility_hook = ProcessUtility_hook;
137+
ProcessUtility_hook = backtrace_utility_hook;
138+
114139
DefineCustomEnumVariable("pg_backtrace.level",
115140
"Set error level for dumping backtrace",
116141
NULL,
@@ -123,6 +148,7 @@ void _PG_init(void)
123148
void _PG_fini(void)
124149
{
125150
ExecutorRun_hook = prev_executor_run_hook;
151+
ProcessUtility_hook = prev_utility_hook;
126152
pqsignal(SIGSEGV, signal_handlers[SIGSEGV]);
127153
pqsignal(SIGBUS, signal_handlers[SIGBUS]);
128154
pqsignal(SIGFPE, signal_handlers[SIGFPE]);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy