Limiting the CPU and Priority of a job

posted Nov 5, 2014, 6:24 PM by Sachchida Ojha   [ updated Nov 5, 2014, 6:28 PM ]
On an active database, you're often running larger tasks that can impact the performance of the database, which is bad for a fast database-backed Web site. For example, pg_dump to make backups, COPY/CREATE INDEX as in the above examples, etc. Not to fear; there are some simple things you can do to lessen the impact, e.g.:

nice -n 20 ionice -n 4 pg_dump
nice -n 20 ionice -n 4 psql

This wraps the command to have the lowest cpu priority (via nice) and ip priority (viaionice). Additionally I will often also use

cpulimit -e pg_dump -l 20 -z

which will limit pg_dump (in this case) to 10% of CPU while the current one ie running and then exit (via cpulimit). Of course, it also usually makes sense to run these things at off times using cron, etc.