Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How would you check if your postgresql DB instance needs more RAM memory to handle their current working data?

share|improve this question

migrated from stackoverflow.com Jun 4 '12 at 21:26

This question came from our site for professional and enthusiast programmers.

7  
No need to check, you always need more RAM. :) –  Alex Howansky Jun 4 '12 at 19:37
1  
Not a programming question, I'm voting to move it to ServerFault. –  GManNickG Jun 4 '12 at 19:40
1  
I'm not a DBA, but I'd start by seeing of any common queries are on the edge of being hash joined instead of merge nested looped. There's some db config tuning you can do that might affect how much memory is available to any particular query as well [check the docs or email the mailing list is my suggestion]. It can also be useful to see if you have enough RAM to keep commonly used tables cached. But ultimately, unless your ENTIRE DB fits in RAM you could use more. :) –  Mark Roberts Jun 4 '12 at 19:44

2 Answers 2

up vote 5 down vote accepted

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:

SELECT relname, heap_blks_read, heap_blks_hit,
    round(heap_blks_hit::numeric/(heap_blks_hit + heap_blks_read),3)
FROM pg_statio_user_tables
WHERE heap_blks_read > 0
ORDER BY 4
LIMIT 25;

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.

share|improve this answer

It also works, as said in Heroku doc:

SELECT
    'cache hit rate' AS name,
     sum(heap_blks_hit) / (sum(heap_blks_hit) + sum(heap_blks_read)) AS ratio
FROM pg_statio_user_tables;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.