diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 83b99a98f0..a49dc6f802 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -1329,15 +1329,6 @@ SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
 #endif							/* CHECK_DEADLOCK_RISK */
 	}
 
-	/*
-	 * lock->nRequested and lock->requested[] count the total number of
-	 * requests, whether granted or waiting, so increment those immediately.
-	 * The other counts don't increment till we get the lock.
-	 */
-	lock->nRequested++;
-	lock->requested[lockmode]++;
-	Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
-
 	/*
 	 * We shouldn't already hold the desired lock; else locallock table is
 	 * broken.
@@ -1348,6 +1339,15 @@ SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
 			 lock->tag.locktag_field1, lock->tag.locktag_field2,
 			 lock->tag.locktag_field3);
 
+	/*
+	 * lock->nRequested and lock->requested[] count the total number of
+	 * requests, whether granted or waiting, so increment those immediately.
+	 * The other counts don't increment till we get the lock.
+	 */
+	lock->nRequested++;
+	lock->requested[lockmode]++;
+	Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
+
 	return proclock;
 }
 
@@ -4296,14 +4296,6 @@ lock_twophase_recover(TransactionId xid, uint16 info,
 		Assert((proclock->holdMask & ~lock->grantMask) == 0);
 	}
 
-	/*
-	 * lock->nRequested and lock->requested[] count the total number of
-	 * requests, whether granted or waiting, so increment those immediately.
-	 */
-	lock->nRequested++;
-	lock->requested[lockmode]++;
-	Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
-
 	/*
 	 * We shouldn't already hold the desired lock.
 	 */
@@ -4313,6 +4305,14 @@ lock_twophase_recover(TransactionId xid, uint16 info,
 			 lock->tag.locktag_field1, lock->tag.locktag_field2,
 			 lock->tag.locktag_field3);
 
+	/*
+	 * lock->nRequested and lock->requested[] count the total number of
+	 * requests, whether granted or waiting, so increment those immediately.
+	 */
+	lock->nRequested++;
+	lock->requested[lockmode]++;
+	Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
+
 	/*
 	 * We ignore any possible conflicts and just grant ourselves the lock. Not
 	 * only because we don't bother, but also to avoid deadlocks when
