-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpgloggingfilter.c
249 lines (222 loc) · 7.6 KB
/
pgloggingfilter.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Logging filters for PostgreSQL.
*
* Copyright (c) 2014, Oskari Saarenmaa <[email protected]>
* All rights reserved.
*
* This file is under the Apache License, Version 2.0.
* See the file `LICENSE` for details.
*
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "utils/guc.h"
PG_MODULE_MAGIC;
void _PG_init(void);
void _PG_fini(void);
/* generated perfect hash function for sqlstates */
#include "sqlstatehashfunc.c"
/* the actual hash table */
typedef struct SqlstateLogFilterStruct
{
int loglevel;
int stmt_loglevel;
} SqlstateLogFilter;
static SqlstateLogFilter filter_by_sqlstate_hash[HASH_SQLSTATE_MODULO];
static char *str_log_min_messages_by_sqlstate;
static char *str_log_min_error_statement_by_sqlstate;
static emit_log_hook_type prev_emit_log_hook;
static bool check_filter_by_sqlstate(char **newval, void **extra, GucSource source);
static void assign_filter_by_sqlstate_msgs(const char *newval, void *extra);
static void assign_filter_by_sqlstate_stmt(const char *newval, void *extra);
static void pglf_log_hook(ErrorData *edata);
/* Copied from src/backend/utils/misc/guc.c */
static const struct config_enum_entry server_message_level_options[] = {
{"debug", DEBUG2, true},
{"debug5", DEBUG5, false},
{"debug4", DEBUG4, false},
{"debug3", DEBUG3, false},
{"debug2", DEBUG2, false},
{"debug1", DEBUG1, false},
{"info", INFO, false},
{"notice", NOTICE, false},
{"warning", WARNING, false},
{"error", ERROR, false},
{"log", LOG, false},
{"fatal", FATAL, false},
{"panic", PANIC, false},
{NULL, 0, false}
};
void _PG_init(void)
{
int i;
for (i=0; i<HASH_SQLSTATE_MODULO; i++)
{
filter_by_sqlstate_hash[i].loglevel = -1;
filter_by_sqlstate_hash[i].stmt_loglevel = -1;
}
DefineCustomStringVariable("pgloggingfilter.log_min_messages_by_sqlstate",
"Increases minimum error level required to log "
"errors per SQLSTATE.",
"Comma-separated list of SQLSTATE:ERRORLEVEL "
"pairs which can be used to suppress error "
"logging for some SQLSTATEs. This option "
"can not enable logging for errors which "
"are suppressed by log_min_messages.",
&str_log_min_messages_by_sqlstate,
NULL,
PGC_SUSET,
GUC_LIST_INPUT,
check_filter_by_sqlstate,
assign_filter_by_sqlstate_msgs,
NULL);
DefineCustomStringVariable("pgloggingfilter.log_min_error_statement_by_sqlstate",
"Increases minimum error level required to log "
"statements casuing errors per SQLSTATE.",
"Comma-separated list of SQLSTATE:ERRORLEVEL "
"pairs which can be used to suppress "
"logging of statements that caused errors "
"based on their SQLSTATEs. This option "
"can not enable logging for errors which "
"are suppressed by "
"log_min_error_statement.",
&str_log_min_error_statement_by_sqlstate,
NULL,
PGC_SUSET,
GUC_LIST_INPUT,
check_filter_by_sqlstate,
assign_filter_by_sqlstate_stmt,
NULL);
prev_emit_log_hook = emit_log_hook;
emit_log_hook = pglf_log_hook;
}
/*
* This is called when we're being unloaded from a process. Note that this
* only happens when we're being replaced by a LOAD (it doesn't happen on
* process exit), so we can't depend on it being called.
*/
void _PG_fini(void)
{
if (emit_log_hook == pglf_log_hook)
{
emit_log_hook = prev_emit_log_hook;
prev_emit_log_hook = NULL;
}
}
static void
pglf_log_hook(ErrorData *edata)
{
SqlstateLogFilter *filter;
if (!edata->output_to_server)
return; /* nothing we can do about this */
filter = &filter_by_sqlstate_hash[hash_sqlstate(edata->sqlerrcode)];
if (filter->loglevel != -1 && edata->elevel < filter->loglevel)
edata->output_to_server = false; /* suppress */
if (filter->stmt_loglevel != -1 && edata->elevel < filter->stmt_loglevel)
edata->hide_stmt = true;
}
/*
* check_filter_by_sqlstate - validate sqlstate:errorlevel
* lists and create a (perfect) hash table from them.
*/
static bool
check_filter_by_sqlstate(char **newval, void **extra, GucSource source)
{
char *rawstring;
List *elemlist;
ListCell *l;
int i;
int *new_table;
bool success = true;
if (!*newval)
{
*extra = NULL;
return true;
}
/* Need a modifiable copy of string */
rawstring = pstrdup(*newval);
/* Parse string into list of identifiers */
if (!SplitIdentifierString(rawstring, ',', &elemlist))
{
/* syntax error in list */
GUC_check_errdetail("List syntax is invalid.");
pfree(rawstring);
list_free(elemlist);
return false;
}
/* GUC wants malloced results so allocate a new mapping */
new_table = (int *) malloc(sizeof(int) * HASH_SQLSTATE_MODULO);
if (!new_table)
{
pfree(rawstring);
list_free(elemlist);
return false;
}
for (i=0; i<HASH_SQLSTATE_MODULO; i++)
new_table[i] = -1;
/* validate list and insert the results in the hash table */
foreach (l, elemlist)
{
const struct config_enum_entry *enum_entry;
char *tok = lfirst(l), *level_str = strchr(tok, ':');
int sqlstate, loglevel = -1;
if (level_str != NULL && (level_str - tok) == 5)
{
for (enum_entry = server_message_level_options;
enum_entry && enum_entry->name;
enum_entry++)
{
if (pg_strcasecmp(enum_entry->name, level_str + 1) == 0)
{
loglevel = enum_entry->val;
break;
}
}
}
if (loglevel < 0)
{
GUC_check_errdetail("Invalid sqlstate error definition: \"%s\".", tok);
success = false;
break;
}
sqlstate = MAKE_SQLSTATE(pg_ascii_toupper(tok[0]), pg_ascii_toupper(tok[1]),
pg_ascii_toupper(tok[2]), pg_ascii_toupper(tok[3]),
pg_ascii_toupper(tok[4]));
new_table[hash_sqlstate(sqlstate)] = loglevel;
}
pfree(rawstring);
list_free(elemlist);
if (!success)
{
free(new_table);
return false;
}
*extra = new_table;
return true;
}
/*
* assign_filter_by_sqlstate_msgs - take the result generated by
* check_filter_by_sqlstate into use for messages.
* 'extra' contains the new hash table.
*/
static void
assign_filter_by_sqlstate_msgs(const char *newval, void *extra)
{
unsigned int *new_table = (unsigned int *) extra;
int i;
for (i=0; i<HASH_SQLSTATE_MODULO; i++)
filter_by_sqlstate_hash[i].loglevel = new_table ? new_table[i] : -1;
}
/*
* assign_filter_by_sqlstate_stmt - take the result generated by
* check_filter_by_sqlstate into use for statements.
* 'extra' contains the new hash table.
*/
static void
assign_filter_by_sqlstate_stmt(const char *newval, void *extra)
{
unsigned int *new_table = (unsigned int *) extra;
int i;
for (i=0; i<HASH_SQLSTATE_MODULO; i++)
filter_by_sqlstate_hash[i].stmt_loglevel = new_table ? new_table[i] : -1;
}