diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 781a736..d810c3c 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -1168,16 +1168,18 @@ ExplainNode(PlanState *planstate, List *ancestors,
 	{
 		if (es->format == EXPLAIN_FORMAT_TEXT)
 		{
-			appendStringInfo(es->str, "  (cost=%.2f..%.2f rows=%.0f width=%d)",
+			appendStringInfo(es->str, "  (cost=%.2f..%.2f rows=%.0f width=%d%s)",
 							 plan->startup_cost, plan->total_cost,
-							 plan->plan_rows, plan->plan_width);
+							 plan->plan_rows, plan->plan_width,
+							 plan->parallel_start ? " parallel" : "");
 		}
 		else
 		{
 			ExplainPropertyFloat("Startup Cost", plan->startup_cost, 2, es);
 			ExplainPropertyFloat("Total Cost", plan->total_cost, 2, es);
 			ExplainPropertyFloat("Plan Rows", plan->plan_rows, 0, es);
-			ExplainPropertyInteger("Plan Width", plan->plan_width, es);
+			ExplainPropertyText("Parallel Exec",
+								plan->parallel_start ? "true" : "false", es);
 		}
 	}
 
diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index c81efe9..85d78b4 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -53,6 +53,8 @@ typedef struct pushdown_safety_info
 /* These parameters are set by GUC */
 bool		enable_geqo = false;	/* just in case GUC doesn't set it */
 int			geqo_threshold;
+double		parallel_cost_threshold = 10000;
+double		parallel_ratio_threshold = 0.6;
 
 /* Hook for plugins to replace standard_join_search() */
 join_search_hook_type join_search_hook = NULL;
@@ -112,6 +114,104 @@ static void recurse_push_qual(Node *setOp, Query *topquery,
 				  RangeTblEntry *rte, Index rti, Node *qual);
 static void remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel);
 
