<html><head><style> p {margin-top:0px;margin-bottom:0px;} </style></head>
<body><div style="font-size:12px; font-family:굴림,굴림체,Gulim,Baekmuk Dotum,Undotum,Apple Gothic,Latin font,sans-serif;">
<table border=0 width=100% style='margin 0 auto;background: ;' cellpadding=0 cellspacing=0>
<tr>
<td valign=top style='padding:8pt;'>
<div class="tx-hanmail-content-wrapper" style="color: rgb(51, 51, 51); font-family: 돋움; font-size: 10pt; background-color: transparent; line-height: 1.5;"><p><span></span>Hi, hackers !</p><p><br></p><p>TopMemoryContext is created with malloc in MemoryContextInit().</p><p>All the other MemoryContexts such as ErrContext, MessageContext are allocated with palloc in a block of TopMemoryContext.</p><p>So, to release all the allocated memories, TopMemoryContext should be freed with MemoryContextReset and freeing itself.</p><p>But, I cannot find the place where it happens in source files.</p><p><br></p><p>To check if TopMemoryContext is released before exiting a process, I did some work like below and checked the result.</p><p>1. Add some codes in src/backend/tcop/postgres.c</p><p>-----------------------------------------------------------<br></p><p>void PostgresMain(...)</p><p>{</p><p>......</p><p> for(;;)</p><p> {</p><p> ....</p><p> switch(firstchar)</p><p> {</p><p> ....</p><p> case 'X' :</p><p> case EOF :</p><p> ...</p><p> <span style="font-weight: bold;">ereport(DEBUG2,(errmsg("************ process(%d) is about to exit. ***************",getpid())));</span></p><p> proc_exit(0);<br> <span style="font-weight: bold;">ereport(DEBUG2,(errmsg("process(%d) is just exited.",getpid())));</span><br> case 'd': /* copy data */<br> .....<br></p><p> }<br></p><p> }<br></p><p>}</p><p>--------------------------------------------------------</p><p>Later, I am going to run psql as a backend process.<br></p><p>When psql quits itself, ereport would leave a debug message.</p><p><br></p><p>2. Add some codes in src/backend/storage/ipc/ipc.c<br></p><p>-----------------------------------<br></p><p>...</p><p><span style="font-weight: bold;">#include "utils/memutils.h"</span></p><p>...</p><p>void proc_exit(..)</p><p>{</p><p>....</p><p> elog(DEBUG3, "exit(%d)", code);<br><br><span style="font-weight: bold;">ereport(DEBUG2,(errmsg("**************** process(%d) will call exit(): **************",getpid())));</span><br><span style="font-weight: bold;">MemoryContextStats(TopMemoryContext);</span><br><br> exit(code);<br></p><p>}</p><p>-----------------------------------</p><p>MemoryContextStats will leave message about the stats of TopMemoryContext just before a process is terminated.<br></p><p><br></p><p>3. compile and install</p><p>4. start postgresql server</p><p> postgres <span style="font-weight: bold;">-d 5</span> -D /usr/local/pgsql/data </p><p>5. run psql and quit</p><p> psql test</p><p> test=# \q<br>6. log message for a backend(psql) is like below.</p><p>----------------------------</p><p>DEBUG: forked new backend, pid=<span style="font-weight: bold;">7967</span> socket=8<br>LOG: connection received: host=[local]<br>DEBUG: postgres child[7967]: starting with (<br>DEBUG: postgres<br>DEBUG: )<br>DEBUG: InitPostgres<br>DEBUG: my backend ID is 2<br>DEBUG: StartTransaction<br>DEBUG: name: unnamed; blockState: DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: <br>LOG: connection authorized: user=postgres database=test<br>DEBUG: CommitTransaction<br>DEBUG: name: unnamed; blockState: STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: <br>DEBUG: <span style="font-weight: bold;">************ process(7967) is about to exit. ***************</span><br>DEBUG: shmem_exit(0): 1 before_shmem_exit callbacks to make<br>DEBUG: shmem_exit(0): 6 on_shmem_exit callbacks to make<br>DEBUG: proc_exit(0): 4 callbacks to make<br>LOG: disconnection: session time: 0:00:02.104 user=postgres database=test host=[local]<br>DEBUG: exit(0)<br>DEBUG: <span style="font-weight: bold;">**************** process(7967) will call exit(): **************</span><br>TopMemoryContext: 88792 total in 6 blocks; 19824 free (18 chunks); 68968 used<br> MessageContext: 8192 total in 1 blocks; 7144 free (1 chunks); 1048 used<br> Operator class cache: 8192 total in 1 blocks; 4424 free (0 chunks); 3768 used<br> ........<br> Timezones: 104024 total in 2 blocks; 5520 free (0 chunks); 98504 used<br> ErrorContext: 8192 total in 1 blocks; 8176 free (7 chunks); 16 used<br><span style="font-style: italic;"><span style="font-weight: bold;"><span style="text-decoration: underline;">Grand total: 934556 bytes in 94 blocks; 325496 free (29 chunks); 609060 used</span></span></span><br>DEBUG: shmem_exit(-1): 0 before_shmem_exit callbacks to make<br>DEBUG: shmem_exit(-1): 0 on_shmem_exit callbacks to make<br>DEBUG: proc_exit(-1): 0 callbacks to make<br>DEBUG: reaping dead processes<br>DEBUG: server process (PID 7967) exited with exit code 0<br></p><p>---------------------------<br></p><p><br></p><p>From the message above, TopMemoryContext has 94blocks of memory until just before exit is called.</p><p>Then, when or where is TopMemoryContext released ??<br></p></div></td></tr>
</table>
</div></body></html>
<!-- __Hanmail-sig-Start__ -->
<br><br><a href="mailto:juemcc(at)hanmail(dot)net"><img src="http://nametag.hanmail.net/PhqSvPUpJ7Kd72chbZDZKg00" border="0"></a>
<!-- __Hanmail-sig-End__ -->
<img src="http://wwl1422.hanmail.net:4280/@from=juemcc&rcpt=pgsql%2Dhackers%40postgresql%2Eorg&msgid=%3C20161017152628%2EHM%2EB000000000FpEKn%40juemcc%2Ewwl1422%2Ehanmail%2Enet%3E">