Get memory contexts of an arbitrary backend process

torikoshia <torikoshia@oss.nttdata.com>

From: torikoshia <torikoshia@oss.nttdata.com>
To: pgsql-hackers@postgresql.org
Date: 2020-08-31T11:22:18Z
Lists: pgsql-hackers

Attachments

Hi,

After commit 3e98c0bafb28de, we can display the usage of the
memory contexts using pg_backend_memory_contexts system
view.

However, its target is limited to the  process attached to
the current session.

As discussed in the thread[1], it'll be useful to make it
possible to get the memory contexts of an arbitrary backend
process.

Attached PoC patch makes pg_get_backend_memory_contexts()
display meory contexts of the specified PID of the process.


   =# -- PID of the target process is 17051
   =# SELECT * FROM  pg_get_backend_memory_contexts(17051) ;
            name          | ident |      parent      | level | 
total_bytes | total_nblocks | free_bytes | free_chunks | used_bytes
   
-----------------------+-------+------------------+-------+-------------+---------------+------------+-------------+------------
    TopMemoryContext      |       |                  |     0 |       
68720 |             5 |      16816 |          16 |      51904
    RowDescriptionContext |       | TopMemoryContext |     1 |        
8192 |             1 |       6880 |           0 |       1312
    MessageContext        |       | TopMemoryContext |     1 |       
65536 |             4 |      19912 |           1 |      45624
    ...

It doesn't display contexts of all the backends but only
the contexts of specified process.
I think it would be enough because I suppose this function
is used after investigations using ps command or other OS
level utilities.


The rough idea of implementation is like below:

   1. send  a signal to the specified process
   2. signaled process dumps its memory contexts to a file
   3. read the dumped file and display it to the user


Any thoughts?

[1] 
https://www.postgresql.org/message-id/72a656e0f71d0860161e0b3f67e4d771%40oss.nttdata.com


Regards,

--
Atsushi Torikoshi
NTT DATA CORPORATION

Commits

  1. Add function to log the memory contexts of specified backend process.

  2. Add pg_backend_memory_contexts system view.

  3. Limit the verbosity of memory context statistics dumps.