Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4738cc3

Browse files
committed
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.
1 parent f64f4c3 commit 4738cc3

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

src/backend/access/gin/ginxlog.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -502,17 +502,19 @@ ginRedoUpdateMetapage(XLogRecPtr lsn, XLogRecord *record)
502502
Page metapage;
503503
Buffer buffer;
504504

505+
/*
506+
* Restore the metapage. This is essentially the same as a full-page image,
507+
* so restore the metapage unconditionally without looking at the LSN, to
508+
* avoid torn page hazards.
509+
*/
505510
metabuffer = XLogReadBuffer(data->node, GIN_METAPAGE_BLKNO, false);
506511
if (!BufferIsValid(metabuffer))
507512
return; /* assume index was deleted, nothing to do */
508513
metapage = BufferGetPage(metabuffer);
509514

510-
if (lsn > PageGetLSN(metapage))
511-
{
512-
memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
513-
PageSetLSN(metapage, lsn);
514-
MarkBufferDirty(metabuffer);
515-
}
515+
memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
516+
PageSetLSN(metapage, lsn);
517+
MarkBufferDirty(metabuffer);
516518

517519
if (data->ntuples > 0)
518520
{
@@ -662,12 +664,9 @@ ginRedoDeleteListPages(XLogRecPtr lsn, XLogRecord *record)
662664
return; /* assume index was deleted, nothing to do */
663665
metapage = BufferGetPage(metabuffer);
664666

665-
if (lsn > PageGetLSN(metapage))
666-
{
667-
memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
668-
PageSetLSN(metapage, lsn);
669-
MarkBufferDirty(metabuffer);
670-
}
667+
memcpy(GinPageGetMeta(metapage), &data->metadata, sizeof(GinMetaPageData));
668+
PageSetLSN(metapage, lsn);
669+
MarkBufferDirty(metabuffer);
671670

672671
/*
673672
* In normal operation, shiftList() takes exclusive lock on all the

0 commit comments

Comments
 (0)