|
33 | 33 | #include "storage/predicate.h"
|
34 | 34 | #include "utils/snapmgr.h"
|
35 | 35 |
|
| 36 | +static void _bt_cachemetadata(Relation rel, BTMetaPageData *metad); |
36 | 37 | static bool _bt_mark_page_halfdead(Relation rel, Buffer buf, BTStack stack);
|
37 | 38 | static bool _bt_unlink_halfdead_page(Relation rel, Buffer leafbuf,
|
38 | 39 | bool *rightsib_empty);
|
@@ -105,6 +106,44 @@ _bt_upgrademetapage(Page page)
|
105 | 106 | ((char *) metad + sizeof(BTMetaPageData)) - (char *) page;
|
106 | 107 | }
|
107 | 108 |
|
| 109 | +/* |
| 110 | + * Cache metadata from meta page to rel->rd_amcache. |
| 111 | + */ |
| 112 | +static void |
| 113 | +_bt_cachemetadata(Relation rel, BTMetaPageData *metad) |
| 114 | +{ |
| 115 | + /* We assume rel->rd_amcache was already freed by caller */ |
| 116 | + Assert(rel->rd_amcache == NULL); |
| 117 | + rel->rd_amcache = MemoryContextAlloc(rel->rd_indexcxt, |
| 118 | + sizeof(BTMetaPageData)); |
| 119 | + |
| 120 | + /* |
| 121 | + * Meta page should be of supported version (should be already checked by |
| 122 | + * caller). |
| 123 | + */ |
| 124 | + Assert(metad->btm_version >= BTREE_MIN_VERSION && |
| 125 | + metad->btm_version <= BTREE_VERSION); |
| 126 | + |
| 127 | + if (metad->btm_version == BTREE_VERSION) |
| 128 | + { |
| 129 | + /* Last version of meta-data, no need to upgrade */ |
| 130 | + memcpy(rel->rd_amcache, metad, sizeof(BTMetaPageData)); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + BTMetaPageData *cached_metad = (BTMetaPageData *) rel->rd_amcache; |
| 135 | + |
| 136 | + /* |
| 137 | + * Upgrade meta-data: copy available information from meta-page and |
| 138 | + * fill new fields with default values. |
| 139 | + */ |
| 140 | + memcpy(rel->rd_amcache, metad, offsetof(BTMetaPageData, btm_oldest_btpo_xact)); |
| 141 | + cached_metad->btm_version = BTREE_VERSION; |
| 142 | + cached_metad->btm_oldest_btpo_xact = InvalidTransactionId; |
| 143 | + cached_metad->btm_last_cleanup_num_heap_tuples = -1.0; |
| 144 | + } |
| 145 | +} |
| 146 | + |
108 | 147 | /*
|
109 | 148 | * _bt_update_meta_cleanup_info() -- Update cleanup-related information in
|
110 | 149 | * the metapage.
|
@@ -403,9 +442,7 @@ _bt_getroot(Relation rel, int access)
|
403 | 442 | /*
|
404 | 443 | * Cache the metapage data for next time
|
405 | 444 | */
|
406 |
| - rel->rd_amcache = MemoryContextAlloc(rel->rd_indexcxt, |
407 |
| - sizeof(BTMetaPageData)); |
408 |
| - memcpy(rel->rd_amcache, metad, sizeof(BTMetaPageData)); |
| 445 | + _bt_cachemetadata(rel, metad); |
409 | 446 |
|
410 | 447 | /*
|
411 | 448 | * We are done with the metapage; arrange to release it via first
|
@@ -604,9 +641,7 @@ _bt_getrootheight(Relation rel)
|
604 | 641 | /*
|
605 | 642 | * Cache the metapage data for next time
|
606 | 643 | */
|
607 |
| - rel->rd_amcache = MemoryContextAlloc(rel->rd_indexcxt, |
608 |
| - sizeof(BTMetaPageData)); |
609 |
| - memcpy(rel->rd_amcache, metad, sizeof(BTMetaPageData)); |
| 644 | + _bt_cachemetadata(rel, metad); |
610 | 645 |
|
611 | 646 | _bt_relbuf(rel, metabuf);
|
612 | 647 | }
|
|
0 commit comments