Skip to content

Instantly share code, notes, and snippets.

@jfrost
Last active May 2, 2017 20:30
Show Gist options
  • Save jfrost/c282697478157bbc18bade6dfbce6165 to your computer and use it in GitHub Desktop.
Save jfrost/c282697478157bbc18bade6dfbce6165 to your computer and use it in GitHub Desktop.
erb template for calculating a few memory based settings for postgresql.conf. Put postgresql.conf.erb in ~/erb-templates then you can run postgresqlconf-erb.sh <memorysize> <max_connections> <filesystem>
#########################################################
#
# Memory size: <%= memorysize %>
# max_connections: <%= max_connections %>
# Lun Fileystems: <%= lun_filesystem %>
#
#########################################################
listen_addresses = '*'
port = 5432
max_connections = <%= max_connections %>
tcp_keepalives_idle = 30min
<% if ((memorysize.to_f * (memorysize.include?('GB') ? 1024 : 1 )) * 0.25).round <=8096 -%>
shared_buffers = <%= ((memorysize.to_f * (memorysize.include?('GB') ? 1024 : 1 )) * 0.25).round %>MB
<% else -%>
shared_buffers = 8096MB
<% end -%>
temp_buffers = <%= ((memorysize.to_f * (memorysize.include?('GB') ? 1024 : 1 )) / max_connections * 2 ).round %>MB
work_mem = <%= ((memorysize.to_f * (memorysize.include?('GB') ? 1024 : 1 )) / max_connections ).round %>MB
<% if ((memorysize.to_f * (memorysize.include?('GB') ? 1024 : 1 )) / 16 ).round <= 2048 -%>
maintenance_work_mem = <%= ((memorysize.to_f * (memorysize.include?('GB') ? 1024 : 1 )) / 16 ).round %>MB
<% else -%>
maintenance_work_mem = 2048MB
<% end -%>
wal_level = hot_standby
fsync = on
checkpoint_segments = 15
<% if lun_filesystem == "ext3" -%>
checkpoint_completion_target = 0.0
<% else -%>
checkpoint_completion_target = 0.9
<% end -%>
effective_cache_size = <%= ((memorysize.to_f * (memorysize.include?('GB') ? 1024 : 1 )) * 0.75 ).round %>MB
logging_collector = on
log_destination = csvlog
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_file_mode = 0644
log_truncate_on_rotation = on
log_rotation_age = 1d
log_rotation_size = 0
log_min_duration_statement = 1000
log_checkpoints = on
log_lock_waits = on
log_temp_files = 0
autovacuum = on
#autovacuum_freeze_max_age = 200000000
#!/bin/bash
TEMPFILE=$(mktemp /tmp/postgresql.conf.erb.XXXXXXXX)
MEMORYSIZE=${1:-"64 GB"}
MAX_CONNECTIONS=${2:-"100"}
FILESYSTEM=${3:-"ext4"}
update_and_render()
{
cat ~/erb-templates/postgresql.conf.erb >> ${TEMPFILE}
erb -T '-' ${TEMPFILE}
cat /dev/null > ${TEMPFILE}
}
cat << _EOF_ > ${TEMPFILE}
<% memorysize = "${MEMORYSIZE}" -%>
<% lun_filesystem = "${FILESYSTEM}" -%>
<% max_connections = ${MAX_CONNECTIONS} -%>
_EOF_
update_and_render
echo
rm -f ${TEMPFILE}
@HParker
Copy link

HParker commented May 2, 2017

I think checkpoint segments is deprecated now:

https://www.postgresql.org/docs/9.6/static/release-9-5.html#AEN133348

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment