From 1a9405d26537c6d95269bf48f5ea80fbf7967260 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 13 Jul 2012 11:37:39 -0400
Subject: [PATCH] Cosmetic cleanup of ginInsertValue().

Make it clearer that the passed stack mustn't be empty, and that we
are not supposed to fall off the end of the stack in the main loop.
Tighten the loop that extracts the root block number, too.

Markus Wanner and Tom Lane
---
 src/backend/access/gin/ginbtree.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/backend/access/gin/ginbtree.c b/src/backend/access/gin/ginbtree.c
index b160551b5ed..82ac53e1487 100644
--- a/src/backend/access/gin/ginbtree.c
+++ b/src/backend/access/gin/ginbtree.c
@@ -275,20 +275,22 @@ ginFindParents(GinBtree btree, GinBtreeStack *stack,
 void
 ginInsertValue(GinBtree btree, GinBtreeStack *stack, GinStatsData *buildStats)
 {
-	GinBtreeStack *parent = stack;
-	BlockNumber rootBlkno = InvalidBuffer;
+	GinBtreeStack *parent;
+	BlockNumber rootBlkno;
 	Page		page,
 				rpage,
 				lpage;
 
-	/* remember root BlockNumber */
-	while (parent)
-	{
-		rootBlkno = parent->blkno;
+	/* extract root BlockNumber from stack */
+	Assert(stack != NULL);
+	parent = stack;
+	while (parent->parent)
 		parent = parent->parent;
-	}
+	rootBlkno = parent->blkno;
+	Assert(BlockNumberIsValid(rootBlkno));
 
-	while (stack)
+	/* this loop crawls up the stack until the insertion is complete */
+	for (;;)
 	{
 		XLogRecData *rdata;
 		BlockNumber savedRightLink;
@@ -457,7 +459,7 @@ ginInsertValue(GinBtree btree, GinBtreeStack *stack, GinStatsData *buildStats)
 				 */
 				ginFindParents(btree, stack, rootBlkno);
 				parent = stack->parent;
-				page = BufferGetPage(parent->buffer);
+				Assert(parent != NULL);
 				break;
 			}
 
-- 
GitLab