From 42e975fcf9e1c2c7920544721965c641ce6bb1a1 Mon Sep 17 00:00:00 2001
From: "dgrowley@gmail.com" <dgrowley@gmail.com>
Date: Fri, 22 Jun 2018 15:40:46 +1200
Subject: [PATCH v1 2/2] Unsafe locking reduction for partitioned
 INSERT/UPDATEs

For performance demonstration purposes only.
---
 src/backend/executor/execPartition.c | 20 ++------------------
 1 file changed, 2 insertions(+), 18 deletions(-)

diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c
index 1a3a67dd0d..cb6a4c3ff0 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -65,9 +65,6 @@ static void find_matching_subplans_recurse(PartitionPruneState *prunestate,
  * tuple routing for partitioned tables, encapsulates it in
  * PartitionTupleRouting, and returns it.
  *
- * Note that all the relations in the partition tree are locked using the
- * RowExclusiveLock mode upon return from this function.
- *
  * While we allocate the arrays of pointers of ResultRelInfo and
  * TupleConversionMap for all partitions here, actual objects themselves are
  * lazily allocated for a given partition if a tuple is actually routed to it;
@@ -87,11 +84,6 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, Relation rel)
 	int			nparts;
 	ModifyTable *node = mtstate ? (ModifyTable *) mtstate->ps.plan : NULL;
 
-	/*
-	 * Get the information about the partition tree after locking all the
-	 * partitions.
-	 */
-	(void) find_all_inheritors(RelationGetRelid(rel), RowExclusiveLock, NULL);
 	proute = (PartitionTupleRouting *) palloc(sizeof(PartitionTupleRouting));
 	proute->partition_dispatch_info =
 		RelationGetPartitionDispatchInfo(rel, &proute->num_dispatch,
@@ -386,11 +378,7 @@ ExecInitPartitionInfo(ModifyTableState *mtstate,
 	AttrNumber *part_attnos = NULL;
 	bool		found_whole_row;
 
-	/*
-	 * We locked all the partitions in ExecSetupPartitionTupleRouting
-	 * including the leaf partitions.
-	 */
-	partrel = heap_open(proute->partition_oids[partidx], NoLock);
+	partrel = heap_open(proute->partition_oids[partidx], RowExclusiveLock);
 
 	/*
 	 * Keep ResultRelInfo and other information for this partition in the
@@ -1111,11 +1099,7 @@ get_partition_dispatch_recurse(Relation rel, Relation parent,
 		}
 		else
 		{
-			/*
-			 * We assume all tables in the partition tree were already locked
-			 * by the caller.
-			 */
-			Relation	partrel = heap_open(partrelid, NoLock);
+			Relation	partrel = heap_open(partrelid, RowExclusiveLock);
 
 			*n_leaf_part_oids = oid_array_used;
 			*leaf_part_oid_size = oid_array_size;
-- 
2.16.2.windows.1

