Skip to content

Commit 692df42

Browse files
committed
Fix data-corruption hazard in WAL-logged CREATE DATABASE.
RelationCopyStorageUsingBuffer thought it could skip copying empty pages, but of course that does not work at all, because subsequent blocks will be out of place. Also fix it to acquire share lock on the source buffer. It *might* be safe to not do that, but it's not very certain, and I don't think this code deserves any benefit of the doubt. Dilip Kumar, per complaint from me Discussion: https://postgr.es/m/3679800.1659654066@sss.pgh.pa.us
1 parent 922a8fa commit 692df42

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3741,23 +3741,19 @@ RelationCopyStorageUsingBuffer(Relation src, Relation dst, ForkNumber forkNum,
37413741
srcBuf = ReadBufferWithoutRelcache(src->rd_locator, forkNum, blkno,
37423742
RBM_NORMAL, bstrategy_src,
37433743
permanent);
3744+
LockBuffer(srcBuf, BUFFER_LOCK_SHARE);
37443745
srcPage = BufferGetPage(srcBuf);
3745-
if (PageIsNew(srcPage) || PageIsEmpty(srcPage))
3746-
{
3747-
ReleaseBuffer(srcBuf);
3748-
continue;
3749-
}
37503746

37513747
/* Use P_NEW to extend the destination relation. */
37523748
dstBuf = ReadBufferWithoutRelcache(dst->rd_locator, forkNum, P_NEW,
37533749
RBM_NORMAL, bstrategy_dst,
37543750
permanent);
37553751
LockBuffer(dstBuf, BUFFER_LOCK_EXCLUSIVE);
3752+
dstPage = BufferGetPage(dstBuf);
37563753

37573754
START_CRIT_SECTION();
37583755

37593756
/* Copy page data from the source to the destination. */
3760-
dstPage = BufferGetPage(dstBuf);
37613757
memcpy(dstPage, srcPage, BLCKSZ);
37623758
MarkBufferDirty(dstBuf);
37633759

@@ -3768,7 +3764,7 @@ RelationCopyStorageUsingBuffer(Relation src, Relation dst, ForkNumber forkNum,
37683764
END_CRIT_SECTION();
37693765

37703766
UnlockReleaseBuffer(dstBuf);
3771-
ReleaseBuffer(srcBuf);
3767+
UnlockReleaseBuffer(srcBuf);
37723768
}
37733769
}
37743770

0 commit comments

Comments
 (0)