diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c
index 469a32c..9fe697f 100644
--- a/src/backend/executor/execParallel.c
+++ b/src/backend/executor/execParallel.c
@@ -162,7 +162,7 @@ ExecSerializePlan(Plan *plan, EState *estate)
 	pstmt->rtable = estate->es_range_table;
 	pstmt->resultRelations = NIL;
 	pstmt->nonleafResultRelations = NIL;
-	pstmt->subplans = estate->es_plannedstmt->subplans;
+	pstmt->subplans = estate->es_plannedstmt->parallelSafeSubplans;
 	pstmt->rewindPlanIDs = NULL;
 	pstmt->rowMarks = NIL;
 	pstmt->relationOids = NIL;
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 4d5ee01..07a621f 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -234,6 +234,7 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
 
 	glob->boundParams = boundParams;
 	glob->subplans = NIL;
+	glob->parallelSafeSubplans = NIL;
 	glob->subroots = NIL;
 	glob->rewindPlanIDs = NULL;
 	glob->finalrtable = NIL;
@@ -436,6 +437,7 @@ standard_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
 	result->resultRelations = glob->resultRelations;
 	result->nonleafResultRelations = glob->nonleafResultRelations;
 	result->subplans = glob->subplans;
+	result->parallelSafeSubplans = glob->parallelSafeSubplans;
 	result->rewindPlanIDs = glob->rewindPlanIDs;
 	result->rowMarks = glob->finalrowmarks;
 	result->relationOids = glob->relationOids;
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index 8d0d8ae..bbb2ff0 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -848,9 +848,15 @@ build_subplan(PlannerInfo *root, Plan *plan, PlannerInfo *subroot,
 	}
 
 	/*
-	 * Add the subplan and its PlannerInfo to the global lists.
+	 * Add the subplan and its PlannerInfo to the global lists.  For not
+	 * parallel-safe plans, we just leave a hole in the parallelSafeSubplans
+	 * list.
 	 */
 	root->glob->subplans = lappend(root->glob->subplans, plan);
+	if (splan->parallel_safe)
+		root->glob->parallelSafeSubplans = lappend(root->glob->parallelSafeSubplans, plan);
+	else
+		root->glob->parallelSafeSubplans = lappend(root->glob->parallelSafeSubplans, NULL);
 	root->glob->subroots = lappend(root->glob->subroots, subroot);
 	splan->plan_id = list_length(root->glob->subplans);
 
@@ -1244,9 +1250,15 @@ SS_process_ctes(PlannerInfo *root)
 		splan->setParam = list_make1_int(paramid);
 
 		/*
-		 * Add the subplan and its PlannerInfo to the global lists.
+		 * Add the subplan and its PlannerInfo to the global lists.  For not
+		 * parallel-safe plans, we just leave a hole in the
+		 * parallelSafeSubplans list.
 		 */
 		root->glob->subplans = lappend(root->glob->subplans, plan);
+		if (splan->parallel_safe)
+			root->glob->parallelSafeSubplans = lappend(root->glob->parallelSafeSubplans, plan);
+		else
+			root->glob->parallelSafeSubplans = lappend(root->glob->parallelSafeSubplans, NULL);
 		root->glob->subroots = lappend(root->glob->subroots, subroot);
 		splan->plan_id = list_length(root->glob->subplans);
 
@@ -2907,9 +2919,12 @@ SS_make_initplan_from_plan(PlannerInfo *root,
 	SubPlan    *node;
 
 	/*
-	 * Add the subplan and its PlannerInfo to the global lists.
+	 * Add the subplan and its PlannerInfo to the global lists. As InitPlans
+	 * are not parallel-safe, we just leave a hole in the parallelSafeSubplans
+	 * list for them.
 	 */
 	root->glob->subplans = lappend(root->glob->subplans, plan);
+	root->glob->parallelSafeSubplans = lappend(root->glob->parallelSafeSubplans, NULL);
 	root->glob->subroots = lappend(root->glob->subroots, subroot);
 
 	/*
diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 12f9f61..fd5bb68 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -70,6 +70,9 @@ typedef struct PlannedStmt
 
 	List	   *subplans;		/* Plan trees for SubPlan expressions */
 
+	List	   *parallelSafeSubplans;	/* Plan trees for parallel safe
+										 * SubPlan expressions */
+
 	Bitmapset  *rewindPlanIDs;	/* indices of subplans that require REWIND */
 
 	List	   *rowMarks;		/* a list of PlanRowMark's */
diff --git a/src/include/nodes/relation.h b/src/include/nodes/relation.h
index 7a8e2fd..8200448 100644
--- a/src/include/nodes/relation.h
+++ b/src/include/nodes/relation.h
@@ -97,6 +97,9 @@ typedef struct PlannerGlobal
 
 	List	   *subplans;		/* Plans for SubPlan nodes */
 
+	List	   *parallelSafeSubplans;	/* Plans for parallel safe SubPlan
+										 * nodes */
+
 	List	   *subroots;		/* PlannerInfos for SubPlan nodes */
 
 	Bitmapset  *rewindPlanIDs;	/* indices of subplans that require REWIND */
