Skip to content

Instantly share code, notes, and snippets.

@pcaro
Created December 4, 2012 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcaro/4202916 to your computer and use it in GitHub Desktop.
Save pcaro/4202916 to your computer and use it in GitHub Desktop.
Fabric posrgres tunning
def postgres_tunning(pgversion=9.1):
# Use postgres conservative values, for about 1GB RAM machine
# Following ideas from:
# * http://www.juancarlosmoral.es/postgresql-hardware-tunning
# * http://doc.nuxeo.com/display/ADMINDOC/Configuring+PostgreSQL#ConfiguringPostgreSQL-Performancetuning
# * http://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server/es
fabtools.require.service.started("postgresql")
postgresql_conf = "/etc/postgresql/%s/main/postgresql.conf" % pgversion
# To Change postgres shared_buffers we need change kernel config first
sysctl_file = "/etc/sysctl.d/30-postgresql-shm.conf"
shmax_original = "#kernel.shmmax = 33554432"
shmmax = "kernel.shmmax = %i" % (120 * 1024 * 1024) # 120MB
if not fabric.contrib.files.contains(sysctl_file, shmmax, exact=True):
fabric.contrib.files.sed(sysctl_file, shmax_original, shmmax,
use_sudo=True, backup='')
# Do settings doing replacements from original debian postgresql.conf
replacements = [
("#shared_buffers = 24MB", "shared_buffers = 100MB"), # (10% of total)
("#work_mem = 1MB", "work_mem = 40MB"), # Work Memoy work_men (4% of total)
("#effective_cache_size = 128MB", "effective_cache_size = 512MB",),
("#maintenance_work_mem = 16MB", "maintenance_work_mem = 128MB",),
("#random_page_cost = 4.0", "random_page_cost = 2.0",),
("max_connections = 100", "max_connections = 64"),
]
with fabtools.files.watch(postgresql_conf) as config:
for orig, repl in replacements:
if not fabric.contrib.files.contains(postgresql_conf, repl):
fabric.contrib.files.sed(postgresql_conf, orig, repl, use_sudo=True)
if config.changed:
sudo('etckeeper commit "Postgres configuration"')
fabtools.require.service.started("pgbouncer")
with fabtools.files.watch(postgresql_conf) as config:
# pg_bouncer
fabtools.require.files.template_file("/etc/pgbouncer/pgbouncer.ini",
template_source="templates/pgbouncer.ini",
use_sudo=True, owner="postgres",
group="postgres", mode="640")
fabtools.require.files.template_file("/etc/default/pgbouncer",
template_contents="START=1",
use_sudo=True, owner="root",
group="root", mode="644")
if config.changed:
sudo("service pgbouncer reload")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment