Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9a45a65

Browse files
committed
Fix race condition in GIN posting tree page deletion.
If a page is deleted, and reused for something else, just as a search is following a rightlink to it from its left sibling, the search would continue scanning whatever the new contents of the page are. That could lead to incorrect query results, or even something more curious if the page is reused for a different kind of a page. To fix, modify the search algorithm to lock the next page before releasing the previous one, and refrain from deleting pages from the leftmost branch of the tree. Add a new Concurrency section to the README, explaining why this works. There is a lot more one could say about concurrency in GIN, but that's for another patch. Backpatch to all supported versions.
1 parent f7171c7 commit 9a45a65

File tree

5 files changed

+123
-63
lines changed

5 files changed

+123
-63
lines changed

src/backend/access/gin/README

+50
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,56 @@ fit on one pending-list page must have those pages to itself, even if this
210210
results in wasting much of the space on the preceding page and the last
211211
page for the tuple.)
212212

213+
Concurrency
214+
-----------
215+
216+
The entry tree and each posting tree is a B-tree, with right-links connecting
217+
sibling pages at the same level. This is the same structure that is used in
218+
the regular B-tree indexam (invented by Lehman & Yao), but we don't support
219+
scanning a GIN trees backwards, so we don't need left-links.
220+
221+
To avoid deadlocks, B-tree pages must always be locked in the same order:
222+
left to right, and bottom to top. When searching, the tree is traversed from
223+
top to bottom, so the lock on the parent page must be released before
224+
descending to the next level. Concurrent page splits move the keyspace to
225+
right, so after following a downlink, the page actually containing the key
226+
we're looking for might be somewhere to the right of the page we landed on.
227+
In that case, we follow the right-links until we find the page we're looking
228+
for.
229+
230+
To delete a page, the page's left sibling, the target page, and its parent,
231+
are locked in that order, and the page is marked as deleted. However, a
232+
concurrent search might already have read a pointer to the page, and might be
233+
just about to follow it. A page can be reached via the right-link of its left
234+
sibling, or via its downlink in the parent.
235+
236+
To prevent a backend from reaching a deleted page via a right-link, when
237+
following a right-link the lock on the previous page is not released until
238+
the lock on next page has been acquired.
239+
240+
The downlink is more tricky. A search descending the tree must release the
241+
lock on the parent page before locking the child, or it could deadlock with
242+
a concurrent split of the child page; a page split locks the parent, while
243+
already holding a lock on the child page. However, posting trees are only
244+
fully searched from left to right, starting from the leftmost leaf. (The
245+
tree-structure is only needed by insertions, to quickly find the correct
246+
insert location). So as long as we don't delete the leftmost page on each
247+
level, a search can never follow a downlink to page that's about to be
248+
deleted.
249+
250+
The previous paragraph's reasoning only applies to searches, and only to
251+
posting trees. To protect from inserters following a downlink to a deleted
252+
page, vacuum simply locks out all concurrent insertions to the posting tree,
253+
by holding a super-exclusive lock on the posting tree root. Inserters hold a
254+
pin on the root page, but searches do not, so while new searches cannot begin
255+
while root page is locked, any already-in-progress scans can continue
256+
concurrently with vacuum. In the entry tree, we never delete pages.
257+
258+
(This is quite different from the mechanism the btree indexam uses to make
259+
page-deletions safe; it stamps the deleted pages with an XID and keeps the
260+
deleted pages around with the right-link intact until all concurrent scans
261+
have finished.)
262+
213263
Limitations
214264
-----------
215265

src/backend/access/gin/ginbtree.c

+42-11
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ ginFindLeafPage(GinBtree btree, GinBtreeStack *stack)
112112
/* rightmost page */
113113
break;
114114

115+
stack->buffer = ginStepRight(stack->buffer, btree->index, access);
115116
stack->blkno = rightlink;
116-
LockBuffer(stack->buffer, GIN_UNLOCK);
117-
stack->buffer = ReleaseAndReadBuffer(stack->buffer, btree->index, stack->blkno);
118-
LockBuffer(stack->buffer, access);
119117
page = BufferGetPage(stack->buffer);
120118
}
121119

@@ -151,6 +149,41 @@ ginFindLeafPage(GinBtree btree, GinBtreeStack *stack)
151149
return NULL;
152150
}
153151

152+
/*
153+
* Step right from current page.
154+
*
155+
* The next page is locked first, before releasing the current page. This is
156+
* crucial to protect from concurrent page deletion (see comment in
157+
* ginDeletePage).
158+
*/
159+
Buffer
160+
ginStepRight(Buffer buffer, Relation index, int lockmode)
161+
{
162+
Buffer nextbuffer;
163+
Page page = BufferGetPage(buffer);
164+
bool isLeaf = GinPageIsLeaf(page);
165+
bool isData = GinPageIsData(page);
166+
BlockNumber blkno = GinPageGetOpaque(page)->rightlink;
167+
168+
nextbuffer = ReadBuffer(index, blkno);
169+
LockBuffer(nextbuffer, lockmode);
170+
UnlockReleaseBuffer(buffer);
171+
172+
/* Sanity check that the page we stepped to is of similar kind. */
173+
page = BufferGetPage(nextbuffer);
174+
if (isLeaf != GinPageIsLeaf(page) || isData != GinPageIsData(page))
175+
elog(ERROR, "right sibling of GIN page is of different type");
176+
177+
/*
178+
* Given the proper lock sequence above, we should never land on a
179+
* deleted page.
180+
*/
181+
if (GinPageIsDeleted(page))
182+
elog(ERROR, "right sibling of GIN page was deleted");
183+
184+
return nextbuffer;
185+
}
186+
154187
void
155188
freeGinBtreeStack(GinBtreeStack *stack)
156189
{
@@ -239,12 +272,12 @@ ginFindParents(GinBtree btree, GinBtreeStack *stack,
239272
while ((offset = btree->findChildPtr(btree, page, stack->blkno, InvalidOffsetNumber)) == InvalidOffsetNumber)
240273
{
241274
blkno = GinPageGetOpaque(page)->rightlink;
242-
LockBuffer(buffer, GIN_UNLOCK);
243-
ReleaseBuffer(buffer);
244275
if (blkno == InvalidBlockNumber)
276+
{
277+
UnlockReleaseBuffer(buffer);
245278
break;
246-
buffer = ReadBuffer(btree->index, blkno);
247-
LockBuffer(buffer, GIN_EXCLUSIVE);
279+
}
280+
buffer = ginStepRight(buffer, btree->index, GIN_EXCLUSIVE);
248281
page = BufferGetPage(buffer);
249282
}
250283

@@ -447,23 +480,21 @@ ginInsertValue(GinBtree btree, GinBtreeStack *stack, GinStatsData *buildStats)
447480
{
448481
BlockNumber rightlink = GinPageGetOpaque(page)->rightlink;
449482

450-
LockBuffer(parent->buffer, GIN_UNLOCK);
451-
452483
if (rightlink == InvalidBlockNumber)
453484
{
454485
/*
455486
* rightmost page, but we don't find parent, we should use
456487
* plain search...
457488
*/
489+
LockBuffer(parent->buffer, GIN_UNLOCK);
458490
ginFindParents(btree, stack, rootBlkno);
459491
parent = stack->parent;
460492
page = BufferGetPage(parent->buffer);
461493
break;
462494
}
463495

496+
parent->buffer = ginStepRight(parent->buffer, btree->index, GIN_EXCLUSIVE);
464497
parent->blkno = rightlink;
465-
parent->buffer = ReleaseAndReadBuffer(parent->buffer, btree->index, parent->blkno);
466-
LockBuffer(parent->buffer, GIN_EXCLUSIVE);
467498
page = BufferGetPage(parent->buffer);
468499
}
469500

src/backend/access/gin/ginget.c

+8-23
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,11 @@ moveRightIfItNeeded(GinBtreeData *btree, GinBtreeStack *stack)
105105
/*
106106
* We scanned the whole page, so we should take right page
107107
*/
108-
stack->blkno = GinPageGetOpaque(page)->rightlink;
109-
110108
if (GinPageRightMost(page))
111109
return false; /* no more pages */
112110

113-
LockBuffer(stack->buffer, GIN_UNLOCK);
114-
stack->buffer = ReleaseAndReadBuffer(stack->buffer,
115-
btree->index,
116-
stack->blkno);
117-
LockBuffer(stack->buffer, GIN_SHARE);
111+
stack->buffer = ginStepRight(stack->buffer, btree->index, GIN_SHARE);
112+
stack->blkno = BufferGetBlockNumber(stack->buffer);
118113
stack->off = FirstOffsetNumber;
119114
}
120115

@@ -132,7 +127,6 @@ scanPostingTree(Relation index, GinScanEntry scanEntry,
132127
GinPostingTreeScan *gdi;
133128
Buffer buffer;
134129
Page page;
135-
BlockNumber blkno;
136130

137131
/* Descend to the leftmost leaf page */
138132
gdi = ginPrepareScanPostingTree(index, rootPostingTree, TRUE);
@@ -162,10 +156,7 @@ scanPostingTree(Relation index, GinScanEntry scanEntry,
162156
if (GinPageRightMost(page))
163157
break; /* no more pages */
164158

165-
blkno = GinPageGetOpaque(page)->rightlink;
166-
LockBuffer(buffer, GIN_UNLOCK);
167-
buffer = ReleaseAndReadBuffer(buffer, index, blkno);
168-
LockBuffer(buffer, GIN_SHARE);
159+
buffer = ginStepRight(buffer, index, GIN_SHARE);
169160
}
170161

171162
UnlockReleaseBuffer(buffer);
@@ -544,7 +535,6 @@ static void
544535
entryGetNextItem(GinState *ginstate, GinScanEntry entry)
545536
{
546537
Page page;
547-
BlockNumber blkno;
548538

549539
for (;;)
550540
{
@@ -562,23 +552,18 @@ entryGetNextItem(GinState *ginstate, GinScanEntry entry)
562552
* It's needed to go by right link. During that we should refind
563553
* first ItemPointer greater that stored
564554
*/
565-
566-
blkno = GinPageGetOpaque(page)->rightlink;
567-
568-
LockBuffer(entry->buffer, GIN_UNLOCK);
569-
if (blkno == InvalidBlockNumber)
555+
if (GinPageRightMost(page))
570556
{
571-
ReleaseBuffer(entry->buffer);
557+
UnlockReleaseBuffer(entry->buffer);
572558
ItemPointerSetInvalid(&entry->curItem);
573559
entry->buffer = InvalidBuffer;
574560
entry->isFinished = TRUE;
575561
return;
576562
}
577563

578-
entry->buffer = ReleaseAndReadBuffer(entry->buffer,
579-
ginstate->index,
580-
blkno);
581-
LockBuffer(entry->buffer, GIN_SHARE);
564+
entry->buffer = ginStepRight(entry->buffer,
565+
ginstate->index,
566+
GIN_SHARE);
582567
page = BufferGetPage(entry->buffer);
583568

584569
entry->offset = InvalidOffsetNumber;

src/backend/access/gin/ginvacuum.c

+22-29
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ ginVacuumPostingTreeLeaves(GinVacuumState *gvs, BlockNumber blkno, bool isRoot,
238238
return hasVoidPage;
239239
}
240240

241+
/*
242+
* Delete a posting tree page.
243+
*/
241244
static void
242245
ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkno,
243246
BlockNumber parentBlkno, OffsetNumber myoff, bool isParentRoot)
@@ -247,39 +250,35 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn
247250
Buffer pBuffer;
248251
Page page,
249252
parentPage;
253+
BlockNumber rightlink;
250254

255+
/*
256+
* Lock the pages in the same order as an insertion would, to avoid
257+
* deadlocks: left, then right, then parent.
258+
*/
259+
lBuffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, leftBlkno,
260+
RBM_NORMAL, gvs->strategy);
251261
dBuffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, deleteBlkno,
252262
RBM_NORMAL, gvs->strategy);
253-
254-
if (leftBlkno != InvalidBlockNumber)
255-
lBuffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, leftBlkno,
256-
RBM_NORMAL, gvs->strategy);
257-
else
258-
lBuffer = InvalidBuffer;
259-
260263
pBuffer = ReadBufferExtended(gvs->index, MAIN_FORKNUM, parentBlkno,
261264
RBM_NORMAL, gvs->strategy);
262265

266+
LockBuffer(lBuffer, GIN_EXCLUSIVE);
263267
LockBuffer(dBuffer, GIN_EXCLUSIVE);
264268
if (!isParentRoot) /* parent is already locked by
265269
* LockBufferForCleanup() */
266270
LockBuffer(pBuffer, GIN_EXCLUSIVE);
267-
if (leftBlkno != InvalidBlockNumber)
268-
LockBuffer(lBuffer, GIN_EXCLUSIVE);
269271

270272
START_CRIT_SECTION();
271273

272-
if (leftBlkno != InvalidBlockNumber)
273-
{
274-
BlockNumber rightlink;
275-
276-
page = BufferGetPage(dBuffer);
277-
rightlink = GinPageGetOpaque(page)->rightlink;
274+
/* Unlink the page by changing left sibling's rightlink */
275+
page = BufferGetPage(dBuffer);
276+
rightlink = GinPageGetOpaque(page)->rightlink;
278277

279-
page = BufferGetPage(lBuffer);
280-
GinPageGetOpaque(page)->rightlink = rightlink;
281-
}
278+
page = BufferGetPage(lBuffer);
279+
GinPageGetOpaque(page)->rightlink = rightlink;
282280

281+
/* Delete downlink from parent */
283282
parentPage = BufferGetPage(pBuffer);
284283
#ifdef USE_ASSERT_CHECKING
285284
do
@@ -364,10 +363,7 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn
364363
if (!isParentRoot)
365364
LockBuffer(pBuffer, GIN_UNLOCK);
366365
ReleaseBuffer(pBuffer);
367-
368-
if (leftBlkno != InvalidBlockNumber)
369-
UnlockReleaseBuffer(lBuffer);
370-
366+
UnlockReleaseBuffer(lBuffer);
371367
UnlockReleaseBuffer(dBuffer);
372368

