Skip to content

Instantly share code, notes, and snippets.

@matthew-n
Last active September 11, 2018 15:53
Show Gist options
  • Save matthew-n/a9a296c20c973099553530f7a4df087b to your computer and use it in GitHub Desktop.
Save matthew-n/a9a296c20c973099553530f7a4df087b to your computer and use it in GitHub Desktop.
#!/bin/bash
#set -x
#set -e
page_size=`getconf PAGE_SIZE`
phys_pages=`getconf _PHYS_PAGES`
phys_memory_kb=$[ $phys_pages * $page_size / 1024 ]
kb_pct=$[ 1024 / 100]
# find the hugepage line in meminfo and keep only the number of kB
huge_page_size=`awk '/^Hugepagesize:.*kB$/{print $2}' /proc/meminfo`
peak_mem=`awk '/VmPeak:.*kB$/{print $2}' /proc/$(cat /var/run/postgresql/9.6-main.pid)/status`
## estimate peak memory
#peak_mem=$[ $phys_memory_kb / 10 * 4]
shmall=`expr $phys_pages / 2`
shmmax=`expr $shmall \* $page_size`
read -r -d '' sysctrl_conf <<- EOM
####################################
# /etc/sysctl.d/40-postgresql.conf
####################################
# constaint settings
vm.swappiness = 10 # discurage swap don't turn it off?
vm.overcommit_memory = 2 # do not overcommit (go up to 1.5 ram allocation)
# set how much memory a process can hove
kernel.shmmax = $shmmax #(default: `sysctl -n kernel.shmmax`)
kernel.shmall = $shmall #(default: `sysctl -n kernel.shmall`)
# set max large memory allocation the kernel can make
# note: shows up at > 8GB RAM, inefficiant use
# default: `sysctl -n vm.nr_hugepages`
vm.nr_hugepages = $[ $peak_mem / $huge_page_size +1]
# when to start cleaning dirty io blocks
# note: try not to overwhelm your disk's cache results in platues of buffers written, write early
# default: $(sysctl -n vm.dirty_background_ratio) -> $(numfmt --to=iec-i --suffix=B $(sysctl -n vm.dirty_background_bytes))
wvm.dirty_background_ratio = 2 # vm.dirty_background_bytes = $(numfmt --to=iec-i --suffix=B $[ $phys_memory_kb * 2 * $kb_pct ])
# when to stop prcesses to cleaning dirty io blocks
# default: $(sysctl -n vm.dirty_ratio) -> $(numfmt --to=iec-i --suffix=B $[ $phys_memory_kb * $(vm.dirty_bytes) * $kb_pct])
vm.dirty_ratio = 10 # vm.dirty_bytes = $(sysctl -n vm.dirty_bytes)
EOM
echo "$sysctrl_conf"
echo -e "\n\n\n"
read -r -d '' grub_conf <<- EOM
#################################
# /etc/grub.d/99-postgresql-HTP.conf
#################################
transparent_hugepage=never
echo never > /sys/kernel/mm/transparent_hugepage/enabled
EOM
echo "$grub_conf"
echo -e "\n\n\n"w
@matthew-n
Copy link
Author

[https://www.slideshare.net/PostgreSQL-Consulting/linux-tuning-to-improve-postgresql-performance]
[https://www.slideshare.net/fuzzycz/postgresql-on-ext4-xfs-btrfs-and-zfs]
[https://blog.pgaddict.com/posts/postgresql-io-schedulers-cfq-noop-deadline]
[https://blog.pgaddict.com/posts/postgresql-performance-on-ext4-and-xfs]

@matthew-n
Copy link
Author

matthew-n commented Apr 30, 2018

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