forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcardinality_hooks.c
393 lines (350 loc) · 10.8 KB
/
cardinality_hooks.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
/*
*******************************************************************************
*
* CARDINALITY ESTIMATION HOOKS
*
* This functions controls cardinality prediction in query optimization.
* If use_aqo flag is false, then hooks just call default postgresql
* cardinality estimator. Otherwise, they try to use AQO cardinality
* prediction engine.
* If use_aqo flag in true, hooks generate set of all clauses and all
* absolute relids used in the relation being built and pass this
* information to predict_for_relation function. Also these hooks compute
* and pass to predict_for_relation marginal cardinalities for clauses.
* If predict_for_relation returns non-negative value, then hooks assume it
* to be true cardinality for given relation. Negative returned value means
* refusal to predict cardinality. In this case hooks also use default
* postgreSQL cardinality estimator.
*
*******************************************************************************
*
* Copyright (c) 2016-2021, Postgres Professional
*
* IDENTIFICATION
* aqo/cardinality_hooks.c
*
*/
#include "aqo.h"
double predicted_ppi_rows;
double fss_ppi_hash;
static void call_default_set_baserel_rows_estimate(PlannerInfo *root,
RelOptInfo *rel);
static double call_default_get_parameterized_baserel_size(PlannerInfo *root,
RelOptInfo *rel,
List *param_clauses);
static void call_default_set_joinrel_size_estimates(PlannerInfo *root,
RelOptInfo *rel,
RelOptInfo *outer_rel,
RelOptInfo *inner_rel,
SpecialJoinInfo *sjinfo,
List *restrictlist);
static double call_default_get_parameterized_joinrel_size(PlannerInfo *root,
RelOptInfo *rel,
Path *outer_path,
Path *inner_path,
SpecialJoinInfo *sjinfo,
List *restrict_clauses);
/*
* Calls standard set_baserel_rows_estimate or its previous hook.
*/
void
call_default_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
{
if (prev_set_baserel_rows_estimate_hook)
prev_set_baserel_rows_estimate_hook(root, rel);
else
set_baserel_rows_estimate_standard(root, rel);
}
/*
* Calls standard get_parameterized_baserel_size or its previous hook.
*/
double
call_default_get_parameterized_baserel_size(PlannerInfo *root,
RelOptInfo *rel,
List *param_clauses)
{
if (prev_get_parameterized_baserel_size_hook)
return prev_get_parameterized_baserel_size_hook(root, rel, param_clauses);
else
return get_parameterized_baserel_size_standard(root, rel, param_clauses);
}
/*
* Calls standard get_parameterized_joinrel_size or its previous hook.
*/
double
call_default_get_parameterized_joinrel_size(PlannerInfo *root,
RelOptInfo *rel,
Path *outer_path,
Path *inner_path,
SpecialJoinInfo *sjinfo,
List *restrict_clauses)
{
if (prev_get_parameterized_joinrel_size_hook)
return prev_get_parameterized_joinrel_size_hook(root, rel,
outer_path,
inner_path,
sjinfo,
restrict_clauses);
else
return get_parameterized_joinrel_size_standard(root, rel,
outer_path,
inner_path,
sjinfo,
restrict_clauses);
}
/*
* Calls standard set_joinrel_size_estimates or its previous hook.
*/
void
call_default_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
RelOptInfo *outer_rel,
RelOptInfo *inner_rel,
SpecialJoinInfo *sjinfo,
List *restrictlist)
{
if (prev_set_joinrel_size_estimates_hook)
prev_set_joinrel_size_estimates_hook(root, rel,
outer_rel,
inner_rel,
sjinfo,
restrictlist);
else
set_joinrel_size_estimates_standard(root, rel,
outer_rel,
inner_rel,
sjinfo,
restrictlist);
}
/*
* Our hook for setting baserel rows estimate.
* Extracts clauses, their selectivities and list of relation relids and
* passes them to predict_for_relation.
*/
void
aqo_set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
{
double predicted;
Oid relid;
List *relids;
List *selectivities = NULL;
List *restrict_clauses;
int fss = 0;
if (query_context.use_aqo || query_context.learn_aqo)
selectivities = get_selectivities(root, rel->baserestrictinfo, 0,
JOIN_INNER, NULL);
if (!query_context.use_aqo)
{
if (query_context.learn_aqo)
list_free_deep(selectivities);
call_default_set_baserel_rows_estimate(root, rel);
return;
}
relid = planner_rt_fetch(rel->relid, root)->relid;
relids = list_make1_int(relid);
restrict_clauses = list_copy(rel->baserestrictinfo);
predicted = predict_for_relation(restrict_clauses, selectivities,
relids, &fss);
rel->fss_hash = fss;
if (predicted >= 0)
{
rel->rows = predicted;
rel->predicted_cardinality = predicted;
}
else
{
call_default_set_baserel_rows_estimate(root, rel);
rel->predicted_cardinality = -1.;
}
list_free_deep(selectivities);
list_free(restrict_clauses);
list_free(relids);
}
void
ppi_hook(ParamPathInfo *ppi)
{
ppi->predicted_ppi_rows = predicted_ppi_rows;
ppi->fss_ppi_hash = fss_ppi_hash;
}
/*
* Our hook for estimating parameterized baserel rows estimate.
* Extracts clauses (including parametrization ones), their selectivities
* and list of relation relids and passes them to predict_for_relation.
*/
double
aqo_get_parameterized_baserel_size(PlannerInfo *root,
RelOptInfo *rel,
List *param_clauses)
{
double predicted;
Oid relid = InvalidOid;
List *relids = NULL;
List *allclauses = NULL;
List *selectivities = NULL;
ListCell *l;
ListCell *l2;
int nargs;
int *args_hash;
int *eclass_hash;
int current_hash;
int fss = 0;
if (query_context.use_aqo || query_context.learn_aqo)
{
MemoryContext mcxt;
allclauses = list_concat(list_copy(param_clauses),
list_copy(rel->baserestrictinfo));
selectivities = get_selectivities(root, allclauses, rel->relid,
JOIN_INNER, NULL);
relid = planner_rt_fetch(rel->relid, root)->relid;
get_eclasses(allclauses, &nargs, &args_hash, &eclass_hash);
mcxt = MemoryContextSwitchTo(CacheMemoryContext);
forboth(l, allclauses, l2, selectivities)
{
current_hash = get_clause_hash(
((RestrictInfo *) lfirst(l))->clause,
nargs, args_hash, eclass_hash);
cache_selectivity(current_hash, rel->relid, relid,
*((double *) lfirst(l2)));
}
MemoryContextSwitchTo(mcxt);
pfree(args_hash);
pfree(eclass_hash);
}
if (!query_context.use_aqo)
{
if (query_context.learn_aqo)
{
list_free_deep(selectivities);
list_free(allclauses);
}
return call_default_get_parameterized_baserel_size(root, rel,
param_clauses);
}
relids = list_make1_int(relid);
predicted = predict_for_relation(allclauses, selectivities, relids, &fss);
predicted_ppi_rows = predicted;
fss_ppi_hash = fss;
if (predicted >= 0)
return predicted;
else
return call_default_get_parameterized_baserel_size(root, rel,
param_clauses);
}
/*
* Our hook for setting joinrel rows estimate.
* Extracts clauses, their selectivities and list of relation relids and
* passes them to predict_for_relation.
*/
void
aqo_set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
RelOptInfo *outer_rel,
RelOptInfo *inner_rel,
SpecialJoinInfo *sjinfo,
List *restrictlist)
{
double predicted;
List *relids;
List *outer_clauses;
List *inner_clauses;
List *allclauses;
List *selectivities;
List *inner_selectivities;
List *outer_selectivities;
List *current_selectivities = NULL;
int fss = 0;
if (query_context.use_aqo || query_context.learn_aqo)
current_selectivities = get_selectivities(root, restrictlist, 0,
sjinfo->jointype, sjinfo);
if (!query_context.use_aqo)
{
if (query_context.learn_aqo)
list_free_deep(current_selectivities);
call_default_set_joinrel_size_estimates(root, rel,
outer_rel,
inner_rel,
sjinfo,
restrictlist);
return;
}
relids = get_list_of_relids(root, rel->relids);
outer_clauses = get_path_clauses(outer_rel->cheapest_total_path, root,
&outer_selectivities);
inner_clauses = get_path_clauses(inner_rel->cheapest_total_path, root,
&inner_selectivities);
allclauses = list_concat(list_copy(restrictlist),
list_concat(outer_clauses, inner_clauses));
selectivities = list_concat(current_selectivities,
list_concat(outer_selectivities,
inner_selectivities));
predicted = predict_for_relation(allclauses, selectivities, relids, &fss);
rel->fss_hash = fss;
if (predicted >= 0)
{
rel->predicted_cardinality = predicted;
rel->rows = predicted;
}
else
{
rel->predicted_cardinality = -1;
call_default_set_joinrel_size_estimates(root, rel,
outer_rel,
inner_rel,
sjinfo,
restrictlist);
}
}
/*
* Our hook for estimating parameterized joinrel rows estimate.
* Extracts clauses (including parametrization ones), their selectivities
* and list of relation relids and passes them to predict_for_relation.
*/
double
aqo_get_parameterized_joinrel_size(PlannerInfo *root,
RelOptInfo *rel,
Path *outer_path,
Path *inner_path,
SpecialJoinInfo *sjinfo,
List *restrict_clauses)
{
double predicted;
List *relids;
List *outer_clauses;
List *inner_clauses;
List *allclauses;
List *selectivities;
List *inner_selectivities;
List *outer_selectivities;
List *current_selectivities = NULL;
int fss = 0;
if (query_context.use_aqo || query_context.learn_aqo)
current_selectivities = get_selectivities(root, restrict_clauses, 0,
sjinfo->jointype, sjinfo);
if (!query_context.use_aqo)
{
if (query_context.learn_aqo)
list_free_deep(current_selectivities);
return call_default_get_parameterized_joinrel_size(root, rel,
outer_path,
inner_path,
sjinfo,
restrict_clauses);
}
relids = get_list_of_relids(root, rel->relids);
outer_clauses = get_path_clauses(outer_path, root, &outer_selectivities);
inner_clauses = get_path_clauses(inner_path, root, &inner_selectivities);
allclauses = list_concat(list_copy(restrict_clauses),
list_concat(outer_clauses, inner_clauses));
selectivities = list_concat(current_selectivities,
list_concat(outer_selectivities,
inner_selectivities));
predicted = predict_for_relation(allclauses, selectivities, relids, &fss);
predicted_ppi_rows = predicted;
fss_ppi_hash = fss;
if (predicted >= 0)
return predicted;
else
return call_default_get_parameterized_joinrel_size(root, rel,
outer_path,
inner_path,
sjinfo,
restrict_clauses);
}