373369
END_CRIT_SECTION();
@@ -435,15 +431,12 @@ ginScanToDelete(GinVacuumState *gvs, BlockNumber blkno, bool isRoot, DataPageDel
435431

436432
if (GinPageGetOpaque(page)->maxoff < FirstOffsetNumber)
437433
{
438-
if (!(me->leftBlkno == InvalidBlockNumber && GinPageRightMost(page)))
434+
/* we never delete the left- or rightmost branch */
435+
if (me->leftBlkno != InvalidBlockNumber && !GinPageRightMost(page))
439436
{
440-
/* we never delete right most branch */
441437
Assert(!isRoot);
442-
if (GinPageGetOpaque(page)->maxoff < FirstOffsetNumber)
443-
{
444-
ginDeletePage(gvs, blkno, me->leftBlkno, me->parent->blkno, myoff, me->parent->isRoot);
445-
meDelete = TRUE;
446-
}
438+
ginDeletePage(gvs, blkno, me->leftBlkno, me->parent->blkno, myoff, me->parent->isRoot);
439+
meDelete = TRUE;
447440
}
448441
}
449442

src/include/access/gin_private.h

+1
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ typedef struct GinBtreeData
513513

514514
extern GinBtreeStack *ginPrepareFindLeafPage(GinBtree btree, BlockNumber blkno);
515515
extern GinBtreeStack *ginFindLeafPage(GinBtree btree, GinBtreeStack *stack);
516+
extern Buffer ginStepRight(Buffer buffer, Relation index, int lockmode);
516517
extern void freeGinBtreeStack(GinBtreeStack *stack);
517518
extern void ginInsertValue(GinBtree btree, GinBtreeStack *stack,
518519
GinStatsData *buildStats);

0 commit comments

Comments
 (0)