From: Robert Haas Date: Wed, 26 Feb 2014 13:18:17 +0000 (-0500) Subject: More hacking. X-Git-Url: http://git.postgresql.org/gitweb/static/developers.postgresql.org?a=commitdiff_plain;h=997a74483b8ed5ddee68822cd24ba3a5c4a87f79;p=users%2Frhaas%2Fpostgres.git More hacking. --- diff --git a/src/backend/utils/mmgr/freepage.c b/src/backend/utils/mmgr/freepage.c index 45bfc4a80f..a891a039da 100644 --- a/src/backend/utils/mmgr/freepage.c +++ b/src/backend/utils/mmgr/freepage.c @@ -84,6 +84,8 @@ typedef struct FreePageBtreeSearchResult /* Helper functions */ static void FreePageBtreeAdjustAncestorKeys(FreePageManager *fpm, FreePageBtree *btp); +static FreePageBtree *FreePageBtreeFindRightSibling(char *base, + FreePageBtree *btp); static Size FreePageBtreeFirstKey(FreePageBtree *btp); static FreePageBtree *FreePageBtreeGetRecycled(FreePageManager *fpm); static void FreePageBtreeInsertInternal(char *base, FreePageBtree *btp, @@ -364,6 +366,45 @@ FreePageBtreeAdjustAncestorKeys(FreePageManager *fpm, FreePageBtree *btp) } } +/* + * Find the leaf page which follows the passed page in key order. + */ +static FreePageBtree * +FreePageBtreeFindRightSibling(char *base, FreePageBtree *btp) +{ + Assert(btp->hdr.magic == FREE_PAGE_LEAF_MAGIC); + + /* Move up until we can move right. */ + for (;;) + { + Size first_page; + Size index; + + if (btp->hdr.magic == FREE_PAGE_LEAF_MAGIC) + first_page = btp->u.leaf_key[0].first_page; + else + first_page = btp->u.internal_key[0].first_page; + btp = relptr_access(base, btp->hdr.parent); + + if (btp == NULL) + return NULL; /* we were passed the rightmost page */ + + index = FreePageBtreeSearchInternal(btp, first_page); + if (index < btp->hdr.nused - 1) + { + btp = relptr_access(base, btp->u.internal_key[index + 1].child); + break; + } + Assert(index == btp->hdr.nused - 1); + } + + /* Descend left. */ + while (btp->hdr.magic == FREE_PAGE_INTERNAL_MAGIC) + btp = relptr_access(base, btp->u.internal_key[0].child); + + return btp; +} + /* * Get the first key on a btree page. */ @@ -1044,31 +1085,10 @@ FreePageManagerPutInternal(FreePageManager *fpm, Size first_page, Size npages, } else { - /* - * We need to find the right sibling of the insertion point to - * determine whether we can consolidate with the following key. - * Move up until we can move right; then descend left. - */ - np = result.page; - for (;;) - { - np = relptr_access(base, np->hdr.parent); - if (np == NULL) - break; /* no next key exists */ - nindex = FreePageBtreeSearchInternal(np, first_page); - if (nindex < np->hdr.nused) - { - np = relptr_access(base, np->u.internal_key[nindex].child); - break; - } - } + np = FreePageBtreeFindRightSibling(base, result.page); + nindex = 0; if (np != NULL) - { - while (np->hdr.magic == FREE_PAGE_INTERNAL_MAGIC) - np = relptr_access(base, np->u.internal_key[0].child); nextkey = &np->u.leaf_key[0]; - nindex = 0; - } } /* Consolidate with the previous entry if possible. */