Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6523f2e

Browse files
Fix nbtree metapage cache upgrade bug.
Commit857f9c3, which taught nbtree VACUUM to avoid unnecessaryindex scans, bumped the nbtree version number from 2 to 3, while addingthe ability for nbtree indexes to be upgraded on-the-fly. Variousassertions that assumed that an nbtree index was always on version 2 hadto be changed to accept any supported version (version 2 or 3 onPostgres 11).However, a few assertions were missed in the initial commit, all ofwhich were in code paths that cache a local copy of the metapagemetadata, where the index had been expected to be on the current version(no longer version 2) as a generic sanity check. Rather than simplyupdate the assertions, follow-up commit0a64b45 intentionally madethe metapage caching code update the per-backend cached metadata versionwithout changing the on-disk version at the same time. This could evenhappen when the planner needed to determine the height of a B-Tree forcosting purposes. The assertions only fail on Postgres v12 whenupgrading from v10, because they were adjusted to use the authoritativeshared memory metapage by v12's commitdd299df.To fix, remove the cache-only upgrade mechanism entirely, and update theassertions themselves to accept any supported version (go back to usingthe cached version in v12). The fix is almost a full revert of commit0a64b45 on the v11 branch.VACUUM only considers the authoritative metapage, and never bothers witha locally cached version, whereas everywhere else isn't interested inthe metapage fields that were added by commit857f9c3. It seemsunlikely that this bug has affected any user on v11.Reported-By: Christoph BergBug: #15896Discussion:https://postgr.es/m/15896-5b25e260fdb0b081%40postgresql.orgBackpatch: 11-, where VACUUM was taught to avoid unnecessary index scans.
1 parentbcafb67 commit6523f2e

File tree

3 files changed

+13
-45
lines changed

3 files changed

+13
-45
lines changed

‎src/backend/access/nbtree/nbtpage.c

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include"storage/predicate.h"
3434
#include"utils/snapmgr.h"
3535

36-
staticvoid_bt_cachemetadata(Relationrel,BTMetaPageData*metad);
3736
staticbool_bt_mark_page_halfdead(Relationrel,Bufferbuf,BTStackstack);
3837
staticbool_bt_unlink_halfdead_page(Relationrel,Bufferleafbuf,
3938
bool*rightsib_empty);
@@ -106,44 +105,6 @@ _bt_upgrademetapage(Page page)
106105
((char*)metad+sizeof(BTMetaPageData))- (char*)page;
107106
}
108107

109-
/*
110-
* Cache metadata from meta page to rel->rd_amcache.
111-
*/
112-
staticvoid
113-
_bt_cachemetadata(Relationrel,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-
147108
/*
148109
*_bt_update_meta_cleanup_info() -- Update cleanup-related information in
149110
* the metapage.
@@ -442,7 +403,9 @@ _bt_getroot(Relation rel, int access)
442403
/*
443404
* Cache the metapage data for next time
444405
*/
445-
_bt_cachemetadata(rel,metad);
406+
rel->rd_amcache=MemoryContextAlloc(rel->rd_indexcxt,
407+
sizeof(BTMetaPageData));
408+
memcpy(rel->rd_amcache,metad,sizeof(BTMetaPageData));
446409

447410
/*
448411
* We are done with the metapage; arrange to release it via first
@@ -641,15 +604,18 @@ _bt_getrootheight(Relation rel)
641604
/*
642605
* Cache the metapage data for next time
643606
*/
644-
_bt_cachemetadata(rel,metad);
645-
607+
rel->rd_amcache=MemoryContextAlloc(rel->rd_indexcxt,
608+
sizeof(BTMetaPageData));
609+
memcpy(rel->rd_amcache,metad,sizeof(BTMetaPageData));
646610
_bt_relbuf(rel,metabuf);
647611
}
648612

613+
/* Get cached page */
649614
metad= (BTMetaPageData*)rel->rd_amcache;
650615
/* We shouldn't have cached it if any of these fail */
651616
Assert(metad->btm_magic==BTREE_MAGIC);
652-
Assert(metad->btm_version==BTREE_VERSION);
617+
Assert(metad->btm_version >=BTREE_MIN_VERSION);
618+
Assert(metad->btm_version <=BTREE_VERSION);
653619
Assert(metad->btm_fastroot!=P_NONE);
654620

655621
returnmetad->btm_fastlevel;

‎src/backend/access/nbtree/nbtxlog.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ _bt_restore_meta(XLogReaderState *record, uint8 block_id)
108108
md->btm_level=xlrec->level;
109109
md->btm_fastroot=xlrec->fastroot;
110110
md->btm_fastlevel=xlrec->fastlevel;
111+
/* Cannot log BTREE_MIN_VERSION index metapage without upgrade */
112+
Assert(md->btm_version==BTREE_VERSION);
111113
md->btm_oldest_btpo_xact=xlrec->oldest_btpo_xact;
112114
md->btm_last_cleanup_num_heap_tuples=xlrec->last_cleanup_num_heap_tuples;
113115

‎src/include/access/nbtree.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ typedef BTPageOpaqueData *BTPageOpaque;
9797
typedefstructBTMetaPageData
9898
{
9999
uint32btm_magic;/* should contain BTREE_MAGIC */
100-
uint32btm_version;/*should containBTREE_VERSION */
100+
uint32btm_version;/*nbtree version (always <=BTREE_VERSION) */
101101
BlockNumberbtm_root;/* current root location */
102102
uint32btm_level;/* tree level of the root page */
103103
BlockNumberbtm_fastroot;/* current "fast" root location */
104104
uint32btm_fastlevel;/* tree level of the "fast" root page */
105-
/*following fieldsare available since page version 3 */
105+
/*remaining fieldsonly valid when btm_version is BTREE_VERSION */
106106
TransactionIdbtm_oldest_btpo_xact;/* oldest btpo_xact among all deleted
107107
* pages */
108108
float8btm_last_cleanup_num_heap_tuples;/* number of heap tuples

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp