Skip to content

Commit c6a67bb

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 ee0df4d commit c6a67bb

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/backend/access/heap/heapam.c

+10
Original file line numberDiff line numberDiff line change
@@ -7642,6 +7642,16 @@ heap_xlog_newpage(XLogRecPtr lsn, XLogRecord *record)
76427642
}
76437643

76447644
MarkBufferDirty(buffer);
7645+
7646+
/*
7647+
* At the end of crash recovery the init forks of unlogged relations are
7648+
* copied, without going through shared buffers. So we need to force the
7649+
* on-disk state of init forks to always be in sync with the state in
7650+
* shared buffers.
7651+
*/
7652+
if (xlrec->forknum == INIT_FORKNUM)
7653+
FlushOneBuffer(buffer);
7654+
76457655
UnlockReleaseBuffer(buffer);
76467656
}
76477657

src/backend/storage/buffer/bufmgr.c

+21
Original file line numberDiff line numberDiff line change
@@ -2548,6 +2548,27 @@ FlushDatabaseBuffers(Oid dbid)
25482548
}
25492549
}
25502550

2551+
/*
2552+
* Flush a previously, shared or exclusively, locked and pinned buffer to the
2553+
* OS.
2554+
*/
2555+
void
2556+
FlushOneBuffer(Buffer buffer)
2557+
{
2558+
volatile BufferDesc *bufHdr;
2559+
2560+
/* currently not needed, but no fundamental reason not to support */
2561+
Assert(!BufferIsLocal(buffer));
2562+
2563+
Assert(BufferIsPinned(buffer));
2564+
2565+
bufHdr = &BufferDescriptors[buffer - 1];
2566+
2567+
LWLockHeldByMe(bufHdr->content_lock);
2568+
2569+
FlushBuffer(bufHdr, NULL);
2570+
}
2571+
25512572
/*
25522573
* ReleaseBuffer -- release the pin on a buffer
25532574
*/

src/include/storage/bufmgr.h

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ extern void CheckPointBuffers(int flags);
191191
extern BlockNumber BufferGetBlockNumber(Buffer buffer);
192192
extern BlockNumber RelationGetNumberOfBlocksInFork(Relation relation,
193193
ForkNumber forkNum);
194+
extern void FlushOneBuffer(Buffer buffer);
194195
extern void FlushRelationBuffers(Relation rel);
195196
extern void FlushDatabaseBuffers(Oid dbid);
196197
extern void DropRelFileNodeBuffers(RelFileNodeBackend rnode,

0 commit comments

Comments
 (0)