<p>We are running Postgres in Kubernetes. Our Prometheus pod monitoring always shows that Postgres fills up the entire pods memory in a shared or cached state.</p>
<pre><code>free -h
              total        used        free      shared  buff/cache   available
Mem:            62G        1.7G         13G         11G         47G         48G


ps -u postgres o pid= | sed 's#.*#/proc/&amp;/smaps#' | sed 's/ //g#/proc/&amp;/smaps#' | \
    xargs grep -s ^Pss: | awk '{A+=$2} END{print &quot;PSS: &quot; A}' 
PSS: 13220013 kb

ps -u postgres o pid,rss:8,cmd | awk 'NR&gt;1 {A+=$2} {print} END{print &quot;RSS: &quot; A}' | tail -1
RSS: 38794236 kb
</code></pre>
<p>Correct me if I am wrong, but since the memory displayed in <code>top</code> and <code>ps</code>(RSS/RES) is shared memory, this means that Posgres isnt using that memory, its only reserved for when it needs it and other processes can also use that memory. Some <a href="https://www.depesz.com/2012/06/09/how-much-ram-is-postgresql-using/" rel="nofollow noreferrer">articles</a> say that one needs to <code>cat /proc/&lt;PID&gt;/smaps</code> and check the PSS to find the actual memory usage of Postgres.</p>
<p>We recently got OOM errors, but we where unable to pick it up in our monitoring. Due to the fact that our pod memory monitoring is always displaying 90% usage as its only monitoring the RSS/RES memory which is includes the shared/cached memory as well. So we didnt see any increase in RAM when the OOM errors happened and our database went down. The error was caused by a new query we introduced to our backend which used large amounts of memory per query.</p>
<p>We have prometheus-postgres-exporter installed giving us good Postgres metrics, but this didnt show us that we had queries using large amounts memory, but maybe our Grafana graph is missing that graph?</p>
<p>Because we are running Postgres in Kubernetes exporting the PSS memory is a hassle. So it feels like I am missing something.</p>
<p>So how does one monitor the memory usage of Postgres ? How does one pick up on queries using too much memory or pick up on postgres using too much memory due to load?</p>

Narayana
<p>I recommend that you disable memory overcommit by setting <code>vm.overcommit_memory = 2</code> in <code>/etc/sysctl.conf</code> and running <code>sysctl -p</code>. Don't forget to set <code>vm.overcommit_ratio</code> appropriately, based on the RAM and swap that you have.</p>
<p>Than should keep the OOM killer at bay.</p>
<p>Then you can examine <code>/proc/meminfo</code> to find if you are getting tight on memory:</p>
<ul>
<li><p><code>CommitLimit</code> shows how much memory the kernel is willing to hand out</p>
</li>
<li><p><code>Committed_AS</code> shows how much memory is allocated by processes</p>
</li>
</ul>
<p>Look at the <a href="https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/filesystems/proc.rst?id=HEAD#n1068" rel="nofollow noreferrer">kernel documentation</a> for details.</p>
