Commit 023430ab authored by Heikki Linnakangas's avatar Heikki Linnakangas

Fix handling of all-zero pages in SP-GiST vacuum.

SP-GiST initialized an all-zeros page at vacuum, but that was not
WAL-logged, which is not safe. You might get a torn page write, when it gets
flushed to disk, and end-up with a half-initialized index page. To fix,
leave it in the all-zeros state, and add it to the FSM. It will be
initialized when reused. Also don't set the page-deleted flag when recycling
an empty page. That was also not WAL-logged, and a torn write of that would
cause the page to have an invalid checksum.

Backpatch to 9.2, where SP-GiST indexes were added.
parent 65c384c5
...@@ -621,14 +621,10 @@ spgvacuumpage(spgBulkDeleteState *bds, BlockNumber blkno) ...@@ -621,14 +621,10 @@ spgvacuumpage(spgBulkDeleteState *bds, BlockNumber blkno)
{ {
/* /*
* We found an all-zero page, which could happen if the database * We found an all-zero page, which could happen if the database
* crashed just after extending the file. Initialize and recycle it. * crashed just after extending the file. Recycle it.
*/ */
SpGistInitBuffer(buffer, 0);
SpGistPageSetDeleted(page);
/* We don't bother to WAL-log this action; easy to redo */
MarkBufferDirty(buffer);
} }
else if (SpGistPageIsDeleted(page)) else if (PageIsEmpty(page))
{ {
/* nothing to do */ /* nothing to do */
} }
...@@ -654,30 +650,23 @@ spgvacuumpage(spgBulkDeleteState *bds, BlockNumber blkno) ...@@ -654,30 +650,23 @@ spgvacuumpage(spgBulkDeleteState *bds, BlockNumber blkno)
/* /*
* The root pages must never be deleted, nor marked as available in FSM, * The root pages must never be deleted, nor marked as available in FSM,
* because we don't want them ever returned by a search for a place to put * because we don't want them ever returned by a search for a place to put
* a new tuple. Otherwise, check for empty/deletable page, and make sure * a new tuple. Otherwise, check for empty page, and make sure the FSM
* FSM knows about it. * knows about it.
*/ */
if (!SpGistBlockIsRoot(blkno)) if (!SpGistBlockIsRoot(blkno))
{ {
/* If page is now empty, mark it deleted */ if (PageIsEmpty(page))
if (PageIsEmpty(page) && !SpGistPageIsDeleted(page))
{
SpGistPageSetDeleted(page);
/* We don't bother to WAL-log this action; easy to redo */
MarkBufferDirty(buffer);
}
if (SpGistPageIsDeleted(page))
{ {
RecordFreeIndexPage(index, blkno); RecordFreeIndexPage(index, blkno);
bds->stats->pages_deleted++; bds->stats->pages_deleted++;
} }
else else
{
SpGistSetLastUsedPage(index, buffer);
bds->lastFilledBlock = blkno; bds->lastFilledBlock = blkno;
}
} }
SpGistSetLastUsedPage(index, buffer);
UnlockReleaseBuffer(buffer); UnlockReleaseBuffer(buffer);
} }
......
...@@ -48,14 +48,14 @@ typedef SpGistPageOpaqueData *SpGistPageOpaque; ...@@ -48,14 +48,14 @@ typedef SpGistPageOpaqueData *SpGistPageOpaque;
/* Flag bits in page special space */ /* Flag bits in page special space */
#define SPGIST_META (1<<0) #define SPGIST_META (1<<0)
#define SPGIST_DELETED (1<<1) #define SPGIST_DELETED (1<<1) /* never set, but keep for backwards
* compatibility */
#define SPGIST_LEAF (1<<2) #define SPGIST_LEAF (1<<2)
#define SPGIST_NULLS (1<<3) #define SPGIST_NULLS (1<<3)
#define SpGistPageGetOpaque(page) ((SpGistPageOpaque) PageGetSpecialPointer(page)) #define SpGistPageGetOpaque(page) ((SpGistPageOpaque) PageGetSpecialPointer(page))
#define SpGistPageIsMeta(page) (SpGistPageGetOpaque(page)->flags & SPGIST_META) #define SpGistPageIsMeta(page) (SpGistPageGetOpaque(page)->flags & SPGIST_META)
#define SpGistPageIsDeleted(page) (SpGistPageGetOpaque(page)->flags & SPGIST_DELETED) #define SpGistPageIsDeleted(page) (SpGistPageGetOpaque(page)->flags & SPGIST_DELETED)
#define SpGistPageSetDeleted(page) (SpGistPageGetOpaque(page)->flags |= SPGIST_DELETED)
#define SpGistPageIsLeaf(page) (SpGistPageGetOpaque(page)->flags & SPGIST_LEAF) #define SpGistPageIsLeaf(page) (SpGistPageGetOpaque(page)->flags & SPGIST_LEAF)
#define SpGistPageStoresNulls(page) (SpGistPageGetOpaque(page)->flags & SPGIST_NULLS) #define SpGistPageStoresNulls(page) (SpGistPageGetOpaque(page)->flags & SPGIST_NULLS)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment