forked from postgrespro/aqo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aqo_pg9_6.patch
758 lines (663 loc) · 26.7 KB
/
aqo_pg9_6.patch
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index 3244c76..8229702 100644
--- a/src/backend/nodes/copyfuncs.c
+++ b/src/backend/nodes/copyfuncs.c
@@ -121,6 +121,12 @@ CopyPlanFields(const Plan *from, Plan *newnode)
COPY_NODE_FIELD(lefttree);
COPY_NODE_FIELD(righttree);
COPY_NODE_FIELD(initPlan);
+ COPY_SCALAR_FIELD(had_path);
+ COPY_NODE_FIELD(path_clauses);
+ COPY_NODE_FIELD(path_relids);
+ COPY_SCALAR_FIELD(path_jointype);
+ COPY_SCALAR_FIELD(path_parallel_workers);
+ COPY_SCALAR_FIELD(was_parametrized);
COPY_BITMAPSET_FIELD(extParam);
COPY_BITMAPSET_FIELD(allParam);
}
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 2a49639..7b9bdef 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -100,6 +100,10 @@
#define LOG2(x) (log(x) / 0.693147180559945)
+set_baserel_rows_estimate_hook_type set_baserel_rows_estimate_hook = NULL;
+get_parameterized_baserel_size_hook_type get_parameterized_baserel_size_hook = NULL;
+get_parameterized_joinrel_size_hook_type get_parameterized_joinrel_size_hook = NULL;
+set_joinrel_size_estimates_hook_type set_joinrel_size_estimates_hook = NULL;
double seq_page_cost = DEFAULT_SEQ_PAGE_COST;
double random_page_cost = DEFAULT_RANDOM_PAGE_COST;
@@ -3754,6 +3758,49 @@ approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
/*
+ * set_baserel_rows_estimate
+ * Set the rows estimate for the given base relation.
+ *
+ * Rows is the estimated number of output tuples after applying
+ * restriction clauses.
+ *
+ * To support loadable plugins that monitor or modify cardinality estimation,
+ * we provide a hook variable that lets a plugin get control before and
+ * after the cardinality estimation.
+ * The hook must set rel->rows.
+ */
+void
+set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
+{
+ if (set_baserel_rows_estimate_hook)
+ (*set_baserel_rows_estimate_hook) (root, rel);
+ else
+ set_baserel_rows_estimate_standard(root, rel);
+}
+
+/*
+ * set_baserel_rows_estimate
+ * Set the rows estimate for the given base relation.
+ *
+ * Rows is the estimated number of output tuples after applying
+ * restriction clauses.
+ */
+void
+set_baserel_rows_estimate_standard(PlannerInfo *root, RelOptInfo *rel)
+{
+ double nrows;
+
+ nrows = rel->tuples *
+ clauselist_selectivity(root,
+ rel->baserestrictinfo,
+ 0,
+ JOIN_INNER,
+ NULL);
+
+ rel->rows = clamp_row_est(nrows);
+}
+
+/*
* set_baserel_size_estimates
* Set the size estimates for the given base relation.
*
@@ -3769,19 +3816,10 @@ approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
void
set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
{
- double nrows;
-
/* Should only be applied to base relations */
Assert(rel->relid > 0);
- nrows = rel->tuples *
- clauselist_selectivity(root,
- rel->baserestrictinfo,
- 0,
- JOIN_INNER,
- NULL);
-
- rel->rows = clamp_row_est(nrows);
+ set_baserel_rows_estimate(root, rel);
cost_qual_eval(&rel->baserestrictcost, rel->baserestrictinfo, root);
@@ -3792,13 +3830,33 @@ set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel)
* get_parameterized_baserel_size
* Make a size estimate for a parameterized scan of a base relation.
*
+ * To support loadable plugins that monitor or modify cardinality estimation,
+ * we provide a hook variable that lets a plugin get control before and
+ * after the cardinality estimation.
+ */
+double
+get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel,
+ List *param_clauses)
+{
+ if (get_parameterized_baserel_size_hook)
+ return (*get_parameterized_baserel_size_hook) (root, rel,
+ param_clauses);
+ else
+ return get_parameterized_baserel_size_standard(root, rel,
+ param_clauses);
+}
+
+/*
+ * get_parameterized_baserel_size_standard
+ * Make a size estimate for a parameterized scan of a base relation.
+ *
* 'param_clauses' lists the additional join clauses to be used.
*
* set_baserel_size_estimates must have been applied already.
*/
double
-get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel,
- List *param_clauses)
+get_parameterized_baserel_size_standard(PlannerInfo *root, RelOptInfo *rel,
+ List *param_clauses)
{
List *allclauses;
double nrows;
@@ -3828,6 +3886,36 @@ get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel,
* set_joinrel_size_estimates
* Set the size estimates for the given join relation.
*
+ * To support loadable plugins that monitor or modify cardinality estimation,
+ * we provide a hook variable that lets a plugin get control before and
+ * after the cardinality estimation.
+ * The hook must set rel->rows value.
+ */
+void
+set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
+ RelOptInfo *outer_rel,
+ RelOptInfo *inner_rel,
+ SpecialJoinInfo *sjinfo,
+ List *restrictlist)
+{
+ if (set_joinrel_size_estimates_hook)
+ (*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);
+}
+
+/*
+ * set_joinrel_size_estimates_standard
+ * Set the size estimates for the given join relation.
+ *
* The rel's targetlist must have been constructed already, and a
* restriction clause list that matches the given component rels must
* be provided.
@@ -3847,11 +3935,11 @@ get_parameterized_baserel_size(PlannerInfo *root, RelOptInfo *rel,
* build_joinrel_tlist, and baserestrictcost is not used for join rels.
*/
void
-set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
- RelOptInfo *outer_rel,
- RelOptInfo *inner_rel,
- SpecialJoinInfo *sjinfo,
- List *restrictlist)
+set_joinrel_size_estimates_standard(PlannerInfo *root, RelOptInfo *rel,
+ RelOptInfo *outer_rel,
+ RelOptInfo *inner_rel,
+ SpecialJoinInfo *sjinfo,
+ List *restrictlist)
{
rel->rows = calc_joinrel_size_estimate(root,
outer_rel,
@@ -3866,6 +3954,35 @@ set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
* get_parameterized_joinrel_size
* Make a size estimate for a parameterized scan of a join relation.
*
+ * To support loadable plugins that monitor or modify cardinality estimation,
+ * we provide a hook variable that lets a plugin get control before and
+ * after the cardinality estimation.
+ */
+double
+get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel,
+ Path *outer_path,
+ Path *inner_path,
+ SpecialJoinInfo *sjinfo,
+ List *restrict_clauses)
+{
+ if (get_parameterized_joinrel_size_hook)
+ return (*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);
+}
+
+/*
+ * get_parameterized_joinrel_size_standard
+ * Make a size estimate for a parameterized scan of a join relation.
+ *
* 'rel' is the joinrel under consideration.
* 'outer_path', 'inner_path' are (probably also parameterized) Paths that
* produce the relations being joined.
@@ -3878,11 +3995,11 @@ set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
* set_joinrel_size_estimates must have been applied already.
*/
double
-get_parameterized_joinrel_size(PlannerInfo *root, RelOptInfo *rel,
- Path *outer_path,
- Path *inner_path,
- SpecialJoinInfo *sjinfo,
- List *restrict_clauses)
+get_parameterized_joinrel_size_standard(PlannerInfo *root, RelOptInfo *rel,
+ Path *outer_path,
+ Path *inner_path,
+ SpecialJoinInfo *sjinfo,
+ List *restrict_clauses)
{
double nrows;
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 54d601f..b212325 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -67,6 +67,8 @@
#define CP_SMALL_TLIST 0x0002 /* Prefer narrower tlists */
#define CP_LABEL_TLIST 0x0004 /* tlist must contain sortgrouprefs */
+/* Hook for plugins to get control in creating plan from path */
+copy_generic_path_info_hook_type copy_generic_path_info_hook = NULL;
static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path,
int flags);
@@ -154,7 +156,7 @@ static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_pat
static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
static List *get_switched_clauses(List *clauses, Relids outerrelids);
static List *order_qual_clauses(PlannerInfo *root, List *clauses);
-static void copy_generic_path_info(Plan *dest, Path *src);
+static void copy_generic_path_info(PlannerInfo *root, Plan *dest, Path *src);
static void copy_plan_costsize(Plan *dest, Plan *src);
static void label_sort_with_costsize(PlannerInfo *root, Sort *plan,
double limit_tuples);
@@ -977,7 +979,7 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path)
false)),
NULL);
- copy_generic_path_info(plan, (Path *) best_path);
+ copy_generic_path_info(root, plan, (Path *) best_path);
return plan;
}
@@ -1003,7 +1005,7 @@ create_append_plan(PlannerInfo *root, AppendPath *best_path)
plan = make_append(subplans, tlist);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return (Plan *) plan;
}
@@ -1031,7 +1033,7 @@ create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
* prepare_sort_from_pathkeys on it before we do so on the individual
* child plans, to make cross-checking the sort info easier.
*/
- copy_generic_path_info(plan, (Path *) best_path);
+ copy_generic_path_info(root, plan, (Path *) best_path);
plan->targetlist = tlist;
plan->qual = NIL;
plan->lefttree = NULL;
@@ -1136,7 +1138,7 @@ create_result_plan(PlannerInfo *root, ResultPath *best_path)
plan = make_result(tlist, (Node *) quals, NULL);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -1164,7 +1166,7 @@ create_material_plan(PlannerInfo *root, MaterialPath *best_path, int flags)
plan = make_material(subplan);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -1367,7 +1369,7 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path, int flags)
}
/* Copy cost data from Path to Plan */
- copy_generic_path_info(plan, &best_path->path);
+ copy_generic_path_info(root, plan, &best_path->path);
return plan;
}
@@ -1399,7 +1401,7 @@ create_gather_plan(PlannerInfo *root, GatherPath *best_path)
best_path->single_copy,
subplan);
- copy_generic_path_info(&gather_plan->plan, &best_path->path);
+ copy_generic_path_info(root, &gather_plan->plan, &best_path->path);
/* use parallel mode for parallel plans. */
root->glob->parallelModeNeeded = true;
@@ -1460,7 +1462,7 @@ create_projection_plan(PlannerInfo *root, ProjectionPath *best_path)
/* We need a Result node */
plan = (Plan *) make_result(tlist, NULL, subplan);
- copy_generic_path_info(plan, (Path *) best_path);
+ copy_generic_path_info(root, plan, (Path *) best_path);
}
return plan;
@@ -1515,7 +1517,7 @@ create_sort_plan(PlannerInfo *root, SortPath *best_path, int flags)
plan = make_sort_from_pathkeys(subplan, best_path->path.pathkeys);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -1552,7 +1554,7 @@ create_group_plan(PlannerInfo *root, GroupPath *best_path)
extract_grouping_ops(best_path->groupClause),
subplan);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -1580,7 +1582,7 @@ create_upper_unique_plan(PlannerInfo *root, UpperUniquePath *best_path, int flag
best_path->path.pathkeys,
best_path->numkeys);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -1621,7 +1623,7 @@ create_agg_plan(PlannerInfo *root, AggPath *best_path)
best_path->numGroups,
subplan);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -1809,7 +1811,7 @@ create_groupingsets_plan(PlannerInfo *root, GroupingSetsPath *best_path)
subplan);
/* Copy cost data from Path to Plan */
- copy_generic_path_info(&plan->plan, &best_path->path);
+ copy_generic_path_info(root, &plan->plan, &best_path->path);
}
return (Plan *) plan;
@@ -1864,7 +1866,7 @@ create_minmaxagg_plan(PlannerInfo *root, MinMaxAggPath *best_path)
plan = make_result(tlist, (Node *) best_path->quals, NULL);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
/*
* During setrefs.c, we'll need to replace references to the Agg nodes
@@ -1958,7 +1960,7 @@ create_windowagg_plan(PlannerInfo *root, WindowAggPath *best_path)
wc->endOffset,
subplan);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -2100,7 +2102,7 @@ create_setop_plan(PlannerInfo *root, SetOpPath *best_path, int flags)
best_path->firstFlag,
numGroups);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -2136,7 +2138,7 @@ create_recursiveunion_plan(PlannerInfo *root, RecursiveUnionPath *best_path)
best_path->distinctList,
numGroups);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -2159,7 +2161,7 @@ create_lockrows_plan(PlannerInfo *root, LockRowsPath *best_path,
plan = make_lockrows(subplan, best_path->rowMarks, best_path->epqParam);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -2217,7 +2219,7 @@ create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path)
best_path->onconflict,
best_path->epqParam);
- copy_generic_path_info(&plan->plan, &best_path->path);
+ copy_generic_path_info(root, &plan->plan, &best_path->path);
return plan;
}
@@ -2241,7 +2243,7 @@ create_limit_plan(PlannerInfo *root, LimitPath *best_path, int flags)
best_path->limitOffset,
best_path->limitCount);
- copy_generic_path_info(&plan->plan, (Path *) best_path);
+ copy_generic_path_info(root, &plan->plan, (Path *) best_path);
return plan;
}
@@ -2287,7 +2289,7 @@ create_seqscan_plan(PlannerInfo *root, Path *best_path,
scan_clauses,
scan_relid);
- copy_generic_path_info(&scan_plan->plan, best_path);
+ copy_generic_path_info(root, &scan_plan->plan, best_path);
return scan_plan;
}
@@ -2333,7 +2335,7 @@ create_samplescan_plan(PlannerInfo *root, Path *best_path,
scan_relid,
tsc);
- copy_generic_path_info(&scan_plan->scan.plan, best_path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, best_path);
return scan_plan;
}
@@ -2514,7 +2516,7 @@ create_indexscan_plan(PlannerInfo *root,
indexorderbyops,
best_path->indexscandir);
- copy_generic_path_info(&scan_plan->plan, &best_path->path);
+ copy_generic_path_info(root, &scan_plan->plan, &best_path->path);
return scan_plan;
}
@@ -2627,7 +2629,7 @@ create_bitmap_scan_plan(PlannerInfo *root,
bitmapqualorig,
baserelid);
- copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, &best_path->path);
return scan_plan;
}
@@ -2888,7 +2890,7 @@ create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
scan_relid,
tidquals);
- copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, &best_path->path);
return scan_plan;
}
@@ -2938,7 +2940,7 @@ create_subqueryscan_plan(PlannerInfo *root, SubqueryScanPath *best_path,
scan_relid,
subplan);
- copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, &best_path->path);
return scan_plan;
}
@@ -2981,7 +2983,7 @@ create_functionscan_plan(PlannerInfo *root, Path *best_path,
scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
functions, rte->funcordinality);
- copy_generic_path_info(&scan_plan->scan.plan, best_path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, best_path);
return scan_plan;
}
@@ -3025,7 +3027,7 @@ create_valuesscan_plan(PlannerInfo *root, Path *best_path,
scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
values_lists);
- copy_generic_path_info(&scan_plan->scan.plan, best_path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, best_path);
return scan_plan;
}
@@ -3118,7 +3120,7 @@ create_ctescan_plan(PlannerInfo *root, Path *best_path,
scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
plan_id, cte_param_id);
- copy_generic_path_info(&scan_plan->scan.plan, best_path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, best_path);
return scan_plan;
}
@@ -3178,7 +3180,7 @@ create_worktablescan_plan(PlannerInfo *root, Path *best_path,
scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
cteroot->wt_param_id);
- copy_generic_path_info(&scan_plan->scan.plan, best_path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, best_path);
return scan_plan;
}
@@ -3238,7 +3240,7 @@ create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
outer_plan);
/* Copy cost data from Path to Plan; no need to make FDW do this */
- copy_generic_path_info(&scan_plan->scan.plan, &best_path->path);
+ copy_generic_path_info(root, &scan_plan->scan.plan, &best_path->path);
/* Copy foreign server OID; likewise, no need to make FDW do this */
scan_plan->fs_server = rel->serverid;
@@ -3365,7 +3367,7 @@ create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
* Copy cost data from Path to Plan; no need to make custom-plan providers
* do this
*/
- copy_generic_path_info(&cplan->scan.plan, &best_path->path);
+ copy_generic_path_info(root, &cplan->scan.plan, &best_path->path);
/* Likewise, copy the relids that are represented by this custom scan */
cplan->custom_relids = best_path->path.parent->relids;
@@ -3496,7 +3498,7 @@ create_nestloop_plan(PlannerInfo *root,
inner_plan,
best_path->jointype);
- copy_generic_path_info(&join_plan->join.plan, &best_path->path);
+ copy_generic_path_info(root, &join_plan->join.plan, &best_path->path);
return join_plan;
}
@@ -3800,7 +3802,7 @@ create_mergejoin_plan(PlannerInfo *root,
best_path->jpath.jointype);
/* Costs of sort and material steps are included in path cost already */
- copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
+ copy_generic_path_info(root, &join_plan->join.plan, &best_path->jpath.path);
return join_plan;
}
@@ -3939,7 +3941,7 @@ create_hashjoin_plan(PlannerInfo *root,
(Plan *) hash_plan,
best_path->jpath.jointype);
- copy_generic_path_info(&join_plan->join.plan, &best_path->jpath.path);
+ copy_generic_path_info(root, &join_plan->join.plan, &best_path->jpath.path);
return join_plan;
}
@@ -4578,13 +4580,16 @@ order_qual_clauses(PlannerInfo *root, List *clauses)
* Also copy the parallel-aware flag, which the executor *will* use.
*/
static void
-copy_generic_path_info(Plan *dest, Path *src)
+copy_generic_path_info(PlannerInfo *root, Plan *dest, Path *src)
{
dest->startup_cost = src->startup_cost;
dest->total_cost = src->total_cost;
dest->plan_rows = src->rows;
dest->plan_width = src->pathtarget->width;
dest->parallel_aware = src->parallel_aware;
+
+ if (copy_generic_path_info_hook)
+ (*copy_generic_path_info_hook) (root, dest, src);
}
/*
diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 369179f..6e81ae8 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -125,6 +125,16 @@ typedef struct Plan
* subselects) */
/*
+ * information for adaptive query optimization
+ */
+ bool had_path;
+ List *path_clauses;
+ List *path_relids;
+ JoinType path_jointype;
+ int path_parallel_workers;
+ bool was_parametrized;
+
+ /*
* Information for management of parameter-change-driven rescanning
*
* extParam includes the paramIDs of all external PARAM_EXEC params
diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h
index 2a4df2f..64994a7 100644
--- a/src/include/optimizer/cost.h
+++ b/src/include/optimizer/cost.h
@@ -39,6 +39,34 @@ typedef enum
} ConstraintExclusionType;
+/* Hook for plugins to get control of cardinality estimation */
+typedef void (*set_baserel_rows_estimate_hook_type) (PlannerInfo *root,
+ RelOptInfo *rel);
+extern PGDLLIMPORT set_baserel_rows_estimate_hook_type
+ set_baserel_rows_estimate_hook;
+typedef double (*get_parameterized_baserel_size_hook_type) (PlannerInfo *root,
+ RelOptInfo *rel,
+ List *param_clauses);
+extern PGDLLIMPORT get_parameterized_baserel_size_hook_type
+ get_parameterized_baserel_size_hook;
+typedef double (*get_parameterized_joinrel_size_hook_type) (PlannerInfo *root,
+ RelOptInfo *rel,
+ Path *outer_path,
+ Path *inner_path,
+ SpecialJoinInfo *sjinfo,
+ List *restrict_clauses);
+extern PGDLLIMPORT get_parameterized_joinrel_size_hook_type
+ get_parameterized_joinrel_size_hook;
+typedef void (*set_joinrel_size_estimates_hook_type) (PlannerInfo *root,
+ RelOptInfo *rel,
+ RelOptInfo *outer_rel,
+ RelOptInfo *inner_rel,
+ SpecialJoinInfo *sjinfo,
+ List *restrictlist);
+extern PGDLLIMPORT set_joinrel_size_estimates_hook_type
+ set_joinrel_size_estimates_hook;
+
+
/*
* prototypes for costsize.c
* routines to compute costs and sizes
@@ -161,21 +189,37 @@ extern void compute_semi_anti_join_factors(PlannerInfo *root,
SpecialJoinInfo *sjinfo,
List *restrictlist,
SemiAntiJoinFactors *semifactors);
+extern void set_baserel_rows_estimate(PlannerInfo *root, RelOptInfo *rel);
+extern void set_baserel_rows_estimate_standard(PlannerInfo *root, RelOptInfo *rel);
extern void set_baserel_size_estimates(PlannerInfo *root, RelOptInfo *rel);
extern double get_parameterized_baserel_size(PlannerInfo *root,
RelOptInfo *rel,
List *param_clauses);
+extern double get_parameterized_baserel_size_standard(PlannerInfo *root,
+ RelOptInfo *rel,
+ List *param_clauses);
extern double get_parameterized_joinrel_size(PlannerInfo *root,
RelOptInfo *rel,
Path *outer_path,
Path *inner_path,
SpecialJoinInfo *sjinfo,
List *restrict_clauses);
+extern double get_parameterized_joinrel_size_standard(PlannerInfo *root,
+ RelOptInfo *rel,
+ Path *outer_path,
+ Path *inner_path,
+ SpecialJoinInfo *sjinfo,
+ List *restrict_clauses);
extern void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *rel,
RelOptInfo *outer_rel,
RelOptInfo *inner_rel,
SpecialJoinInfo *sjinfo,
List *restrictlist);
+extern void set_joinrel_size_estimates_standard(PlannerInfo *root, RelOptInfo *rel,
+ RelOptInfo *outer_rel,
+ RelOptInfo *inner_rel,
+ SpecialJoinInfo *sjinfo,
+ List *restrictlist);
extern void set_subquery_size_estimates(PlannerInfo *root, RelOptInfo *rel);
extern void set_function_size_estimates(PlannerInfo *root, RelOptInfo *rel);
extern void set_values_size_estimates(PlannerInfo *root, RelOptInfo *rel);
diff --git a/src/include/optimizer/planmain.h b/src/include/optimizer/planmain.h
index 4fbb6cc..def55e5 100644
--- a/src/include/optimizer/planmain.h
+++ b/src/include/optimizer/planmain.h
@@ -33,6 +33,12 @@ extern int force_parallel_mode;
/* query_planner callback to compute query_pathkeys */
typedef void (*query_pathkeys_callback) (PlannerInfo *root, void *extra);
+/* hook for plugins to get control in creating plan from path */
+typedef void (*copy_generic_path_info_hook_type) (PlannerInfo *root,
+ Plan *dest, Path *src);
+
+extern PGDLLIMPORT copy_generic_path_info_hook_type copy_generic_path_info_hook;
+
/*
* prototypes for plan/planmain.c
*/
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 7a6545f..42b58c0 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -46,6 +46,9 @@ ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL;
/* Hook for plugins to get control in explain_get_index_name() */
explain_get_index_name_hook_type explain_get_index_name_hook = NULL;
+/* Hook for plugins to get control in ExplainOnePlan() */
+ExplainOnePlan_hook_type ExplainOnePlan_hook = NULL;
+
/* OR-able flags for ExplainXMLTag() */
#define X_OPENING 0
@@ -558,6 +561,10 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
3, es);
}
+ if (ExplainOnePlan_hook)
+ ExplainOnePlan_hook(plannedstmt, into, es,
+ queryString, params, planduration);
+
ExplainCloseGroup("Query", NULL, true, es);
}
diff --git a/src/include/commands/explain.h b/src/include/commands/explain.h
index 1f0bde7..9ca637c 100644
--- a/src/include/commands/explain.h
+++ b/src/include/commands/explain.h
@@ -58,6 +58,12 @@ extern PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook;
typedef const char *(*explain_get_index_name_hook_type) (Oid indexId);
extern PGDLLIMPORT explain_get_index_name_hook_type explain_get_index_name_hook;
+/* Hook for plugins to get control in ExplainOnePlan() */
+typedef void (*ExplainOnePlan_hook_type) (PlannedStmt *plannedstmt, IntoClause *into,
+ ExplainState *es, const char *queryString,
+ ParamListInfo params, const instr_time *planduration);
+extern PGDLLIMPORT ExplainOnePlan_hook_type ExplainOnePlan_hook;
+
extern void ExplainQuery(ExplainStmt *stmt, const char *queryString,
ParamListInfo params, DestReceiver *dest);