forked from pgbouncer/pgbouncer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvarcache.c
307 lines (244 loc) · 7.29 KB
/
varcache.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* PgBouncer - Lightweight connection pooler for PostgreSQL.
*
* Copyright (c) 2007-2009 Marko Kreen, Skype Technologies OÜ
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Operations with server config parameters.
*/
#include "bouncer.h"
#include <usual/pgutil.h>
#include <usual/string.h>
#include "common/uthash_lowercase.h"
static int num_var_cached = 0;
struct var_lookup {
const char *name; /* key (string is WITHIN the structure) */
int idx;
UT_hash_handle hh; /* makes this structure hashable */
};
static struct var_lookup *lookup_map;
static struct StrPool *vpool;
static inline struct PStr *get_value(VarCache *cache, const struct var_lookup *lk)
{
return cache->var_list[lk->idx];
}
static bool sl_add(void *arg, const char *s)
{
return strlist_append(arg, s);
}
int get_num_var_cached(void)
{
return num_var_cached;
}
static void init_var_lookup_from_config(const char *cf_track_extra_parameters, int *num_vars)
{
char *var_name = NULL;
struct var_lookup *lookup = NULL;
struct StrList *sl = strlist_new(NULL);
if (!parse_word_list(cf_track_extra_parameters, sl_add, sl))
die("failed to parse track_extra_parameters in config %s", cf_track_extra_parameters);
while (!strlist_empty(sl)) {
var_name = strlist_pop(sl);
if (!var_name)
continue;
HASH_FIND_STR(lookup_map, var_name, lookup);
/* If the var name is already on the hash map, do not update its idx */
if (lookup != NULL)
continue;
lookup = (struct var_lookup *)malloc(sizeof *lookup);
lookup->name = strdup(var_name);
lookup->idx = (*num_vars)++;
HASH_ADD_KEYPTR(hh, lookup_map, lookup->name, strlen(lookup->name), lookup);
free(var_name);
}
strlist_free(sl);
}
void init_var_lookup(const char *cf_track_extra_parameters)
{
const char *names[] = { "DateStyle", "client_encoding", "TimeZone", "standard_conforming_strings", "application_name", NULL };
int idx = 0;
struct var_lookup *lookup = NULL;
/* Always add the static list of names for compatibility */
for (; names[idx]; idx++) {
lookup = (struct var_lookup *)malloc(sizeof *lookup);
lookup->name = names[idx];
lookup->idx = idx;
HASH_ADD_KEYPTR(hh, lookup_map, lookup->name, strlen(lookup->name), lookup);
}
init_var_lookup_from_config(cf_track_extra_parameters, &idx);
num_var_cached = idx;
}
bool varcache_set(VarCache *cache, const char *key, const char *value)
{
const struct var_lookup *lk = NULL;
struct PStr *pstr = NULL;
if (!vpool) {
vpool = strpool_create(USUAL_ALLOC);
if (!vpool)
return false;
}
HASH_FIND_STR(lookup_map, key, lk);
if (lk == NULL)
return false;
/* drop old value */
strpool_decref(cache->var_list[lk->idx]);
cache->var_list[lk->idx] = NULL;
/* NULL value? */
if (!value)
return false;
/* set new value */
pstr = strpool_get(vpool, value, strlen(value));
if (!pstr)
return false;
cache->var_list[lk->idx] = pstr;
return true;
}
static bool variable_is_guc_list_quote(const char *key)
{
if (strcasecmp("search_path", key) == 0)
return true;
return false;
}
static int apply_var(PktBuf *pkt, const char *key,
const struct PStr *cval,
const struct PStr *sval)
{
char buf[300];
char qbuf[128];
unsigned len;
const char *tmp;
/* if unset, skip */
if (!cval || !sval)
return 0;
/* if equal, skip */
if (cval == sval)
return 0;
/* ignore case difference */
if (strcasecmp(cval->str, sval->str) == 0)
return 0;
/* parameters that are marked GUC_LIST_QUOTE are returned already fully quoted
* re-quoting them using pg_quote_literal will result in malformed values. */
if (variable_is_guc_list_quote(key)) {
/* zero length elements of the form "" should be specially handled.*/
if (strcmp(cval->str, "\"\"") == 0) {
tmp = "''";
} else {
tmp = cval->str;
}
} else if (pg_quote_literal(qbuf, cval->str, sizeof(qbuf))) {
tmp = qbuf;
} else {
return 0;
}
/* add SET statement to packet */
len = snprintf(buf, sizeof(buf), "SET %s=%s;", key, tmp);
if (len < sizeof(buf)) {
pktbuf_put_bytes(pkt, buf, len);
} else {
char *buf2 = malloc(sizeof(char)*len);
if (!buf2)
die("failed to allocate memory in apply_var");
snprintf(buf2, len, "SET %s=%s;", key, tmp);
pktbuf_put_bytes(pkt, buf2, len);
free(buf2);
}
return 1;
}
bool varcache_apply(PgSocket *server, PgSocket *client, bool *changes_p)
{
int changes = 0;
struct PStr *cval, *sval;
const struct var_lookup *lk, *tmp;
int sql_ofs;
struct PktBuf *pkt = pktbuf_temp();
pktbuf_start_packet(pkt, PqMsg_Query);
/* grab query position inside pkt */
sql_ofs = pktbuf_written(pkt);
HASH_ITER(hh, lookup_map, lk, tmp) {
sval = get_value(&server->vars, lk);
cval = get_value(&client->vars, lk);
changes += apply_var(pkt, lk->name, cval, sval);
}
*changes_p = changes > 0;
if (!changes)
return true;
pktbuf_put_char(pkt, 0);
pktbuf_finish_packet(pkt);
slog_debug(server, "varcache_apply: %s", pkt->buf + sql_ofs);
return pktbuf_send_immediate(pkt, server);
}
void varcache_set_canonical(PgSocket *server, PgSocket *client)
{
struct PStr *server_val, *client_val;
const struct var_lookup *lk, *tmp;
HASH_ITER(hh, lookup_map, lk, tmp) {
server_val = server->vars.var_list[lk->idx];
client_val = client->vars.var_list[lk->idx];
if (client_val && server_val && client_val != server_val) {
slog_debug(client, "varcache_set_canonical: setting %s to its canonical version %s -> %s",
lk->name, client_val->str, server_val->str);
strpool_incref(server_val);
strpool_decref(client_val);
client->vars.var_list[lk->idx] = server_val;
}
}
}
void varcache_apply_startup(PktBuf *pkt, PgSocket *client)
{
const struct var_lookup *lk, *tmp;
HASH_ITER(hh, lookup_map, lk, tmp) {
struct PStr *val = get_value(&client->vars, lk);
if (!val)
continue;
slog_debug(client, "varcache_apply_startup: %s=%s", lk->name, val->str);
pktbuf_put_string(pkt, lk->name);
pktbuf_put_string(pkt, val->str);
}
}
void varcache_fill_unset(VarCache *src, PgSocket *dst)
{
struct PStr *srcval, *dstval;
const struct var_lookup *lk, *tmp;
HASH_ITER(hh, lookup_map, lk, tmp) {
srcval = src->var_list[lk->idx];
dstval = dst->vars.var_list[lk->idx];
if (!dstval) {
strpool_incref(srcval);
dst->vars.var_list[lk->idx] = srcval;
}
}
}
void varcache_clean(VarCache *cache)
{
for (int i = 0; i < num_var_cached; i++) {
strpool_decref(cache->var_list[i]);
cache->var_list[i] = NULL;
}
}
void varcache_add_params(PktBuf *pkt, VarCache *vars)
{
struct PStr *val;
const struct var_lookup *lk, *tmp;
HASH_ITER(hh, lookup_map, lk, tmp) {
val = vars->var_list[lk->idx];
if (val)
pktbuf_write_ParameterStatus(pkt, lk->name, val->str);
}
}
void varcache_deinit(void)
{
strpool_free(vpool);
vpool = NULL;
}