4

I searched a lot about how to calculate memory usage of PostgreSQL process on Linux. I read article about how to calculate Memory usage for a generic process but I think that PostgreSQL has some peculiarity. For example, it has some basic processes:logger, checkpointer, bg writer, etc. But Linux creates also a process for each client connection on the master node.

The easy way to calculate the memory usage is with ps command listing the RSS of each process:

ps -aux | grep -v grep | grep postgres | awk '{ print $6 }'

and then sum it. But this doesn't work since the result is larger than the total memory. Some articles suggest the use of:

/proc/PID/smaps

but as said above I have more PID and I am unable to find a website that shows a script that let me easily calculate this information.

I found this interesting article, but it's not clear to me how to convert it into a working script. https://www.depesz.com/2012/06/09/how-much-ram-is-postgresql-using/

Does anyone know which is the best approach to solve this issue?

2
  • 1
    Stack Overflow is for programming questions. You might try asking on Unix & Linux, Server Fault or Super User instead. Commented Dec 15, 2020 at 18:07
  • Very strange. Search "linux memory usage" you'll find thousands of questions like mine, unfortunately, I haven't found an answer. However, I will follow your suggestion. However, my final request is about a working script that can be considered "programming" in some way. Commented Dec 15, 2020 at 18:12

1 Answer 1

3

To apply the information from the blog you quote:

ps -u postgres o pid= | \
sed 's# *\(.*\)#/proc/\1/smaps#' | \
xargs sudo grep ^Pss: | \
awk '{A+=$2} END{print A}'
  • first, get the process numbers of all processes running under user postgres

  • then get the name of the corresponding smaps file (/proc/PID/smaps)

  • then get the Pss line from that file, which contains the “proportional stack size” which divides shared memory by the number of processes attached to it

  • finally, add up the numbers

Sign up to request clarification or add additional context in comments.

7 Comments

Only one doubt. The original article says: "Basically, Pss is at most Rss, but gets decreased if the same pages of memory are used by more than one process." so PSS is (RSS-common memory), but my point is that common memory should be count at least once and I am not sure this occurs. Am I correct?
I mean, shared memory should be count at least once.
Correct. If there is only one process mapping the shared memory segment, PSS = RSS for that segment. If there are more, the value gets divided by the number of processes. So if you calculate the sum, you will end up with the correct number.
I calculated RSS and PSS for each PostgreSQL process on a slave node and I don't see any process where PSS=RSS. So I assume I have to add it in some way. Any suggestion? docs.google.com/document/d/…
Even on a standby server there are always some background processes, like the checkpointer and the background writer (and of course the WAL receiver). They all attach to and use shared memory.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.