+/*
+ * choose_parallel_walker -- Walk and make whole path tree decide whichever
+ * doing parallel execution or not.
+ */
+static
+void choose_parallel_walker(Path *path, bool run_parallel)
+{
+	ListCell *lc;
+	List *subpaths = NIL;
+	Path *left = NULL, *right = NULL;
+
+	switch (nodeTag(path))
+	{
+	case T_Path:
+	case T_IndexPath:
+	case T_BitmapHeapPath:
+	case T_TidPath:
+	case T_BitmapAndPath:
+	case T_BitmapOrPath:
+	case T_ResultPath:
+		/* These paths is imparallelizable so far, return immediately. */
+		return;
+	case T_ForeignPath: /**/
+		break;
+	case T_NestPath: /**/
+		left  = ((NestPath*)path)->outerjoinpath;
+		right = ((NestPath*)path)->innerjoinpath;
+		break;
+	case T_MergePath: /**/
+		left  = ((MergePath*)path)->jpath.outerjoinpath;
+		right = ((MergePath*)path)->jpath.innerjoinpath;
+		break;
+	case T_HashPath: /**/
+		left  = ((HashPath*)path)->jpath.outerjoinpath;
+		right = ((HashPath*)path)->jpath.innerjoinpath;
+		break;
+	case T_AppendPath: /**/
+		subpaths = ((AppendPath*)path)->subpaths;
+		break;
+	case T_MergeAppendPath: /**/
+		subpaths = ((MergeAppendPath*)path)->subpaths;
+		break;
+	case T_MaterialPath: /**/
+		left = ((MaterialPath*)path)->subpath;
+	case T_UniquePath: /**/
+		left = ((UniquePath*)path)->subpath;
+		break;
+	default:
+		elog(ERROR, "unrecognized path node type: %d",
+			 (int) nodeTag(path));
+		break;
+	}
+
+	/* Make this node can be treated ignoreing parallel costs hereafter. */
+	if (run_parallel)
+	{
+		path->startup_cost = path->pstartup_cost;
+		path->total_cost = path->ptotal_cost;
+	}
+	else
+	{
+		path->parallel = false;
+	}
+
+	/* Walk into lower level */
+	if (left)  choose_parallel_walker(left, run_parallel);
+	if (right) choose_parallel_walker(right, run_parallel);
+	foreach (lc, subpaths)
+	{
+		choose_parallel_walker((Path*) lfirst(lc), run_parallel);
+	}
+
+}
+
+/*
+ * choose_parallel_walker --- Decide whether this relation should run in
+ * parallel or not.
+ * 
+ * The relation has path tree having costs for both parallel and non-parallel
+ * execution and we can decide only by looking into the top node of the path
+ * tree to decide whether to do parallel or not. Then make the whole tree to
+ * be handled ignoring parallel execution stuff.
+ */
+void
+choose_parallel_scans(RelOptInfo *rel)
+{
+	ListCell *lc;
+
+	foreach (lc, rel->pathlist)
+	{
+		Path *path = (Path*) lfirst(lc);
+		bool run_parallel = (path->parallel &&
+				 path->total_cost > parallel_cost_threshold &&
+				 path->ptotal_cost < path->total_cost * parallel_ratio_threshold);
+
+		choose_parallel_walker(path, run_parallel);
+	}
+}
 
 /*
  * make_one_rel
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 0cdb790..1f5bcd8 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -1334,6 +1334,74 @@ cost_sort(Path *path, PlannerInfo *root,
 }
 
 /*
+ * cost_append
+ *	  Determines and returns the cost of a Append node.
+ *
+ * We charge nothing extra for the Append itself, which perhaps is too
+ * optimistic, but since it doesn't do any selection or projection, it is a
+ * pretty cheap node.  If you change this, see also make_append().
+ */
+void
+cost_append(Path *path, List *subpaths)
+{
+	double max_par_startup = 0.0;
+	double par_running = 0.0;
+	double nonpar_startup = -1.0;
+	double nonpar_running = 0.0;
+	ListCell *lc;
+
+	path->rows = 0;
+	path->startup_cost = 0;
+	path->total_cost = 0;
+	foreach(lc, subpaths)
+	{
+		Path	   *subpath = (Path *) lfirst(lc);
+		
+		path->rows += subpath->rows;
+		
+		if (lc == list_head(subpaths)) /* first node? */
+		{
+			path->startup_cost = subpath->startup_cost;
+			path->parallel = subpath->parallel;
+		}
+
+		path->total_cost += subpath->total_cost;
+	}
+
+	if (! path->parallel) return;
+
+	/* calculate cost for parallel execution */
+	foreach (lc, subpaths)
+	{
+		Path *subpath = (Path*)lfirst (lc);
+
+		if (subpath->parallel)
+		{
+			if (max_par_startup < subpath->pstartup_cost)
+				max_par_startup = subpath->pstartup_cost;
+			par_running += subpath->ptotal_cost - subpath->pstartup_cost;
+		}
+		else
+		{
+			if (nonpar_startup < 0)
+			{
+				nonpar_startup = subpath->startup_cost;
+				nonpar_running = subpath->total_cost - subpath->startup_cost;
+			}
+			else
+			{
+				nonpar_running += subpath->total_cost;
+			}
+		}
+	}
+	
+	path->pstartup_cost = (max_par_startup < nonpar_startup ?
+						 nonpar_startup : max_par_startup);
+	path->ptotal_cost = path->pstartup_cost +
+		(par_running < nonpar_running ? nonpar_running : par_running);
+}
+
+/*
  * cost_merge_append
  *	  Determines and returns the cost of a MergeAppend node.
  *
@@ -1356,12 +1424,17 @@ cost_sort(Path *path, PlannerInfo *root,
  * 'n_streams' is the number of input streams
  * 'input_startup_cost' is the sum of the input streams' startup costs
  * 'input_total_cost' is the sum of the input streams' total costs
+ * 'parallel_estimate' is whether to estimate for parallel execution
+ * 'input_pstart_cost' is the sum of the input streams' parallel startup costs
+ * 'input_ptotal_cost' is the sum of the input streams' parallel total costs
  * 'tuples' is the number of tuples in all the streams
  */
 void
 cost_merge_append(Path *path, PlannerInfo *root,
 				  List *pathkeys, int n_streams,
 				  Cost input_startup_cost, Cost input_total_cost,
+				  bool parallel_estimate,
+				  Cost input_pstartup_cost, Cost input_ptotal_cost,
 				  double tuples)
 {
 	Cost		startup_cost = 0;
@@ -1395,6 +1468,13 @@ cost_merge_append(Path *path, PlannerInfo *root,
 
 	path->startup_cost = startup_cost + input_startup_cost;
 	path->total_cost = startup_cost + run_cost + input_total_cost;
+
+	if (parallel_estimate)
+	{
+		path->pstartup_cost = startup_cost + input_pstartup_cost;
+		path->ptotal_cost = startup_cost + run_cost + input_ptotal_cost;
+		path->parallel = true;
+	}
 }
 
 /*
@@ -1648,6 +1728,54 @@ cost_group(Path *path, PlannerInfo *root,
 	path->total_cost = total_cost;
 }
 
+
+/*
+ * Set the parallel costs for the workspace as non-parallel node.
+ */
+static void
+clear_cost_parallel(JoinCostWorkspace *workspace)
+{
+	workspace->pstartup_cost = workspace->startup_cost;
+	workspace->ptotal_cost = workspace->total_cost;
+	workspace->parallel = false;
+}
+
+/*
+ * Set the initial cost numbers of parallel node for join estimation.
+ * This should be called only if the path node can executed in parallel.
+ */
+static void
+initial_cost_parallel(JoinCostWorkspace *workspace,
+					  Cost outer_pstartup_cost,
+					  Cost inner_pstartup_cost)
+{
+	double pstartup =
+		(outer_pstartup_cost > inner_pstartup_cost ?
+		 outer_pstartup_cost : inner_pstartup_cost);
+	workspace->ptotal_cost = 
+		workspace->total_cost -	(workspace->startup_cost - pstartup);
+	workspace->pstartup_cost = pstartup;
+	workspace->parallel = true;
+}
+
+/*
+ * Set the final cost numbers of parallel node for join estimation.
+ * This does nothing if this node won't be executed in parallel.
+ */
+static void
+final_cost_parallel(Path *path, JoinCostWorkspace *workspace,
+					Cost startup_cost, Cost run_cost)
+{
+	if (workspace->parallel)
+	{
+		path->pstartup_cost =
+			startup_cost + workspace->pstartup_cost;
+		path->ptotal_cost =
+			path->pstartup_cost + run_cost;
+		path->parallel = true;
+	}
+}
+
 /*
  * initial_cost_nestloop
  *	  Preliminary estimate of the cost of a nestloop join path.
@@ -1765,6 +1893,14 @@ initial_cost_nestloop(PlannerInfo *root, JoinCostWorkspace *workspace,
 	/* Save private data for final_cost_nestloop */
 	workspace->run_cost = run_cost;
 	workspace->inner_rescan_run_cost = inner_rescan_run_cost;
+
+	clear_cost_parallel(workspace);
+	if (outer_path->parallel)
+	{
+		initial_cost_parallel(workspace,
+							  outer_path->pstartup_cost,
+							  inner_path->pstartup_cost);
+	}
 }
 
 /*
@@ -1786,7 +1922,7 @@ final_cost_nestloop(PlannerInfo *root, NestPath *path,
 	Path	   *inner_path = path->innerjoinpath;
 	double		outer_path_rows = outer_path->rows;
 	double		inner_path_rows = inner_path->rows;
-	Cost		startup_cost = workspace->startup_cost;
+	Cost		startup_cost = 0.0;
 	Cost		run_cost = workspace->run_cost;
 	Cost		inner_rescan_run_cost = workspace->inner_rescan_run_cost;
 	Cost		cpu_per_tuple;
@@ -1861,8 +1997,10 @@ final_cost_nestloop(PlannerInfo *root, NestPath *path,
 	cpu_per_tuple = cpu_tuple_cost + restrict_qual_cost.per_tuple;
 	run_cost += cpu_per_tuple * ntuples;
 
-	path->path.startup_cost = startup_cost;
-	path->path.total_cost = startup_cost + run_cost;
+	path->path.startup_cost = startup_cost + workspace->startup_cost;
+	path->path.total_cost = startup_cost + workspace->startup_cost + run_cost;
+
+	final_cost_parallel(&path->path, workspace, startup_cost, run_cost);
 }
 
 /*
@@ -1917,6 +2055,8 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 				innerstartsel,
 				innerendsel;
 	Path		sort_path;		/* dummy for result of cost_sort */
