5
5
#include "postgres.h"
6
6
#include "miscadmin.h"
7
7
#include "utils/guc.h"
8
+ #include "tcop/utility.h"
8
9
#include "executor/executor.h"
9
10
10
11
#ifdef PG_MODULE_MAGIC
@@ -24,6 +25,7 @@ PG_FUNCTION_INFO_V1(pg_backtrace_sigsegv);
24
25
static int backtrace_level = ERROR ;
25
26
static ErrorContextCallback backtrace_callback ;
26
27
static ExecutorRun_hook_type prev_executor_run_hook ;
28
+ static ProcessUtility_hook_type prev_utility_hook ;
27
29
static bool inside_signal_handler ;
28
30
static pqsigfunc signal_handlers [_NSIG ];
29
31
@@ -52,11 +54,7 @@ backtrace_callback_function(void* arg)
52
54
}
53
55
}
54
56
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 )
60
58
{
61
59
ErrorContextCallback * esp ;
62
60
for (esp = error_context_stack ; esp != NULL && esp != & backtrace_callback ; esp = esp -> previous );
@@ -66,13 +64,37 @@ backtrace_executor_run_hook(QueryDesc *queryDesc,
66
64
backtrace_callback .previous = error_context_stack ;
67
65
error_context_stack = & backtrace_callback ;
68
66
}
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 ();
69
76
if (prev_executor_run_hook )
70
77
(* prev_executor_run_hook )(queryDesc , direction , count , execute_once );
71
78
else
72
79
standard_ExecutorRun (queryDesc , direction , count , execute_once );
73
80
}
74
81
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
+ }
76
98
77
99
static void
78
100
backtrace_handler (SIGNAL_ARGS )
@@ -111,6 +133,9 @@ void _PG_init(void)
111
133
prev_executor_run_hook = ExecutorRun_hook ;
112
134
ExecutorRun_hook = backtrace_executor_run_hook ;
113
135
136
+ prev_utility_hook = ProcessUtility_hook ;
137
+ ProcessUtility_hook = backtrace_utility_hook ;
138
+
114
139
DefineCustomEnumVariable ("pg_backtrace.level" ,
115
140
"Set error level for dumping backtrace" ,
116
141
NULL ,
@@ -123,6 +148,7 @@ void _PG_init(void)
123
148
void _PG_fini (void )
124
149
{
125
150
ExecutorRun_hook = prev_executor_run_hook ;
151
+ ProcessUtility_hook = prev_utility_hook ;
126
152
pqsignal (SIGSEGV , signal_handlers [SIGSEGV ]);
127
153
pqsignal (SIGBUS , signal_handlers [SIGBUS ]);
128
154
pqsignal (SIGFPE , signal_handlers [SIGFPE ]);
0 commit comments