Hi, hackers !

TopMemoryContext is created with malloc in MemoryContextInit().

All the other MemoryContexts such as ErrContext, MessageContext are allocated with palloc in a block of TopMemoryContext.

So, to release all the allocated memories, TopMemoryContext should be freed with MemoryContextReset and freeing itself.

But, I cannot find the place where it happens in source files.

To check if TopMemoryContext is released before exiting a process, I did some work like below and checked the result.

1.  Add some codes in src/backend/tcop/postgres.c


void PostgresMain(...)









           case 'X' :

           case EOF :


        ereport(DEBUG2,(errmsg("************  process(%d) is about to exit. ***************",getpid())));

       ereport(DEBUG2,(errmsg("process(%d) is just exited.",getpid())));
           case 'd':                       /* copy data */





Later, I am going to run psql as a backend process.

When psql quits itself, ereport would leave a debug message.

2. Add some codes in src/backend/storage/ipc/ipc.c



#include "utils/memutils.h"


void proc_exit(..)



        elog(DEBUG3, "exit(%d)", code);

ereport(DEBUG2,(errmsg("**************** process(%d) will call exit(): **************",getpid())));




MemoryContextStats will leave message about the stats of TopMemoryContext just before a process is terminated.

3. compile and install

4. start postgresql server

    postgres -d 5 -D /usr/local/pgsql/data

5. run psql and quit

    psql test

    test=# \q
6. log message for a backend(psql) is like below.


DEBUG:  forked new backend, pid=7967 socket=8
LOG:  connection received: host=[local]
DEBUG:  postgres child[7967]: starting with (
DEBUG:      postgres
DEBUG:  InitPostgres
DEBUG:  my backend ID is 2
DEBUG:  StartTransaction
DEBUG:  name: unnamed; blockState:       DEFAULT; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
LOG:  connection authorized: user=postgres database=test
DEBUG:  CommitTransaction
DEBUG:  name: unnamed; blockState:       STARTED; state: INPROGR, xid/subid/cid: 0/1/0, nestlvl: 1, children:
DEBUG:  ************  process(7967) is about to exit. ***************
DEBUG:  shmem_exit(0): 1 before_shmem_exit callbacks to make
DEBUG:  shmem_exit(0): 6 on_shmem_exit callbacks to make
DEBUG:  proc_exit(0): 4 callbacks to make
LOG:  disconnection: session time: 0:00:02.104 user=postgres database=test host=[local]
DEBUG:  exit(0)
DEBUG:  **************** process(7967) will call exit(): **************
TopMemoryContext: 88792 total in 6 blocks; 19824 free (18 chunks); 68968 used
  MessageContext: 8192 total in 1 blocks; 7144 free (1 chunks); 1048 used
  Operator class cache: 8192 total in 1 blocks; 4424 free (0 chunks); 3768 used
  Timezones: 104024 total in 2 blocks; 5520 free (0 chunks); 98504 used
  ErrorContext: 8192 total in 1 blocks; 8176 free (7 chunks); 16 used
Grand total: 934556 bytes in 94 blocks; 325496 free (29 chunks); 609060 used
DEBUG:  shmem_exit(-1): 0 before_shmem_exit callbacks to make
DEBUG:  shmem_exit(-1): 0 on_shmem_exit callbacks to make
DEBUG:  proc_exit(-1): 0 callbacks to make
DEBUG:  reaping dead processes
DEBUG:  server process (PID 7967) exited with exit code 0


From the message above, TopMemoryContext has 94blocks of memory until just before exit is called.

Then, when or where is TopMemoryContext released ??

Reply via email to