Fix checkpointer shared memory allocation
authorAlexander Korotkov <[email protected]>
Thu, 7 Aug 2025 11:29:02 +0000 (14:29 +0300)
committerAlexander Korotkov <[email protected]>
Thu, 7 Aug 2025 11:29:02 +0000 (14:29 +0300)
Use Min(NBuffers, MAX_CHECKPOINT_REQUESTS) instead of NBuffers in
CheckpointerShmemSize() to match the actual array size limit set in
CheckpointerShmemInit().  This prevents wasting shared memory when
NBuffers > MAX_CHECKPOINT_REQUESTS.  Also, fix the comment.

Reported-by: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/1439188.1754506714%40sss.pgh.pa.us
Author: Xuneng Zhou <[email protected]>
Co-authored-by: Alexander Korotkov <[email protected]>
src/backend/postmaster/checkpointer.c

index 8490148a47d52a625459569a74f51dcb8d4040ba..e84e8663e966bd33614a392d8a87dc33427e5355 100644 (file)
@@ -953,11 +953,14 @@ CheckpointerShmemSize(void)
    Size        size;
 
    /*
-    * Currently, the size of the requests[] array is arbitrarily set equal to
-    * NBuffers.  This may prove too large or small ...
+    * The size of the requests[] array is arbitrarily set equal to NBuffers.
+    * But there is a cap of MAX_CHECKPOINT_REQUESTS to prevent accumulating
+    * too many checkpoint requests in the ring buffer.
     */
    size = offsetof(CheckpointerShmemStruct, requests);
-   size = add_size(size, mul_size(NBuffers, sizeof(CheckpointerRequest)));
+   size = add_size(size, mul_size(Min(NBuffers,
+                                      MAX_CHECKPOINT_REQUESTS),
+                                  sizeof(CheckpointerRequest)));
 
    return size;
 }