When is TopMemoryContext released ?

From: 서정협 <juemcc(at)hanmail(dot)net>
To: <pgsql-hackers(at)postgresql(dot)org>
Subject: When is TopMemoryContext released ?
Date: 2016-10-17 06:26:28
Message-ID: 20161017152628.HM.B000000000FpEKn@juemcc.wwl1422.hanmail.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

<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.&nbsp; Add some codes in src/backend/tcop/postgres.c</p><p>-----------------------------------------------------------<br></p><p>void PostgresMain(...)</p><p>{</p><p>......</p><p>&nbsp;&nbsp;&nbsp; for(;;)</p><p>&nbsp;&nbsp;&nbsp; {</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ....</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; switch(firstchar)</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ....</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 'X' :</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case EOF :</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ...</p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-weight: bold;">ereport(DEBUG2,(errmsg("************&nbsp; process(%d) is about to exit. ***************",getpid())));</span></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proc_exit(0);<br>&nbsp; &nbsp; &nbsp;&nbsp; <span style="font-weight: bold;">ereport(DEBUG2,(errmsg("process(%d) is just exited.",getpid())));</span><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 'd':&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* copy data */<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; .....<br></p><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br></p><p>&nbsp;&nbsp;&nbsp; }<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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp; postgres <span style="font-weight: bold;">-d 5</span> -D /usr/local/pgsql/data </p><p>5. run psql and quit</p><p>&nbsp;&nbsp;&nbsp; psql test</p><p>&nbsp;&nbsp;&nbsp; test=# \q<br>6. log message for a backend(psql) is like below.</p><p>----------------------------</p><p>DEBUG:&nbsp; forked new backend, pid=<span style="font-weight: bold;">7967</span> socket=8<br>LOG:&nbsp; connection received: host=[local]<br>DEBUG:&nbsp; postgres child[7967]: starting with (<br>DEBUG:&nbsp; &nbsp;&nbsp;&nbsp; postgres<br>DEBUG:&nbsp; )<br>DEBUG:&nbsp; InitPostgres<br>DEBUG:&nbsp; my backend ID is 2<br>DEBUG:&nbsp; StartTransaction<br>DEBUG:&nbsp; name: unnamed; blockState:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: <br>LOG:&nbsp; connection authorized: user=postgres database=test<br>DEBUG:&nbsp; CommitTransaction<br>DEBUG:&nbsp; name: unnamed; blockState:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children: <br>DEBUG:&nbsp; <span style="font-weight: bold;">************&nbsp; process(7967) is about to exit. ***************</span><br>DEBUG:&nbsp; shmem_exit(0): 1 before_shmem_exit callbacks to make<br>DEBUG:&nbsp; shmem_exit(0): 6 on_shmem_exit callbacks to make<br>DEBUG:&nbsp; proc_exit(0): 4 callbacks to make<br>LOG:&nbsp; disconnection: session time: 0:00:02.104 user=postgres database=test host=[local]<br>DEBUG:&nbsp; exit(0)<br>DEBUG:&nbsp; <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>&nbsp; MessageContext: 8192 total in 1 blocks; 7144 free (1 chunks); 1048 used<br>&nbsp; Operator class cache: 8192 total in 1 blocks; 4424 free (0 chunks); 3768 used<br>&nbsp; ........<br>&nbsp; Timezones: 104024 total in 2 blocks; 5520 free (0 chunks); 98504 used<br>&nbsp; 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:&nbsp; shmem_exit(-1): 0 before_shmem_exit callbacks to make<br>DEBUG:&nbsp; shmem_exit(-1): 0 on_shmem_exit callbacks to make<br>DEBUG:&nbsp; proc_exit(-1): 0 callbacks to make<br>DEBUG:&nbsp; reaping dead processes<br>DEBUG:&nbsp; 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">

Attachment Content-Type Size
unknown_filename text/html 6.8 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Heikki Linnakangas 2016-10-17 06:42:16 Re: When is TopMemoryContext released ?
Previous Message Michael Paquier 2016-10-17 06:18:23 Re: postgres_fdw super user checks