Skip to content

Commit 1cf175c

Browse files
committed
Prevent GIN deleted pages from being reclaimed too early
When GIN vacuum deletes a posting tree page, it assumes that no concurrent searchers can access it, thanks to ginStepRight() locking two pages at once. However, since 9.4 searches can skip parts of posting trees descending from the root. That leads to the risk that page is deleted and reclaimed before concurrent search can access it. This commit prevents the risk of above by waiting for every transaction, which might wait to reference this page, to finish. Due to binary compatibility we can't change GinPageOpaqueData to store corresponding transaction id. Instead we reuse page header pd_prune_xid field, which is unused in index pages. Discussion: https://postgr.es/m/31a702a.14dd.166c1366ac1.Coremail.chjischj%40163.com Author: Andrey Borodin, Alexander Korotkov Reviewed-by: Alexander Korotkov Backpatch-through: 9.4
1 parent 19cf52e commit 1cf175c

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

src/backend/access/gin/README

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,10 @@ the lock on next page has been acquired.
304304
The downlink is more tricky. A search descending the tree must release the
305305
lock on the parent page before locking the child, or it could deadlock with
306306
a concurrent split of the child page; a page split locks the parent, while
307-
already holding a lock on the child page. However, posting trees are only
308-
fully searched from left to right, starting from the leftmost leaf. (The
309-
tree-structure is only needed by insertions, to quickly find the correct
310-
insert location). So as long as we don't delete the leftmost page on each
311-
level, a search can never follow a downlink to page that's about to be
312-
deleted.
307+
already holding a lock on the child page. So, deleted page cannot be reclaimed
308+
immediately. Instead, we have to wait for every transaction, which might wait
309+
to reference this page, to finish. Corresponding processes must observe that
310+
the page is marked deleted and recover accordingly.
313311

314312
The previous paragraph's reasoning only applies to searches, and only to
315313
posting trees. To protect from inserters following a downlink to a deleted

src/backend/access/gin/ginutil.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,7 @@ GinNewBuffer(Relation index)
227227
*/
228228
if (ConditionalLockBuffer(buffer))
229229
{
230-
Page page = BufferGetPage(buffer);
231-
232-
if (PageIsNew(page))
233-
return buffer; /* OK to use, if never initialized */
234-
235-
if (GinPageIsDeleted(page))
230+
if (GinPageIsRecyclable(BufferGetPage(buffer)))
236231
return buffer; /* OK to use */
237232

238233
LockBuffer(buffer, GIN_UNLOCK);

src/backend/access/gin/ginvacuum.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn
261261
page = BufferGetPage(dBuffer);
262262
rightlink = GinPageGetOpaque(page)->rightlink;
263263

264+
/* For deleted page remember last xid which could knew its address */
265+
GinPageSetDeleteXid(page, ReadNewTransactionId());
266+
264267
page = BufferGetPage(lBuffer);
265268
GinPageGetOpaque(page)->rightlink = rightlink;
266269

@@ -300,6 +303,7 @@ ginDeletePage(GinVacuumState *gvs, BlockNumber deleteBlkno, BlockNumber leftBlkn
300303
data.parentOffset = myoff;
301304
data.leftBlkno = leftBlkno;
302305
data.rightLink = GinPageGetOpaque(page)->rightlink;
306+
data.deleteXid = GinPageGetDeleteXid(page);
303307

304308
/*
305309
* We can't pass buffer_std = TRUE, because we didn't set pd_lower on
@@ -777,7 +781,7 @@ ginvacuumcleanup(PG_FUNCTION_ARGS)
777781
LockBuffer(buffer, GIN_SHARE);
778782
page = (Page) BufferGetPage(buffer);
779783

780-
if (PageIsNew(page) || GinPageIsDeleted(page))
784+
if (GinPageIsRecyclable(page))
781785
{
782786
Assert(blkno != GIN_ROOT_BLKNO);
783787
RecordFreeIndexPage(index, blkno);

src/include/access/gin_private.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
#include "access/genam.h"
1414
#include "access/gin.h"
1515
#include "access/itup.h"
16+
#include "access/transam.h"
1617
#include "fmgr.h"
1718
#include "storage/bufmgr.h"
1819
#include "utils/rbtree.h"
20+
#include "utils/snapmgr.h"
1921

2022

2123
/*
@@ -131,6 +133,15 @@ typedef struct GinMetaPageData
131133

132134
#define GinPageRightMost(page) ( GinPageGetOpaque(page)->rightlink == InvalidBlockNumber)
133135

136+
/*
137+
* We should reclaim deleted page only once every transaction started before
138+
* its deletion is over.
139+
*/
140+
#define GinPageGetDeleteXid(page) ( ((PageHeader) (page))->pd_prune_xid )
141+
#define GinPageSetDeleteXid(page, xid) ( ((PageHeader) (page))->pd_prune_xid = xid)
142+
#define GinPageIsRecyclable(page) ( PageIsNew(page) || (GinPageIsDeleted(page) \
143+
&& TransactionIdPrecedes(GinPageGetDeleteXid(page), RecentGlobalDataXmin)))
144+
134145
/*
135146
* We use our own ItemPointerGet(BlockNumber|OffsetNumber)
136147
* to avoid Asserts, since sometimes the ip_posid isn't "valid"
@@ -572,6 +583,7 @@ typedef struct ginxlogDeletePage
572583
OffsetNumber parentOffset;
573584
BlockNumber leftBlkno;
574585
BlockNumber rightLink;
586+
TransactionId deleteXid; /* last Xid which could see this page in scan */
575587
} ginxlogDeletePage;
576588

577589
#define XLOG_GIN_UPDATE_META_PAGE 0x60

0 commit comments

Comments
 (0)