Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Using tmpfs to improve PostgreSQL performance

In PostgreSQL, the statistics collector collects the following information:

1. statistics about table and index accesses


2. usage of user-defined functions
3. current command executed by any server process.

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/

I will use tmpfs for stats_temp_directory, here are my steps


1. create directory for our new mount point:

# mkdir /mnt/tmp
2. mount tmpfs

# mount -t tmpfs tmpfs /mnt/tmp/ -o size=200m


now we have a file system of 200M.

# 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

tmpfs /mnt/tmp tmpfs defaults,size=200m 0 0

3. edit postgres.conf, add this line:

stats_temp_directory = '/mnt/tmp'
4. restart database

$ pg_ctl -D /var/lib/pgsql/data restart


5. confirm we are using the tmpfs

[postgres@linux ~]$ psql


psql (8.4.11)
Type "help" for help.

postgres=# show stats_temp_directory;


stats_temp_directory
----------------------
/mnt/tmp
(1 row)

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.

In Linux, you set up a ramdisk like this:

As root:

'mkdir /var/lib/pgsql_stats_tmp' [1]

'chmod 777 /var/lib/pgsql_stats_tmp'

'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.

tmpfs /var/lib/pgsql_stats_tmp tmpfs size=2G,uid=postgres,gid=postgres 0 0

'mount /var/lib/pgsql_stats_tmp'

Then, as postgres:

Change the stats_temp_directory setting in postgresql.conf:

stats_temp_directory = '/var/lib/pgsql_stats_tmp'

Tell PostgreSQL to re-read its configuration:

'pg_ctl -D YOUR_DATA_DIR reload'

And that's it!

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.

You might also like