@@ -1840,8 +1840,10 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1840
1840
BTPageOpaque rootopaque ;
1841
1841
ItemId itemid ;
1842
1842
IndexTuple item ;
1843
- Size itemsz ;
1844
- IndexTuple new_item ;
1843
+ IndexTuple left_item ;
1844
+ Size left_item_sz ;
1845
+ IndexTuple right_item ;
1846
+ Size right_item_sz ;
1845
1847
Buffer metabuf ;
1846
1848
Page metapg ;
1847
1849
BTMetaPageData * metad ;
@@ -1860,6 +1862,26 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1860
1862
metapg = BufferGetPage (metabuf );
1861
1863
metad = BTPageGetMeta (metapg );
1862
1864
1865
+ /*
1866
+ * Create downlink item for left page (old root). Since this will be the
1867
+ * first item in a non-leaf page, it implicitly has minus-infinity key
1868
+ * value, so we need not store any actual key in it.
1869
+ */
1870
+ left_item_sz = sizeof (IndexTupleData );
1871
+ left_item = (IndexTuple ) palloc (left_item_sz );
1872
+ left_item -> t_info = left_item_sz ;
1873
+ ItemPointerSet (& (left_item -> t_tid ), lbkno , P_HIKEY );
1874
+
1875
+ /*
1876
+ * Create downlink item for right page. The key for it is obtained from
1877
+ * the "high key" position in the left page.
1878
+ */
1879
+ itemid = PageGetItemId (lpage , P_HIKEY );
1880
+ right_item_sz = ItemIdGetLength (itemid );
1881
+ item = (IndexTuple ) PageGetItem (lpage , itemid );
1882
+ right_item = CopyIndexTuple (item );
1883
+ ItemPointerSet (& (right_item -> t_tid ), rbkno , P_HIKEY );
1884
+
1863
1885
/* NO EREPORT(ERROR) from here till newroot op is logged */
1864
1886
START_CRIT_SECTION ();
1865
1887
@@ -1877,16 +1899,6 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1877
1899
metad -> btm_fastroot = rootblknum ;
1878
1900
metad -> btm_fastlevel = rootopaque -> btpo .level ;
1879
1901
1880
- /*
1881
- * Create downlink item for left page (old root). Since this will be the
1882
- * first item in a non-leaf page, it implicitly has minus-infinity key
1883
- * value, so we need not store any actual key in it.
1884
- */
1885
- itemsz = sizeof (IndexTupleData );
1886
- new_item = (IndexTuple ) palloc (itemsz );
1887
- new_item -> t_info = itemsz ;
1888
- ItemPointerSet (& (new_item -> t_tid ), lbkno , P_HIKEY );
1889
-
1890
1902
/*
1891
1903
* Insert the left page pointer into the new root page. The root page is
1892
1904
* the rightmost page on its level so there is no "high key" in it; the
@@ -1895,32 +1907,20 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1895
1907
* Note: we *must* insert the two items in item-number order, for the
1896
1908
* benefit of _bt_restore_page().
1897
1909
*/
1898
- if (PageAddItem (rootpage , (Item ) new_item , itemsz , P_HIKEY ,
1910
+ if (PageAddItem (rootpage , (Item ) left_item , left_item_sz , P_HIKEY ,
1899
1911
false, false) == InvalidOffsetNumber )
1900
1912
elog (PANIC , "failed to add leftkey to new root page"
1901
1913
" while splitting block %u of index \"%s\"" ,
1902
1914
BufferGetBlockNumber (lbuf ), RelationGetRelationName (rel ));
1903
- pfree (new_item );
1904
-
1905
- /*
1906
- * Create downlink item for right page. The key for it is obtained from
1907
- * the "high key" position in the left page.
1908
- */
1909
- itemid = PageGetItemId (lpage , P_HIKEY );
1910
- itemsz = ItemIdGetLength (itemid );
1911
- item = (IndexTuple ) PageGetItem (lpage , itemid );
1912
- new_item = CopyIndexTuple (item );
1913
- ItemPointerSet (& (new_item -> t_tid ), rbkno , P_HIKEY );
1914
1915
1915
1916
/*
1916
1917
* insert the right page pointer into the new root page.
1917
1918
*/
1918
- if (PageAddItem (rootpage , (Item ) new_item , itemsz , P_FIRSTKEY ,
1919
+ if (PageAddItem (rootpage , (Item ) right_item , right_item_sz , P_FIRSTKEY ,
1919
1920
false, false) == InvalidOffsetNumber )
1920
1921
elog (PANIC , "failed to add rightkey to new root page"
1921
1922
" while splitting block %u of index \"%s\"" ,
1922
1923
BufferGetBlockNumber (lbuf ), RelationGetRelationName (rel ));
1923
- pfree (new_item );
1924
1924
1925
1925
MarkBufferDirty (rootbuf );
1926
1926
MarkBufferDirty (metabuf );
@@ -1968,6 +1968,9 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1968
1968
/* done with metapage */
1969
1969
_bt_relbuf (rel , metabuf );
1970
1970
1971
+ pfree (left_item );
1972
+ pfree (right_item );
1973
+
1971
1974
return rootbuf ;
1972
1975
}
1973
1976
0 commit comments