diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index c2f0e0f..94d114a 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -2083,6 +2083,7 @@ _outRelOptInfo(StringInfo str, const RelOptInfo *node) WRITE_BOOL_FIELD(consider_param_startup); WRITE_BOOL_FIELD(consider_parallel); WRITE_NODE_FIELD(reltarget); + WRITE_BOOL_FIELD(has_target_non_vars); WRITE_NODE_FIELD(pathlist); WRITE_NODE_FIELD(ppilist); WRITE_NODE_FIELD(partial_pathlist); diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index 873a764..e185d47 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -598,14 +598,16 @@ set_rel_consider_parallel(PlannerInfo *root, RelOptInfo *rel, } /* - * If there's anything in baserestrictinfo that's parallel-restricted, - * we give up on parallelizing access to this relation. We could consider - * instead postponing application of the restricted quals until we're - * above all the parallelism in the plan tree, but it's not clear that - * this would be a win in very many cases, and it might be tricky to make - * outer join clauses work correctly. + * If there's anything in baserestrictinfo or reltarget that's + * parallel-restricted, we give up on parallelizing access to this + * relation. We could consider instead postponing application of the + * restricted clauses until we're above all the parallelism in the plan + * tree, but it's not clear that this would be a win in very many cases. + * It might be tricky to make outer join clauses work correctly. */ - if (has_parallel_hazard((Node *) rel->baserestrictinfo, false)) + if (has_parallel_hazard((Node *) rel->baserestrictinfo, false) || + (rel->has_target_non_vars && + has_parallel_hazard((Node *) rel->reltarget->exprs, false))) return; /* We have a winner. */ diff --git a/src/backend/optimizer/util/placeholder.c b/src/backend/optimizer/util/placeholder.c index b210914..0423ae0 100644 --- a/src/backend/optimizer/util/placeholder.c +++ b/src/backend/optimizer/util/placeholder.c @@ -393,6 +393,7 @@ add_placeholders_to_base_rels(PlannerInfo *root) rel->reltarget->exprs = lappend(rel->reltarget->exprs, copyObject(phinfo->ph_var)); + rel->has_target_non_vars = true; /* reltarget's cost and width fields will be updated later */ } } @@ -427,6 +428,7 @@ add_placeholders_to_joinrel(PlannerInfo *root, RelOptInfo *joinrel, /* Yup, add it to the output */ joinrel->reltarget->exprs = lappend(joinrel->reltarget->exprs, phinfo->ph_var); + joinrel->has_target_non_vars = true; joinrel->reltarget->width += phinfo->ph_width; /* diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c index 1e87a73..21a61b1 100644 --- a/src/backend/optimizer/util/relnode.c +++ b/src/backend/optimizer/util/relnode.c @@ -109,6 +109,7 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptKind reloptkind) rel->consider_parallel = false; /* might get changed later */ rel->rel_parallel_degree = -1; /* set up in GetRelationInfo */ rel->reltarget = create_empty_pathtarget(); + rel->has_target_non_vars = false; rel->pathlist = NIL; rel->ppilist = NIL; rel->partial_pathlist = NIL; @@ -396,6 +397,7 @@ build_join_rel(PlannerInfo *root, joinrel->consider_param_startup = false; joinrel->consider_parallel = false; joinrel->reltarget = create_empty_pathtarget(); + joinrel->has_target_non_vars = false; joinrel->pathlist = NIL; joinrel->ppilist = NIL; joinrel->partial_pathlist = NIL; @@ -506,8 +508,8 @@ build_join_rel(PlannerInfo *root, * Set the consider_parallel flag if this joinrel could potentially be * scanned within a parallel worker. If this flag is false for either * inner_rel or outer_rel, then it must be false for the joinrel also. - * Even if both are true, there might be parallel-restricted quals at our - * level. + * Even if both are true, there might be parallel-restricted expression in + * targetlist or quals at our level. * * Note that if there are more than two rels in this relation, they could * be divided between inner_rel and outer_rel in any arbitrary way. We @@ -517,7 +519,9 @@ build_join_rel(PlannerInfo *root, * here. */ if (inner_rel->consider_parallel && outer_rel->consider_parallel && - !has_parallel_hazard((Node *) restrictlist, false)) + !has_parallel_hazard((Node *) restrictlist, false) && + !(joinrel->has_target_non_vars && + has_parallel_hazard((Node *) joinrel->reltarget->exprs, false))) joinrel->consider_parallel = true; /* diff --git a/src/include/nodes/relation.h b/src/include/nodes/relation.h index 45739c3..f16d932 100644 --- a/src/include/nodes/relation.h +++ b/src/include/nodes/relation.h @@ -490,6 +490,8 @@ typedef struct RelOptInfo /* default result targetlist for Paths scanning this relation */ struct PathTarget *reltarget; /* list of Vars/Exprs, cost, width */ + bool has_target_non_vars; /* true if any expression in + * PathTarget is NonVar */ /* materialization information */ List *pathlist; /* Path structures */