From da7fce78e6e5e375676423bd5b11eb2cb5994d8f Mon Sep 17 00:00:00 2001
From: James Coleman <jtc331@gmail.com>
Date: Wed, 1 Apr 2020 21:59:41 -0400
Subject: [PATCH v51 4/4] cleanup xxx in generate_useful_gather_paths

---
 src/backend/optimizer/path/allpaths.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)

diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c
index 0534bb24c5..9b70d091ae 100644
--- a/src/backend/optimizer/path/allpaths.c
+++ b/src/backend/optimizer/path/allpaths.c
@@ -2864,31 +2864,26 @@ generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_r
 													 &presorted_keys);
 
 			/*
-			 * When the partial path is already sorted, we can just add a gather
-			 * merge on top, and we're done - no point in adding explicit sort.
+			 * We don't need to consider the case where a subpath is already
+			 * fully sorted because generate_gather_paths already creates a
+			 * gather merge path for every subpath that has pathkeys present.
 			 *
-			 * XXX Can't we skip this (maybe only for the cheapest partial path)
-			 * when the path is already sorted? Then it's likely duplicate with
-			 * the path created by generate_gather_paths.
+			 * But since the subpath is already sorted, we know we don't need
+			 * to consider adding a sort (other either kind) on top of it, so
+			 * we can continue here.
 			 */
 			if (is_sorted)
-			{
-				path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
-												subpath->pathkeys, NULL, rowsp);
-
-				add_path(rel, &path->path);
 				continue;
-			}
 
 			/*
 			 * Consider regular sort for the cheapest partial path (for each
 			 * useful pathkeys). We know the path is not sorted, because we'd
 			 * not get here otherwise.
 			 *
-			 * XXX This is not redundant with the gather merge path created in
-			 * generate_gather_paths, because that merely preserves ordering of
-			 * the cheapest partial path, while here we add an explicit sort to
-			 * get match the useful ordering.
+			 * This is not redundant with the gather paths created in
+			 * generate_gather_paths, because that doesn't generate ordered
+			 * output. Here we add an explicit sort to match the useful
+			 * ordering.
 			 */
 			if (cheapest_partial_path == subpath)
 			{
-- 
2.17.1

