commit a78551a6ad38ee3b9f252ae1556259062c3d689d
Author: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com>
Date:   Tue Feb 10 11:39:58 2026 +0530

    fixup! Memory and address space management for buffer resizing

diff --git a/src/backend/port/sysv_shmem.c b/src/backend/port/sysv_shmem.c
index c766f5c44fd..5ce3e28b41b 100644
--- a/src/backend/port/sysv_shmem.c
+++ b/src/backend/port/sysv_shmem.c
@@ -109,34 +109,19 @@ typedef enum
 volatile bool delay_shmem_resize = false;
 
 /*
- * Anonymous mapping layout we use looks like this:
+ * Anonymous mapping layout:
  *
- * 00400000-00c2a000 r-xp 			/bin/postgres
- * ...
- * 3f526000-3f590000 rw-p 			[heap]
- * 7fbd827fe000-7fbd8bdde000 rw-s 	/memfd:main (deleted)
- * 7fbd8bdde000-7fbe82800000 ---s 	/memfd:main (deleted)
- * 7fbe82800000-7fbe90670000 r--p 	/usr/lib/locale/locale-archive
- * 7fbe90800000-7fbe90941000 r-xp 	/usr/lib64/libstdc++.so.6.0.34
- * ...
- *
- * We need to place shared memory mappings in such a way, that there will be
- * gaps between them in the address space. Those gaps have to be large enough
- * to resize the mapping up to certain size, without counting towards the total
- * memory consumption.
+ * We need to place shared memory mappings in such a way, that there will be gaps
+ * between them in the address space. Those gaps have to be large enough to
+ * resize shared memory mapped to this address space upto a predefined maximum
+ * size, without counting towards the total memory consumption.
  *
  * To achieve this, for each shared memory segment we first create an anonymous
- * file of specified size using memfd_create, which will accomodate actual
- * shared memory mapping content. It is represented by the first /memfd:main
- * with rw permissions. Then we create a mapping for this file using mmap, with
- * size much larger than required and flags PROT_NONE (allows to make sure the
- * reserved space will not be used) and MAP_NORESERVE (prevents the space from
+ * file using memfd_create, which will accomodate actual shared memory mapping
+ * content. Then we create a mapping for this file using mmap, with size much
+ * larger than required with flag MAP_NORESERVE which prevents the space from
  * being counted against memory limits). The mapping serves as an address space
- * reservation, into which shared memory segment can be extended and is
- * represented by the second /memfd:main with no permissions.
- *
- * The reserved space for buffer manager related segments is calculated based on
- * MaxNBuffers.
+ * reservation, into which shared memory segment can be extended.
  */
 
 PGUsedShmemInfo UsedShmemInfo[NUM_MEMORY_MAPPINGS];
@@ -759,7 +744,7 @@ round_off_mapping_sizes_for_hugepages(MemoryMappingSizes *mapping, int hugepages
 		return;
 
 	if (mapping->shmem_req_size % hugepagesize != 0)
-		mapping->shmem_req_size += add_size(mapping->shmem_req_size,
+		mapping->shmem_req_size = add_size(mapping->shmem_req_size,
 											hugepagesize - (mapping->shmem_req_size % hugepagesize));
 
 	if (mapping->shmem_reserved % hugepagesize != 0)
@@ -889,6 +874,11 @@ CreateAnonymousSegment(int segment_id, MemoryMappingSizes *mapping)
 						 "\"max_connections\".",
 						 mapping->shmem_req_size) : 0));
 	}
+	/*
+	 * TODO: this step seems to be causing almost 30s delay when starting up on
+	 * slower machines with large shared buffer size. (See
+	 * https://www.postgresql.org/message-id/CAKZiRmwxVqEbp7JgOed=BCT6cq8RNuHk3N0vuwro65Tsw9E8NA@mail.gmail.com)
+	 */
 	shmem_fallocate(anonshmem->fd, segname, mapping->shmem_req_size, FATAL);
 
 	anonshmem->addr = ptr;
