How would you check if your postgresql DB instance needs more RAM memory to handle their current working data?
migrated from stackoverflow.com Jun 4 '12 at 21:26
This question came from our site for professional and enthusiast programmers.
If all you are on Linux, your total physical RAM should be larger than your database size on disk in order to minimize I/O. Eventually it the entire database will be in the OS read cache and I/O will be limited to committing changes to disk. I prefer to find the DB size by running "du -shc $PGDATA/base"--that method aggregates all databases into a single number. As long as you are larger than that, it should be fine.
Additionally, you can look at the cache hit rate of heap and index block fetches. These measure the rate of hits into PostgreSQL's shared buffers. The numbers can be a little misleading--even though it may have been a miss in the shared buffers cache, it may still be a hit in the OS read cache. Still, hits in shared buffers are still less expensive than hits in the OS read cache (which are, in turn, less expensive by a couple of orders of magnitude than having to go back to disk).
In order to look at the shared buffers hit rate, I use this query:
This gives you the top 25 worst offenders where the buffer cache is missed for all tables where at least one block had to be fetched from "disk" (again, which could be either the OS read cache or actual disk I/O). You can increase the value in the WHERE clause or add another condition for heap_blks_hit to filter out seldom-used tables.
The same basic query can be used to check the per-table total index hit rate by globally replacing the string "heap" with "idx". Take a look at pg_statio_user_indexes to get a per-index breakdown.
A quick note about shared buffers: a good rule of thumb for this in Linux is to set the configuration parameter *shared_buffers* to 1/4 of RAM, but no more than 8GB. This is not a hard-and-fast rule, but rather a good starting point for tuning a server. If your database is only 4GB and you have a 32GB server, 8GB of shared buffers is actually overkill and you should be able to set this to 5 or 6 GB and still have room for future growth.
It also works, as said in Heroku doc: