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

Commit bdf7db8

Browse files
committed
In generic WAL application and replay, ensure page "hole" is always zero.
The previous coding could allow the contents of the "hole" between pd_lower and pd_upper to diverge during replay from what it had been when the update was originally applied. This would pose a problem if checksums were in use, and in any case would complicate forensic comparisons between master and slave servers. So force the "hole" to contain zeroes, both at initial application of a generically-logged action, and at replay. Alexander Korotkov, adjusted slightly by me
1 parent 813b456 commit bdf7db8

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

src/backend/access/transam/generic_xlog.c

+37-3
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,31 @@ GenericXLogFinish(GenericXLogState *state)
338338
{
339339
PageData *pageData = &state->pages[i];
340340
Page page;
341+
PageHeader pageHeader;
341342

342343
if (BufferIsInvalid(pageData->buffer))
343344
continue;
344345

345346
page = BufferGetPage(pageData->buffer, NULL, NULL,
346347
BGP_NO_SNAPSHOT_TEST);
348+
pageHeader = (PageHeader) pageData->image;
347349

348350
if (pageData->fullImage)
349351
{
350-
/* A full page image does not require anything special */
351-
memcpy(page, pageData->image, BLCKSZ);
352+
/*
353+
* A full-page image does not require us to supply any xlog
354+
* data. Just apply the image, being careful to zero the
355+
* "hole" between pd_lower and pd_upper in order to avoid
356+
* divergence between actual page state and what replay would
357+
* produce.
358+
*/
359+
memcpy(page, pageData->image, pageHeader->pd_lower);
360+
memset(page + pageHeader->pd_lower, 0,
361+
pageHeader->pd_upper - pageHeader->pd_lower);
362+
memcpy(page + pageHeader->pd_upper,
363+
pageData->image + pageHeader->pd_upper,
364+
BLCKSZ - pageHeader->pd_upper);
365+
352366
XLogRegisterBuffer(i, pageData->buffer,
353367
REGBUF_FORCE_IMAGE | REGBUF_STANDARD);
354368
}
@@ -359,7 +373,15 @@ GenericXLogFinish(GenericXLogState *state)
359373
* associated with this page.
360374
*/
361375
computeDelta(pageData, page, (Page) pageData->image);
362-
memcpy(page, pageData->image, BLCKSZ);
376+
377+
/* Apply the image, with zeroed "hole" as above */
378+
memcpy(page, pageData->image, pageHeader->pd_lower);
379+
memset(page + pageHeader->pd_lower, 0,
380+
pageHeader->pd_upper - pageHeader->pd_lower);
381+
memcpy(page + pageHeader->pd_upper,
382+
pageData->image + pageHeader->pd_upper,
383+
BLCKSZ - pageHeader->pd_upper);
384+
363385
XLogRegisterBuffer(i, pageData->buffer, REGBUF_STANDARD);
364386
XLogRegisterBufData(i, pageData->delta, pageData->deltaLen);
365387
}
@@ -395,6 +417,7 @@ GenericXLogFinish(GenericXLogState *state)
395417
BGP_NO_SNAPSHOT_TEST),
396418
pageData->image,
397419
BLCKSZ);
420+
/* We don't worry about zeroing the "hole" in this case */
398421
MarkBufferDirty(pageData->buffer);
399422
}
400423
END_CRIT_SECTION();
@@ -473,6 +496,7 @@ generic_redo(XLogReaderState *record)
473496
if (action == BLK_NEEDS_REDO)
474497
{
475498
Page page;
499+
PageHeader pageHeader;
476500
char *blockDelta;
477501
Size blockDeltaSize;
478502

@@ -481,6 +505,16 @@ generic_redo(XLogReaderState *record)
481505
blockDelta = XLogRecGetBlockData(record, block_id, &blockDeltaSize);
482506
applyPageRedo(page, blockDelta, blockDeltaSize);
483507

508+
/*
509+
* Since the delta contains no information about what's in the
510+
* "hole" between pd_lower and pd_upper, set that to zero to
511+
* ensure we produce the same page state that application of the
512+
* logged action by GenericXLogFinish did.
513+
*/
514+
pageHeader = (PageHeader) page;
515+
memset(page + pageHeader->pd_lower, 0,
516+
pageHeader->pd_upper - pageHeader->pd_lower);
517+
484518
PageSetLSN(page, lsn);
485519
MarkBufferDirty(buffers[block_id]);
486520
}

0 commit comments

Comments
 (0)