diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index fde2042..c1b2b42 100644
*** a/src/backend/storage/lmgr/lock.c
--- b/src/backend/storage/lmgr/lock.c
*************** LockReassignOwner(LOCALLOCK *locallock, 
*** 2407,2417 ****
  static bool
  FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode)
  {
! 	uint32		f;
! 	uint32		unused_slot = FP_LOCK_SLOTS_PER_BACKEND;
  
! 	/* Scan for existing entry for this relid, remembering empty slot. */
! 	for (f = 0; f < FP_LOCK_SLOTS_PER_BACKEND; f++)
  	{
  		if (FAST_PATH_GET_BITS(MyProc, f) == 0)
  			unused_slot = f;
--- 2407,2421 ----
  static bool
  FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode)
  {
! 	int			f;
! 	int			unused_slot = -1;
  
! 	/*
! 	 * Scan for existing entry for this relid, remembering first empty slot.
! 	 * We prefer to assign a low-numbered slot so that later search loops,
! 	 * which search the array forwards, will find entries more quickly.
! 	 */
! 	for (f = FP_LOCK_SLOTS_PER_BACKEND - 1; f >= 0; f--)
  	{
  		if (FAST_PATH_GET_BITS(MyProc, f) == 0)
  			unused_slot = f;
*************** FastPathGrantRelationLock(Oid relid, LOC
*** 2423,2430 ****
  		}
  	}
  
! 	/* If no existing entry, use any empty slot. */
! 	if (unused_slot < FP_LOCK_SLOTS_PER_BACKEND)
  	{
  		MyProc->fpRelId[unused_slot] = relid;
  		FAST_PATH_SET_LOCKMODE(MyProc, unused_slot, lockmode);
--- 2427,2434 ----
  		}
  	}
  
! 	/* If no existing entry, use first empty slot. */
! 	if (unused_slot >= 0)
  	{
  		MyProc->fpRelId[unused_slot] = relid;
  		FAST_PATH_SET_LOCKMODE(MyProc, unused_slot, lockmode);
