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