+	double 		outer_startup_cost,
+				inner_startup_cost;
 
 	/* Protect some assumptions below that rowcounts aren't zero or NaN */
 	if (outer_path_rows <= 0 || isnan(outer_path_rows))
@@ -2035,6 +2175,7 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 				  0.0,
 				  work_mem,
 				  -1.0);
+		outer_startup_cost = sort_path.startup_cost;
 		startup_cost += sort_path.startup_cost;
 		startup_cost += (sort_path.total_cost - sort_path.startup_cost)
 			* outerstartsel;
@@ -2043,6 +2184,7 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 	}
 	else
 	{
+		outer_startup_cost = outer_path->startup_cost;
 		startup_cost += outer_path->startup_cost;
 		startup_cost += (outer_path->total_cost - outer_path->startup_cost)
 			* outerstartsel;
@@ -2061,6 +2203,7 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 				  0.0,
 				  work_mem,
 				  -1.0);
+		inner_startup_cost = sort_path.startup_cost;
 		startup_cost += sort_path.startup_cost;
 		startup_cost += (sort_path.total_cost - sort_path.startup_cost)
 			* innerstartsel;
@@ -2069,6 +2212,7 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 	}
 	else
 	{
+		inner_startup_cost = inner_path->startup_cost;
 		startup_cost += inner_path->startup_cost;
 		startup_cost += (inner_path->total_cost - inner_path->startup_cost)
 			* innerstartsel;
@@ -2096,6 +2240,16 @@ initial_cost_mergejoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 	workspace->inner_rows = inner_rows;
 	workspace->outer_skip_rows = outer_skip_rows;
 	workspace->inner_skip_rows = inner_skip_rows;
+
+	clear_cost_parallel(workspace);
+	if (outer_path->parallel)
+	{
+		initial_cost_parallel(workspace, 
+							  outer_startup_cost - outer_path->total_cost + 
+							  outer_path->ptotal_cost,
+							  inner_startup_cost - inner_path->total_cost + 
+							  inner_path->ptotal_cost);
+	}
 }
 
 /*
@@ -2128,7 +2282,7 @@ final_cost_mergejoin(PlannerInfo *root, MergePath *path,
 	double		inner_path_rows = inner_path->rows;
 	List	   *mergeclauses = path->path_mergeclauses;
 	List	   *innersortkeys = path->innersortkeys;
-	Cost		startup_cost = workspace->startup_cost;
+	Cost		startup_cost = 0;
 	Cost		run_cost = workspace->run_cost;
 	Cost		inner_run_cost = workspace->inner_run_cost;
 	double		outer_rows = workspace->outer_rows;
@@ -2320,8 +2474,10 @@ final_cost_mergejoin(PlannerInfo *root, MergePath *path,
 	cpu_per_tuple = cpu_tuple_cost + qp_qual_cost.per_tuple;
 	run_cost += cpu_per_tuple * mergejointuples;
 
-	path->jpath.path.startup_cost = startup_cost;
-	path->jpath.path.total_cost = startup_cost + run_cost;
+	path->jpath.path.startup_cost = startup_cost + workspace->startup_cost;
+	path->jpath.path.total_cost = path->jpath.path.startup_cost + run_cost;
+
+	final_cost_parallel(&path->jpath.path, workspace, startup_cost, run_cost);
 }
 
 /*
@@ -2485,6 +2641,17 @@ initial_cost_hashjoin(PlannerInfo *root, JoinCostWorkspace *workspace,
 	workspace->run_cost = run_cost;
 	workspace->numbuckets = numbuckets;
 	workspace->numbatches = numbatches;
+
+	/* costs for parallel execution */
+	clear_cost_parallel(workspace);
+
+	/* Any side is executed first in hash join... */
+	if (inner_path->parallel || outer_path->parallel)
+	{
+		initial_cost_parallel(workspace,
+							  outer_path->pstartup_cost,
+							  inner_path->pstartup_cost);
+	}
 }
 
 /*
@@ -2510,7 +2677,7 @@ final_cost_hashjoin(PlannerInfo *root, HashPath *path,
 	double		outer_path_rows = outer_path->rows;
 	double		inner_path_rows = inner_path->rows;
 	List	   *hashclauses = path->path_hashclauses;
-	Cost		startup_cost = workspace->startup_cost;
+	Cost		startup_cost = 0;
 	Cost		run_cost = workspace->run_cost;
 	int			numbuckets = workspace->numbuckets;
 	int			numbatches = workspace->numbatches;
@@ -2700,8 +2867,10 @@ final_cost_hashjoin(PlannerInfo *root, HashPath *path,
 	cpu_per_tuple = cpu_tuple_cost + qp_qual_cost.per_tuple;
 	run_cost += cpu_per_tuple * hashjointuples;
 
-	path->jpath.path.startup_cost = startup_cost;
-	path->jpath.path.total_cost = startup_cost + run_cost;
+	path->jpath.path.startup_cost = startup_cost + workspace->startup_cost;
+	path->jpath.path.total_cost = path->jpath.path.startup_cost + run_cost;
+
+	final_cost_parallel(&path->jpath.path, workspace, startup_cost, run_cost);
 }
 
 
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 4b641a2..67d801a 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -3163,6 +3163,9 @@ copy_path_costsize(Plan *dest, Path *src)
 	{
 		dest->startup_cost = src->startup_cost;
 		dest->total_cost = src->total_cost;
+		dest->parallel_start = src->parallel;
+		dest->pstartup_cost = src->pstartup_cost;
+		dest->ptotal_cost = src->ptotal_cost;
 		dest->plan_rows = src->rows;
 		dest->plan_width = src->parent->width;
 	}
@@ -3170,6 +3173,9 @@ copy_path_costsize(Plan *dest, Path *src)
 	{
 		dest->startup_cost = 0;
 		dest->total_cost = 0;
+		dest->parallel_start = false;
+		dest->pstartup_cost = 0;
+		dest->ptotal_cost = 0;
 		dest->plan_rows = 0;
 		dest->plan_width = 0;
 	}
@@ -3462,6 +3468,7 @@ ForeignScan *
 make_foreignscan(List *qptlist,
 				 List *qpqual,
 				 Index scanrelid,
+				 bool  parallel,
 				 List *fdw_exprs,
 				 List *fdw_private)
 {
@@ -3474,6 +3481,7 @@ make_foreignscan(List *qptlist,
 	plan->lefttree = NULL;
 	plan->righttree = NULL;
 	node->scan.scanrelid = scanrelid;
+	node->scan.parallel = parallel;
 	node->fdw_exprs = fdw_exprs;
 	node->fdw_private = fdw_private;
 	/* fsSystemCol will be filled in by create_foreignscan_plan */
