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

Commit2225168

Browse files
committed
Detect internal GiST page splits correctly during index build.
As we descend the GiST tree during insertion, we modify any downlinks onthe way down to include the new tuple we're about to insert (if they don'tcover it already). Modifying an existing downlink might cause an internalpage to split, if the new downlink tuple is larger than the old one. Ifthat happens, we need to back up to the parent and re-choose a page toinsert to. We used to detect that situation, thanks to the NSN-LSNinterlock normally used to detect concurrent page splits, but that gotbroken by commit9155580. With that commit, we now use a dummy constantLSN value for every page during index build, so the LSN-NSN interlock nolonger works. I thought that was OK because there can't be any otherbackends modifying the index during index build, but missed that theinsertion itself can modify the page we're inserting to. The consequencewas that we would sometimes insert the new tuple to an incorrect page, onewhose downlink doesn't cover the new tuple.To fix, add a flag to the stack that keeps track of the state whiledescending tree, to indicate that a page was split, and that we need toretry the descend from the parent.Thomas Munro first reported that the contrib/intarray regression test wasfailing occasionally on the buildfarm after commit9155580. The failurewas intermittent, because the gistchoose() function is not deterministic,and would only occasionally create the right circumstances for this bug tocause the failure.Patch by Anastasia Lubennikova, with some changes by me to make it workcorrectly also when the internal page split also causes the "grandparent"to be split.Discussion:https://www.postgresql.org/message-id/CA%2BhUKGJRzLo7tZExWfSbwM3XuK7aAK7FhdBV0FLkbUG%2BW0v0zg%40mail.gmail.com
1 parente95d550 commit2225168

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

‎src/backend/access/gist/gist.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ gistdoinsert(Relation r, IndexTuple itup, Size freespace,
639639
/* Start from the root */
640640
firststack.blkno=GIST_ROOT_BLKNO;
641641
firststack.lsn=0;
642+
firststack.retry_from_parent= false;
642643
firststack.parent=NULL;
643644
firststack.downlinkoffnum=InvalidOffsetNumber;
644645
state.stack=stack=&firststack;
@@ -651,6 +652,21 @@ gistdoinsert(Relation r, IndexTuple itup, Size freespace,
651652
*/
652653
for (;;)
653654
{
655+
/*
656+
* If we split an internal page while descending the tree, we have to
657+
* retry at the parent. (Normally, the LSN-NSN interlock below would
658+
* also catch this and cause us to retry. But LSNs are not updated
659+
* during index build.)
660+
*/
661+
while (stack->retry_from_parent)
662+
{
663+
if (xlocked)
664+
LockBuffer(stack->buffer,GIST_UNLOCK);
665+
xlocked= false;
666+
ReleaseBuffer(stack->buffer);
667+
state.stack=stack=stack->parent;
668+
}
669+
654670
if (XLogRecPtrIsInvalid(stack->lsn))
655671
stack->buffer=ReadBuffer(state.r,stack->blkno);
656672

@@ -1376,6 +1392,23 @@ gistfinishsplit(GISTInsertState *state, GISTInsertStack *stack,
13761392
unlockbuf/* Unlock stack->buffer if caller wants that */
13771393
);
13781394
Assert(left->buf==stack->buffer);
1395+
1396+
/*
1397+
* If we split the page because we had to adjust the downlink on an
1398+
* internal page, while descending the tree for inserting a new tuple,
1399+
* then this might no longer be the correct page for the new tuple. The
1400+
* downlink to this page might not cover the new tuple anymore, it might
1401+
* need to go to the newly-created right sibling instead. Tell the caller
1402+
* to walk back up the stack, to re-check at the parent which page to
1403+
* insert to.
1404+
*
1405+
* Normally, the LSN-NSN interlock during the tree descend would also
1406+
* detect that a concurrent split happened (by ourselves), and cause us to
1407+
* retry at the parent. But that mechanism doesn't work during index
1408+
* build, because we don't do WAL-logging, and don't update LSNs, during
1409+
* index build.
1410+
*/
1411+
stack->retry_from_parent= true;
13791412
}
13801413

13811414
/*

‎src/include/access/gist_private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ typedef struct GISTInsertStack
215215
*/
216216
GistNSNlsn;
217217

218+
/*
219+
* If set, we split the page while descending the tree to find an
220+
* insertion target. It means that we need to retry from the parent,
221+
* because the downlink of this page might no longer cover the new key.
222+
*/
223+
boolretry_from_parent;
224+
218225
/* offset of the downlink in the parent page, that points to this page */
219226
OffsetNumberdownlinkoffnum;
220227

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp