Skip to content

Commit 560bb56

Browse files
committed
Fix memory leak and inefficiency in CREATE DATABASE ... STRATEGY WAL_LOG
RelationCopyStorageUsingBuffer() did not free the strategies used to access the source / target relation. They memory was released at the end of the transaction, but when using a template database with a lot of relations, the temporary leak can become big prohibitively big. RelationCopyStorageUsingBuffer() acquired the buffer for the target relation with RBM_NORMAL, therefore requiring a read of a block guaranteed to be zero. Use RBM_ZERO_AND_LOCK instead. Reviewed-by: Robert Haas <robertmhaas@gmail.com> Discussion: https://postgr.es/m/20230321070113.o2vqqxogjykwgfrr@awork3.anarazel.de Backpatch: 15-, where STRATEGY WAL_LOG was introduced
1 parent a70e6e4 commit 560bb56

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3756,11 +3756,9 @@ RelationCopyStorageUsingBuffer(RelFileNode srcnode,
37563756
LockBuffer(srcBuf, BUFFER_LOCK_SHARE);
37573757
srcPage = BufferGetPage(srcBuf);
37583758

3759-
/* Use P_NEW to extend the destination relation. */
37603759
dstBuf = ReadBufferWithoutRelcache(dstnode, forkNum, blkno,
3761-
RBM_NORMAL, bstrategy_dst,
3760+
RBM_ZERO_AND_LOCK, bstrategy_dst,
37623761
permanent);
3763-
LockBuffer(dstBuf, BUFFER_LOCK_EXCLUSIVE);
37643762
dstPage = BufferGetPage(dstBuf);
37653763

37663764
START_CRIT_SECTION();
@@ -3778,6 +3776,9 @@ RelationCopyStorageUsingBuffer(RelFileNode srcnode,
37783776
UnlockReleaseBuffer(dstBuf);
37793777
UnlockReleaseBuffer(srcBuf);
37803778
}
3779+
3780+
FreeAccessStrategy(bstrategy_src);
3781+
FreeAccessStrategy(bstrategy_dst);
37813782
}
37823783

37833784
/* ---------------------------------------------------------------------

0 commit comments

Comments
 (0)