commit 8437d7d23a4978923989aa2696268f7eefbfc034
Author: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date:   Wed Feb 5 09:46:10 2014 +0200

    Fix _bt_moveright again.
    
    Forgot to assign result of _bt_getbuf

diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 4d60ae0..9d8ea10 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -1794,8 +1794,8 @@ _bt_insert_parent(Relation rel,
  * routines won't allow to insert on a page that is incompletely split.
  * Before inserting on such a page, call _bt_finish_split().
  *
- * On entry, we hold write-mode lock on it, and the lock is released on
- * exit.
+ * On entry, 'lbuf' must be locked in write-mode.  On exit, it is unlocked
+ * and unpinned.
  */
 void
 _bt_finish_split(Relation rel, Buffer lbuf, BTStack stack)
diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c
index d4220d9..0753420 100644
--- a/src/backend/access/nbtree/nbtsearch.c
+++ b/src/backend/access/nbtree/nbtsearch.c
@@ -210,22 +210,20 @@ _bt_moveright(Relation rel,
 		{
 			BlockNumber blkno = BufferGetBlockNumber(buf);
 
+			/* upgrade our lock if necessary */
 			if (access == BT_READ)
 			{
-				/* upgrade the lock, and re-check */
 				LockBuffer(buf, BUFFER_LOCK_UNLOCK);
 				LockBuffer(buf, BT_WRITE);
-				if (P_INCOMPLETE_SPLIT(opaque))
-				{
-					_bt_finish_split(rel, buf, stack);
-					_bt_getbuf(rel, blkno, access);
-				}
 			}
-			else
-			{
+
+			if (P_INCOMPLETE_SPLIT(opaque))
 				_bt_finish_split(rel, buf, stack);
-				_bt_getbuf(rel, blkno, access);
-			}
+			else
+				_bt_relbuf(rel, buf);
+
+			/* re-acquire the lock in the right mode, and re-check */
+			buf = _bt_getbuf(rel, blkno, access);
 			continue;
 		}
 
