Salvatore D'angelo
Salvatore D'angelo

Reputation: 1149

How to calculate PostgreSQL memory usage on Linux?

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?

Upvotes: 3

Views: 3715

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246013

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

Upvotes: 3

Related Questions