Monday, February 27, 2012

postgresql and tmp

"Because PostgreSQL writes the write-ahead log to disk on every transaction commit using fsync(), and waits for that write to complete, users will see a huge performance boost if a write cache is used. Therefore, for performance and reliability, it is ideal if PostgreSQL can use a battery-backed write cache."

Moreover, Postgres recommends that if you are using RAID5, you should mount your /tmp dir on a spare drive if you have one.

But what do you do if you don't have a spare drive? And you're using everything for your RAID5 array?

mount /tmp to a 2G ram disk. of course.

Let's do it!

With any install, /tmp is usually always there. Usually. And since we're dealing with a DB, we want the data to be around, like just in case.
# mkdir /tmp <---- if it isn't there already.
Check and see if anyone is using /tmp ; if these are crucial daemons; I'd suggest stopping them.

Add this line to /etc/fstab in to mount the drive at boot-time:
tmpfs           /tmp tmpfs      defaults,size=2048M 0 0
tmpfs, by virtue of being tmpfs doesn't allocate all of that space in one go; only as needed. tmpfs is alright using up to half of your available RAM; use free -m to figure it out. I guess it is also worth mentioning that you do not need to recreate tmpfs each time the system is rebooted; it will auto-create between boots due to it being tmpfs.

That being said, mount the new filesystem after adding its entry in /etc/fstab.
# mount /tmp
Check to see that it's mounted
# mount
# df -h
You should see the following in mount and df -h output:
tmpfs on /tmp type tmpfs (rw,relatime,size=2097152k)
tmpfs         2.0G  0.0G  2.0G   0% /tmp
Next we need to create a directory to store the backup copies of whatever we've got in /tmp. /var is as good a place as any.
# mkdir /var/tmp-bak
Create script /etc/init.d/tmp-bak:
#! /bin/sh 
# /etc/init.d/tmp-bak
#
 
case "$1" in
  start)
    echo "copying files to tmp-bak"
    rsync -av /var/tmp-bak/ /tmp/
    echo [`date +"%Y-%m-%d %H:%M"`] tmp synched >> /var/log/tmp-bak_sync.log
    ;;
  sync)
    echo "synching files from tmp to tmp-bak"
    echo [`date +"%Y-%m-%d %H:%M"`] tmp synched to tmp-bak >> /var/log/tmp-bak_sync.log
    rsync -av --delete --recursive --force /tmp/ /var/tmp-bak/
    ;;
  stop)
    echo "synching files from tmp to tmp-bak"
    echo [`date +"%Y-%m-%d %H:%M"`] tmp synched to tmp-bak >> /var/log/ramdisk_sync.log
    rsync -av --delete --recursive --force /tmp/ /var/tmp-bak/
    ;;
  *)
    echo "Usage: /etc/init.d/tmp-bak {start|stop|sync}"
    exit 1
    ;;
esac

exit 0
Now set tmp-bak to run at startup:
# update-rc.d tmp-bak defaults 00 99
As a good rule of thumb, place the sync process in /etc/crontab:
5 * * * * root        /etc/init.d/tmp-bak sync >> /dev/null 2>&1

No comments: