@@ -1828,8 +1828,10 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1828
1828
BTPageOpaque rootopaque ;
1829
1829
ItemId itemid ;
1830
1830
IndexTuple item ;
1831
- Size itemsz ;
1832
- IndexTuple new_item ;
1831
+ IndexTuple left_item ;
1832
+ Size left_item_sz ;
1833
+ IndexTuple right_item ;
1834
+ Size right_item_sz ;
1833
1835
Buffer metabuf ;
1834
1836
Page metapg ;
1835
1837
BTMetaPageData * metad ;
@@ -1848,6 +1850,26 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1848
1850
metapg = BufferGetPage (metabuf );
1849
1851
metad = BTPageGetMeta (metapg );
1850
1852
1853
+ /*
1854
+ * Create downlink item for left page (old root). Since this will be the
1855
+ * first item in a non-leaf page, it implicitly has minus-infinity key
1856
+ * value, so we need not store any actual key in it.
1857
+ */
1858
+ left_item_sz = sizeof (IndexTupleData );
1859
+ left_item = (IndexTuple ) palloc (left_item_sz );
1860
+ left_item -> t_info = left_item_sz ;
1861
+ ItemPointerSet (& (left_item -> t_tid ), lbkno , P_HIKEY );
1862
+
1863
+ /*
1864
+ * Create downlink item for right page. The key for it is obtained from
1865
+ * the "high key" position in the left page.
1866
+ */
1867
+ itemid = PageGetItemId (lpage , P_HIKEY );
1868
+ right_item_sz = ItemIdGetLength (itemid );
1869
+ item = (IndexTuple ) PageGetItem (lpage , itemid );
1870
+ right_item = CopyIndexTuple (item );
1871
+ ItemPointerSet (& (right_item -> t_tid ), rbkno , P_HIKEY );
1872
+
1851
1873
/* NO EREPORT(ERROR) from here till newroot op is logged */
1852
1874
START_CRIT_SECTION ();
1853
1875
@@ -1865,16 +1887,6 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1865
1887
metad -> btm_fastroot = rootblknum ;
1866
1888
metad -> btm_fastlevel = rootopaque -> btpo .level ;
1867
1889
1868
- /*
1869
- * Create downlink item for left page (old root). Since this will be the
1870
- * first item in a non-leaf page, it implicitly has minus-infinity key
1871
- * value, so we need not store any actual key in it.
1872
- */
1873
- itemsz = sizeof (IndexTupleData );
1874
- new_item = (IndexTuple ) palloc (itemsz );
1875
- new_item -> t_info = itemsz ;
1876
- ItemPointerSet (& (new_item -> t_tid ), lbkno , P_HIKEY );
1877
-
1878
1890
/*
1879
1891
* Insert the left page pointer into the new root page. The root page is
1880
1892
* the rightmost page on its level so there is no "high key" in it; the
@@ -1883,32 +1895,20 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1883
1895
* Note: we *must* insert the two items in item-number order, for the
1884
1896
* benefit of _bt_restore_page().
1885
1897
*/
1886
- if (PageAddItem (rootpage , (Item ) new_item , itemsz , P_HIKEY ,
1898
+ if (PageAddItem (rootpage , (Item ) left_item , left_item_sz , P_HIKEY ,
1887
1899
false, false) == InvalidOffsetNumber )
1888
1900
elog (PANIC , "failed to add leftkey to new root page"
1889
1901
" while splitting block %u of index \"%s\"" ,
1890
1902
BufferGetBlockNumber (lbuf ), RelationGetRelationName (rel ));
1891
- pfree (new_item );
1892
-
1893
- /*
1894
- * Create downlink item for right page. The key for it is obtained from
1895
- * the "high key" position in the left page.
1896
- */
1897
- itemid = PageGetItemId (lpage , P_HIKEY );
1898
- itemsz = ItemIdGetLength (itemid );
1899
- item = (IndexTuple ) PageGetItem (lpage , itemid );
1900
- new_item = CopyIndexTuple (item );
1901
- ItemPointerSet (& (new_item -> t_tid ), rbkno , P_HIKEY );
1902
1903
1903
1904
/*
1904
1905
* insert the right page pointer into the new root page.
1905
1906
*/
1906
- if (PageAddItem (rootpage , (Item ) new_item , itemsz , P_FIRSTKEY ,
1907
+ if (PageAddItem (rootpage , (Item ) right_item , right_item_sz , P_FIRSTKEY ,
1907
1908
false, false) == InvalidOffsetNumber )
1908
1909
elog (PANIC , "failed to add rightkey to new root page"
1909
1910
" while splitting block %u of index \"%s\"" ,
1910
1911
BufferGetBlockNumber (lbuf ), RelationGetRelationName (rel ));
1911
- pfree (new_item );
1912
1912
1913
1913
MarkBufferDirty (rootbuf );
1914
1914
MarkBufferDirty (metabuf );
@@ -1956,6 +1956,9 @@ _bt_newroot(Relation rel, Buffer lbuf, Buffer rbuf)
1956
1956
/* done with metapage */
1957
1957
_bt_relbuf (rel , metabuf );
1958
1958
1959
+ pfree (left_item );
1960
+ pfree (right_item );
1961
+
1959
1962
return rootbuf ;
1960
1963
}
1961
1964
0 commit comments