@@ -3489,6 +3497,10 @@ make_append(List *appendplans, List *tlist)
 	Plan	   *plan = &node->plan;
 	double		total_size;
 	ListCell   *subnode;
+	double      max_par_startup = 0;
+	double      par_running = 0;
+	double      nonpar_startup = 0;
+	double      nonpar_running = 0;
 
 	/*
 	 * Compute cost as sum of subplan costs.  We charge nothing extra for the
@@ -3509,12 +3521,27 @@ make_append(List *appendplans, List *tlist)
 	{
 		Plan	   *subplan = (Plan *) lfirst(subnode);
 
-		if (subnode == list_head(appendplans))	/* first node? */
-			plan->startup_cost = subplan->startup_cost;
-		plan->total_cost += subplan->total_cost;
+		if (subplan->parallel_start)
+		{
+			if (max_par_startup < subplan->startup_cost)
+				max_par_startup = subplan->startup_cost;
+			par_running += subplan->total_cost - subplan->startup_cost;
+		}
+		else
+		{
+			nonpar_startup += subplan->startup_cost;
+			nonpar_running += (subplan->total_cost - subplan->startup_cost);
+		}
+
 		plan->plan_rows += subplan->plan_rows;
 		total_size += subplan->plan_width * subplan->plan_rows;
 	}
