forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaqo.c
380 lines (331 loc) · 10.3 KB
/
aqo.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
/*
* aqo.c
* Adaptive query optimization extension
*
* Copyright (c) 2016-2021, Postgres Professional
*
* IDENTIFICATION
* aqo/aqo.c
*/
#include "postgres.h"
#include "access/relation.h"
#include "access/table.h"
#include "catalog/pg_extension.h"
#include "commands/extension.h"
#include "miscadmin.h"
#include "utils/selfuncs.h"
#include "aqo.h"
#include "cardinality_hooks.h"
#include "ignorance.h"
#include "path_utils.h"
#include "preprocessing.h"
#include "profile_mem.h"
PG_MODULE_MAGIC;
void _PG_init(void);
#define AQO_MODULE_MAGIC (1234)
/* Strategy of determining feature space for new queries. */
int aqo_mode;
bool aqo_enabled = false; /* Signals that CREATE EXTENSION have executed and
all extension tables is ready for use. */
bool force_collect_stat;
/*
* Show special info in EXPLAIN mode.
*
* aqo_show_hash - show query class (hash) and a feature space value (hash)
* of each plan node. This is instance-dependent value and can't be used
* in regression and TAP tests.
*
* aqo_show_details - show AQO settings for this class and prediction
* for each plan node.
*/
bool aqo_show_hash;
bool aqo_show_details;
/* GUC variables */
static const struct config_enum_entry format_options[] = {
{"intelligent", AQO_MODE_INTELLIGENT, false},
{"forced", AQO_MODE_FORCED, false},
{"controlled", AQO_MODE_CONTROLLED, false},
{"learn", AQO_MODE_LEARN, false},
{"frozen", AQO_MODE_FROZEN, false},
{"disabled", AQO_MODE_DISABLED, false},
{NULL, 0, false}
};
/* Parameters of autotuning */
int aqo_stat_size = 20;
int auto_tuning_window_size = 5;
double auto_tuning_exploration = 0.1;
int auto_tuning_max_iterations = 50;
int auto_tuning_infinite_loop = 8;
/* stat_size > infinite_loop + window_size + 3 is required for auto_tuning*/
/* Machine learning parameters */
/*
* Defines where we do not perform learning procedure
*/
const double object_selection_prediction_threshold = 0.3;
/*
* This parameter tell us that the new learning sample object has very small
* distance from one whose features stored in matrix already.
* In this case we will not to add new line in matrix, but will modify this
* nearest neighbor features and cardinality with linear smoothing by
* learning_rate coefficient.
*/
const double object_selection_threshold = 0.1;
const double learning_rate = 1e-1;
/* The number of nearest neighbors which will be chosen for ML-operations */
int aqo_k = 3;
double log_selectivity_lower_bound = -30;
/*
* Currently we use it only to store query_text string which is initialized
* after a query parsing and is used during the query planning.
*/
MemoryContext AQOMemoryContext;
QueryContextData query_context;
/* Additional plan info */
int njoins;
/* Saved hook values */
post_parse_analyze_hook_type prev_post_parse_analyze_hook;
planner_hook_type prev_planner_hook;
ExecutorStart_hook_type prev_ExecutorStart_hook;
ExecutorEnd_hook_type prev_ExecutorEnd_hook;
set_baserel_rows_estimate_hook_type prev_set_foreign_rows_estimate_hook;
set_baserel_rows_estimate_hook_type prev_set_baserel_rows_estimate_hook;
get_parameterized_baserel_size_hook_type prev_get_parameterized_baserel_size_hook;
set_joinrel_size_estimates_hook_type prev_set_joinrel_size_estimates_hook;
get_parameterized_joinrel_size_hook_type prev_get_parameterized_joinrel_size_hook;
create_plan_hook_type prev_create_plan_hook;
ExplainOnePlan_hook_type prev_ExplainOnePlan_hook;
ExplainOneNode_hook_type prev_ExplainOneNode_hook;
/*****************************************************************************
*
* CREATE/DROP EXTENSION FUNCTIONS
*
*****************************************************************************/
static void
aqo_free_callback(ResourceReleasePhase phase,
bool isCommit,
bool isTopLevel,
void *arg)
{
if (phase != RESOURCE_RELEASE_AFTER_LOCKS)
return;
if (isTopLevel)
{
list_free(cur_classes);
cur_classes = NIL;
}
}
void
_PG_init(void)
{
/*
* In order to create our shared memory area, we have to be loaded via
* shared_preload_libraries. If not, report an ERROR.
*/
if (!process_shared_preload_libraries_in_progress)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("AQO module could be loaded only on startup."),
errdetail("Add 'aqo' into the shared_preload_libraries list.")));
DefineCustomEnumVariable("aqo.mode",
"Mode of aqo usage.",
NULL,
&aqo_mode,
AQO_MODE_CONTROLLED,
format_options,
PGC_USERSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable(
"aqo.force_collect_stat",
"Collect statistics at all AQO modes",
NULL,
&force_collect_stat,
false,
PGC_USERSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable(
"aqo.show_hash",
"Show query and node hash on explain.",
"Hash value depend on each instance and is not good to enable it in regression or TAP tests.",
&aqo_show_hash,
false,
PGC_USERSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable(
"aqo.show_details",
"Show AQO state on a query.",
NULL,
&aqo_show_details,
false,
PGC_USERSET,
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable(
"aqo.log_ignorance",
"Log in a special table all feature spaces for which the AQO prediction was not successful.",
NULL,
&aqo_log_ignorance,
false,
PGC_SUSET,
0,
NULL,
set_ignorance,
NULL
);
DefineCustomIntVariable(
"aqo.profile_classes",
"Sets the maximum number of classes to be used in a query profiling hash table.",
NULL,
&aqo_profile_classes,
100,
-1,
INT_MAX,
PGC_POSTMASTER, /* Defines a max size of used shared memory segment. So, change only on restart. */
0,
NULL,
NULL,
NULL
);
DefineCustomBoolVariable(
"aqo.profile_enable",
"Enable AQO profiling feature.",
NULL,
&aqo_profile_enable,
false,
PGC_USERSET,
0,
check_aqo_profile_enable, /* Check that a profiling buffer in shared memory has allocated on startup. */
NULL,
NULL
);
prev_planner_hook = planner_hook;
planner_hook = aqo_planner;
prev_ExecutorStart_hook = ExecutorStart_hook;
ExecutorStart_hook = aqo_ExecutorStart;
prev_ExecutorEnd_hook = ExecutorEnd_hook;
ExecutorEnd_hook = aqo_ExecutorEnd;
/* Cardinality prediction hooks. */
prev_set_baserel_rows_estimate_hook = set_baserel_rows_estimate_hook;
set_foreign_rows_estimate_hook = aqo_set_baserel_rows_estimate;
set_baserel_rows_estimate_hook = aqo_set_baserel_rows_estimate;
prev_get_parameterized_baserel_size_hook = get_parameterized_baserel_size_hook;
get_parameterized_baserel_size_hook = aqo_get_parameterized_baserel_size;
prev_set_joinrel_size_estimates_hook = set_joinrel_size_estimates_hook;
set_joinrel_size_estimates_hook = aqo_set_joinrel_size_estimates;
prev_get_parameterized_joinrel_size_hook = get_parameterized_joinrel_size_hook;
get_parameterized_joinrel_size_hook = aqo_get_parameterized_joinrel_size;
prev_estimate_num_groups_hook = estimate_num_groups_hook;
estimate_num_groups_hook = aqo_estimate_num_groups_hook;
parampathinfo_postinit_hook = ppi_hook;
prev_create_plan_hook = create_plan_hook;
create_plan_hook = aqo_create_plan_hook;
/* Service hooks. */
prev_ExplainOnePlan_hook = ExplainOnePlan_hook;
ExplainOnePlan_hook = print_into_explain;
prev_ExplainOneNode_hook = ExplainOneNode_hook;
ExplainOneNode_hook = print_node_explain;
prev_create_upper_paths_hook = create_upper_paths_hook;
create_upper_paths_hook = aqo_store_upper_signature_hook;
prev_shmem_startup_hook = shmem_startup_hook;
shmem_startup_hook = profile_shmem_startup;
init_deactivated_queries_storage();
AQOMemoryContext = AllocSetContextCreate(TopMemoryContext,
"AQOMemoryContext",
ALLOCSET_DEFAULT_SIZES);
RegisterResourceReleaseCallback(aqo_free_callback, NULL);
RegisterAQOPlanNodeMethods();
/* Set shared memory hooks. */
profile_init();
}
PG_FUNCTION_INFO_V1(invalidate_deactivated_queries_cache);
/*
* Clears the cache of deactivated queries if the user changed aqo_queries
* manually.
*/
Datum
invalidate_deactivated_queries_cache(PG_FUNCTION_ARGS)
{
fini_deactivated_queries_storage();
init_deactivated_queries_storage();
PG_RETURN_POINTER(NULL);
}
/*
* Return AQO schema's Oid or InvalidOid if that's not possible.
*/
Oid
get_aqo_schema(void)
{
Oid result;
Relation rel;
SysScanDesc scandesc;
HeapTuple tuple;
ScanKeyData entry[1];
Oid ext_oid;
/* It's impossible to fetch pg_aqo's schema now */
if (!IsTransactionState())
return InvalidOid;
ext_oid = get_extension_oid("aqo", true);
if (ext_oid == InvalidOid)
return InvalidOid; /* exit if pg_aqo does not exist */
ScanKeyInit(&entry[0],
#if PG_VERSION_NUM >= 120000
Anum_pg_extension_oid,
#else
ObjectIdAttributeNumber,
#endif
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(ext_oid));
rel = relation_open(ExtensionRelationId, AccessShareLock);
scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
NULL, 1, entry);
tuple = systable_getnext(scandesc);
/* We assume that there can be at most one matching tuple */
if (HeapTupleIsValid(tuple))
result = ((Form_pg_extension) GETSTRUCT(tuple))->extnamespace;
else
result = InvalidOid;
systable_endscan(scandesc);
relation_close(rel, AccessShareLock);
return result;
}
/*
* Init userlock
*/
void
init_lock_tag(LOCKTAG *tag, uint32 key1, uint32 key2)
{
tag->locktag_field1 = AQO_MODULE_MAGIC;
tag->locktag_field2 = key1;
tag->locktag_field3 = key2;
tag->locktag_field4 = 0;
tag->locktag_type = LOCKTAG_USERLOCK;
tag->locktag_lockmethodid = USER_LOCKMETHOD;
}
/*
* AQO is really needed for any activity?
*/
bool
IsQueryDisabled(void)
{
if (!query_context.learn_aqo && !query_context.use_aqo &&
!query_context.auto_tuning && !query_context.collect_stat &&
!query_context.adding_query && !query_context.explain_only &&
INSTR_TIME_IS_ZERO(query_context.start_planning_time) &&
query_context.planning_time < 0.)
return true;
return false;
}