Hi Melih, Jeff, I tested the v4 patch along with the memory statistics reporting patch from [1] thread.
After running a plpgsql procedure periodically and querying its memory consumption, I see various child contexts of CacheMemoryContext created as shown in the image below or link [2]. [image: Memorycontext.drawio.png] Observations: 1. While there are a number of child contexts like index info of RelCacheContext, CatCacheContext does not have any children. 2. While there is a bunch of used memory in RelCacheContext and CatCacheContext, SPICacheContext and PlanCacheContext do not have any allocations of their own and serve only as parents for SPI and CachedPlan related contexts respectively. Having reviewed the discussion regarding potential fragmentation issues caused by creating a large number of new contexts in each backend, I would like to take a step back and better understand the motivation behind separating these contexts. IIUC, segregating cache memory allocations into RelCacheContext and CatCacheContext allows for grouping a large number of memory allocations under a common context, which, in turn, aids in monitoring memory consumption. However, I believe this reasoning does not apply to SPICacheContext and PlanCacheContext, as these contexts do not have any allocations of their own. How, then, does separating these contexts from CacheMemoryContext improve monitoring? Additionally, IIUC, these contexts are created as long-lived contexts, so they are not designed to optimize deletion of all their children via MemoryContextDelete on the parent. > Attached a separate patch to change initial sizes for relcache and > catcache contexts as they grow > > large from the start. This was suggested in the thread previously [1]. > Also changed CacheMemoryContext to use ALLOCSET_START_SMALL_SIZES, so it > starts from 1KB. > Applying the same change to use ALLOCSET_START_SMALL_SIZES would be beneficial for SPICacheContext and PlanCacheContext contexts as well. On documentation front, the newly added contexts would require a mention in src/backend/utils/mmgr/README. [1] PostgreSQL: Enhancing Memory Context Statistics Reporting <https://www.postgresql.org/message-id/flat/cah2l28v8mc9hdt8qosj8trmkau_8fm_hks41neo9-6zakuz...@mail.gmail.com> [2] Memorycontext.png - Page-1 <https://viewer.diagrams.net/index.html?tags=%7B%7D&lightbox=1&highlight=0000ff&edit=_blank&layers=1&nav=1&title=Memorycontext.png#R%3Cmxfile%3E%3Cdiagram%20id%3D%22prtHgNgQTEPvFCAcTncT%22%20name%3D%22Page-1%22%3E7Ztbc9o4FMc%2FDY9kLF%2FhMSHptjNNhy17aZ92hBFYHWN5ZRFgP%2F1KtoSvgCE2mAwvrXWs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