+
+	plan->startup_cost = 
+		(max_par_startup < nonpar_startup ? nonpar_startup : max_par_startup);
+	plan->total_cost = 
+		plan->startup_cost + par_running + nonpar_running;
+
 	if (plan->plan_rows > 0)
 		plan->plan_width = rint(total_size / plan->plan_rows);
 	else
diff --git a/src/backend/optimizer/plan/planmain.c b/src/backend/optimizer/plan/planmain.c
index 93484a0..9d75dd7 100644
--- a/src/backend/optimizer/plan/planmain.c
+++ b/src/backend/optimizer/plan/planmain.c
@@ -240,5 +240,8 @@ query_planner(PlannerInfo *root, List *tlist,
 		final_rel->cheapest_total_path->param_info != NULL)
 		elog(ERROR, "failed to construct the join relation");
 
+	/* Decide each parallelizable path should be run in parallel */
+	choose_parallel_scans(final_rel);
+
 	return final_rel;
 }
diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c
index 319e8b2..d716dcb 100644
--- a/src/backend/optimizer/util/pathnode.c
+++ b/src/backend/optimizer/util/pathnode.c
@@ -900,27 +900,11 @@ create_append_path(RelOptInfo *rel, List *subpaths, Relids required_outer)
 										 * unsorted */
 	pathnode->subpaths = subpaths;
 
-	/*
-	 * We don't bother with inventing a cost_append(), but just do it here.
-	 *
-	 * Compute rows and costs as sums of subplan rows and costs.  We charge
-	 * nothing extra for the Append itself, which perhaps is too optimistic,
-	 * but since it doesn't do any selection or projection, it is a pretty
-	 * cheap node.  If you change this, see also make_append().
-	 */
-	pathnode->path.rows = 0;
-	pathnode->path.startup_cost = 0;
-	pathnode->path.total_cost = 0;
+	cost_append(&pathnode->path, subpaths);
+
 	foreach(l, subpaths)
 	{
-		Path	   *subpath = (Path *) lfirst(l);
-
-		pathnode->path.rows += subpath->rows;
-
-		if (l == list_head(subpaths))	/* first node? */
-			pathnode->path.startup_cost = subpath->startup_cost;
-		pathnode->path.total_cost += subpath->total_cost;
-
+		Path *subpath = (Path*)lfirst(l);
 		/* All child paths must have same parameterization */
 		Assert(bms_equal(PATH_REQ_OUTER(subpath), required_outer));
 	}
@@ -943,7 +927,14 @@ create_merge_append_path(PlannerInfo *root,
 	MergeAppendPath *pathnode = makeNode(MergeAppendPath);
 	Cost		input_startup_cost;
 	Cost		input_total_cost;
+	Cost		input_pstartup_cost;
+	Cost		input_ptotal_cost;
 	ListCell   *l;
+	Cost		max_par_startup = 0.0;
+	Cost		par_running = 0.0;
+	Cost		nonpar_startup = 0.0;
+	Cost 		nonpar_running = 0.0;
+	bool		parallel_exists = false;
 
 	pathnode->path.pathtype = T_MergeAppend;
 	pathnode->path.parent = rel;
@@ -970,14 +961,16 @@ create_merge_append_path(PlannerInfo *root,
 	foreach(l, subpaths)
 	{
 		Path	   *subpath = (Path *) lfirst(l);
+		double additional_startup_cost = 0;
+		double additional_total_cost = 0;
 
 		pathnode->path.rows += subpath->rows;
 
 		if (pathkeys_contained_in(pathkeys, subpath->pathkeys))
 		{
 			/* Subpath is adequately ordered, we won't need to sort it */
-			input_startup_cost += subpath->startup_cost;
-			input_total_cost += subpath->total_cost;
+			additional_startup_cost = subpath->startup_cost;
+			additional_total_cost = subpath->total_cost;
 		}
 		else
 		{
@@ -993,18 +986,55 @@ create_merge_append_path(PlannerInfo *root,
 					  0.0,
 					  work_mem,
 					  pathnode->limit_tuples);
-			input_startup_cost += sort_path.startup_cost;
-			input_total_cost += sort_path.total_cost;
+			additional_startup_cost = sort_path.startup_cost;
+			additional_total_cost = sort_path.total_cost;
+		}
+
+		input_startup_cost += additional_startup_cost;
+		input_total_cost += additional_total_cost;
+
+		/* cost calculation for parallel execution  */
+		if (subpath->parallel)
+		{
+			Cost thiscost =
+				subpath->pstartup_cost + additional_startup_cost;
+			if (max_par_startup < thiscost)
+				max_par_startup = thiscost;
+			par_running +=
+				(subpath->ptotal_cost - subpath->pstartup_cost) +
+				(additional_total_cost - additional_startup_cost) -
+				(subpath->total_cost - subpath->startup_cost);
+			parallel_exists = true;
+		}
+		else
+		{
+			nonpar_startup +=
+				subpath->startup_cost + additional_startup_cost;
+			nonpar_running +=
+				additional_total_cost - additional_startup_cost;
 		}
 
 		/* All child paths must have same parameterization */
 		Assert(bms_equal(PATH_REQ_OUTER(subpath), required_outer));
 	}
+	
+
+	if (parallel_exists)
+	{
+		input_pstartup_cost =
+			(max_par_startup < nonpar_startup ?
+			 nonpar_startup : max_par_startup);
+		input_ptotal_cost = 
+			input_pstartup_cost +
+			(par_running < nonpar_running ? nonpar_running : par_running);
+	}
 
 	/* Now we can compute total costs of the MergeAppend */
 	cost_merge_append(&pathnode->path, root,
 					  pathkeys, list_length(subpaths),
 					  input_startup_cost, input_total_cost,
+					  parallel_exists,
+					  input_pstartup_cost, input_ptotal_cost,
 					  rel->tuples);
 
 	return pathnode;
@@ -1066,6 +1096,12 @@ create_material_path(RelOptInfo *rel, Path *subpath)
 				  subpath->rows,
 				  rel->width);
 
+	if (subpath->parallel)
+	{
+		Cost diff = subpath->pstartup_cost - subpath->startup_cost;
+		pathnode->path.pstartup_cost = pathnode->path.startup_cost + diff;
+		pathnode->path.ptotal_cost   = pathnode->path.total_cost + diff;
+	}
 	return pathnode;
 }
 
@@ -1292,6 +1328,12 @@ create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
 		pathnode->path.total_cost = subpath->total_cost;
 		pathnode->path.pathkeys = subpath->pathkeys;
 
+		if (subpath->parallel)
+		{
+			pathnode->path.startup_cost = subpath->startup_cost;
+			pathnode->path.total_cost = subpath->total_cost;
+			pathnode->path.parallel = true;
+		}
 		rel->cheapest_unique_path = (Path *) pathnode;
 
 		MemoryContextSwitchTo(oldcontext);
@@ -1328,6 +1370,12 @@ create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
 				pathnode->path.total_cost = subpath->total_cost;
 				pathnode->path.pathkeys = subpath->pathkeys;
 
+				if (subpath->parallel)
+				{
+					pathnode->path.startup_cost = subpath->startup_cost;
+					pathnode->path.total_cost = subpath->total_cost;
+					pathnode->path.parallel = true;
+				}
 				rel->cheapest_unique_path = (Path *) pathnode;
 
 				MemoryContextSwitchTo(oldcontext);
@@ -1407,6 +1455,19 @@ create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
 		pathnode->path.total_cost = sort_path.total_cost;
 	}
 
+	/* calculate parallel execution costs */
+	if (subpath->parallel)
+	{
+		pathnode->path.pstartup_cost = 
+			sort_path.startup_cost - subpath->startup_cost
+			+ subpath->pstartup_cost;
+		pathnode->path.ptotal_cost = 
+			sort_path.total_cost - subpath->total_cost
+			+ subpath->total_cost;
+		pathnode->path.parallel = true;
+	}
+		
+
 	rel->cheapest_unique_path = (Path *) pathnode;
 
 	MemoryContextSwitchTo(oldcontext);
@@ -1576,9 +1637,8 @@ create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel,
 ForeignPath *
 create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
 						double rows, Cost startup_cost, Cost total_cost,
-						List *pathkeys,
-						Relids required_outer,
-						List *fdw_private)
+						List *pathkeys, Relids required_outer,
+						bool parallel, List *fdw_private)
 {
 	ForeignPath *pathnode = makeNode(ForeignPath);
 
@@ -1589,6 +1649,9 @@ create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
 	pathnode->path.rows = rows;
 	pathnode->path.startup_cost = startup_cost;
 	pathnode->path.total_cost = total_cost;
+	pathnode->path.parallel = parallel;
+	pathnode->path.pstartup_cost = startup_cost;
+	pathnode->path.ptotal_cost = total_cost;
 	pathnode->path.pathkeys = pathkeys;
 
 	pathnode->fdw_private = fdw_private;
diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 6c52db8..cf3f104 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -2691,6 +2691,24 @@ static struct config_real ConfigureNamesReal[] =
 		0.5, 0.0, 1.0,
 		NULL, NULL, NULL
 	},
+	{
+		{"parallel_cost_threshold", PGC_USERSET, QUERY_TUNING_METHOD,
+		 	gettext_noop("Set the minimum total cost to allow parallel plans."),
+			NULL
+		},
+		&parallel_cost_threshold,
+		10000.0, 0.0, 1.0e10,	/* disable_cost */
+		NULL, NULL, NULL
+	},
+	{
+		{"parallel_ratio_threshold", PGC_USERSET, QUERY_TUNING_METHOD,
+		 	gettext_noop("Set threshold to select parallel plans, as the ratio of parallel cost to non-parallel cost."),
+			NULL
+		},
+		&parallel_ratio_threshold,
+		0.8, 0.0, 10.0,
+		NULL, NULL, NULL
+	},
 
 	/* End-of-list marker */
 	{
diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 3b9c683..f4f9deb 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -95,6 +95,9 @@ typedef struct Plan
 	 */
 	Cost		startup_cost;	/* cost expended before fetching any tuples */
 	Cost		total_cost;		/* total cost (assuming all tuples fetched) */
+	bool		parallel_start; /* this node will run in parallel */
+	Cost		pstartup_cost;  /* startup cost for parallel execution */
+	Cost		ptotal_cost;    /* total cost for parallel execution */
 
 	/*
 	 * planner's estimate of result size of this plan step
@@ -264,6 +267,7 @@ typedef struct Scan
 {
 	Plan		plan;
 	Index		scanrelid;		/* relid is index into the range table */
+	bool		parallel;		/* This scan will run in parallel */
 } Scan;
 
 /* ----------------
diff --git a/src/include/nodes/relation.h b/src/include/nodes/relation.h
index dacbe9c..08d59aa 100644
--- a/src/include/nodes/relation.h
+++ b/src/include/nodes/relation.h
@@ -729,6 +729,10 @@ typedef struct Path
 	Cost		startup_cost;	/* cost expended before fetching any tuples */
 	Cost		total_cost;		/* total cost (assuming all tuples fetched) */
 
