Fix data-corruption hazard in WAL-logged CREATE DATABASE.
authorTom Lane
Sat, 6 Aug 2022 15:50:23 +0000 (11:50 -0400)
committerTom Lane
Sat, 6 Aug 2022 15:50:23 +0000 (11:50 -0400)
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

src/backend/storage/buffer/bufmgr.c

index c532ca716d13d1f2207fc83b4521b1af83084807..8aabf5991b0e78017823ffc25789150235d5e7f6 100644 (file)
@@ -3742,23 +3742,19 @@ RelationCopyStorageUsingBuffer(Relation src, Relation dst, ForkNumber forkNum,
        srcBuf = ReadBufferWithoutRelcache(src->rd_node, forkNum, blkno,
                                           RBM_NORMAL, bstrategy_src,
                                           permanent);
+       LockBuffer(srcBuf, BUFFER_LOCK_SHARE);
        srcPage = BufferGetPage(srcBuf);
-       if (PageIsNew(srcPage) || PageIsEmpty(srcPage))
-       {
-           ReleaseBuffer(srcBuf);
-           continue;
-       }
 
        /* Use P_NEW to extend the destination relation. */
        dstBuf = ReadBufferWithoutRelcache(dst->rd_node, forkNum, P_NEW,
                                           RBM_NORMAL, bstrategy_dst,
                                           permanent);
        LockBuffer(dstBuf, BUFFER_LOCK_EXCLUSIVE);
+       dstPage = BufferGetPage(dstBuf);
 
        START_CRIT_SECTION();
 
        /* Copy page data from the source to the destination. */
-       dstPage = BufferGetPage(dstBuf);
        memcpy(dstPage, srcPage, BLCKSZ);
        MarkBufferDirty(dstBuf);
 
@@ -3769,7 +3765,7 @@ RelationCopyStorageUsingBuffer(Relation src, Relation dst, ForkNumber forkNum,
        END_CRIT_SECTION();
 
        UnlockReleaseBuffer(dstBuf);
-       ReleaseBuffer(srcBuf);
+       UnlockReleaseBuffer(srcBuf);
    }
 }