-
Notifications
You must be signed in to change notification settings - Fork 0
/
hide_statements_in_log.c
83 lines (70 loc) · 2.34 KB
/
hide_statements_in_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "postgres.h"
#include "fmgr.h"
#include "utils/guc.h"
PG_MODULE_MAGIC;
void _PG_init(void);
void _PG_fini(void);
static void do_emit_log(ErrorData *errorData);
static void hide_statements_in_log(ErrorData *errorData);
static emit_log_hook_type prev_emit_log_hook = NULL;
static bool guc_delete_log_entry = true;
static char *log_dummy_message = NULL;
void _PG_init(void) {
DefineCustomBoolVariable("hide_statements_in_log.delete_log",
"Delete log entry with statement.",
NULL,
&guc_delete_log_entry,
true,
PGC_USERSET,
0,
NULL,
NULL,
NULL);
DefineCustomStringVariable("hide_statements_in_log.dummy_message",
"Replace statement in log entry with text.",
NULL,
&log_dummy_message,
"message is hidden",
PGC_USERSET,
0,
NULL,
NULL,
NULL);
prev_emit_log_hook = emit_log_hook;
emit_log_hook = do_emit_log;
}
void _PG_fini(void) {
if (emit_log_hook == do_emit_log)
emit_log_hook = prev_emit_log_hook;
}
static void do_emit_log(ErrorData *errorData) {
static bool in_hook = false;
if (prev_emit_log_hook)
prev_emit_log_hook(errorData);
if (!in_hook) {
in_hook = true;
hide_statements_in_log(errorData);
in_hook = false;
}
}
#define CLEAN_ERROR_DATA(errorData, target_field) \
{ \
if (guc_delete_log_entry) { \
errorData->output_to_server = false; \
} else { \
pfree(errorData->target_field); \
errorData->target_field = pstrdup(log_dummy_message); \
} \
}
static void hide_statements_in_log(ErrorData *errorData) {
errorData->hide_stmt = true;
errorData->hide_ctx = true;
if (errorData->elevel == LOG) {
if (errorData->message) {
CLEAN_ERROR_DATA(errorData, message);
}
}
if (errorData->detail) {
CLEAN_ERROR_DATA(errorData, detail);
}
}