Skip to content

Commit f75482f

Browse files
committed
Fix the problem with incorrect result tuples calculation of a node in the case of parallel execution.
1 parent 1fb13a5 commit f75482f

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

aqo_pg12.patch

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
2-
index 92969636b7..1e705c709f 100644
2+
index 92969636b7..42446d4e93 100644
33
--- a/src/backend/commands/explain.c
44
+++ b/src/backend/commands/explain.c
55
@@ -46,6 +46,9 @@ ExplainOneQuery_hook_type ExplainOneQuery_hook = NULL;
@@ -22,8 +22,26 @@ index 92969636b7..1e705c709f 100644
2222
+
2323
ExplainCloseGroup("Query", NULL, true, es);
2424
}
25-
26-
@@ -1523,6 +1530,24 @@ ExplainNode(PlanState *planstate, List *ancestors,
25+
26+
@@ -1041,6 +1048,17 @@ ExplainPreScanNode(PlanState *planstate, Bitmapset **rels_used)
27+
return planstate_tree_walker(planstate, ExplainPreScanNode, rels_used);
28+
}
29+
30+
+static bool
31+
+we_need_to_sum_tuples(const Plan *plan)
32+
+{
33+
+ if (plan->path_parallel_workers > 0 && (
34+
+ plan->parallel_aware || nodeTag(plan) == T_HashJoin ||
35+
+ nodeTag(plan) == T_MergeJoin ||
36+
+ nodeTag(plan) == T_NestLoop))
37+
+ return true;
38+
+ return false;
39+
+}
40+
+
41+
/*
42+
* ExplainNode -
43+
* Appends a description of a plan tree to es->str
44+
@@ -1523,6 +1541,24 @@ ExplainNode(PlanState *planstate, List *ancestors,
2745
appendStringInfo(es->str,
2846
" (actual rows=%.0f loops=%.0f)",
2947
rows, nloops);
@@ -33,8 +51,8 @@ index 92969636b7..1e705c709f 100644
3351
+ int wrkrs = 1;
3452
+ double error = -1.;
3553
+
36-
+ if (planstate->worker_instrument && plan->parallel_aware)
37-
+ wrkrs = planstate->worker_instrument->num_workers + 1;
54+
+ if (planstate->worker_instrument && we_need_to_sum_tuples(plan))
55+
+ wrkrs += planstate->worker_instrument->num_workers;
3856
+
3957
+ if (plan->predicted_cardinality > 0.)
4058
+ {
@@ -859,7 +877,7 @@ index f8b79ec120..b5eda01907 100644
859877
extern void ExplainQuery(ParseState *pstate, ExplainStmt *stmt, const char *queryString,
860878
ParamListInfo params, QueryEnvironment *queryEnv, DestReceiver *dest);
861879
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
862-
index 4b7703d478..490d67b098 100644
880+
index 441e64eca9..484bca379a 100644
863881
--- a/src/include/nodes/pathnodes.h
864882
+++ b/src/include/nodes/pathnodes.h
865883
@@ -710,6 +710,10 @@ typedef struct RelOptInfo

postprocessing.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ learn_sample(List *clauselist, List *selectivities, List *relidslist,
105105
fss_hash = get_fss_for_object(clauselist, selectivities, relidslist,
106106
&nfeatures, &features);
107107

108-
/* In the case of zero matrix we not need to learn */
109-
110108
if (nfeatures > 0)
111109
for (i = 0; i < aqo_K; ++i)
112110
matrix[i] = palloc(sizeof(double) * nfeatures);
@@ -186,6 +184,17 @@ restore_selectivities(List *clauselist,
186184
return lst;
187185
}
188186

187+
static bool
188+
we_need_to_sum_tuples(const Plan *plan)
189+
{
190+
if (plan->path_parallel_workers > 0 && (
191+
plan->parallel_aware || nodeTag(plan) == T_HashJoin ||
192+
nodeTag(plan) == T_MergeJoin ||
193+
nodeTag(plan) == T_NestLoop))
194+
return true;
195+
return false;
196+
}
197+
189198
/*
190199
* Walks over obtained PlanState tree, collects relation objects with their
191200
* clauses, selectivities and relids and passes each object to learn_sample.
@@ -237,7 +246,7 @@ learnOnPlanState(PlanState *p, void *context)
237246
if (p->instrument->nloops > 0.)
238247
{
239248
/* If we can strongly calculate produced rows, do it. */
240-
if (p->worker_instrument)
249+
if (p->worker_instrument && we_need_to_sum_tuples(p->plan))
241250
{
242251
double wnloops = 0.;
243252
double wntuples = 0.;

0 commit comments

Comments
 (0)