Skip to content

Commit 7b6a37f

Browse files
committed
Don't clear btpo_cycleid during _bt_vacuum_one_page.
When "vacuuming" a single btree page by removing LP_DEAD tuples, we are not actually within a vacuum operation, but rather in an ordinary insertion process that could well be running concurrently with a vacuum. So clearing the cycleid is incorrect, and could cause the concurrent vacuum to miss removing tuples that it needs to remove. This is a longstanding bug introduced by commit e628464 of 2006-07-25. I believe it explains Maxim Boguk's recent report of index corruption, and probably some other previously unexplained reports. In 9.0 and up this is a one-line fix; before that we need to introduce a flag to tell _bt_delitems what to do.
1 parent a863963 commit 7b6a37f

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

src/backend/access/nbtree/nbtinsert.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ _bt_vacuum_one_page(Relation rel, Buffer buffer)
19781978
}
19791979

19801980
if (ndeletable > 0)
1981-
_bt_delitems(rel, buffer, deletable, ndeletable);
1981+
_bt_delitems(rel, buffer, deletable, ndeletable, false);
19821982

19831983
/*
19841984
* Note: if we didn't find any LP_DEAD items, then the page's

src/backend/access/nbtree/nbtpage.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,8 @@ _bt_page_recyclable(Page page)
656656
*/
657657
void
658658
_bt_delitems(Relation rel, Buffer buf,
659-
OffsetNumber *itemnos, int nitems)
659+
OffsetNumber *itemnos, int nitems,
660+
bool inVacuum)
660661
{
661662
Page page = BufferGetPage(buf);
662663
BTPageOpaque opaque;
@@ -668,11 +669,12 @@ _bt_delitems(Relation rel, Buffer buf,
668669
PageIndexMultiDelete(page, itemnos, nitems);
669670

670671
/*
671-
* We can clear the vacuum cycle ID since this page has certainly been
672-
* processed by the current vacuum scan.
672+
* If this is within VACUUM, we can clear the vacuum cycle ID since this
673+
* page has certainly been processed by the current vacuum scan.
673674
*/
674675
opaque = (BTPageOpaque) PageGetSpecialPointer(page);
675-
opaque->btpo_cycleid = 0;
676+
if (inVacuum)
677+
opaque->btpo_cycleid = 0;
676678

677679
/*
678680
* Mark the page as not containing any LP_DEAD items. This is not

src/backend/access/nbtree/nbtree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ btvacuumpage(BTVacState *vstate, BlockNumber blkno, BlockNumber orig_blkno)
857857
*/
858858
if (ndeletable > 0)
859859
{
860-
_bt_delitems(rel, buf, deletable, ndeletable);
860+
_bt_delitems(rel, buf, deletable, ndeletable, true);
861861
stats->tuples_removed += ndeletable;
862862
/* must recompute maxoff */
863863
maxoff = PageGetMaxOffsetNumber(page);

src/include/access/nbtree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ extern void _bt_relbuf(Relation rel, Buffer buf);
537537
extern void _bt_pageinit(Page page, Size size);
538538
extern bool _bt_page_recyclable(Page page);
539539
extern void _bt_delitems(Relation rel, Buffer buf,
540-
OffsetNumber *itemnos, int nitems);
540+
OffsetNumber *itemnos, int nitems, bool inVacuum);
541541
extern int _bt_pagedel(Relation rel, Buffer buf,
542542
BTStack stack, bool vacuum_full);
543543

0 commit comments

Comments
 (0)