0002-Teach-expand_inherited_rtentry-to-add-partitions-in-.patch

text/plain

Filename: 0002-Teach-expand_inherited_rtentry-to-add-partitions-in-.patch
Type: text/plain
Part: 1
Message: Re: UPDATE of partition key

Patch

Format: format-patch
Series: patch 0002
Subject: Teach expand_inherited_rtentry() to add partitions in bound order
File+
src/backend/optimizer/prep/prepunion.c 35 10
From 6fc272c637ba1b49f7ac0cba242f997656a3a4ea Mon Sep 17 00:00:00 2001
From: amit <amitlangote09@gmail.com>
Date: Wed, 26 Jul 2017 13:26:58 +0900
Subject: [PATCH 2/3] Teach expand_inherited_rtentry() to add partitions in
 bound order

---
 src/backend/optimizer/prep/prepunion.c | 45 ++++++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 10 deletions(-)

diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c
index cf46b74782..b327dd9ebc 100644
--- a/src/backend/optimizer/prep/prepunion.c
+++ b/src/backend/optimizer/prep/prepunion.c
@@ -33,6 +33,7 @@
 #include "access/heapam.h"
 #include "access/htup_details.h"
 #include "access/sysattr.h"
+#include "catalog/partition.h"
 #include "catalog/pg_inherits_fn.h"
 #include "catalog/pg_type.h"
 #include "miscadmin.h"
@@ -1370,7 +1371,8 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 	Oid			parentOID;
 	PlanRowMark *oldrc;
 	Relation	oldrelation;
-	LOCKMODE	lockmode;
+	LOCKMODE	lockmode,
+				child_lockmode;
 	List	   *inhOIDs;
 	List	   *appinfos;
 	ListCell   *l;
@@ -1417,8 +1419,35 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 	else
 		lockmode = AccessShareLock;
 
+	child_lockmode = lockmode;
+
+	/*
+	 * Must open the parent relation to examine its tupdesc.  We need not lock
+	 * it; we assume the rewriter already did.
+	 */
+	oldrelation = heap_open(parentOID, NoLock);
+
 	/* Scan for all members of inheritance set, acquire needed locks */
-	inhOIDs = find_all_inheritors(parentOID, lockmode, NULL);
+	if (rte->relkind != RELKIND_PARTITIONED_TABLE)
+	{
+		inhOIDs = find_all_inheritors(parentOID, lockmode, NULL);
+		child_lockmode = NoLock;	/* No need to lock the tables in inhOIDs */
+	}
+	else
+	{
+		List   *ptinfos,
+			   *tmp = NIL;
+
+		RelationGetPartitionDispatchInfo(oldrelation, lockmode,
+										 &ptinfos, &inhOIDs);
+		foreach(l, ptinfos)
+		{
+			PartitionedTableInfo   *ptinfo = lfirst(l);
+
+			tmp = lappend_oid(tmp, ptinfo->relid);
+		}
+		inhOIDs = list_concat(tmp, inhOIDs);
+	}
 
 	/*
 	 * Check that there's at least one descendant, else treat as no-child
@@ -1429,6 +1458,7 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 	{
 		/* Clear flag before returning */
 		rte->inh = false;
+		heap_close(oldrelation, NoLock);
 		return;
 	}
 
@@ -1440,12 +1470,6 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 	if (oldrc)
 		oldrc->isParent = true;
 
-	/*
-	 * Must open the parent relation to examine its tupdesc.  We need not lock
-	 * it; we assume the rewriter already did.
-	 */
-	oldrelation = heap_open(parentOID, NoLock);
-
 	/* Scan the inheritance set and expand it */
 	appinfos = NIL;
 	need_append = false;
@@ -1457,9 +1481,9 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 		Index		childRTindex;
 		AppendRelInfo *appinfo;
 
-		/* Open rel if needed; we already have required locks */
+		/* Open rel if needed, taking a lock if a partition (see above) */
 		if (childOID != parentOID)
-			newrelation = heap_open(childOID, NoLock);
+			newrelation = heap_open(childOID, child_lockmode);
 		else
 			newrelation = oldrelation;
 
@@ -1471,6 +1495,7 @@ expand_inherited_rtentry(PlannerInfo *root, RangeTblEntry *rte, Index rti)
 		 */
 		if (childOID != parentOID && RELATION_IS_OTHER_TEMP(newrelation))
 		{
+			/* Note that not using child_lockmode here. */
 			heap_close(newrelation, lockmode);
 			continue;
 		}
-- 
2.11.0