Set up Linux kernel parameters for PostgreSQL

Run this bash script:

#!/bin/bash

set -e
set -u

SYSCTL=/sbin/sysctl

echo "# add the output of this script to /etc/sysctl.conf,"
echo "# and then, as root, run"
echo
echo "# sysctl -p /etc/sysctl.conf"
echo
echo "# to load change the kernel settings for these parameters."
echo

PAGE_SIZE=$(getconf PAGE_SIZE)

echo "# page size is: $PAGE_SIZE"

NUM_PHYS_PAGES=$(getconf _PHYS_PAGES)

echo "# number of physical pages on this box: $NUM_PHYS_PAGES"

CURR_SHMALL=$($SYSCTL -n kernel.shmall)
PREF_SHMALL=$(expr $NUM_PHYS_PAGES / 2)

echo "# kernel.shmall should be half of the number of pages. Current kernel.shmall, in pages, is: $CURR_SHMALL"
echo "# kernel.shmall should be:"
echo
echo "kernel.shmall = $PREF_SHMALL"
echo

CURR_SHMMAX=$($SYSCTL -n kernel.shmmax)
PREF_SHMMAX=$(expr $PREF_SHMALL \* $PAGE_SIZE)

echo "# kernel.shmmax should be half of available RAM, in kB. Current kernel.shmmax, in kB, is: $CURR_SHMMAX"
echo "# kernel.shmmax should be:"
echo
echo "kernel.shmmax = $PREF_SHMMAX"
echo

# CURR_SHMMIN=$($SYSCTL -n kernel.shmmin)  # XXX: does not exist on linux
# CURR_SHMSEG=$($SYSCTL -n kernel.shmseg)  # XXX: does not exist on linux

CURR_SHMMNI=$($SYSCTL -n kernel.shmmni)

echo "# kernel.shmmni is usually set to a sane amount on Linux. Currently, it is: $CURR_SHMMNI"

# CURR_SEMMNI=$($SYSCTL -n kernel.semmni)  # XXX: does not exist on linux
# CURR_SHMMNI=$($SYSCTL -n kernel.semmns)  # XXX: does not exist on linux
# CURR_SHMMSL=$($SYSCTL -n kernel.semmsl)  # XXX: does not exist on linux
# CURR_SHMMSL=$($SYSCTL -n kernel.semmap)  # XXX: does not exist on linux
# CURR_SHMMSL=$($SYSCTL -n kernel.semmvx)  # XXX: does not exist on linux

CURR_SEM=$($SYSCTL -n kernel.sem)

echo "# kernel.sem usually has sane defauls. They are currently: $CURR_SEM"