I spent a bit of time looking into this. Using current HEAD, I instrumented CreateSharedMemoryAndSemaphores to log the size estimates returned by the various estimation subroutines, plus the shared memory space actually consumed (i.e, the change in ShmemSegHdr->freeoffset) by the various shared memory initialization functions. There were only two estimates that were way off: LockShmemSize requested 1651771 more bytes than InitLocks actually consumed, and PredicateLockShmemSize requested 167058 more bytes than InitPredicateLocks consumed. I believe both of those are intentional, reflecting space that may be eaten by the lock tables later.
Meanwhile, looking at ShmemSegHdr->freeoffset vs ShmemSegHdr->totalsize, the actual remaining shmem space after postmaster startup is 1919488 bytes. (Running the core regression tests doesn't consume any of that remaining space, btw.) Subtracting the extra lock-table space, we have 100659 bytes to spare, which is as near as makes no difference to the intended slop of 100000 bytes. My conclusion is that we don't need to do anything, indeed the proposed changes will probably just lead to overestimation. It's certainly possible that there's something amiss somewhere. These numbers were all taken with out-of-the-box configuration, so it could be that changing some postgresql.conf entries would expose that some module is not scaling its request correctly. Also, I don't have any extensions loaded, so this proves nothing about the correctness of any of those. But it appears to me that the general scheme is working perfectly fine, so we do not need to complicate it. regards, tom lane
ApplyLauncherShmemInit used 512 ApplyLauncherShmemSize est 424 AsyncShmemInit used 69376 AsyncShmemSize est 69308 AutoVacuumShmemInit used 5376 AutoVacuumShmemSize est 5368 BTreeShmemInit used 1536 BTreeShmemSize est 1476 CreateSharedBackendStatus used 203520 BackendStatusShmemSize est 203304 BackgroundWorkerShmemInit used 4608 BackgroundWorkerShmemSize est 4496 InitBufferPool used 137047040 BufferShmemSize est 137044560 CLOGShmemInit used 529152 CLOGShmemSize est 529152 CheckpointerShmemInit used 393344 CheckpointerShmemSize est 393280 CommitTsShmemInit used 133760 CommitTsShmemSize est 133600 CreateLWLocks used 26880 LWLockShmemSize est 26756 InitLocks used 1394560 LockShmemSize est 3046331 <-- 1651771 not consumed MultiXactShmemInit used 201600 MultiXactShmemSize est 201412 PGReserveSemaphores used 16128 PGSemaphoreShmemSize est 16128 PMSignalShmemInit used 1024 PMSignalShmemSize est 1024 InitPredicateLocks used 2396160 PredicateLockShmemSize est 2563218 <-- 167058 not consumed CreateSharedProcArray used 40320 ProcArrayShmemSize est 40178 InitProcGlobal used 112128 ProcGlobalShmemSize est 111883 ProcSignalShmemInit used 9344 ProcSignalShmemSize est 9296 ReplicationOriginShmemInit used 640 ReplicationOriginShmemSize est 568 ReplicationSlotsShmemInit used 2688 ReplicationSlotsShmemSize est 2640 InitShmemIndex used 10624 SHMEM_INDEX hash est 13040 CreateSharedInvalidationState used 69504 SInvalShmemSize est 69464 SUBTRANSShmemInit used 267008 SUBTRANSShmemSize est 267008 SnapMgrInit used 128 SnapMgrShmemSize est 68 SyncScanShmemInit used 768 SyncScanShmemSize est 656 TwoPhaseShmemInit used 128 TwoPhaseShmemSize est 16 WalRcvShmemInit used 2304 WalRcvShmemSize est 2248 WalSndShmemInit used 1152 WalSndShmemSize est 1040 XLOGShmemInit used 4208768 XLOGShmemSize est 4208280 dsm_postmaster_startup used 0 dsm_estimate_size est 0 dsm_shmem_init used 0 final roundup est 3602 total_addin_request est 0