diff --git a/src/backend/optimizer/path/equivclass.c b/src/backend/optimizer/path/equivclass.c
index 711b161..734ed47 100644
--- a/src/backend/optimizer/path/equivclass.c
+++ b/src/backend/optimizer/path/equivclass.c
@@ -1940,6 +1940,7 @@ add_child_rel_equivalences(PlannerInfo *root,
 				Expr	   *child_expr;
 				Relids		new_relids;
 				Relids		new_nullable_relids;
+				bool		has_children = false;
 
 				child_expr = (Expr *)
 					adjust_appendrel_attrs(root,
@@ -1969,9 +1970,15 @@ add_child_rel_equivalences(PlannerInfo *root,
 														  child_rel->relids);
 				}
 
+				/*
+				 * Does this child_rel have children? If and only if so, tell
+				 * add_eq_member to register new_relids to cur_ec.
+				 */
+				has_children =
+					root->simple_rte_array[child_rel->relid]->inh;
 				(void) add_eq_member(cur_ec, child_expr,
 									 new_relids, new_nullable_relids,
-									 true, cur_em->em_datatype);
+									 !has_children, cur_em->em_datatype);
 			}
 		}
 	}
diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c
index 64b1705..0e3cf4b 100644
--- a/src/backend/optimizer/util/pathnode.c
+++ b/src/backend/optimizer/util/pathnode.c
@@ -944,7 +944,8 @@ create_merge_append_path(PlannerInfo *root,
 	MergeAppendPath *pathnode = makeNode(MergeAppendPath);
 	Cost		input_startup_cost;
 	Cost		input_total_cost;
-	ListCell   *l;
+	ListCell   *l, *lm;
+	bool		collapse_subpaths = false;
 
 	pathnode->path.pathtype = T_MergeAppend;
 	pathnode->path.parent = rel;
@@ -953,6 +954,47 @@ create_merge_append_path(PlannerInfo *root,
 	pathnode->path.pathkeys = pathkeys;
 	pathnode->subpaths = subpaths;
 
+
+	/*
+	 * If subpaths containes MergeAppendPaths already ordered on the pathkeys
+	 * of the creating node, they can be expanded onto this node.
+	 */
+	foreach (lm, subpaths)
+	{
+		Path *spath = (Path *) lfirst(lm);
+
+		if (IsA(spath, MergeAppendPath) &&
+			pathkeys_contained_in(pathkeys, spath->pathkeys))
+		{
+			collapse_subpaths = true;
+			break;
+		}
+	}
+
+	if (collapse_subpaths)
+	{
+		subpaths = NIL;
+
+		foreach (lm, pathnode->subpaths)
+		{
+			MergeAppendPath *mpath = (MergeAppendPath *) lfirst(lm);
+			ListCell *lcm;
+			
+			if (IsA(mpath, MergeAppendPath) &&
+				pathkeys_contained_in(pathkeys, mpath->path.pathkeys))
+			{
+				foreach (lcm, mpath->subpaths)
+				{
+					Path *smpath = (Path*) lfirst (lcm);
+					subpaths = lappend(subpaths, smpath);
+				}
+			}
+			else
+				subpaths = lappend(subpaths, subpaths);
+		}
+		pathnode->subpaths = subpaths;
+	}
+
 	/*
 	 * Apply query-wide LIMIT if known and path is for sole base relation.
 	 * (Handling this at this low level is a bit klugy.)
