From 6630c7916851bce3e7e86e5e6ca71433e71283e4 Mon Sep 17 00:00:00 2001
From: Rushabh Lathia <rushabh.lathia@enterprisedb.com>
Date: Tue, 2 Jan 2018 14:50:55 +0530
Subject: [PATCH 4/4] Calculate parallel worker inside index_create().

---
 src/backend/access/nbtree/nbtsort.c | 36 +++++++-----------------------------
 src/backend/catalog/index.c         | 24 ++++++++++++++++++++++++
 src/include/nodes/execnodes.h       |  2 ++
 3 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c
index 7868481..91e32a3 100644
--- a/src/backend/access/nbtree/nbtsort.c
+++ b/src/backend/access/nbtree/nbtsort.c
@@ -75,8 +75,6 @@
 #include "catalog/catalog.h"
 #include "catalog/index.h"
 #include "miscadmin.h"
-#include "optimizer/planner.h"
-#include "optimizer/planmain.h"
 #include "storage/smgr.h"
 #include "tcop/tcopprot.h"		/* pgrminclude ignore */
 #include "utils/rel.h"
@@ -370,7 +368,6 @@ _bt_heapscan(Relation heap, Relation index, BTBuildState *buildstate,
 	BTSpool    *btspool = (BTSpool *) palloc0(sizeof(BTSpool));
 	SortCoordinate coordinate = NULL;
 	double		reltuples = 0;
-	bool        leaderasworker = parallel_leader_participation;
 
 	/*
 	 * We size the sort area as maintenance_work_mem rather than work_mem to
@@ -383,29 +380,13 @@ _bt_heapscan(Relation heap, Relation index, BTBuildState *buildstate,
 	btspool->index = index;
 	btspool->isunique = indexInfo->ii_Unique;
 
-	/*
-	 * Parallel index builds on catalog relations are unsupported.  This is
-	 * because reindex_relation() will call RelationSetIndexList() to forcibly
-	 * disable the use of indexes on the table being reindexed (e.g., when
-	 * REINDEX_REL_SUPPRESS_INDEX_USE is specified by CLUSTER caller), and
-	 * that is not propagated to workers.
-	 */
-	if (!IsCatalogRelation(heap))
+	/* attempt to launch the worker */
+	if (indexInfo->ii_ParallelWorkers > 0)
 	{
-		Oid			table = RelationGetRelid(btspool->heap);
-		Oid			index = RelationGetRelid(btspool->index);
-		int			request;
-
-		/* Determine optimal number of worker processes, and attempt launch */
-		request = plan_create_index_workers(table, index, leaderasworker);
-
-		if (request > 0)
-		{
-			buildstate->btleader = _bt_begin_parallel(btspool,
-													  indexInfo->ii_Concurrent,
-													  request,
-													  leaderasworker);
-		}
+		buildstate->btleader = _bt_begin_parallel(btspool,
+												  indexInfo->ii_Concurrent,
+												  indexInfo->ii_ParallelWorkers,
+												  indexInfo->ii_LeaderAsWorker);
 	}
 
 	/*
@@ -500,7 +481,7 @@ _bt_heapscan(Relation heap, Relation index, BTBuildState *buildstate,
 	else
 	{
 		/* Parallel scans are underway by now.  Join as worker. */
-		if (leaderasworker)
+		if (indexInfo->ii_LeaderAsWorker)
 			_bt_leader_sort_as_worker(buildstate);
 
 		/* Wait on worker processes to finish (should be almost instant) */
@@ -1387,9 +1368,6 @@ _bt_leader_sort_as_worker(BTBuildState *buildstate)
 	BTSpool    *leaderworker2;
 	int			sortmem;
 
-	if (!parallel_leader_participation)
-		return;
-
 	/* Allocate memory for leader-as-worker's own private spool */
 	leaderworker = (BTSpool *) palloc0(sizeof(BTSpool));
 
diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index fdc9bdc..52c247b 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -51,6 +51,8 @@
 #include "commands/trigger.h"
 #include "executor/executor.h"
 #include "miscadmin.h"
+#include "optimizer/planner.h"
+#include "optimizer/planmain.h"
 #include "nodes/makefuncs.h"
 #include "nodes/nodeFuncs.h"
 #include "optimizer/clauses.h"
@@ -1072,6 +1074,28 @@ index_create(Relation heapRelation,
 	CommandCounterIncrement();
 
 	/*
+	 * Currently only nbtree supports the parallel index build.
+	 *
+	 * Parallel index builds on catalog relations are unsupported.  This is
+	 * because reindex_relation() will call RelationSetIndexList() to forcibly
+	 * disable the use of indexes on the table being reindexed (e.g., when
+	 * REINDEX_REL_SUPPRESS_INDEX_USE is specified by CLUSTER caller), and
+	 * that is not propagated to workers.
+	 */
+	indexInfo->ii_LeaderAsWorker = parallel_leader_participation;
+	if (!IsCatalogRelation(heapRelation) &&
+		indexRelation->rd_rel->relam == BTREE_AM_OID)
+	{
+		/* Determine optimal number of worker processes for CREATE INDEX. */
+		indexInfo->ii_ParallelWorkers =
+			plan_create_index_workers(heapRelationId,
+									  RelationGetRelid(indexRelation),
+									  indexInfo->ii_LeaderAsWorker);
+	}
+	else
+		indexInfo->ii_ParallelWorkers = 0;
+
+	/*
 	 * In bootstrap mode, we have to fill in the index strategy structure with
 	 * information from the catalogs.  If we aren't bootstrapping, then the
 	 * relcache entry has already been rebuilt thanks to sinval update during
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index 94351ea..e10becc 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -159,6 +159,8 @@ typedef struct IndexInfo
 	bool		ii_ReadyForInserts;
 	bool		ii_Concurrent;
 	bool		ii_BrokenHotChain;
+	bool		ii_LeaderAsWorker;
+	int			ii_ParallelWorkers; /* number of parallel workers */
 	void	   *ii_AmCache;
 	MemoryContext ii_Context;
 } IndexInfo;
-- 
1.8.3.1

