Using Tmpfs To Improve PostgreSQL Performance
Using Tmpfs To Improve PostgreSQL Performance
The statistics collector then passes the information to backends using temporary files, location of the temporary files are
defined by stats_temp_directory in postgresql.conf, by defaults it points to $PGDATA/pg_stat_tmp
As PostgreSQL is running, there are continuous I/O in stats_temp_directory, the disk IO may affect database
performance. PostgreSQL recommends to point stats_temp_directory to RAM-based file system, decreasing
physical I/O, thus increasing database performance.
We can use either ramfs or tmpfs, for the differences of the two, see http://www.thegeekstuff.com/2008/11/overview-of-
ramfs-and-tmpfs-on-linux/
# mkdir /mnt/tmp
2. mount tmpfs
# df -h /mnt/tmp/
Filesystem Size Used Avail Use% Mounted on
tmpfs 200M 8.0K 200M 1% /mnt/tmp
every time the server reboots, /mnt/tmp will be gone, to make the configuration persistent, add this line
to/etc/fstab
stats_temp_directory = '/mnt/tmp'
4. restart database
postgres=# \q
[postgres@linux ~]$ ls -lh /mnt/tmp/
total 8.0K
-rw------- 1 postgres postgres 6.0K Sep 24 13:41 pgstat.stat
[postgres@linux ~]$
Putting stats_temp_directory on a ramdisk
This hack is an old chestnut among PostgreSQL performance tuners, but it doesn't seem to be widely known
elsewhere. That's a shame, because it's pure win, and it's ridiculously easy to set up. You don't even need to restart
PostgreSQL.
Here's the situation: PostgreSQL writes certain temporary statistics. These go in the dir given by
the stats_temp_directory setting. By default, that's pg_stat_tmp in the data dir. Temp files get written a lot,
but there's no need for them to persist.
That makes them perfect candidates for a ramdisk (a.k.a. RAM drive). A ramdisk is a chunk of memory treated as a
block device by the OS. Because it's RAM, it's super-fast. As far as the app is concerned, the ramdisk just holds a
filesystem that it can read and write like any other. Moreover, PostgreSQL generally only needs a few hundred kilobytes
for stats_temp_directory; any modern server can fit that in RAM.
As root:
'chmod +t /var/lib/pgsql_stats_tmp'
Add this line to /etc/fstab. That 2G is an upper limit; the system will use only as much as it needs.
'mount /var/lib/pgsql_stats_tmp'
Then, as postgres:
stats_temp_directory = '/var/lib/pgsql_stats_tmp'
Other operating systems have different ways to set up ramdisks. Perhaps I'll cover them in a later post.
[1] The directory /var/lib/pgsql_stats_tmp is an arbitrary choice, but it works well for Debian's filesystem
layout.