From dad1f4638235e5ff5696b948b88ba24cd99b415e Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 11 Apr 2011 13:46:37 +0300
Subject: [PATCH] TransferPredicateLocksToNewTarget should initialize a new
 lock entry's commitSeqNo to that of the old one being transferred, or take
 the minimum commitSeqNo if it is merging two lock entries.

Also, CreatePredicateLock should initialize commitSeqNo for to
InvalidSerCommitSeqNo instead of to 0. (I don't think using 0 would
actually affect anything, but we should be consistent.)

I also added a couple of assertions I used to track this down: a
lock's commitSeqNo should never be zero, and it should be
InvalidSerCommitSeqNo if and only if the lock is not held by
OldCommittedSxact.

Dan Ports, to fix leak of predicate locks reported by YAMAMOTO Takashi.
---
 src/backend/storage/lmgr/predicate.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c
index eceb384e581..f02d5d5a1e4 100644
--- a/src/backend/storage/lmgr/predicate.c
+++ b/src/backend/storage/lmgr/predicate.c
@@ -2074,7 +2074,7 @@ CreatePredicateLock(const PREDICATELOCKTARGETTAG *targettag,
 		SHMQueueInsertBefore(&(target->predicateLocks), &(lock->targetLink));
 		SHMQueueInsertBefore(&(sxact->predicateLocks),
 							 &(lock->xactLink));
-		lock->commitSeqNo = 0;
+		lock->commitSeqNo = InvalidSerCommitSeqNo;
 	}
 
 	LWLockRelease(partitionLock);
@@ -2508,6 +2508,7 @@ TransferPredicateLocksToNewTarget(const PREDICATELOCKTARGETTAG oldtargettag,
 			SHM_QUEUE  *predlocktargetlink;
 			PREDICATELOCK *nextpredlock;
 			PREDICATELOCK *newpredlock;
+			SerCommitSeqNo oldCommitSeqNo = oldpredlock->commitSeqNo;
 
 			predlocktargetlink = &(oldpredlock->targetLink);
 			nextpredlock = (PREDICATELOCK *)
@@ -2552,8 +2553,17 @@ TransferPredicateLocksToNewTarget(const PREDICATELOCKTARGETTAG oldtargettag,
 									 &(newpredlock->targetLink));
 				SHMQueueInsertBefore(&(newpredlocktag.myXact->predicateLocks),
 									 &(newpredlock->xactLink));
-				newpredlock->commitSeqNo = InvalidSerCommitSeqNo;
+				newpredlock->commitSeqNo = oldCommitSeqNo;
 			}
+			else
+			{
+				if (newpredlock->commitSeqNo < oldCommitSeqNo)
+					newpredlock->commitSeqNo = oldCommitSeqNo;
+			}
+
+			Assert(newpredlock->commitSeqNo != 0);
+			Assert((newpredlock->commitSeqNo == InvalidSerCommitSeqNo)
+				   || (newpredlock->tag.myXact == OldCommittedSxact));
 
 			oldpredlock = nextpredlock;
 		}
@@ -3137,6 +3147,8 @@ ClearOldPredicateLocks(void)
 						 offsetof(PREDICATELOCK, xactLink));
 
 		LWLockAcquire(SerializableXactHashLock, LW_SHARED);
+		Assert(predlock->commitSeqNo != 0);
+		Assert(predlock->commitSeqNo != InvalidSerCommitSeqNo);
 		canDoPartialCleanup = (predlock->commitSeqNo <= PredXact->CanPartialClearThrough);
 		LWLockRelease(SerializableXactHashLock);
 
@@ -3261,6 +3273,8 @@ ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
 						 errhint("You might need to increase max_pred_locks_per_transaction.")));
 			if (found)
 			{
+				Assert(predlock->commitSeqNo != 0);
+				Assert(predlock->commitSeqNo != InvalidSerCommitSeqNo);
 				if (predlock->commitSeqNo < sxact->commitSeqNo)
 					predlock->commitSeqNo = sxact->commitSeqNo;
 			}
-- 
GitLab