Creating a function for exposing memory usage of backend process

From: torikoshia <torikoshia(at)oss(dot)nttdata(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Creating a function for exposing memory usage of backend process
Date: 2020-06-17 13:00:21
Message-ID: 72a656e0f71d0860161e0b3f67e4d771@oss.nttdata.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

As you may know better than I do, backend processes sometimes use a lot
of memory because of the various reasons like caches, prepared
statements and cursors.
When supporting PostgreSQL, I face situations for investigating the
reason of memory bloat.

AFAIK, the way to examine it is attaching a debugger and call
MemoryContextStats(TopMemoryContext), however, I feel some difficulties
doing it:

- some production environments don't allow us to run a debugger easily
- many lines about memory contexts are hard to analyze

Using an extension(pg_stat_get_memory_context() in pg_cheat_funcs[1]),
we can get the view of the memory contexts, but it's the memory contexts
of the backend which executed the pg_stat_get_memory_context().

[user interface]
If we have a function exposing memory contexts for specified PID,
we can easily examine them.
I imagine a user interface something like this:

=# SELECT * FROM pg_stat_get_backend_memory_context(PID);

name | parent | level | total_bytes |
total_nblocks | free_bytes | free_chunks | used_bytes | some other
attibutes..
--------------------------+--------------------+-------+-------------+---------------+------------+-------------+------------
TopMemoryContext | | 0 | 68720 |
5 | 9936 | 16 | 58784
TopTransactionContext | TopMemoryContext | 1 | 8192 |
1 | 7720 | 0 | 472
PL/pgSQL function | TopMemoryContext | 1 | 16384 |
2 | 5912 | 1 | 10472
PL/pgSQL function | TopMemoryContext | 1 | 32768 |
3 | 15824 | 3 | 16944
dynahash | TopMemoryContext | 1 | 8192 |
1 | 512 | 0 | 7680
...

[rough implementation ideas and challenges]
I suppose communication between a process which runs
pg_stat_get_backend_memory_context()(referred to as A) and
target backend(referred to as B) is like:

1. A sends a message to B and order to dump the memory contexts
2. B dumps its memory contexts to some shared area
3. A reads the shared area and returns it to the function invoker

To do so, there seem some challenges.

(1) how to share memory contexts information between backend processes
The amount of memory contexts greatly varies depending on the
situation, so it's not appropriate to share the memory contexts using
fixed shared memory.
Also using the file on 'stats_temp_directory' seems difficult thinking
about the background of the shared-memory based stats collector
proposal[2].
Instead, I'm thinking about using dsm_mq, which allows messages of
arbitrary length to be sent and receive.

(2) how to send messages wanting memory contexts
Communicating via signal seems simple but assigning a specific number
of signal for this purpose seems wasting.
I'm thinking about using fixed shared memory to put dsm_mq handle.
To send a message, A creates a dsm_mq and put the handle on the shared
memory area. When B founds a handle, B dumps the memory contexts to the
corresponding dsm_mq.

However, enabling B to find the handle needs to check the shared memory
periodically. I'm not sure the suitable location and timing for this
checking yet, and doubt this way of communication is acceptable because
it gives certain additional loads to all the backends.

(3) clarifying the necessary attributes
As far as reading the past disucussion[3], it's not so clear what kind
of information should be exposed regarding memory contexts.

As a first step, to deal with (3) I'd like to add
pg_stat_get_backend_memory_context() which target is limited to the
local backend process.

Thanks for reading and how do you think?

[1]
https://github.com/MasaoFujii/pg_cheat_funcs#setof-record-pg_stat_get_memory_context
[2]
https://www.postgresql.org/message-id/flat/20180629(dot)173418(dot)190173462(dot)horiguchi(dot)kyotaro(at)lab(dot)ntt(dot)co(dot)jp
[3]
https://www.postgresql.org/message-id/20190805171608.g22gxwmfr2r7uf6t%40alap3.anarazel.de

Regards,

--
Atsushi Torikoshi
NTT DATA CORPORATION

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Kapila 2020-06-17 13:02:12 Re: [Patch] ALTER SYSTEM READ ONLY
Previous Message Jürgen Purtz 2020-06-17 12:52:19 Re: Add A Glossary