From 526f0ecf8313084c725fbd69bb62b82a4c5d4022 Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Fri, 12 Oct 2018 19:12:53 +0900
Subject: [PATCH v24 3/3] Do not lock all partitions at the beginning

---
 src/backend/optimizer/util/inherit.c | 16 ++++------------
 1 file changed, 4 insertions(+), 12 deletions(-)

diff --git a/src/backend/optimizer/util/inherit.c b/src/backend/optimizer/util/inherit.c
index d63fb017dc..bb2f281f80 100644
--- a/src/backend/optimizer/util/inherit.c
+++ b/src/backend/optimizer/util/inherit.c
@@ -362,10 +362,6 @@ expand_regular_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte,
  *
  * Partitions are added to the query in order in which they are found in
  * the parent's PartitionDesc.
- *
- * Note: even though only the unpruned partitions will be added to the
- * resulting plan, this still locks *all* partitions via find_all_inheritors
- * when this function is called for the root partitioned table.
  */
 static void
 expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
@@ -376,10 +372,6 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
 	LOCKMODE	lockmode = parentrte->rellockmode;
 	int			i;
 
-	/* If root partitioned table, lock *all* partitions in the tree. */
-	if (parentRTindex == rootParentRTindex)
-		(void) find_all_inheritors(parentrte->relid, lockmode, NULL);
-
 	/* Already locked. */
 	parentrel = table_open(parentrte->relid, NoLock);
 	partdesc = RelationGetPartitionDesc(parentrel);
@@ -394,8 +386,8 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
 		for (i = 0; i < partdesc->nparts; i++)
 		{
 			Oid			childOID = partdesc->oids[i];
-			/* Already locked. */
-			Relation	childrel = table_open(childOID, NoLock);
+			/* Lock the child as it's the first time it's being opened. */
+			Relation	childrel = table_open(childOID, lockmode);
 			RangeTblEntry *childrte = NULL;
 			Index		childRTindex = 0;
 			AppendRelInfo *appinfo = NULL;
@@ -472,8 +464,8 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
 		while ((i = bms_next_member(partindexes, i)) >= 0)
 		{
 			Oid			childOID = partdesc->oids[i];
-			/* Already locked. */
-			Relation	childrel = table_open(childOID, NoLock);
+			/* Lock the child as it's the first time it's being opened. */
+			Relation	childrel = table_open(childOID, lockmode);
 			RangeTblEntry *childrte = NULL;
 			Index		childRTindex = 0;
 			AppendRelInfo *appinfo = NULL;
-- 
2.17.2 (Apple Git-113)

