]> granicus.if.org Git - postgresql/commitdiff
Don't clear btpo_cycleid during _bt_vacuum_one_page.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 21 Feb 2012 20:03:56 +0000 (15:03 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 21 Feb 2012 20:03:56 +0000 (15:03 -0500)
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 e6284649b9e30372b3990107a082bc7520325676 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.

src/backend/access/nbtree/nbtinsert.c
src/backend/access/nbtree/nbtpage.c
src/backend/access/nbtree/nbtree.c
src/include/access/nbtree.h

index da82265705fef78ba4f1fe837fc7b06d6268274c..f866143e59bb7a741131cbbfa5bd853879f1ee0b 100644 (file)
@@ -1978,7 +1978,7 @@ _bt_vacuum_one_page(Relation rel, Buffer buffer)
        }
 
        if (ndeletable > 0)
-               _bt_delitems(rel, buffer, deletable, ndeletable);
+               _bt_delitems(rel, buffer, deletable, ndeletable, false);
 
        /*
         * Note: if we didn't find any LP_DEAD items, then the page's
index 0688c2c7d885c6c7e82d3a790a4fe3c0f8abf4fb..ad458a9fe457ac8352ac8017ac441ab431eed8c2 100644 (file)
@@ -656,7 +656,8 @@ _bt_page_recyclable(Page page)
  */
 void
 _bt_delitems(Relation rel, Buffer buf,
-                        OffsetNumber *itemnos, int nitems)
+                        OffsetNumber *itemnos, int nitems,
+                        bool inVacuum)
 {
        Page            page = BufferGetPage(buf);
        BTPageOpaque opaque;
@@ -668,11 +669,12 @@ _bt_delitems(Relation rel, Buffer buf,
        PageIndexMultiDelete(page, itemnos, nitems);
 
        /*
-        * We can clear the vacuum cycle ID since this page has certainly been
-        * processed by the current vacuum scan.
+        * If this is within VACUUM, we can clear the vacuum cycle ID since this
+        * page has certainly been processed by the current vacuum scan.
         */
        opaque = (BTPageOpaque) PageGetSpecialPointer(page);
-       opaque->btpo_cycleid = 0;
+       if (inVacuum)
+               opaque->btpo_cycleid = 0;
 
        /*
         * Mark the page as not containing any LP_DEAD items.  This is not
index 2b76e7cd453725a15c90b13fccd25907018342a5..1ffc91f0def147ec39d2f39bbb8b5ff3a788eb97 100644 (file)
@@ -857,7 +857,7 @@ restart:
                 */
                if (ndeletable > 0)
                {
-                       _bt_delitems(rel, buf, deletable, ndeletable);
+                       _bt_delitems(rel, buf, deletable, ndeletable, true);
                        stats->tuples_removed += ndeletable;
                        /* must recompute maxoff */
                        maxoff = PageGetMaxOffsetNumber(page);
index 1d3e42d99bcee5e9cb3195a8ac9b79bd06b276b6..7656c488e1166f462cf502668b80bcea35370678 100644 (file)
@@ -537,7 +537,7 @@ extern void _bt_relbuf(Relation rel, Buffer buf);
 extern void _bt_pageinit(Page page, Size size);
 extern bool _bt_page_recyclable(Page page);
 extern void _bt_delitems(Relation rel, Buffer buf,
-                        OffsetNumber *itemnos, int nitems);
+                        OffsetNumber *itemnos, int nitems, bool inVacuum);
 extern int _bt_pagedel(Relation rel, Buffer buf,
                        BTStack stack, bool vacuum_full);