<div dir="ltr">Currently pgpool does not contains any local memory management API, it relies on standard C Library memory allocation functions for acquiring and releasing the memory. Absence of a memory manager especially<br>
<br>with the growth of source code and feature makes it vulnerable to serious memory issues/leakages. A proper implementation of memory manager for pgpool can also aid in debugging and could improve the overall<br><br>performance of the system. Postgres already contains the memory manager API and this document proposes to adopt the memory management used by PG in pgpool.<br>
<br><br><b>Some notes about the PG&#39;s memory management API.<br></b><br><br>The core of memory management in the Postgres is a MemoryContext and<br>Postgres do almost all memory allocation in the &quot;memory contexts&quot;, which has<br>
a defined lifecycle.<br><br><b>The basic operations on a memory context are:<br></b>* create a context<br>* allocate a chunk of memory within a context (equivalent of standard C library&#39;s malloc())<br>* delete a context (including freeing all the memory allocated therein)<br>
* reset a context (free all memory allocated in the context, but not the context object itself)<br><br>Given a chunk of memory previously allocated from a context, one can free it or reallocate it larger or smaller (corresponding to standard library&#39;s free() and realloc() routines). <div>
These operations return memory to or get more memory from the same context the chunk was originally allocated in.<br>At all times there is a &quot;current&quot; context denoted by the CurrentMemoryContext global variable.  The backend macro palloc()  implicitly allocates space in that context.  The MemoryContextSwitchTo() operation selects a new current context (and returns the previous context, so that the caller can restore the previous context before exiting).<br>
<br>The main advantage of memory contexts over plain use of malloc/free is that the entire contents of a memory context can be freed easily, this saves the trouble of freeing individual chunks of memory. </div><div>This is both faster and more reliable than per-chunk bookkeeping. </div>
<div>PG already use this fact to clean up at transaction end, The memory is reclaimed by resetting all the active contexts.<br><br><b>Some Notes About the PG memory API(palloc) Versus Standard C Library</b><br><br></div><div>
The behaviour of palloc and friends is similar to the standard C library&#39;s malloc and friends, but there are some deliberate differences too.  Here are some notes to clarify the behaviour.<br><br>* If out of memory, palloc and repalloc exit via elog(ERROR).  They never return NULL, and it is not necessary or useful to test for such a result.<br>
<br>* palloc(0) is explicitly a valid operation.  It does not return a NULL pointer, but a valid chunk of which no bytes may be used.  (However, the chunk might later be repalloc&#39;d larger; it can also be pfree&#39;d without error.)  disallowed palloc(0).  It seems more consistent to allow it, however.) Similarly, repalloc allows realloc&#39;ing to zero size.</div>
<div><br>* pfree and repalloc do not accept a NULL pointer.<br><br><b>Using the palloc API in pgpool</b><br><br>As mentioned above, I propose to following the memory management approach used in PG in PGPOOL. I am planning to do the following to hook the Postgres memory management API in pgpool.<br>
<br><br>1- Copy the the memory management source files from PGSRC/src/backend/utils/mmgr/ (mcxt.c and aset.c) int the pgpool source.<br><br>2- Change the behaviour in case of allocation errors, as currently pgpool does not contains elog API.<br>
<br>3- Create a single TopMemoryContext global memory context.<br><br>4- Replace all malloc, free and related memory management functions calls with palloc and friends.<br><br>4- Stabilise the code.<br><br>5- Make more memory contexts, inline with the lifecycle of components of pgpool<br>
<br>6- Stabilise the code.<br><div class="gmail_extra" style="font-family:arial,sans-serif;font-size:13px"><div class="adm"><div id="q_140abaf605f0e278_15" class="h4"></div></div></div></div><div><br></div><div><div style="font-family:arial,sans-serif;font-size:13px">
<b>References</b></div><div style="font-family:arial,sans-serif;font-size:13px">PGSRC/src/backend/utils/mmgr/README </div></div><div style="font-family:arial,sans-serif;font-size:13px"><br></div><div style="font-family:arial,sans-serif;font-size:13px">
Thanks</div><div style="font-family:arial,sans-serif;font-size:13px">Muhammad Usama</div></div>