-
Notifications
You must be signed in to change notification settings - Fork 55
/
aqo_master.patch
735 lines (681 loc) · 25.9 KB
/
aqo_master.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
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 18a5af6b91..18c2ed3bfd 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -25,6 +25,7 @@
#include "nodes/extensible.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
+#include "optimizer/cost.h"
#include "parser/analyze.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteHandler.h"
@@ -48,6 +49,12 @@ 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;
+
+/* Hook for plugins to get control in ExplainOnePlan() */
+ExplainOneNode_hook_type ExplainOneNode_hook = NULL;
+
/* Instrumentation data for SERIALIZE option */
typedef struct SerializeMetrics
@@ -805,6 +812,10 @@ ExplainOnePlan(PlannedStmt *plannedstmt, IntoClause *into, ExplainState *es,
ExplainPropertyFloat("Execution Time", "ms", 1000.0 * totaltime, 3,
es);
+ if (ExplainOnePlan_hook)
+ ExplainOnePlan_hook(plannedstmt, into, es,
+ queryString, params, planduration, queryEnv);
+
ExplainCloseGroup("Query", NULL, true, es);
}
@@ -2001,6 +2012,9 @@ ExplainNode(PlanState *planstate, List *ancestors,
}
}
+ if (ExplainOneNode_hook)
+ ExplainOneNode_hook(es, planstate, plan);
+
/* in text format, first line ends here */
if (es->format == EXPLAIN_FORMAT_TEXT)
appendStringInfoChar(es->str, '\n');
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 2bb6db1df7..ac95740598 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -109,6 +109,11 @@
#include "utils/spccache.h"
#include "utils/tuplesort.h"
+set_baserel_rows_estimate_hook_type set_baserel_rows_estimate_hook = NULL;
+set_foreign_rows_estimate_hook_type set_foreign_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;
#define LOG2(x) (log(x) / 0.693147180559945)
@@ -202,7 +207,6 @@ static void set_rel_width(PlannerInfo *root, RelOptInfo *rel);
static int32 get_expr_width(PlannerInfo *root, const Node *expr);
static double relation_byte_size(double tuples, int width);
static double page_size(double tuples, int width);
-static double get_parallel_divisor(Path *path);
/*
@@ -5309,6 +5313,58 @@ approx_tuple_count(PlannerInfo *root, JoinPath *path, List *quals)
}
+void
+set_foreign_rows_estimate(PlannerInfo *root, RelOptInfo *rel)
+{
+ if (set_foreign_rows_estimate_hook)
+ (*set_foreign_rows_estimate_hook) (root, rel);
+ else
+ rel->rows = 1000; /* entirely bogus default estimate */
+}
+
+/*
+ * 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.
@@ -5325,19 +5381,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);
@@ -5348,13 +5395,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;
@@ -5383,6 +5450,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.
@@ -5402,11 +5499,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,
rel,
@@ -5422,6 +5519,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.
@@ -5434,11 +5560,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;
@@ -6153,7 +6279,7 @@ set_foreign_size_estimates(PlannerInfo *root, RelOptInfo *rel)
/* Should only be applied to base relations */
Assert(rel->relid > 0);
- rel->rows = 1000; /* entirely bogus default estimate */
+ set_foreign_rows_estimate(root, rel);
cost_qual_eval(&rel->baserestrictcost, rel->baserestrictinfo, root);
@@ -6446,7 +6572,7 @@ page_size(double tuples, int width)
* Estimate the fraction of the work that each worker will do given the
* number of workers budgeted for the path.
*/
-static double
+double
get_parallel_divisor(Path *path)
{
double parallel_divisor = path->parallel_workers;
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index c13586c537..2f889570de 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -72,6 +72,7 @@
#define CP_LABEL_TLIST 0x0004 /* tlist must contain sortgrouprefs */
#define CP_IGNORE_TLIST 0x0008 /* caller will replace tlist */
+create_plan_hook_type create_plan_hook = NULL;
static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path,
int flags);
@@ -551,6 +552,10 @@ create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
break;
}
+ if (create_plan_hook)
+ /* Give an extension a chance to do something */
+ (*create_plan_hook)(root, best_path, &plan);
+
return plan;
}
@@ -5458,6 +5463,7 @@ copy_generic_path_info(Plan *dest, Path *src)
dest->plan_width = src->pathtarget->width;
dest->parallel_aware = src->parallel_aware;
dest->parallel_safe = src->parallel_safe;
+ dest->ext_nodes = NIL;
}
/*
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 0f423e9684..8cd228baa1 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -144,7 +144,8 @@ static List *extract_rollup_sets(List *groupingSets);
static List *reorder_grouping_sets(List *groupingSets, List *sortclause);
static void standard_qp_callback(PlannerInfo *root, void *extra);
static double get_number_of_groups(PlannerInfo *root,
- double path_rows,
+ Path *subpath,
+ RelOptInfo *grouped_rel,
grouping_sets_data *gd,
List *target_list);
static RelOptInfo *create_grouping_paths(PlannerInfo *root,
@@ -3697,7 +3698,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
*/
static double
get_number_of_groups(PlannerInfo *root,
- double path_rows,
+ Path *subpath,
+ RelOptInfo *grouped_rel,
grouping_sets_data *gd,
List *target_list)
{
@@ -3734,7 +3736,7 @@ get_number_of_groups(PlannerInfo *root,
GroupingSetData *gs = lfirst_node(GroupingSetData, lc3);
double numGroups = estimate_num_groups(root,
groupExprs,
- path_rows,
+ subpath->rows,
&gset,
NULL);
@@ -3760,7 +3762,7 @@ get_number_of_groups(PlannerInfo *root,
GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
double numGroups = estimate_num_groups(root,
groupExprs,
- path_rows,
+ subpath->rows,
&gset,
NULL);
@@ -3777,8 +3779,8 @@ get_number_of_groups(PlannerInfo *root,
groupExprs = get_sortgrouplist_exprs(root->processed_groupClause,
target_list);
- dNumGroups = estimate_num_groups(root, groupExprs, path_rows,
- NULL, NULL);
+ dNumGroups = estimate_num_groups_ext(root, groupExprs, subpath,
+ grouped_rel, NULL, NULL);
}
}
else if (parse->groupingSets)
@@ -4168,7 +4170,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
* Estimate number of groups.
*/
dNumGroups = get_number_of_groups(root,
- cheapest_path->rows,
+ cheapest_path,
+ grouped_rel,
gd,
extra->targetList);
@@ -7405,13 +7408,15 @@ create_partial_grouping_paths(PlannerInfo *root,
if (cheapest_total_path != NULL)
dNumPartialGroups =
get_number_of_groups(root,
- cheapest_total_path->rows,
+ cheapest_total_path,
+ partially_grouped_rel,
gd,
extra->targetList);
if (cheapest_partial_path != NULL)
dNumPartialPartialGroups =
get_number_of_groups(root,
- cheapest_partial_path->rows,
+ cheapest_partial_path,
+ partially_grouped_rel,
gd,
extra->targetList);
diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
index d7266e4cdb..7e5b771d9f 100644
--- a/src/backend/optimizer/util/relnode.c
+++ b/src/backend/optimizer/util/relnode.c
@@ -286,6 +286,7 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
rel->all_partrels = NULL;
rel->partexprs = NULL;
rel->nullable_partexprs = NULL;
+ rel->ext_nodes = NULL;
/*
* Pass assorted information down the inheritance hierarchy.
@@ -422,7 +423,6 @@ find_base_rel(PlannerInfo *root, int relid)
if (rel)
return rel;
}
-
elog(ERROR, "no relation entry for relid %d", relid);
return NULL; /* keep compiler quiet */
@@ -768,6 +768,7 @@ build_join_rel(PlannerInfo *root,
joinrel->all_partrels = NULL;
joinrel->partexprs = NULL;
joinrel->nullable_partexprs = NULL;
+ joinrel->ext_nodes = NULL;
/* Compute information relevant to the foreign relations. */
set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
@@ -952,6 +953,7 @@ build_child_join_rel(PlannerInfo *root, RelOptInfo *outer_rel,
joinrel->all_partrels = NULL;
joinrel->partexprs = NULL;
joinrel->nullable_partexprs = NULL;
+ joinrel->ext_nodes = NULL;
/* Compute information relevant to foreign relations. */
set_foreign_rel_properties(joinrel, outer_rel, inner_rel);
@@ -1530,6 +1532,7 @@ find_childrel_parents(PlannerInfo *root, RelOptInfo *rel)
}
+set_parampathinfo_postinit_hook_type parampathinfo_postinit_hook = NULL;
/*
* get_baserel_parampathinfo
* Get the ParamPathInfo for a parameterized path for a base relation,
@@ -1622,6 +1625,10 @@ get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel,
ppi->ppi_rows = rows;
ppi->ppi_clauses = pclauses;
ppi->ppi_serials = pserials;
+
+ if (parampathinfo_postinit_hook)
+ (*parampathinfo_postinit_hook)(ppi);
+
baserel->ppilist = lappend(baserel->ppilist, ppi);
return ppi;
@@ -1876,6 +1883,10 @@ get_appendrel_parampathinfo(RelOptInfo *appendrel, Relids required_outer)
ppi->ppi_rows = 0;
ppi->ppi_clauses = NIL;
ppi->ppi_serials = NULL;
+
+ if (parampathinfo_postinit_hook)
+ (*parampathinfo_postinit_hook)(ppi);
+
appendrel->ppilist = lappend(appendrel->ppilist, ppi);
return ppi;
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 08fa6774d9..034b434773 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -146,6 +146,7 @@
/* Hooks for plugins to get control when we ask for stats */
get_relation_stats_hook_type get_relation_stats_hook = NULL;
get_index_stats_hook_type get_index_stats_hook = NULL;
+estimate_num_groups_hook_type estimate_num_groups_hook = NULL;
static double eqsel_internal(PG_FUNCTION_ARGS, bool negate);
static double eqjoinsel_inner(Oid opfuncoid, Oid collation,
@@ -3345,6 +3346,20 @@ add_unique_group_var(PlannerInfo *root, List *varinfos,
return varinfos;
}
+double
+estimate_num_groups_ext(PlannerInfo *root, List *groupExprs, Path *subpath,
+ RelOptInfo *grouped_rel, List **pgset,
+ EstimationInfo *estinfo)
+{
+ double input_rows = subpath->rows;
+
+ if (estimate_num_groups_hook != NULL)
+ return (*estimate_num_groups_hook)(root, groupExprs, subpath, grouped_rel,
+ pgset, estinfo);
+
+ return estimate_num_groups(root, groupExprs, input_rows, pgset, estinfo);
+}
+
/*
* estimate_num_groups - Estimate number of groups in a grouped query
*
diff --git a/src/include/commands/explain.h b/src/include/commands/explain.h
index 3ab0aae78f..5a257cdb0a 100644
--- a/src/include/commands/explain.h
+++ b/src/include/commands/explain.h
@@ -87,6 +87,18 @@ 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,
+ QueryEnvironment *queryEnv);
+extern PGDLLIMPORT ExplainOnePlan_hook_type ExplainOnePlan_hook;
+
+/* Explain a node info */
+typedef void (*ExplainOneNode_hook_type) (ExplainState *es,
+ PlanState *ps,
+ Plan *plan);
+extern PGDLLIMPORT ExplainOneNode_hook_type ExplainOneNode_hook;
extern void ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
ParamListInfo params, DestReceiver *dest);
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
index 07e2415398..1413fbf03c 100644
--- a/src/include/nodes/pathnodes.h
+++ b/src/include/nodes/pathnodes.h
@@ -1049,6 +1049,16 @@ typedef struct RelOptInfo
List **partexprs pg_node_attr(read_write_ignore);
/* Nullable partition key expressions */
List **nullable_partexprs pg_node_attr(read_write_ignore);
+
+ /* For Adaptive optimization DEBUG purposes */
+ double predicted_cardinality;
+ int fss_hash;
+
+ /*
+ * At this list an extension can add additional nodes to pass an info along
+ * the planning and executing stages.
+ */
+ List *ext_nodes;
} RelOptInfo;
/*
@@ -1586,6 +1596,10 @@ typedef struct ParamPathInfo
Cardinality ppi_rows; /* estimated number of result tuples */
List *ppi_clauses; /* join clauses available from outer rels */
Bitmapset *ppi_serials; /* set of rinfo_serial for enforced quals */
+
+ /* AQO DEBUG purposes */
+ double predicted_ppi_rows;
+ double fss_ppi_hash;
} ParamPathInfo;
diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 62cd6a6666..bb47a51ba7 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -170,6 +170,9 @@ typedef struct Plan
*/
Bitmapset *extParam;
Bitmapset *allParam;
+
+ /* Additional field for an extension purposes. */
+ List *ext_nodes;
} Plan;
/* ----------------
diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h
index 854a782944..9e8cde176f 100644
--- a/src/include/optimizer/cost.h
+++ b/src/include/optimizer/cost.h
@@ -41,6 +41,37 @@ typedef enum
} ConstraintExclusionType;
+/* Hook for plugins to get control of cardinality estimation */
+typedef void (*set_baserel_rows_estimate_hook_type) (PlannerInfo *root,
+ RelOptInfo *rel);
+typedef void (*set_foreign_rows_estimate_hook_type) (PlannerInfo *root,
+ RelOptInfo *rel);
+extern PGDLLIMPORT set_baserel_rows_estimate_hook_type
+ set_baserel_rows_estimate_hook;
+extern PGDLLIMPORT set_foreign_rows_estimate_hook_type
+ set_foreign_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
@@ -192,10 +223,22 @@ extern void compute_semi_anti_join_factors(PlannerInfo *root,
SpecialJoinInfo *sjinfo,
List *restrictlist,
SemiAntiJoinFactors *semifactors);
+extern void set_foreign_rows_estimate(PlannerInfo *root, RelOptInfo *rel);
+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_standard(PlannerInfo *root,
+ RelOptInfo *rel,
+ Path *outer_path,
+ Path *inner_path,
+ SpecialJoinInfo *sjinfo,
+ List *restrict_clauses);
extern double get_parameterized_joinrel_size(PlannerInfo *root,
RelOptInfo *rel,
Path *outer_path,
@@ -207,6 +250,11 @@ extern void set_joinrel_size_estimates(PlannerInfo *root, RelOptInfo *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);
@@ -221,5 +269,6 @@ extern double compute_bitmap_pages(PlannerInfo *root, RelOptInfo *baserel,
Path *bitmapqual, double loop_count,
Cost *cost_p, double *tuples_p);
extern double compute_gather_rows(Path *path);
+extern double get_parallel_divisor(Path *path);
#endif /* COST_H */
diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h
index 1035e6560c..27e42b2679 100644
--- a/src/include/optimizer/pathnode.h
+++ b/src/include/optimizer/pathnode.h
@@ -18,6 +18,10 @@
#include "nodes/pathnodes.h"
+typedef void (*set_parampathinfo_postinit_hook_type) (ParamPathInfo *ppi);
+
+extern PGDLLIMPORT set_parampathinfo_postinit_hook_type parampathinfo_postinit_hook;
+
/*
* prototypes for pathnode.c
*/
diff --git a/src/include/optimizer/planmain.h b/src/include/optimizer/planmain.h
index 93137261e4..0621c27595 100644
--- a/src/include/optimizer/planmain.h
+++ b/src/include/optimizer/planmain.h
@@ -24,6 +24,12 @@ extern PGDLLIMPORT double cursor_tuple_fraction;
/* query_planner callback to compute query_pathkeys */
typedef void (*query_pathkeys_callback) (PlannerInfo *root, void *extra);
+
+/* Hook for plugins to get control in ExecutorRun() */
+typedef void (*create_plan_hook_type) (PlannerInfo *root,
+ Path *best_path,
+ Plan **plan);
+extern PGDLLIMPORT create_plan_hook_type create_plan_hook;
/*
* prototypes for plan/planmain.c
*/
diff --git a/src/include/utils/selfuncs.h b/src/include/utils/selfuncs.h
index f2563ad1cb..0beac5f313 100644
--- a/src/include/utils/selfuncs.h
+++ b/src/include/utils/selfuncs.h
@@ -147,6 +147,13 @@ typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
AttrNumber indexattnum,
VariableStatData *vardata);
extern PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook;
+typedef double (*estimate_num_groups_hook_type) (PlannerInfo *root,
+ List *groupExprs,
+ Path *subpath,
+ RelOptInfo *grouped_rel,
+ List **pgset,
+ EstimationInfo *estinfo);
+extern PGDLLIMPORT estimate_num_groups_hook_type estimate_num_groups_hook;
/* Functions in selfuncs.c */
@@ -213,6 +220,9 @@ extern void mergejoinscansel(PlannerInfo *root, Node *clause,
Selectivity *leftstart, Selectivity *leftend,
Selectivity *rightstart, Selectivity *rightend);
+extern double estimate_num_groups_ext(PlannerInfo *root, List *groupExprs,
+ Path *subpath, RelOptInfo *grouped_rel,
+ List **pgset, EstimationInfo *estinfo);
extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
double input_rows, List **pgset,
EstimationInfo *estinfo);