-
Notifications
You must be signed in to change notification settings - Fork 2
/
pg_query_settings.c
403 lines (331 loc) · 10.6 KB
/
pg_query_settings.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*-------------------------------------------------------------------------------------------------
*
* pg_query_settings.c
* Modify one or more GUC parameters on the fly for some queries, based on their query ID.
*
*
* Copyright (c) 2022-2024, Dalibo (Franck Boudehen, Frédéric Yhuel, Guillaume Lelarge, Thibaud Walkowiak)
*
*-------------------------------------------------------------------------------------------------
*/
#ident "pg_query_settings version 0.1"
/* Headers */
#include <postgres.h>
#include <access/heapam.h>
#include <catalog/namespace.h>
#include <miscadmin.h>
#include <executor/executor.h>
#if (PG_VERSION_NUM >= 140000) && (PG_VERSION_NUM < 160000)
#include <utils/queryjumble.h>
#elif (PG_VERSION_NUM >= 160000)
#include <nodes/queryjumble.h>
#endif
#include <optimizer/planner.h>
#include <storage/bufmgr.h>
#include <utils/builtins.h>
#include <utils/guc.h>
#include <lib/ilist.h>
/* This is a module :) */
PG_MODULE_MAGIC;
/* Macro definitions */
#if PG_VERSION_NUM < 140000
#define COMPUTE_LOCAL_QUERYID 1
#else
#define COMPUTE_LOCAL_QUERYID 0
#endif
#if COMPUTE_LOCAL_QUERYID
#include "pgsp_queryid.h"
#include "parser/analyze.h"
#endif
/* Function definitions */
void _PG_init(void);
/* Variables */
static bool enabled = true;
static bool debug = false;
static bool printQueryId = false;
static slist_head paramResetList = SLIST_STATIC_INIT(paramResetList);
#if PG_VERSION_NUM < 130000
static char * pgqs_queryString = NULL;
#endif
/* Constants */
/* Name of our config table */
static const char* pgqs_config ="pgqs_config";
/* Parameter struct */
typedef struct parameter
{
char *name;
char *oldValue;
slist_node node;
} parameter;
/* Our hooks on PostgreSQL */
static planner_hook_type prevHook = NULL;
static ExecutorEnd_hook_type prev_ExecutorEnd = NULL;
#if PG_VERSION_NUM < 130000
static post_parse_analyze_hook_type prev_post_parse_analyze_hook = NULL;
static void pgqs_post_parse_analyze(ParseState *pstate, Query *query);
#endif
// -----------------------------------------------------------------
/* Functions */
#if PG_VERSION_NUM < 130000
static void pgqs_post_parse_analyze(ParseState *pstate, Query *query)
{
/* here we get the query string and put it in pgqs_queryString.
*/
if (debug) elog (DEBUG1,"Entering pgqs_post_parse_analyze");
if (prev_post_parse_analyze_hook)
prev_post_parse_analyze_hook(pstate, query);
if (debug) elog (DEBUG1,"setting pgqs_queryString to \"%s\"",pstate->p_sourcetext);
pgqs_queryString = pstrdup(pstate->p_sourcetext);
if (debug) elog (DEBUG1,"Exiting pgqs_post_parse_analyze");
}
#endif //COMPUTE_LOCAL_QUERYID = 1
/*
* Destroy the list of parameters.
* If 'reset' is true, then restore value of each parameter.
*/
static void DestroyPRList(bool reset)
{
slist_mutable_iter iter;
parameter *param;
if (debug) elog(DEBUG1, "Destroy paramResetList");
slist_foreach_modify(iter, ¶mResetList)
{
param = slist_container(parameter, node, iter.cur);
if (reset)
{
if (debug) elog(DEBUG1, "Reset guc %s", param->name);
SetConfigOption(param->name, param->oldValue, PGC_USERSET, PGC_S_SESSION);
}
slist_delete_current(&iter);
free(param);
}
}
/*
* planner hook function: this is where we set all our GUC if we find a
* configuration for the query
*
* The function scans our 'pgqs_config' table, and for each matching tuple, we
* call SetConfigOption() to set the runtime parameter.
* We also build a list of these parameters so that we can restore them to their
* default values afterwards.
*/
static PlannedStmt *
#if PG_VERSION_NUM < 130000
execPlantuner(Query *parse, int cursorOptions, ParamListInfo boundp)
#else
execPlantuner(Query *parse, const char *query_st, int cursorOptions, ParamListInfo boundp)
#endif
{
PlannedStmt *result;
Relation config_rel;
HeapTuple config_tuple;
Oid config_relid;
TableScanDesc config_scan;
Datum data;
bool isnull;
bool rethrow = false;
int64 id;
char *guc_value = NULL;
char *guc_name = NULL;
const char *oldValue = NULL;
parameter *param = NULL;
uint64 queryid = 0;
if (enabled)
{
config_relid = RelnameGetRelid(pgqs_config);
if (OidIsValid(config_relid))
{
#if PG_VERSION_NUM < 130000
char * query_st;
query_st = pgqs_queryString;
if (debug) elog(DEBUG1,"query_st=%s", query_st);
if (debug) elog(DEBUG1,"pgqs_queryString=%s", pgqs_queryString);
#endif
#if COMPUTE_LOCAL_QUERYID
queryid = hash_query(query_st);
#else
queryid = parse->queryId;
#endif
if (printQueryId) elog(NOTICE, "QueryID is '%li'", queryid);
if (debug) elog(DEBUG1, "query's QueryID is '%li'", queryid);
config_rel = table_open(config_relid, AccessShareLock);
config_scan = table_beginscan(config_rel, GetActiveSnapshot(), 0, NULL);
while ((config_tuple = heap_getnext(config_scan, ForwardScanDirection)) != NULL)
{
/* Get the queryid in the currently read tuple. */
data = heap_getattr(config_tuple, 1, config_rel->rd_att, &isnull);
id = DatumGetInt64(data);
if (debug) elog(DEBUG1, "Config QueryID is '%li'", id);
/* Compare the queryid previously obtained with the queryid
* of the current query. */
if (queryid == id)
{
/* Get the name of the parameter (table field : 'param'). */
data = heap_getattr(config_tuple, 2, config_rel->rd_att, &isnull);
guc_name = pstrdup(TextDatumGetCString(data));
/* Get the value for the parameter (table field : 'value'). */
data = heap_getattr(config_tuple, 3, config_rel->rd_att, &isnull);
guc_value = pstrdup(TextDatumGetCString(data));
param = malloc(sizeof(parameter));
param->name = pstrdup(guc_name);
/* Get and store current value for the parameter. */
oldValue = GetConfigOption(guc_name, true, false);
if (oldValue == NULL)
{
elog(WARNING, "Parameter %s does not exists", guc_name);
continue;
}
param->oldValue = pstrdup(oldValue);
slist_push_head(¶mResetList, ¶m->node);
/*
* Here we use the PostgreSQL try/catch mecanism so that when
* SetConfigOption() returns an error, the current transaction
* is rollbacked and its error message is logged. Such an
* error message could be like:
* 'ERROR: unrecognized configuration parameter "Dalibo"'
* or like:
* 'ERROR: invalid value for parameter "work_mem": "512KB"'.
*/
PG_TRY();
{
elog(DEBUG1, "Setting %s = %s", guc_name,guc_value);
SetConfigOption(guc_name, guc_value, PGC_USERSET, PGC_S_SESSION);
}
PG_CATCH();
{
rethrow = true;
/* Current transaction will be rollbacked when exception is
* re-thrown, so there's no need to reset the parameters that
* may have successfully been set. Let's just destroy the list.
*/
DestroyPRList(false);
goto close;
}
PG_END_TRY();
}
}
close:
table_endscan(config_scan);
table_close(config_rel, AccessShareLock);
if (rethrow)
{
PG_RE_THROW();
}
}
}
/*
* Call next hook if it exists
*/
if (prevHook)
#if PG_VERSION_NUM < 130000
result = prevHook(parse, cursorOptions, boundp);
#else
result = prevHook(parse, query_st, cursorOptions, boundp);
#endif
else
#if PG_VERSION_NUM < 130000
result = standard_planner(parse, cursorOptions, boundp);
#else
result = standard_planner(parse, query_st, cursorOptions, boundp);
#endif
return result;
}
/*
* executor hook function: this is where we reset all our GUC for a specific
* query
*/
static void
PlanTuner_ExecutorEnd(QueryDesc *q)
{
/* FIXME: here, we restore the value of each parameter, but that's a problem
* for prepared queries. More specifically:
* for a given parameter `P`, if the generic plan is selected, and if the executor
* needs to fetch the value of `P`, then it won't get the value specified in the
* `pgqs_config` table.
* For example, the executor fetches the value of `work_mem`
* in order to decide whether to perform an in-memory sort, but at this point the
* value would have been reset to its default (at the end of the PREPARE statement).
* However, there's no problem with parameters that only affect planification, such
* as max_parallel_workers_per_gather. */
DestroyPRList(true);
if (prev_ExecutorEnd)
prev_ExecutorEnd(q);
else
standard_ExecutorEnd(q);
}
/*
* Initialize our library to set hooks and define our GUCs
*/
void
_PG_init(void)
{
/* Create a GUC variable named pg_query_settings.enabled
* used to enable or disable this module. */
DefineCustomBoolVariable(
"pg_query_settings.enabled",
"Disable pg_query_settings module",
"Disable pg_query_settings module",
&enabled,
true,
PGC_USERSET,
GUC_EXPLAIN,
NULL,
NULL,
NULL
);
/* Create a GUC variable named pg_query_settings.debug
* used to print debugging messages. */
DefineCustomBoolVariable(
"pg_query_settings.debug",
"Print debugging messages",
"Print debugging messages",
&debug,
false,
PGC_USERSET,
0,
NULL,
NULL,
NULL
);
/* Create a GUC variable named pg_query_settings.print_queryid
* used to print query identifier with NOTICE level. */
DefineCustomBoolVariable(
"pg_query_settings.print_queryid",
"Print query identifier",
"Print query identifier",
&printQueryId,
false,
PGC_USERSET,
0,
NULL,
NULL,
NULL
);
/* Reserve the GUC prefix */
#if PG_VERSION_NUM < 150000
EmitWarningsOnPlaceholders("pg_query_settings");
#else
MarkGUCPrefixReserved("pg_query_settings");
#endif
#if PG_VERSION_NUM >= 140000
/* Inform core that we require a query identifier to be computed */
EnableQueryId();
#endif
if (debug) elog(DEBUG1,"Entering _PG_init()");
/* Set our two hooks */
if (planner_hook != execPlantuner)
{
prevHook = planner_hook;
planner_hook = execPlantuner;
}
if (ExecutorEnd_hook != PlanTuner_ExecutorEnd)
{
prev_ExecutorEnd = ExecutorEnd_hook;
ExecutorEnd_hook = PlanTuner_ExecutorEnd;
}
#if PG_VERSION_NUM < 130000
prev_post_parse_analyze_hook = post_parse_analyze_hook;
post_parse_analyze_hook = pgqs_post_parse_analyze;
#endif
if (debug) elog(DEBUG1,"Exiting _PG_init()");
}