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.)
