Skip to content

Commit 5c1ed0a

Browse files
Reduce memory block size for decoded tuple storage to 8kB.
Commit a4ccc1c introduced the Generation Context and modified the logical decoding process to use a Generation Context with a fixed block size of 8MB for storing tuple data decoded during logical decoding (i.e., rb->tup_context). Several reports have indicated that the logical decoding process can be terminated due to out-of-memory (OOM) situations caused by excessive memory usage in rb->tup_context. This issue can occur when decoding a workload involving several concurrent transactions, including a long-running transaction that modifies tuples. By design, the Generation Context does not free a memory block until all chunks within that block are released. Consequently, if tuples modified by the long-running transaction are stored across multiple memory blocks, these blocks remain allocated until the long-running transaction completes, leading to substantial memory fragmentation. The memory usage during logical decoding, tracked by rb->size, does not account for memory fragmentation, resulting in potentially much higher memory consumption than the value of the logical_decoding_work_mem parameter. Various improvement strategies were discussed in the relevant thread. This change reduces the block size of the Generation Context used in rb->tup_context from 8MB to 8kB. This modification significantly decreases the likelihood of substantial memory fragmentation occurring and is relatively straightforward to backport. Performance testing across multiple platforms has confirmed that this change will not introduce any performance degradation that would impact actual operation. Backport to all supported branches. Reported-by: Alex Richman, Michael Guissine, Avi Weinberg Reviewed-by: Amit Kapila, Fujii Masao, David Rowley Tested-by: Hayato Kuroda, Shlok Kyal Discussion: https://postgr.es/m/CAD21AoBTY1LATZUmvSXEssvq07qDZufV4AF-OHh9VD2pC0VY2A%40mail.gmail.com Backpatch-through: 12
1 parent 4ca708e commit 5c1ed0a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/backend/replication/logical/reorderbuffer.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,17 @@ ReorderBufferAllocate(void)
328328
SLAB_DEFAULT_BLOCK_SIZE,
329329
sizeof(ReorderBufferTXN));
330330

331+
/*
332+
* To minimize memory fragmentation caused by long-running transactions
333+
* with changes spanning multiple memory blocks, we use a single
334+
* fixed-size memory block for decoded tuple storage. The performance
335+
* testing showed that the default memory block size maintains logical
336+
* decoding performance without causing fragmentation due to concurrent
337+
* transactions.
338+
*/
331339
buffer->tup_context = GenerationContextCreate(new_ctx,
332340
"Tuples",
333-
SLAB_LARGE_BLOCK_SIZE);
341+
SLAB_DEFAULT_BLOCK_SIZE);
334342

335343
hash_ctl.keysize = sizeof(TransactionId);
336344
hash_ctl.entrysize = sizeof(ReorderBufferTXNByIdEnt);

0 commit comments

Comments
 (0)