From 8a71e5dd10ff65d250815dc17f8f64212c2e57b0 Mon Sep 17 00:00:00 2001 From: Dilip Kumar Date: Fri, 5 Aug 2022 11:25:23 +0530 Subject: [PATCH v2 3/3] Optimize copy storage from source to destination Instead of extending block at a time directly bulkextend the destination relation and then just perform the block level copy. --- src/backend/storage/buffer/bufmgr.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/backend/storage/buffer/bufmgr.c b/src/backend/storage/buffer/bufmgr.c index b488306..b7df980 100644 --- a/src/backend/storage/buffer/bufmgr.c +++ b/src/backend/storage/buffer/bufmgr.c @@ -3710,6 +3710,7 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator, Page srcPage; Page dstPage; bool use_wal; + char buffer[BLCKSZ]; BlockNumber nblocks; BlockNumber blkno; BufferAccessStrategy bstrategy_src; @@ -3730,6 +3731,14 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator, if (nblocks == 0) return; + /* + * Bulk extend the destination relation of the same size as the source + * relation before starting to copy block by block. + */ + memset(buffer, 0, BLCKSZ); + smgrextend(smgropen(dstlocator, InvalidBackendId), forkNum, nblocks - 1, + buffer, true); + /* This is a bulk operation, so use buffer access strategies. */ bstrategy_src = GetAccessStrategy(BAS_BULKREAD); bstrategy_dst = GetAccessStrategy(BAS_BULKWRITE); @@ -3748,7 +3757,7 @@ RelationCopyStorageUsingBuffer(RelFileLocator srclocator, srcPage = BufferGetPage(srcBuf); /* Use P_NEW to extend the destination relation. */ - dstBuf = ReadBufferWithoutRelcache(dstlocator, forkNum, P_NEW, + dstBuf = ReadBufferWithoutRelcache(dstlocator, forkNum, blkno, RBM_NORMAL, bstrategy_dst, permanent); LockBuffer(dstBuf, BUFFER_LOCK_EXCLUSIVE); -- 1.8.3.1