aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>2014-03-12 09:59:49 +0200
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>2014-03-12 10:04:57 +0200
commitfecfc2b913c4be5eeed24b32ef51a3239580bd1e (patch)
tree339e1a144d5da9bf866560c9023ac3e31a65a7ee
parente85a5ffba8ae559b612b6fbc07acf1b16636887e (diff)
downloadpostgresql-fecfc2b913c4be5eeed24b32ef51a3239580bd1e.tar.gz
postgresql-fecfc2b913c4be5eeed24b32ef51a3239580bd1e.zip
In WAL replay, restore GIN metapage unconditionally to avoid torn page.
We don't take a full-page image of the GIN metapage; instead, the WAL record contains all the information required to reconstruct it from scratch. But to avoid torn page hazards, we must re-initialize it from the WAL record every time, even if it already has a greater LSN, similar to how normal full page images are restored. This was highly unlikely to cause any problems in practice, because the GIN metapage is small. We rely on an update smaller than a 512 byte disk sector to be atomic elsewhere, at least in pg_control. But better safe than sorry, and this would be easy to overlook if more fields are added to the metapage so that it's no longer small. Reported by Noah Misch. Backpatch to all supported versions.
-rw-r--r--src/backend/access/gin/ginxlog.c23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/backend/access/gin/ginxlog.c b/src/backend/access/gin/ginxlog.c
index 3e86590ca0d..c46f7ac4f7e 100644
--- a/src/backend/access/gin/ginxlog.c
+++ b/src/backend/access/gin/ginxlog.c
@@ -564,17 +564,19 @@ ginRedoUpdateMetapage(XLogRecPtr lsn, XLogRecord *record)
Page metapage;
Buffer buffer;
+ /*
+ * Restore the metapage. This is essentially the same as a full-page image,
+ * so restore the metapage unconditionally without looking at the LSN, to
+ * avoid torn page hazards.
+ */
metabuffer = XLogReadBuffer(data->node, GIN_METAPAGE_BLKNO, false);
if (!BufferIsValid(metabuffer))
return; /* assume index was deleted, nothing to do */
metapage = BufferGetPage(metabuffer);
- if (lsn > PageGetLSN(metapage))
- {
- memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
- PageSetLSN(metapage, lsn);
- MarkBufferDirty(metabuffer);
- }
+ memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
+ PageSetLSN(metapage, lsn);
+ MarkBufferDirty(metabuffer);
if (data->ntuples > 0)
{
@@ -724,12 +726,9 @@ ginRedoDeleteListPages(XLogRecPtr lsn, XLogRecord *record)
return; /* assume index was deleted, nothing to do */
metapage = BufferGetPage(metabuffer);
- if (lsn > PageGetLSN(metapage))
- {
- memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
- PageSetLSN(metapage, lsn);
- MarkBufferDirty(metabuffer);
- }
+ memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
+ PageSetLSN(metapage, lsn);
+ MarkBufferDirty(metabuffer);
/*
* In normal operation, shiftList() takes exclusive lock on all the