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

Commitc9c0589

Browse files
Optimize nbtree backward scan boundary cases.
Teach _bt_binsrch (and related helper routines like _bt_search and_bt_compare) about the initial positioning requirements of backwardscans. Routines like _bt_binsrch already know all about "nextkey"searches, so it seems natural to teach them about "goback"/backwardsearches, too. These concepts are closely related, and are much easierto understand when discussed together.Now that certain implementation details are hidden from _bt_first, it'sstraightforward to add a new optimization: backward scans using the <strategy now avoid extra leaf page accesses in certain "boundary cases".Consider the following example, which uses the tenk1 table (and itstenk1_hundred index) from the standard regression tests:SELECT * FROM tenk1 WHERE hundred < 12 ORDER BY hundred DESC LIMIT 1;Before this commit, nbtree would scan two leaf pages, even though it wasonly really necessary to scan one leaf page. We'll now descend straightto the leaf page containing a (12, -inf) high key instead. The scanwill locate matching non-pivot tuples with "hundred" values startingfrom the value 11. The scan won't waste a page access on the rightsibling leaf page, which cannot possibly contain any matching tuples.You can think of the optimization added by this commit as disabling anoptimization (the _bt_compare "!pivotsearch" behavior that was added toPostgres 12 in commitdd299df) for a small subset of cases where it wasalways counterproductive.Equivalently, you can think of the new optimization as extending the"pivotsearch" behavior that page deletion by VACUUM has long required(since the aforementioned Postgres 12 commit went in) to other, similarcases. Obviously, this isn't strictly necessary for these new cases(unlike VACUUM, _bt_first is prepared to move the scan to the left onceon the leaf level), but the underlying principle is the same.Author: Peter Geoghegan <pg@bowt.ie>Reviewed-By: Matthias van de Meent <boekewurm+postgres@gmail.com>Discussion:https://postgr.es/m/CAH2-Wz=XPzM8HzaLPq278Vms420mVSHfgs9wi5tjFKHcapZCEw@mail.gmail.com
1 parentb437571 commitc9c0589

File tree

7 files changed

+210
-149
lines changed

7 files changed

+210
-149
lines changed

‎contrib/amcheck/verify_nbtree.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,7 +3165,7 @@ invariant_l_offset(BtreeCheckState *state, BTScanInsert key,
31653165
ItemIditemid;
31663166
int32cmp;
31673167

3168-
Assert(key->pivotsearch);
3168+
Assert(!key->nextkey&&key->backward);
31693169

31703170
/* Verify line pointer before checking tuple */
31713171
itemid=PageGetItemIdCareful(state,state->targetblock,state->target,
@@ -3227,7 +3227,7 @@ invariant_leq_offset(BtreeCheckState *state, BTScanInsert key,
32273227
{
32283228
int32cmp;
32293229

3230-
Assert(key->pivotsearch);
3230+
Assert(!key->nextkey&&key->backward);
32313231

32323232
cmp=_bt_compare(state->rel,key,state->target,upperbound);
32333233

@@ -3250,7 +3250,7 @@ invariant_g_offset(BtreeCheckState *state, BTScanInsert key,
32503250
{
32513251
int32cmp;
32523252

3253-
Assert(key->pivotsearch);
3253+
Assert(!key->nextkey&&key->backward);
32543254

32553255
cmp=_bt_compare(state->rel,key,state->target,lowerbound);
32563256

@@ -3288,7 +3288,7 @@ invariant_l_nontarget_offset(BtreeCheckState *state, BTScanInsert key,
32883288
ItemIditemid;
32893289
int32cmp;
32903290

3291-
Assert(key->pivotsearch);
3291+
Assert(!key->nextkey&&key->backward);
32923292

32933293
/* Verify line pointer before checking tuple */
32943294
itemid=PageGetItemIdCareful(state,nontargetblock,nontarget,
@@ -3514,17 +3514,17 @@ palloc_btree_page(BtreeCheckState *state, BlockNumber blocknum)
35143514
* For example, invariant_g_offset() might miss a cross-page invariant failure
35153515
* on an internal level if the scankey built from the first item on the
35163516
* target's right sibling page happened to be equal to (not greater than) the
3517-
* last item on target page. The !pivotsearch tiebreaker in _bt_compare()
3518-
*mightotherwise cause amcheck to assume (rather than actually verify) that
3519-
*thescankey is greater.
3517+
* last item on target page. The !backward tiebreaker in _bt_compare() might
3518+
* otherwise cause amcheck to assume (rather than actually verify) that the
3519+
* scankey is greater.
35203520
*/
35213521
staticinlineBTScanInsert
35223522
bt_mkscankey_pivotsearch(Relationrel,IndexTupleitup)
35233523
{
35243524
BTScanInsertskey;
35253525

35263526
skey=_bt_mkscankey(rel,itup);
3527-
skey->pivotsearch= true;
3527+
skey->backward= true;
35283528

35293529
returnskey;
35303530
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,10 +1958,20 @@ _bt_pagedel(Relation rel, Buffer leafbuf, BTVacState *vstate)
19581958
return;
19591959
}
19601960

1961-
/* we need an insertion scan key for the search, so build one */
1961+
/*
1962+
* We need an insertion scan key, so build one.
1963+
*
1964+
* _bt_search searches for the leaf page that contains any
1965+
* matching non-pivot tuples, but we need it to "search" for
1966+
* the high key pivot from the page that we're set to delete.
1967+
* Compensate for the mismatch by having _bt_search locate the
1968+
* last position < equal-to-untruncated-prefix non-pivots.
1969+
*/
19621970
itup_key=_bt_mkscankey(rel,targetkey);
1963-
/* find the leftmost leaf page with matching pivot/high key */
1964-
itup_key->pivotsearch= true;
1971+
1972+
/* Set up a BTLessStrategyNumber-like insertion scan key */
1973+
itup_key->nextkey= false;
1974+
itup_key->backward= true;
19651975
stack=_bt_search(rel,NULL,itup_key,&sleafbuf,BT_READ);
19661976
/* won't need a second lock or pin on leafbuf */
19671977
_bt_relbuf(rel,sleafbuf);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp