PostgreSQL offers a great deal of flexibility when it comes to resource
allocation and efficient usage of hardware capacities. This allows fancy
monitoring of what PostgreSQL does in production. Sometimes, however, better
operating system integration is needed and helps to use hardware even more
efficiently.
On Linux one way to limit or allocate resources is to use the Linux kernel’s
cgroup interface. cgroups and PostgreSQL are a perfect team for …
- more efficient monitoring
- restriciting use of resources
- resource accounting.
How do cgroups work?
On a Linux system cgroups (= control groups) are basically a way to group
various operating system processes into groups. A group like that can then be
assigned to a certain amount of RAM, a fraction of a CPU or to a certain number
of I/O operations.
Processes can be moved to a group manually or with the help of a daemon which
moves processes to the right group automatically given some user-defined rules.
It is easily possible to move all processes belonging to a user to some special
cgroup and apply constraints to it. Following this approach you can move an
entire PostgreSQL instance and all its processes to a certain cgroup.
A simple config could look like that (/etc/cgconfig.conf):
mount { cpu = /cgroup/main; cpuacct = /cgroup/main; memory = /cgroup/main; blkio = /cgroup/main; } group user_0004 { perm { admin { uid = root; gid = root; } task { uid = user_0004; gid = user_0004; } } memory { memory.limit_in_bytes = 256M; } cpu { cpu.shares = "75"; } } group user_0005 { perm { admin { uid = root; gid = root; } task { uid = user_0005; gid = user_0005; } } memory { memory.limit_in_bytes = 512M; } cpu { cpu.shares = "125"; } }
We got two users which form separate groups. Each group will get certain
fractions of the system’s CPU as well as a certain amount of memory. In other
words: You can box a database instance nicely inside a group.
In the next step you can nicely put processes into a group. This is best done
using /etc/cgrules.conf:
user_0004 * /user_0004 user_0005 * /user_0005
In our case every user’s process is automatically moved to the right process on
startup.
More on cgroups will be available in further blog posts.