When is TopMemoryContext released ? - Mailing list pgsql-hackers

From 서정협
Subject When is TopMemoryContext released ?
Date
Msg-id 20161017152628.HM.B000000000FpEKn@juemcc.wwl1422.hanmail.net
Whole thread Raw
Responses Re: When is TopMemoryContext released ?  (Heikki Linnakangas <hlinnaka@iki.fi>)
List pgsql-hackers
<div style="font-size:12px; font-family:굴림,굴림체,Gulim,Baekmuk Dotum,Undotum,Apple Gothic,Latin font,sans-serif;"><table
border="0"cellpadding="0" cellspacing="0" style="margin 0 auto;background: ;" width="100%"><tr><td style="padding:8pt;"
valign="top"><divclass="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><br /><p>TopMemoryContext is created
withmalloc in MemoryContextInit().<p>All the other MemoryContexts such as ErrContext, MessageContext are allocated with
pallocin a block of TopMemoryContext.<p>So, to release all the allocated memories, TopMemoryContext should be freed
withMemoryContextReset and freeing itself.<p>But, I cannot find the place where it happens in source files.<p><br
/><p>Tocheck if TopMemoryContext is released before exiting a process, I did some work like below and checked the
result.<p>1. Add some codes in
src/backend/tcop/postgres.c<p>-----------------------------------------------------------<br/><p>void
PostgresMain(...)<p>{<p>......<p>   for(;;)<p>    {<p>      ....<p>      switch(firstchar)<p>      {<p>          
....<p>          case 'X' :<p>           case EOF :<p>                ...<p>        <span style="font-weight:
bold;">ereport(DEBUG2,(errmsg("************ process(%d) is about to exit.
***************",getpid())));</span><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>      }<br /><p>    }<br
/><p>}<p>--------------------------------------------------------<p>Later,I am going to run psql as a backend
process.<br/><p>When psql quits itself, ereport would leave a debug message.<p><br /><p>2. Add some codes in
src/backend/storage/ipc/ipc.c<br/><p>-----------------------------------<br /><p>...<p><span style="font-weight:
bold;">#include"utils/memutils.h"</span><p>...<p>void proc_exit(..)<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>MemoryContextStatswill leave message about the stats of TopMemoryContext
justbefore a process is terminated.<br /><p><br /><p>3. compile and install<p>4. start postgresql server<p>    postgres
<spanstyle="font-weight: bold;">-d 5</span> -D /usr/local/pgsql/data <p>5. run psql and quit<p>    psql test<p>   
test=#\q<br />6. log message for a backend(psql) is like below.<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_exitcallbacks to make<br />DEBUG:  proc_exit(0): 4 callbacks to make<br />LOG:  disconnection: session time:
0:00:02.104user=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
deadprocesses<br />DEBUG:  server process (PID 7967) exited with exit code 0<br /><p>---------------------------<br
/><p><br/><p>From the message above, TopMemoryContext has 94blocks of memory until just before exit is called.<p>Then,
whenor where is TopMemoryContext released ??<br /></div></td></tr></table></div><br /><br /><a
href="mailto:juemcc@hanmail.net"><imgborder="0" src="http://nametag.hanmail.net/PhqSvPUpJ7Kd72chbZDZKg00" /></a>  <img
src="http://wwl1422.hanmail.net:4280/@from=juemcc&rcpt=pgsql%2Dhackers%40postgresql%2Eorg&msgid=%3C20161017152628%2EHM%2EB000000000FpEKn%40juemcc%2Ewwl1422%2Ehanmail%2Enet%3E"
/>

pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: postgres_fdw super user checks
Next
From: Heikki Linnakangas
Date:
Subject: Re: When is TopMemoryContext released ?