forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_utils.c
596 lines (514 loc) · 15.4 KB
/
path_utils.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
*******************************************************************************
*
* EXTRACTING PATH INFORMATION UTILITIES
*
*******************************************************************************
*
* Copyright (c) 2016-2021, Postgres Professional
*
* IDENTIFICATION
* aqo/path_utils.c
*
*/
#include "postgres.h"
#include "nodes/readfuncs.h"
#include "optimizer/optimizer.h"
#include "path_utils.h"
#include "aqo.h"
#include "hash.h"
/*
* Hook on creation of a plan node. We need to store AQO-specific data to
* support learning stage.
*/
create_plan_hook_type prev_create_plan_hook = NULL;
create_upper_paths_hook_type prev_create_upper_paths_hook = NULL;
static AQOPlanNode DefaultAQOPlanNode =
{
.node.type = T_ExtensibleNode,
.node.extnodename = AQO_PLAN_NODE,
.had_path = false,
.relids = NIL,
.clauses = NIL,
.selectivities = NIL,
.grouping_exprs = NIL,
.jointype = -1,
.parallel_divisor = -1,
.was_parametrized = false,
.fss = INT_MAX,
.prediction = -1
};
static AQOPlanNode *
create_aqo_plan_node()
{
AQOPlanNode *node = (AQOPlanNode *) newNode(sizeof(AQOPlanNode),
T_ExtensibleNode);
memcpy(node, &DefaultAQOPlanNode, sizeof(AQOPlanNode));
return node;
}
AQOPlanNode *
get_aqo_plan_node(Plan *plan, bool create)
{
AQOPlanNode *node = NULL;
ListCell *lc;
foreach(lc, plan->private)
{
AQOPlanNode *candidate = (AQOPlanNode *) lfirst(lc);
if (!IsA(candidate, ExtensibleNode))
continue;
if (strcmp(candidate->node.extnodename, AQO_PLAN_NODE) != 0)
continue;
node = candidate;
break;
}
if (node == NULL)
{
if (!create)
return &DefaultAQOPlanNode;
node = create_aqo_plan_node();
plan->private = lappend(plan->private, node);
}
Assert(node);
return node;
}
/*
* Returns list of marginal selectivities using as an arguments for each clause
* (root, clause, 0, jointype, NULL).
* That is not quite correct for parameterized baserel and foreign key join
* cases, but nevertheless that is bearable.
*/
List *
get_selectivities(PlannerInfo *root,
List *clauses,
int varRelids,
JoinType jointype,
SpecialJoinInfo *sjinfo)
{
List *res = NIL;
ListCell *l;
double *elem;
foreach(l, clauses)
{
elem = palloc(sizeof(*elem));
*elem = clause_selectivity(root, lfirst(l), varRelids,
jointype, sjinfo);
res = lappend(res, elem);
}
return res;
}
/*
* Transforms given relids from path optimization stage format to list of
* an absolute (independent on query optimization context) relids.
*/
List *
get_list_of_relids(PlannerInfo *root, Relids relids)
{
int i;
RangeTblEntry *entry;
List *l = NIL;
if (relids == NULL)
return NIL;
/*
* Check: don't take into account relations without underlying plane
* source table.
*/
Assert(!bms_is_member(0, relids));
i = -1;
while ((i = bms_next_member(relids, i)) >= 0)
{
entry = planner_rt_fetch(i, root);
if (OidIsValid(entry->relid))
l = lappend_int(l, entry->relid);
}
return l;
}
/*
* Search for any subplans or initplans.
* if subplan is found, replace it by the feature space value of this subplan.
*/
static Node *
subplan_hunter(Node *node, void *context)
{
if (node == NULL)
/* Continue recursion in other subtrees. */
return false;
if (IsA(node, SubPlan))
{
SubPlan *splan = (SubPlan *) node;
PlannerInfo *root = (PlannerInfo *) context;
PlannerInfo *subroot;
RelOptInfo *upper_rel;
A_Const *fss;
subroot = (PlannerInfo *) list_nth(root->glob->subroots,
splan->plan_id - 1);
upper_rel = fetch_upper_rel(subroot, UPPERREL_FINAL, NULL);
Assert(list_length(upper_rel->private) == 1);
Assert(IsA((Node *) linitial(upper_rel->private), A_Const));
fss = (A_Const *) linitial(upper_rel->private);
return (Node *) copyObject(fss);
}
return expression_tree_mutator(node, subplan_hunter, context);
}
/*
* Get independent copy of the clauses list.
* During this operation clauses could be changed and we couldn't walk across
* this list next.
*/
List *
aqo_get_clauses(PlannerInfo *root, List *restrictlist)
{
List *clauses = NIL;
ListCell *lc;
foreach(lc, restrictlist)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
rinfo = copyObject(rinfo);
rinfo->clause = (Expr *) expression_tree_mutator((Node *) rinfo->clause,
subplan_hunter,
(void *) root);
clauses = lappend(clauses, (void *) rinfo);
}
return clauses;
}
/*
* For given path returns the list of all clauses used in it.
* Also returns selectivities for the clauses throw the selectivities variable.
* Both clauses and selectivities returned lists are copies and therefore
* may be modified without corruption of the input data.
*/
List *
get_path_clauses(Path *path, PlannerInfo *root, List **selectivities)
{
List *inner;
List *inner_sel = NIL;
List *outer;
List *outer_sel = NIL;
List *cur = NIL;
List *cur_sel = NIL;
Assert(selectivities != NULL);
*selectivities = NIL;
if (path == NULL)
return NIL;
switch (path->type)
{
case T_NestPath:
case T_MergePath:
case T_HashPath:
cur = ((JoinPath *) path)->joinrestrictinfo;
/* Not quite correct to avoid sjinfo, but we believe in caching */
cur_sel = get_selectivities(root, cur, 0,
((JoinPath *) path)->jointype,
NULL);
outer = get_path_clauses(((JoinPath *) path)->outerjoinpath, root,
&outer_sel);
inner = get_path_clauses(((JoinPath *) path)->innerjoinpath, root,
&inner_sel);
*selectivities = list_concat(cur_sel,
list_concat(outer_sel, inner_sel));
return list_concat(list_copy(cur), list_concat(outer, inner));
break;
case T_UniquePath:
return get_path_clauses(((UniquePath *) path)->subpath, root,
selectivities);
break;
case T_GatherPath:
case T_GatherMergePath:
return get_path_clauses(((GatherPath *) path)->subpath, root,
selectivities);
break;
case T_MaterialPath:
return get_path_clauses(((MaterialPath *) path)->subpath, root,
selectivities);
break;
case T_ProjectionPath:
return get_path_clauses(((ProjectionPath *) path)->subpath, root,
selectivities);
break;
case T_SortPath:
return get_path_clauses(((SortPath *) path)->subpath, root,
selectivities);
break;
case T_GroupPath:
return get_path_clauses(((GroupPath *) path)->subpath, root,
selectivities);
break;
case T_UpperUniquePath:
return get_path_clauses(((UpperUniquePath *) path)->subpath, root,
selectivities);
break;
case T_AggPath:
return get_path_clauses(((AggPath *) path)->subpath, root,
selectivities);
break;
case T_GroupingSetsPath:
return get_path_clauses(((GroupingSetsPath *) path)->subpath, root,
selectivities);
break;
case T_WindowAggPath:
return get_path_clauses(((WindowAggPath *) path)->subpath, root,
selectivities);
break;
case T_SetOpPath:
return get_path_clauses(((SetOpPath *) path)->subpath, root,
selectivities);
break;
case T_LockRowsPath:
return get_path_clauses(((LockRowsPath *) path)->subpath, root,
selectivities);
break;
case T_LimitPath:
return get_path_clauses(((LimitPath *) path)->subpath, root,
selectivities);
break;
case T_SubqueryScanPath:
return get_path_clauses(((SubqueryScanPath *) path)->subpath, root,
selectivities);
break;
case T_AppendPath:
{
ListCell *lc;
foreach (lc, ((AppendPath *) path)->subpaths)
{
Path *subpath = lfirst(lc);
cur = list_concat(cur, list_copy(
get_path_clauses(subpath, root, selectivities)));
cur_sel = list_concat(cur_sel, *selectivities);
}
cur = list_concat(cur, aqo_get_clauses(root,
path->parent->baserestrictinfo));
*selectivities = list_concat(cur_sel,
get_selectivities(root,
path->parent->baserestrictinfo,
0, JOIN_INNER, NULL));
return cur;
}
break;
case T_ForeignPath:
/* The same as in the default case */
default:
cur = list_concat(aqo_get_clauses(root,
path->parent->baserestrictinfo),
path->param_info ?
aqo_get_clauses(root,
path->param_info->ppi_clauses) :
NIL);
if (path->param_info)
cur_sel = get_selectivities(root, cur, path->parent->relid,
JOIN_INNER, NULL);
else
cur_sel = get_selectivities(root, cur, 0, JOIN_INNER, NULL);
*selectivities = cur_sel;
return cur;
break;
}
}
/*
* Converts path info into plan node for collecting it after query execution.
*/
void
aqo_create_plan_hook(PlannerInfo *root, Path *src, Plan **dest)
{
bool is_join_path;
Plan *plan = *dest;
AQOPlanNode *node;
if (prev_create_plan_hook)
prev_create_plan_hook(root, src, dest);
if (!query_context.use_aqo && !query_context.learn_aqo)
return;
is_join_path = (src->type == T_NestPath || src->type == T_MergePath ||
src->type == T_HashPath);
node = get_aqo_plan_node(plan, true);
if (node->had_path)
{
/*
* The convention is that any extension that sets had_path is also
* responsible for setting path_clauses, path_jointype, path_relids,
* path_parallel_workers, and was_parameterized.
*/
return;
}
if (is_join_path)
{
node->clauses = aqo_get_clauses(root, ((JoinPath *) src)->joinrestrictinfo);
node->jointype = ((JoinPath *) src)->jointype;
}
else if (IsA(src, AggPath))
/* Aggregation node must store grouping clauses. */
{
AggPath *ap = (AggPath *) src;
/* Get TLE's from child target list corresponding to the list of exprs. */
List *groupExprs = get_sortgrouplist_exprs(ap->groupClause,
(*dest)->lefttree->targetlist);
/* Copy bare expressions for further AQO learning case. */
node->grouping_exprs = copyObject(groupExprs);
node->relids = get_list_of_relids(root, ap->subpath->parent->relids);
node->jointype = JOIN_INNER;
}
else
{
node->clauses = list_concat(
aqo_get_clauses(root, src->parent->baserestrictinfo),
src->param_info ? aqo_get_clauses(root, src->param_info->ppi_clauses) : NIL);
node->jointype = JOIN_INNER;
}
node->relids = list_concat(node->relids,
get_list_of_relids(root, src->parent->relids));
if (src->parallel_workers > 0)
node->parallel_divisor = get_parallel_divisor(src);
node->was_parametrized = (src->param_info != NULL);
if (src->param_info)
{
node->prediction = src->param_info->predicted_ppi_rows;
node->fss = src->param_info->fss_ppi_hash;
}
else
{
node->prediction = src->parent->predicted_cardinality;
node->fss = src->parent->fss_hash;
}
node->had_path = true;
}
static void
AQOnodeCopy(struct ExtensibleNode *enew, const struct ExtensibleNode *eold)
{
AQOPlanNode *new = (AQOPlanNode *) enew;
AQOPlanNode *old = (AQOPlanNode *) eold;
Assert(IsA(old, ExtensibleNode));
Assert(strcmp(old->node.extnodename, AQO_PLAN_NODE) == 0);
/* Copy static fields in one command */
memcpy(new, old, sizeof(AQOPlanNode));
/* These lists couldn't contain AQO nodes. Use basic machinery */
new->relids = copyObject(old->relids);
new->clauses = copyObject(old->clauses);
new->grouping_exprs = copyObject(old->grouping_exprs);
new->selectivities = copyObject(old->selectivities);
enew = (ExtensibleNode *) new;
}
static bool
AQOnodeEqual(const struct ExtensibleNode *a, const struct ExtensibleNode *b)
{
return false;
}
#define WRITE_INT_FIELD(fldname) \
appendStringInfo(str, " :" CppAsString(fldname) " %d", node->fldname)
/* Write a boolean field */
#define WRITE_BOOL_FIELD(fldname) \
appendStringInfo(str, " :" CppAsString(fldname) " %s", \
booltostr(node->fldname))
#define WRITE_NODE_FIELD(fldname) \
(appendStringInfoString(str, " :" CppAsString(fldname) " "), \
outNode(str, node->fldname))
/* Write an enumerated-type field as an integer code */
#define WRITE_ENUM_FIELD(fldname, enumtype) \
appendStringInfo(str, " :" CppAsString(fldname) " %d", \
(int) node->fldname)
/* Write a float field --- caller must give format to define precision */
#define WRITE_FLOAT_FIELD(fldname,format) \
appendStringInfo(str, " :" CppAsString(fldname) " " format, node->fldname)
static void
AQOnodeOut(struct StringInfoData *str, const struct ExtensibleNode *enode)
{
AQOPlanNode *node = (AQOPlanNode *) enode;
Assert(0);
WRITE_BOOL_FIELD(had_path);
WRITE_NODE_FIELD(relids);
WRITE_NODE_FIELD(clauses);
WRITE_NODE_FIELD(selectivities);
WRITE_NODE_FIELD(grouping_exprs);
WRITE_ENUM_FIELD(jointype, JoinType);
WRITE_INT_FIELD(parallel_divisor);
WRITE_BOOL_FIELD(was_parametrized);
/* For Adaptive optimization DEBUG purposes */
WRITE_INT_FIELD(fss);
WRITE_FLOAT_FIELD(prediction, "%.0f");
}
/* Read an integer field (anything written as ":fldname %d") */
#define READ_INT_FIELD(fldname) \
token = pg_strtok(&length); /* skip :fldname */ \
token = pg_strtok(&length); /* get field value */ \
local_node->fldname = atoi(token)
/* Read an enumerated-type field that was written as an integer code */
#define READ_ENUM_FIELD(fldname, enumtype) \
token = pg_strtok(&length); /* skip :fldname */ \
token = pg_strtok(&length); /* get field value */ \
local_node->fldname = (enumtype) atoi(token)
/* Read a float field */
#define READ_FLOAT_FIELD(fldname) \
token = pg_strtok(&length); /* skip :fldname */ \
token = pg_strtok(&length); /* get field value */ \
local_node->fldname = atof(token)
/* Read a boolean field */
#define READ_BOOL_FIELD(fldname) \
token = pg_strtok(&length); /* skip :fldname */ \
token = pg_strtok(&length); /* get field value */ \
local_node->fldname = strtobool(token)
/* Read a Node field */
#define READ_NODE_FIELD(fldname) \
token = pg_strtok(&length); /* skip :fldname */ \
(void) token; /* in case not used elsewhere */ \
local_node->fldname = nodeRead(NULL, 0)
static void
AQOnodeRead(struct ExtensibleNode *enode)
{
AQOPlanNode *local_node = (AQOPlanNode *) enode;
const char *token;
int length;
Assert(0);
READ_BOOL_FIELD(had_path);
READ_NODE_FIELD(relids);
READ_NODE_FIELD(clauses);
READ_NODE_FIELD(selectivities);
READ_NODE_FIELD(grouping_exprs);
READ_ENUM_FIELD(jointype, JoinType);
READ_INT_FIELD(parallel_divisor);
READ_BOOL_FIELD(was_parametrized);
/* For Adaptive optimization DEBUG purposes */
READ_INT_FIELD(fss);
READ_FLOAT_FIELD(prediction);
}
static const ExtensibleNodeMethods method =
{
.extnodename = AQO_PLAN_NODE,
.node_size = sizeof(AQOPlanNode),
.nodeCopy = AQOnodeCopy,
.nodeEqual = AQOnodeEqual,
.nodeOut = AQOnodeOut,
.nodeRead = AQOnodeRead
};
void
RegisterAQOPlanNodeMethods(void)
{
RegisterExtensibleNodeMethods(&method);
}
/*
* Hook for create_upper_paths_hook
*
* Assume, that we are last in the chain of path creators.
*/
void
aqo_store_upper_signature_hook(PlannerInfo *root,
UpperRelationKind stage,
RelOptInfo *input_rel,
RelOptInfo *output_rel,
void *extra)
{
A_Const *fss_node = makeNode(A_Const);
List *relids;
List *clauses;
List *selectivities;
if (prev_create_upper_paths_hook)
(*prev_create_upper_paths_hook)(root, stage, input_rel, output_rel, extra);
if (!query_context.use_aqo && !query_context.learn_aqo && !force_collect_stat)
/* Includes 'disabled query' state. */
return;
if (stage != UPPERREL_FINAL)
return;
set_cheapest(input_rel);
clauses = get_path_clauses(input_rel->cheapest_total_path,
root, &selectivities);
relids = get_list_of_relids(root, input_rel->relids);
fss_node->val.ival.type = T_Integer;
fss_node->location = -1;
fss_node->val.ival.val = get_fss_for_object(relids, clauses, NIL, NULL, NULL);
output_rel->private = lappend(output_rel->private, (void *) fss_node);
}