From a6cb8aaf8f513eab0bf888c3f43ac949d60cecb1 Mon Sep 17 00:00:00 2001
From: Dilip Kumar <dilip.kumar@enterprisedb.com>
Date: Mon, 9 Mar 2020 17:40:45 +0530
Subject: [PATCH v4 3/3] Conflict Extension/Page lock in group member

---
 src/backend/storage/lmgr/deadlock.c |  9 +++++++++
 src/backend/storage/lmgr/lock.c     | 12 ++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/src/backend/storage/lmgr/deadlock.c b/src/backend/storage/lmgr/deadlock.c
index f8c5df0..49a5998 100644
--- a/src/backend/storage/lmgr/deadlock.c
+++ b/src/backend/storage/lmgr/deadlock.c
@@ -568,6 +568,15 @@ FindLockCycleRecurseMember(PGPROC *checkProc,
 	proclock = (PROCLOCK *) SHMQueueNext(procLocks, procLocks,
 										 offsetof(PROCLOCK, lockLink));
 
+	/*
+	 * Relation extension/page lock never participate in actual deadlock cycle.
+	 * So avoid the wait edge for these type of lock so that we can avoid any
+	 * false cycle detection due to group locking.
+	 */
+	if ((lock->tag.locktag_type == LOCKTAG_RELATION_EXTEND) ||
+		(lock->tag.locktag_type == LOCKTAG_PAGE))
+		return false;
+
 	while (proclock)
 	{
 		PGPROC	   *leader;
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index b89e82a..c1f5e3f 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -1432,6 +1432,18 @@ LockCheckConflicts(LockMethod lockMethodTable,
 	}
 
 	/*
+	 * If it's a relation extension/page lock then it will conflict even between
+	 * the lock group member.
+	 */
+	if ((lock->tag.locktag_type == LOCKTAG_RELATION_EXTEND) ||
+		(lock->tag.locktag_type == LOCKTAG_PAGE))
+	{
+		PROCLOCK_PRINT("LockCheckConflicts: conflicting (simple)",
+				proclock);
+		return true;
+	}
+
+	/*
 	 * Locks held in conflicting modes by members of our own lock group are
 	 * not real conflicts; we can subtract those out and see if we still have
 	 * a conflict.  This is O(N) in the number of processes holding or
-- 
1.8.3.1

