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

Commit 811203d

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 6390bc7 commit 811203d

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
@@ -3742,23 +3742,19 @@ RelationCopyStorageUsingBuffer(Relation src, Relation dst, ForkNumber forkNum,
37423742
srcBuf = ReadBufferWithoutRelcache(src->rd_node, forkNum, blkno,
37433743
RBM_NORMAL, bstrategy_src,
37443744
permanent);
3745+
LockBuffer(srcBuf, BUFFER_LOCK_SHARE);
37453746
srcPage = BufferGetPage(srcBuf);
3746-
if (PageIsNew(srcPage) || PageIsEmpty(srcPage))
3747-
{
3748-
ReleaseBuffer(srcBuf);
3749-
continue;
3750-
}
37513747

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

37583755
START_CRIT_SECTION();
37593756

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

@@ -3769,7 +3765,7 @@ RelationCopyStorageUsingBuffer(Relation src, Relation dst, ForkNumber forkNum,
37693765
END_CRIT_SECTION();
37703766

37713767
UnlockReleaseBuffer(dstBuf);
3772-
ReleaseBuffer(srcBuf);
3768+
UnlockReleaseBuffer(srcBuf);
37733769
}
37743770
}
37753771

0 commit comments

Comments
 (0)