+	bool		parallel;		/* can run in parallel ? */
+	Cost		pstartup_cost;	/* startup cost for parallel execution */
+	Cost		ptotal_cost;	/* total cost for parallel execution */
+
 	List	   *pathkeys;		/* sort ordering of path's output */
 	/* pathkeys is a List of PathKey nodes; see above */
 } Path;
@@ -1626,6 +1630,10 @@ typedef struct JoinCostWorkspace
 	Cost		startup_cost;	/* cost expended before fetching any tuples */
 	Cost		total_cost;		/* total cost (assuming all tuples fetched) */
 
+	bool		parallel;		/* can run in parallel ? */
+	Cost		pstartup_cost;	/* startup cost for parallel execution */
+	Cost		ptotal_cost;	/* total cost for parallel execution */
+	
 	/* Fields below here should be treated as private to costsize.c */
 	Cost		run_cost;		/* non-startup cost components */
 
diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h
index 75e2afb..30b9858 100644
--- a/src/include/optimizer/cost.h
+++ b/src/include/optimizer/cost.h
@@ -91,9 +91,12 @@ extern void cost_sort(Path *path, PlannerInfo *root,
 		  List *pathkeys, Cost input_cost, double tuples, int width,
 		  Cost comparison_cost, int sort_mem,
 		  double limit_tuples);
+extern void cost_append(Path *path, List *subpaths);
 extern void cost_merge_append(Path *path, PlannerInfo *root,
 				  List *pathkeys, int n_streams,
 				  Cost input_startup_cost, Cost input_total_cost,
+				  bool parallel_estimate,
+				  Cost input_pstartup_cost, Cost input_ptotal_cost,
 				  double tuples);
 extern void cost_material(Path *path,
 			  Cost input_startup_cost, Cost input_total_cost,
diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h
index a0bcc82..b84d176 100644
--- a/src/include/optimizer/pathnode.h
+++ b/src/include/optimizer/pathnode.h
@@ -79,9 +79,8 @@ extern Path *create_worktablescan_path(PlannerInfo *root, RelOptInfo *rel,
 						  Relids required_outer);
 extern ForeignPath *create_foreignscan_path(PlannerInfo *root, RelOptInfo *rel,
 						double rows, Cost startup_cost, Cost total_cost,
-						List *pathkeys,
-						Relids required_outer,
-						List *fdw_private);
+						List *pathkeys,	Relids required_outer,
+						bool parallel,	List *fdw_private);
 
 extern Relids calc_nestloop_required_outer(Path *outer_path, Path *inner_path);
 extern Relids calc_non_nestloop_required_outer(Path *outer_path, Path *inner_path);
diff --git a/src/include/optimizer/paths.h b/src/include/optimizer/paths.h
index 9b22fda..36879f1 100644
--- a/src/include/optimizer/paths.h
+++ b/src/include/optimizer/paths.h
@@ -20,8 +20,10 @@
 /*
  * allpaths.c
  */
-extern bool enable_geqo;
-extern int	geqo_threshold;
+extern bool 	enable_geqo;
+extern int		geqo_threshold;
+extern double	parallel_cost_threshold;
+extern double	parallel_ratio_threshold;
 
 /* Hook for plugins to replace standard_join_search() */
 typedef RelOptInfo *(*join_search_hook_type) (PlannerInfo *root,
@@ -30,6 +32,7 @@ typedef RelOptInfo *(*join_search_hook_type) (PlannerInfo *root,
 extern PGDLLIMPORT join_search_hook_type join_search_hook;
 
 
+extern void choose_parallel_scans(RelOptInfo *rel);
 extern RelOptInfo *make_one_rel(PlannerInfo *root, List *joinlist);
 extern RelOptInfo *standard_join_search(PlannerInfo *root, int levels_needed,
 					 List *initial_rels);
diff --git a/src/include/optimizer/planmain.h b/src/include/optimizer/planmain.h
index 4504250..e72ad72 100644
--- a/src/include/optimizer/planmain.h
+++ b/src/include/optimizer/planmain.h
@@ -44,7 +44,7 @@ extern Plan *create_plan(PlannerInfo *root, Path *best_path);
 extern SubqueryScan *make_subqueryscan(List *qptlist, List *qpqual,
 				  Index scanrelid, Plan *subplan);
 extern ForeignScan *make_foreignscan(List *qptlist, List *qpqual,
-				 Index scanrelid, List *fdw_exprs, List *fdw_private);
+			 Index scanrelid, bool parallel, List *fdw_exprs, List *fdw_private);
 extern Append *make_append(List *appendplans, List *tlist);
 extern RecursiveUnion *make_recursive_union(List *tlist,
 					 Plan *lefttree, Plan *righttree, int wtParam,
