Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a VPS with 9GB RAM, 300GB HDD, 3 GB Swap, 7 Cores. The OS is CentOS 5.7 Final.

I have postgres9.0 running on my machine, with proper tuning done (at least by book/wiki of PostgreSQL).

What happens is most of the times when some complex query run (by complex I mean select with maximum 3 Joins), eventhough ~75% of my RAM is unused there is ~99% swapping is happening.

EDIT : the free RAM i am mentioning here is calculated by (free + buffered + cached) The actually free shown by 'free -m' is hardly ~2%. So correct me if I am wrong here in the first place.

Plus it screws up my disk IO which is most of the time reaching ~100% and slows down everything else. (I tend to believe something's wrong with my disk.)

I dont understand the reason of this much of swapping happening. Is it because of context switching?? Most of the time my processors are idle, while the IO wait goes upto 30% during pick times.

Would appreciate if some can shed some light on it.


share|improve this question
shared_buffers is accounted for in free as buffers/cache ... so keep that in mind when considering your "free" memory. – Craig Ringer Nov 20 '12 at 23:12
but if i am not wrong shared_buffers are per process in OS and for postgres they are per connection. Kindly correct if i am wrong. thanks – Anuj Nov 20 '12 at 23:16
How big is the database? If free -m says you have 2% free, then you have 2% free. Everything else that's not application memory is disk cache, which will be used to cache the database if it's available. Don't assume that the "75%" of memory isn't already filled to the brim with your database tables - Your query might just be hitting the ones that didn't fit into the cache. – Darth Android Nov 20 '12 at 23:17
Database is ~165GB. And indeed what happens is the table which I need for most of the queries say ~99% of them is ~150GB of it (including indexes) – Anuj Nov 20 '12 at 23:19
@indyaah shared_buffers are global in PostgreSQL; they're allocated once as shared memory by the postmaster, and shared among all connections. There is one process per connection. Maybe you're thinking of work_mem, which is private to each backend? It's actually per-sort, not per-connection, though. – Craig Ringer Nov 20 '12 at 23:22
up vote 4 down vote accepted

The Linux kernel decides to put memory pages in swap when they're not used too frequently, even when it has some memory available. You can tweak this behaviour by adjusting the 'swappiness' of the memory manager by doing

$ echo 50 > /proc/sys/vm/swappiness

where 50 is a value between 0 (swap least possible) and 100 (swap out as much as you like).

See also Wikipedia: swappiness to read more about this.

share|improve this answer

It seems you may be looking at the right value, but just in case you aren't and for those others who are looking at the wrong figure.

$ free -mt
             total       used       free     shared    buffers     cached
Mem:          2027       1953         73          0         66       1501
-/+ buffers/cache:        385       1641 <---- this value is actually free
Swap:         4086          0       4086
Total:        6113       1954       4159

This is from our head office stock/erp system for 40ish head office staff and coordinating 9 satellite branch stock/erp systems, 80% (1641MB) of our ram usage is considered free, but rather than being wasted is doing something useful and acting as buffers/cache.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .