Skip to content

Commit 5b51805

Browse files
committed
Fix bug leading to restoring unlogged relations from empty files.
At the end of crash recovery, unlogged relations are reset to the empty state, using their init fork as the template. The init fork is copied to the main fork without going through shared buffers. Unfortunately WAL replay so far has not necessarily flushed writes from shared buffers to disk at that point. In normal crash recovery, and before the introduction of 'fast promotions' in fd4ced5 / 9.3, the END_OF_RECOVERY checkpoint flushes the buffers out in time. But with fast promotions that's not the case anymore. To fix, force WAL writes targeting the init fork to be flushed immediately (using the new FlushOneBuffer() function). In 9.5+ that flush can centrally be triggered from the code dealing with restoring full page writes (XLogReadBufferForRedoExtended), in earlier releases that responsibility is in the hands of XLOG_HEAP_NEWPAGE's replay function. Backpatch to 9.1, even if this currently is only known to trigger in 9.3+. Flushing earlier is more robust, and it is advantageous to keep the branches similar. Typical symptoms of this bug are errors like 'ERROR: index "..." contains unexpected zero page at block 0' shortly after promoting a node. Reported-By: Thom Brown Author: Andres Freund and Michael Paquier Discussion: 20150326175024.GJ451@alap3.anarazel.de Backpatch: 9.1-
1 parent 2355faa commit 5b51805

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/backend/access/transam/xlogutils.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,15 @@ XLogReadBufferForRedoExtended(XLogReaderState *record,
368368

369369
MarkBufferDirty(*buf);
370370

371+
/*
372+
* At the end of crash recovery the init forks of unlogged relations
373+
* are copied, without going through shared buffers. So we need to
374+
* force the on-disk state of init forks to always be in sync with the
375+
* state in shared buffers.
376+
*/
377+
if (forknum == INIT_FORKNUM)
378+
FlushOneBuffer(*buf);
379+
371380
return BLK_RESTORED;
372381
}
373382
else

src/backend/storage/buffer/bufmgr.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,27 @@ FlushDatabaseBuffers(Oid dbid)
29272927
}
29282928
}
29292929

2930+
/*
2931+
* Flush a previously, shared or exclusively, locked and pinned buffer to the
2932+
* OS.
2933+
*/
2934+
void
2935+
FlushOneBuffer(Buffer buffer)
2936+
{
2937+
volatile BufferDesc *bufHdr;
2938+
2939+
/* currently not needed, but no fundamental reason not to support */
2940+
Assert(!BufferIsLocal(buffer));
2941+
2942+
Assert(BufferIsPinned(buffer));
2943+
2944+
bufHdr = GetBufferDescriptor(buffer - 1);
2945+
2946+
LWLockHeldByMe(bufHdr->content_lock);
2947+
2948+
FlushBuffer(bufHdr, NULL);
2949+
}
2950+
29302951
/*
29312952
* ReleaseBuffer -- release the pin on a buffer
29322953
*/

src/include/storage/bufmgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ extern void CheckPointBuffers(int flags);
169169
extern BlockNumber BufferGetBlockNumber(Buffer buffer);
170170
extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
171171
ForkNumber forkNum);
172+
extern void FlushOneBuffer(Buffer buffer);
172173
extern void FlushRelationBuffers(Relation rel);
173174
extern void FlushDatabaseBuffers(Oid dbid);
174175
extern void DropRelFileNodeBuffers(RelFileNodeBackend rnode,

0 commit comments

Comments
 (0)