Skip to content

Commit bc7ab30

Browse files
committed
In XLogReadBufferExtended, don't assume P_NEW yields consecutive pages.
In a database that's not yet reached consistency, it's possible that some segments of a relation are not full-size but are not the last ones either. Because of the way smgrnblocks() works, asking for a new page with P_NEW will fill in the last not-full-size segment --- and if that makes it full size, the apparent EOF of the relation will increase by more than one page, so that the next P_NEW request will yield a page past the next consecutive one. This breaks the relation-extension logic in XLogReadBufferExtended, possibly allowing a page update to be applied to some page far past where it was intended to go. This appears to be the explanation for reports of table bloat on replication slaves compared to their masters, and probably explains some corrupted-slave reports as well. Fix the loop to check the page number it actually got, rather than merely Assert()'ing that dead reckoning got it to the desired place. AFAICT, there are no other places that make assumptions about exactly which page they'll get from P_NEW. Problem identified by Greg Stark, though this is not the same as his proposed patch. It's been like this for a long time, so back-patch to all supported branches.
1 parent efb425a commit bc7ab30

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/backend/access/transam/xlogutils.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,15 +337,21 @@ XLogReadBufferExtended(RelFileNode rnode, ForkNumber forknum,
337337
/* we do this in recovery only - no rel-extension lock needed */
338338
Assert(InRecovery);
339339
buffer = InvalidBuffer;
340-
while (blkno >= lastblock)
340+
do
341341
{
342342
if (buffer != InvalidBuffer)
343343
ReleaseBuffer(buffer);
344344
buffer = ReadBufferWithoutRelcache(rnode, forknum,
345345
P_NEW, mode, NULL);
346-
lastblock++;
347346
}
348-
Assert(BufferGetBlockNumber(buffer) == blkno);
347+
while (BufferGetBlockNumber(buffer) < blkno);
348+
/* Handle the corner case that P_NEW returns non-consecutive pages */
349+
if (BufferGetBlockNumber(buffer) != blkno)
350+
{
351+
ReleaseBuffer(buffer);
352+
buffer = ReadBufferWithoutRelcache(rnode, forknum, blkno,
353+
mode, NULL);
354+
}
349355
}
350356

351357
if (mode == RBM_NORMAL)

0 commit comments

Comments
 (0)