<div dir="ltr">Hi Tatsuo<br><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Aug 24, 2013 at 1:47 AM, Tatsuo Ishii <span dir="ltr">&lt;<a href="mailto:ishii@postgresql.org" target="_blank">ishii@postgresql.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">Hi Usama,<br>
<br>
Actually pgpool already has the memory manger, palloc and friends.<br>
See src/parser/pool_memory.c. It&#39;s basically a subset of PostgreSQL&#39;s<br>
memory manager.<br></blockquote><div><br></div><div>Thanks for clearing it out. Actually there are lot of instances of C API memory functions</div><div>in the code like malloc which lead me to the assumption of missing memory manager in most</div>
<div>parts of pgpool code, and my assumption was only parser API and session context</div><div>is using the pool_memory API. </div><div><div><br></div><div>usama$ grep -R malloc pgpool2/ | wc -l</div><div>     272</div></div>
<div>  </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
Besides that, I think complete implementation of memory manager<br>
requires the exception manager (the one implemented in elog of<br>
PostgreSQL using setjmp/longjmp) as well. The current memory manager<br>
of pgpool does not have it. The exception manager will not only<br>
greatly reduce the amount of the source code but improves the<br>
reliability of pgpool. It&#39;s already proved in PostgreSQL. Introducing<br>
the exception manager has already been in Postgres-XC. So I think it&#39;s<br>
not impossible. </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
So my thought is, if you are not going to introduce the exception<br>
manager of PostgreSQL as well, I think its not worth to touch the<br>
current implementation of memory manager.<br></blockquote><div><br></div><div>Totally agreed and importing the Postgres&#39;s exception manager is also in my TODO list</div><div>and now I will prioritise it over importing the complete set of memory manager.</div>
<div><br></div><div>Thanks</div><div>Muhammad Usama</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

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