@@ -3712,6 +3712,7 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
3712
3712
bool use_wal ;
3713
3713
BlockNumber nblocks ;
3714
3714
BlockNumber blkno ;
3715
+ PGAlignedBlock buf ;
3715
3716
BufferAccessStrategy bstrategy_src ;
3716
3717
BufferAccessStrategy bstrategy_dst ;
3717
3718
@@ -3730,6 +3731,14 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
3730
3731
if (nblocks == 0 )
3731
3732
return ;
3732
3733
3734
+ /*
3735
+ * Bulk extend the destination relation of the same size as the source
3736
+ * relation before starting to copy block by block.
3737
+ */
3738
+ memset (buf .data , 0 , BLCKSZ );
3739
+ smgrextend (smgropen (dstlocator , InvalidBackendId ), forkNum , nblocks - 1 ,
3740
+ buf .data , true);
3741
+
3733
3742
/* This is a bulk operation, so use buffer access strategies. */
3734
3743
bstrategy_src = GetAccessStrategy (BAS_BULKREAD );
3735
3744
bstrategy_dst = GetAccessStrategy (BAS_BULKWRITE );
@@ -3747,7 +3756,7 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator,
3747
3756
srcPage = BufferGetPage (srcBuf );
3748
3757
3749
3758
/* Use P_NEW to extend the destination relation. */
3750
- dstBuf = ReadBufferWithoutRelcache (dstlocator , forkNum , P_NEW ,
3759
+ dstBuf = ReadBufferWithoutRelcache (dstlocator , forkNum , blkno ,
3751
3760
RBM_NORMAL , bstrategy_dst ,
3752
3761
permanent );
3753
3762
LockBuffer (dstBuf , BUFFER_LOCK_EXCLUSIVE );
0 commit comments