Postgresql – How should I tune Postgresql for 20 GB of RAM

memoryperformancepostgresql

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?

Best Answer

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.