Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I've been fortunate enough to have the use of a 20GB Linode instance running Ubuntu 64 bit. I want to try to optimize PostGres for this service, but I don't know what I should prioritize changing.

I have several datasets of 20,000 or so rows and the calculations that are being performed are memory intensive queries (spatial analyses) with a small number of rows being written after each request. The total number of users is very small (10 - 50).

I've read through this article on the Postgresql site but I don't know enough about how this works, to know what I should prioritize. I've also looked at advice on what to change for geo type work here.

For example, I tried changing the shared_buffers to 200MB (which is much less than 75% of 20GB). This resulted in the following error message:

  • Restarting PostgreSQL 9.1 database server
    • The PostgreSQL server failed to start. Please check the log output: 2013-03-10 12:21:58 EDT FATAL: could not create shared memory segment: Invalid argument 2013-03-10 12:21:58 EDT DETAIL: Failed system call was shmget(key=5432001, size=47742976, 03600). 2013-03-10 12:21:58 EDT HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter. You can either reduce the request size or reconfigure the kernel with larger SHMMAX. To reduce the request size (currently 47742976 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections. If the request size is already small, it's possible that it is less than your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for. The PostgreSQL documentation contains more information about shared memory configuration.

I returned this to it's original value and tried changing:

work_mem = 50MB
maintenance_work_mem = 256MB 

My problem is that I don't know which values I should try changing, or how I should prioritize which values are key to experiment with and test. What should I specifically do to optimize this database?

share|improve this question
    
In most cases, setting effective_cache_size to 20GB, and shared_buffers to xx MB will suffice. –  wildplasser Mar 10 '13 at 17:09
    
The error message you quoted in full (thankyou) refers to the operating system settings you must change in order to increase shared_buffers. See postgresql.org/docs/current/static/runtime-config-resource.html, postgresql.org/docs/current/static/… –  Craig Ringer Mar 10 '13 at 23:21
    
As for figuring out what you need to do, that depends on all sorts of factors. Your workload - read or write heavy? Lots of small queries or fewer big queries? Client count? Using connection pooling? Many short transactions or fewer longer ones? How big is the database on disk and how much of it is "hot" ie actively and regularly queried? What's the performance of the associated storage system like? –  Craig Ringer Mar 10 '13 at 23:23
    
@CraigRinger I do mention several of these measures in the question - I'm not very familiar with how to measure or categorize client pooling. What do you mean by associated storage system? –  celenius Mar 11 '13 at 2:40
add comment

migrated from stackoverflow.com Mar 11 '13 at 13:32

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

2 Answers

up vote 5 down vote accepted

I would recommend using pgtune written by Greg Smith.

Simply run it on your server as follows:

pgtune -i postgresql.conf -o postgresql-tuned.conf

It has few more options, but just doing that and using generated postgresql.conf will do wonders for your server performance.

I think PostgreSQL should include this in standard install, and even run it by default - such that people cannot complain about bad PostgreSQL performance.

share|improve this answer
    
Fantastic! This is exactly what I need. I'd stumbled towards some of the settings but was not familiar with all the things that I should change. –  celenius Mar 10 '13 at 20:35
    
+1 Useful tool. Didn't get updated much since Postgres 8.4. But mostly still works for the latest version 9.2. –  Erwin Brandstetter Mar 10 '13 at 21:40
    
Once I fixed the SHMMAX problem (solution here: askubuntu.com/questions/44373/… ) pgtune worked like a charm. –  celenius Mar 11 '13 at 2:42
    
Uh, I did not realize you had shmmax issue until I reread your question again! of course you need to tweak sysctl.conf. BTW, in PostgreSQL 9.3 this will not be necessary –  mvp Mar 11 '13 at 3:03
add comment

You will need to increase the shmmax, shmall, msgmax and msgmnb settings on your server. PGTUNE, as mentioned in the other answer, should tell you what to set them to.

You can do that like this:

sysctl -w kernel.shmmax={{value}}
sysctl -w kernel.shmall={{value}}
sysctl -w kernel.msgmax={{value}}
sysctl -w kernel.msgmnb={{value}}

You can check what the values already are like this:

cat /proc/sys/kernel/shmmax
cat /proc/sys/kernel/shmmall
cat /proc/sys/kernel/msgmax
cat /proc/sys/kernel/msgmnb
share|improve this answer
add comment

Your Answer

 
discard

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