]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Deduplicate metapage sanity checks in _bt_gettrueroot()
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Tue, 7 Jul 2026 21:25:24 +0000 (00:25 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Tue, 7 Jul 2026 21:25:24 +0000 (00:25 +0300)
Replace the metapage sanity checks in _bt_gettrueroot() with a call to
_bt_getmeta(), which does exactly the same checks.

Author: Chao Li <li.evan.chao@gmail.com>
Reviewed-by: Neil Chen <carpenter.nail.cz@gmail.com>
Discussion: https://www.postgresql.org/message-id/CAEoWx2nisjqs4iC9o4Hu7-Ab767=cMZZzmhBGb8SaQtMMmVqPQ@mail.gmail.com

src/backend/access/nbtree/nbtpage.c

index 0547038616e045bf19358acb9c1b282dae453bf2..406088dcb577d351801bb6dc59f34ddef1bd0bbf 100644 (file)
@@ -585,8 +585,6 @@ Buffer
 _bt_gettrueroot(Relation rel)
 {
        Buffer          metabuf;
-       Page            metapg;
-       BTPageOpaque metaopaque;
        Buffer          rootbuf;
        Page            rootpage;
        BTPageOpaque rootopaque;
@@ -605,25 +603,7 @@ _bt_gettrueroot(Relation rel)
        rel->rd_amcache = NULL;
 
        metabuf = _bt_getbuf(rel, BTREE_METAPAGE, BT_READ);
-       metapg = BufferGetPage(metabuf);
-       metaopaque = BTPageGetOpaque(metapg);
-       metad = BTPageGetMeta(metapg);
-
-       if (!P_ISMETA(metaopaque) ||
-               metad->btm_magic != BTREE_MAGIC)
-               ereport(ERROR,
-                               (errcode(ERRCODE_INDEX_CORRUPTED),
-                                errmsg("index \"%s\" is not a btree",
-                                               RelationGetRelationName(rel))));
-
-       if (metad->btm_version < BTREE_MIN_VERSION ||
-               metad->btm_version > BTREE_VERSION)
-               ereport(ERROR,
-                               (errcode(ERRCODE_INDEX_CORRUPTED),
-                                errmsg("version mismatch in index \"%s\": file version %d, "
-                                               "current version %d, minimal supported version %d",
-                                               RelationGetRelationName(rel),
-                                               metad->btm_version, BTREE_VERSION, BTREE_MIN_VERSION)));
+       metad = _bt_getmeta(rel, metabuf);
 
        /* if no root page initialized yet, fail */
        if (metad->btm_root == P_NONE)