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

Commit 80d4d8d

Browse files
committed
Prevent deadlock in ginRedoDeletePage()
On standby ginRedoDeletePage() can work concurrently with read-only queries. Those queries can traverse posting tree in two ways. 1) Using rightlinks by ginStepRight(), which locks the next page before unlocking its left sibling. 2) Using downlinks by ginFindLeafPage(), which locks at most one page at time. Original lock order was: page, parent, left sibling. That lock order can deadlock with ginStepRight(). In order to prevent deadlock this commit changes lock order to: left sibling, page, parent. Note, that position of parent in locking order seems insignificant, because we only lock one page at time while traversing downlinks. Reported-by: Chen Huajun Diagnosed-by: Chen Huajun, Peter Geoghegan, Andrey Borodin Discussion: https://postgr.es/m/31a702a.14dd.166c1366ac1.Coremail.chjischj%40163.com Author: Alexander Korotkov Backpatch-through: 9.4
1 parent 12cb7ea commit 80d4d8d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/backend/access/gin/ginxlog.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,19 @@ ginRedoDeletePage(XLogReaderState *record)
511511
Buffer lbuffer;
512512
Page page;
513513

514+
/*
515+
* Lock left page first in order to prevent possible deadlock with
516+
* ginStepRight().
517+
*/
518+
if (XLogReadBufferForRedo(record, 2, &lbuffer) == BLK_NEEDS_REDO)
519+
{
520+
page = BufferGetPage(lbuffer);
521+
Assert(GinPageIsData(page));
522+
GinPageGetOpaque(page)->rightlink = data->rightLink;
523+
PageSetLSN(page, lsn);
524+
MarkBufferDirty(lbuffer);
525+
}
526+
514527
if (XLogReadBufferForRedo(record, 0, &dbuffer) == BLK_NEEDS_REDO)
515528
{
516529
page = BufferGetPage(dbuffer);
@@ -530,15 +543,6 @@ ginRedoDeletePage(XLogReaderState *record)
530543
MarkBufferDirty(pbuffer);
531544
}
532545

533-
if (XLogReadBufferForRedo(record, 2, &lbuffer) == BLK_NEEDS_REDO)
534-
{
535-
page = BufferGetPage(lbuffer);
536-
Assert(GinPageIsData(page));
537-
GinPageGetOpaque(page)->rightlink = data->rightLink;
538-
PageSetLSN(page, lsn);
539-
MarkBufferDirty(lbuffer);
540-
}
541-
542546
if (BufferIsValid(lbuffer))
543547
UnlockReleaseBuffer(lbuffer);
544548
if (BufferIsValid(pbuffer))

0 commit comments

Comments
 (0)