8000 Reduce redundancy in the estimate_num_groups routine. · postgrespro/aqo@0f6585e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f6585e

Browse files
danolivoAndrey Lepikhov
authored andcommitted
Reduce redundancy in the estimate_num_groups routine.
This commit changes Core Patch.
1 parent ffe52ee commit 0f6585e

File tree

4 files changed

+38
-61
lines changed

4 files changed

+38
-61
lines changed

aqo_master.patch

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ index 340db2bac4..b55896bb89 100644
5757
if (es->format == EXPLAIN_FORMAT_TEXT)
5858
appendStringInfoChar(es->str, '\n');
5959
diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
60-
index 9d4893c504..1261be3494 100644
60+
index 29020c908e..f7e673e75d 100644
6161
--- a/src/backend/nodes/copyfuncs.c
6262
+++ b/src/backend/nodes/copyfuncs.c
6363
@@ -129,6 +129,7 @@ CopyPlanFields(const Plan *from, Plan *newnode)
@@ -69,7 +69,7 @@ index 9d4893c504..1261be3494 100644
6969

7070
/*
7171
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
72-
index e73be21bd6..2457b54c39 100644
72+
index 48202d2232..ac24b8edf4 100644
7373
--- a/src/backend/nodes/outfuncs.c
7474
+++ b/src/backend/nodes/outfuncs.c
7575
@@ -342,6 +342,7 @@ _outPlanInfo(StringInfo str, const Plan *node)
@@ -393,15 +393,15 @@ index d3f8639a40..f18e1c1a54 100644
393393

394394
/*
395395
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
396-
index 1868c4eff4..397e78c2ef 100644
396+
index 1868c4eff4..100b0051a2 100644
397397
--- a/src/backend/optimizer/plan/planner.c
398398
+++ b/src/backend/optimizer/plan/planner.c
399399
@@ -143,7 +143,8 @@ static List *extract_rollup_sets(List *groupingSets);
400400
static List *reorder_grouping_sets(List *groupingSets, List *sortclause);
401401
static void standard_qp_callback(PlannerInfo *root, void *extra);
402402
static double get_number_of_groups(PlannerInfo *root,
403403
- double path_rows,
404-
+ RelOptInfo *rel,
404+
+ Path *subpath,
405405
+ RelOptInfo *grouped_rel,
406406
grouping_sets_data *gd,
407407
List *target_list);
@@ -411,89 +411,64 @@ index 1868c4eff4..397e78c2ef 100644
411411
static double
412412
get_number_of_groups(PlannerInfo *root,
413413
- double path_rows,
414-
+ RelOptInfo *rel,
414+
+ Path *subpath,
415415
+ RelOptInfo *grouped_rel,
416416
grouping_sets_data *gd,
417417
List *target_list)
418418
{
419-
@@ -3185,9 +3187,10 @@ get_number_of_groups(PlannerInfo *root,
420-
{
421-
List *gset = (List *) lfirst(lc);
419+
@@ -3187,7 +3189,7 @@ get_number_of_groups(PlannerInfo *root,
422420
GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
423-
- double numGroups = estimate_num_groups(root,
424-
+ double numGroups = estimate_num_groups_ext(root,
421+
double numGroups = estimate_num_groups(root,
425422
groupExprs,
426423
- path_rows,
427-
+ rel,
428-
+ grouped_rel,
424+
+ subpath->rows,
429425
&gset,
430426
NULL);
431427

432-
@@ -3211,9 +3214,10 @@ get_number_of_groups(PlannerInfo *root,
433-
{
434-
List *gset = (List *) lfirst(lc);
428+
@@ -3213,7 +3215,7 @@ get_number_of_groups(PlannerInfo *root,
435429
GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
436-
- double numGroups = estimate_num_groups(root,
437-
+ double numGroups = estimate_num_groups_ext(root,
430+
double numGroups = estimate_num_groups(root,
438431
groupExprs,
439432
- path_rows,
440-
+ rel,
441-
+ grouped_rel,
433+
+ subpath->rows,
442434
&gset,
443435
NULL);
444436

445-
@@ -3230,8 +3234,8 @@ get_number_of_groups(PlannerInfo *root,
437+
@@ -3230,8 +3232,8 @@ get_number_of_groups(PlannerInfo *root,
446438
groupExprs = get_sortgrouplist_exprs(parse->groupClause,
447439
target_list);
448440

449441
- dNumGroups = estimate_num_groups(root, groupExprs, path_rows,
450442
- NULL, NULL);
451-
+ dNumGroups = estimate_num_groups_ext(root, groupExprs, rel,
443+
+ dNumGroups = estimate_num_groups_ext(root, groupExprs, subpath,
452444
+ grouped_rel, NULL, NULL);
453445
}
454446
}
455447
else if (parse->groupingSets)
456-
@@ -3403,7 +3407,7 @@ make_grouping_rel(PlannerInfo *root, RelOptInfo *input_rel,
457-
* NULL. (This could be changed, but might require adjustments
458-
* elsewhere.)
459-
*/
460-
- grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG, NULL);
461-
+ grouped_rel = fetch_upper_rel(root, UPPERREL_GROUP_AGG, input_rel->relids);
462-
}
463-
464-
/* Set target. */
465-
@@ -3529,7 +3533,6 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
466-
GroupPathExtraData *extra,
467-
RelOptInfo **partially_grouped_rel_p)
468-
{
469-
- Path *cheapest_path = input_rel->cheapest_total_path;
470-
RelOptInfo *partially_grouped_rel = NULL;
471-
double dNumGroups;
472-
PartitionwiseAggregateType patype = PARTITIONWISE_AGGREGATE_NONE;
473-
@@ -3618,7 +3621,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
448+
@@ -3618,7 +3620,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
474449
* Estimate number of groups.
475450
*/
476451
dNumGroups = get_number_of_groups(root,
477452
- cheapest_path->rows,
478-
+ input_rel,
453+
+ cheapest_path,
479454
+ grouped_rel,
480455
gd,
481456
extra->targetList);
482457

483-
@@ -6424,13 +6428,15 @@ create_partial_grouping_paths(PlannerInfo *root,
458+
@@ -6424,13 +6427,15 @@ create_partial_grouping_paths(PlannerInfo *root,
484459
if (cheapest_total_path != NULL)
485460
dNumPartialGroups =
486461
get_number_of_groups(root,
487462
- cheapest_total_path->rows,
488-
+ input_rel,
463+
+ cheapest_total_path,
489464
+ partially_grouped_rel,
490465
gd,
491466
extra->targetList);
492467
if (cheapest_partial_path != NULL)
493468
dNumPartialPartialGroups =
494469
get_number_of_groups(root,
495470
- cheapest_partial_path->rows,
496-
+ input_rel,
471+
+ cheapest_partial_path,
497472
+ partially_grouped_rel,
498473
gd,
499474
extra->targetList);
@@ -565,7 +540,7 @@ index e105a4d5f1..d821ea63bd 100644
565540

566541
return ppi;
567542
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
568-
index 0c8c05f6c2..0ddc3fc46c 100644
543+
index 0c8c05f6c2..eba4d982b9 100644
569544
--- a/src/backend/utils/adt/selfuncs.c
570545
+++ b/src/backend/utils/adt/selfuncs.c
571546
@@ -143,6 +143,7 @@
@@ -581,14 +556,14 @@ index 0c8c05f6c2..0ddc3fc46c 100644
581556
}
582557

583558
+double
584-
+estimate_num_groups_ext(PlannerInfo *root, List *groupExprs, RelOptInfo *rel,
559+
+estimate_num_groups_ext(PlannerInfo *root, List *groupExprs, Path *subpath,
585560
+ RelOptInfo *grouped_rel, List **pgset,
586561
+ EstimationInfo *estinfo)
587562
+{
588-
+ double input_rows = rel->cheapest_total_path->rows;
563+
+ double input_rows = subpath->rows;
589564
+
590565
+ if (estimate_num_groups_hook != NULL)
591-
+ return (*estimate_num_groups_hook)(root, groupExprs, rel, grouped_rel,
566+
+ return (*estimate_num_groups_hook)(root, groupExprs, subpath, grouped_rel,
592567
+ pgset, estinfo);
593568
+
594569
+ return estimate_num_groups(root, groupExprs, input_rows, pgset, estinfo);
@@ -621,7 +596,7 @@ index e94d9e49cf..49236ced77 100644
621596
extern void ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
622597
ParamListInfo params, DestReceiver *dest);
623598
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
624-
index a692bcfb53..74aed4b1fb 100644
599+
index e20c245f98..a2ba24aada 100644
625600
--- a/src/include/nodes/pathnodes.h
626601
+++ b/src/include/nodes/pathnodes.h
627602
@@ -751,6 +751,10 @@ typedef struct RelOptInfo
@@ -788,7 +763,7 @@ index bf1adfc52a..9c78e0f4e0 100644
788763
* prototypes for plan/planmain.c
789764
*/
790765
diff --git a/src/include/utils/selfuncs.h b/src/include/utils/selfuncs.h
791-
index 9dd444e1ff..2c0f942250 100644
766+
index 9dd444e1ff..b0b5a65618 100644
792767
--- a/src/include/utils/selfuncs.h
793768
+++ b/src/include/utils/selfuncs.h
794769
@@ -144,6 +144,13 @@ typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
@@ -797,7 +772,7 @@ index 9dd444e1ff..2c0f942250 100644
797772
extern PGDLLIMPORT get_index_stats_hook_type get_index_stats_hook;
798773
+typedef double (*estimate_num_groups_hook_type) (PlannerInfo *root,
799774
+ List *groupExprs,
800-
+ RelOptInfo *rel,
775+
+ Path *subpath,
801776
+ RelOptInfo *grouped_rel,
802777
+ List **pgset,
803778
+ EstimationInfo *estinfo);
@@ -810,7 +785,7 @@ index 9dd444e1ff..2c0f942250 100644
810785
Selectivity *rightstart 6377 , Selectivity *rightend);
811786

812787
+extern double estimate_num_groups_ext(PlannerInfo *root, List *groupExprs,
813-
+ RelOptInfo *rel, RelOptInfo *grouped_rel,
788+
+ Path *subpath, RelOptInfo *grouped_rel,
814789
+ List **pgset, EstimationInfo *estinfo);
815790
extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
816791
double input_rows, List **pgset,

cardinality_hooks.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,25 +398,25 @@ aqo_get_parameterized_joinrel_size(PlannerInfo *root,
398398
}
399399

400400
static double
401-
predict_num_groups(PlannerInfo *root, RelOptInfo *rel, List *group_exprs,
401+
predict_num_groups(PlannerInfo *root, Path *subpath, List *group_exprs,
402402
int *fss)
403403
{
404404
int child_fss = 0;
405405
double prediction;
406406
int rows;
407407
double target;
408408

409-
if (rel->predicted_cardinality > 0.)
409+
if (subpath->parent->predicted_cardinality > 0.)
410410
/* A fast path. Here we can use a fss hash of a leaf. */
411-
child_fss = rel->fss_hash;
411+
child_fss = subpath->parent->fss_hash;
412412
else
413413
{
414414
List *relids;
415415
List *clauses;
416416
List *selectivities = NIL;
417417

418-
relids = get_list_of_relids(root, rel->relids);
419-
clauses = get_path_clauses(rel->cheapest_total_path, root, &selectivities);
418+
relids = get_list_of_relids(root, subpath->parent->relids);
419+
clauses = get_path_clauses(subpath, root, &selectivities);
420420
(void) predict_for_relation(clauses, selectivities, relids, &child_fss);
421421
}
422422

@@ -432,18 +432,19 @@ predict_num_groups(PlannerInfo *root, RelOptInfo *rel, List *group_exprs,
432432

433433
double
434434
aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
435-
RelOptInfo *rel, RelOptInfo *grouped_rel,
435+
Path *subpath, RelOptInfo *grouped_rel,
436436
List **pgset, EstimationInfo *estinfo)
437437
{
438-
double input_rows = rel->cheapest_total_path->rows;
438+
double input_rows = subpath->rows;
439439
double nGroups = -1;
440440
int fss;
441441
double predicted;
442442

443443
if (!query_context.use_aqo)
444444
{
445445
if (prev_estimate_num_groups_hook != NULL)
446-
nGroups = (*prev_estimate_num_groups_hook)(root, groupExprs, rel,
446+
nGroups = (*prev_estimate_num_groups_hook)(root, groupExprs,
447+
subpath,
447448
grouped_rel,
448449
pgset, estinfo);
449450
if (nGroups < 0)
@@ -466,7 +467,7 @@ aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
466467
if (groupExprs == NIL)
467468
return 1.0;
468469

469-
predicted = predict_num_groups(root, rel, groupExprs, &fss);
470+
predicted = predict_num_groups(root, subpath, groupExprs, &fss);
470471

471472
if (predicted > 0.)
472473
{

cardinality_hooks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
extern estimate_num_groups_hook_type prev_estimate_num_groups_hook;
88

99
extern double aqo_estimate_num_groups_hook(PlannerInfo *root, List *groupExprs,
10-
RelOptInfo *rel,
10+
Path *subpath,
1111
RelOptInfo *grouped_rel,
1212
List **pgset,
1313
EstimationInfo *estinfo);

path_utils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ aqo_create_plan_hook(PlannerInfo *root, Path *src, Plan **dest)
376376
root->processed_tlist);
377377
/* Copy bare expressions for further AQO learning case. */
378378
node->grouping_exprs = copyObject(groupExprs);
379+
node->relids = get_list_of_relids(root, ap->subpath->parent->relids);
379380
}
380381
else
381382
{

0 commit comments

Comments
 (0)
0