From 95caaebd3be099b924dc60ab3805b429406d4eb1 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Wed, 3 Jan 2018 22:32:27 -0500
Subject: [PATCH] Code review for Parallel Append.

- Remove unnecessary #include mistakenly added in execnodes.h.
- Fix mistake in comment in choose_next_subplan_for_leader.
- Adjust row estimates in cost_append for a possibly-different
  parallel divisor.
- Clamp row estimates in cost_append after operations that may
  not produce integers.

Amit Kapila, with cosmetic adjustments by me.

Discussion: http://postgr.es/m/CAA4eK1+qcbeai3coPpRW=GFCzFeLUsuY4T-AKHqMjxpEGZBPQg@mail.gmail.com
---
 src/backend/executor/nodeAppend.c     |  7 +++----
 src/backend/optimizer/path/costsize.c | 16 ++++++++++++----
 src/include/nodes/execnodes.h         |  1 -
 3 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c
index 4245d8afaf..64a17fb032 100644
--- a/src/backend/executor/nodeAppend.c
+++ b/src/backend/executor/nodeAppend.c
@@ -446,10 +446,9 @@ choose_next_subplan_for_leader(AppendState *node)
  *
  *		We start from the first plan and advance through the list;
  *		when we get back to the end, we loop back to the first
- *		nonpartial plan.  This assigns the non-partial plans first
- *		in order of descending cost and then spreads out the
- *		workers as evenly as possible across the remaining partial
- *		plans.
+ *		partial plan.  This assigns the non-partial plans first in
+ *		order of descending cost and then spreads out the workers
+ *		as evenly as possible across the remaining partial plans.
  * ----------------------------------------------------------------
  */
 static bool
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 7903b2cb16..8679b14b29 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -1883,18 +1883,26 @@ cost_append(AppendPath *apath)
 											   subpath->startup_cost);
 
 			/*
-			 * Apply parallel divisor to non-partial subpaths.  Also add the
-			 * cost of partial paths to the total cost, but ignore non-partial
-			 * paths for now.
+			 * Apply parallel divisor to subpaths.  Scale the number of rows
+			 * for each partial subpath based on the ratio of the parallel
+			 * divisor originally used for the subpath to the one we adopted.
+			 * Also add the cost of partial paths to the total cost, but
+			 * ignore non-partial paths for now.
 			 */
 			if (i < apath->first_partial_path)
 				apath->path.rows += subpath->rows / parallel_divisor;
 			else
 			{
-				apath->path.rows += subpath->rows;
+				double		subpath_parallel_divisor;
+
+				subpath_parallel_divisor = get_parallel_divisor(subpath);
+				apath->path.rows += subpath->rows * (subpath_parallel_divisor /
+													 parallel_divisor);
 				apath->path.total_cost += subpath->total_cost;
 			}
 
+			apath->path.rows = clamp_row_est(apath->path.rows);
+
 			i++;
 		}
 
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index b121e16688..3ad58cdfe7 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -21,7 +21,6 @@
 #include "lib/pairingheap.h"
 #include "nodes/params.h"
 #include "nodes/plannodes.h"
-#include "storage/spin.h"
 #include "utils/hsearch.h"
 #include "utils/queryenvironment.h"
 #include "utils/reltrigger.h"
-- 
2.13.5 (Apple Git-94)

