Systemd

From PostgreSQL wiki
Jump to navigationJump to search

Service unit configuration

With PostgreSQL 9.6 or newer, it is recommended to build with --with-systemd and use the unit file shown in the documentation.

With older releases, the recommended approach is to write a unit file using Type=forking and use pg_ctl for ExecStart and ExecStop.

RemoveIPC

The setting

RemoveIPC=

in logind.conf controls whether System V IPC objects are removed when a user fully logs out. System users are exempt.

This was turned on by default in systemd version 212 (2014-03-25). RHEL7 ships 219. Debian stable (jessie) ships 215. Apparently, the systemd package in RHEL7 is built with it defaulting to off.

The observed effect of this is that the semaphore objects used by a PostgreSQL server are removed at apparently random times, leading to the server crashing with log messages like

LOG: semctl(1234567890, 0, IPC_RMID, ...) failed: Invalid argument

Shared memory segments that are still attached to are not cleaned up, so systemd will not remove shared memory segments still in use. But semaphores have no concept of a process being attached, so they will be cleaned up even if they are actually still in use.

A "user logging out" might happen as part of a maintenance job or manually when an administrator logs in as the postgres user or similar, so it is hard to prevent.

What is a "system user" is determined at systemd compile time from the SYS_UID_MAX setting in /etc/login.defs.

It is recommended to set

RemoveIPC=no

on all server hosts used for PostgreSQL.

Also, packaging and deployment scripts should be careful to create the postgres user as a system user by using useradd -r or equivalent.

You have to do at least one of these two things, or your PostgreSQL server will be very unreliable.

See also the documentation section systemd RemoveIPC.

KillUserProcesses

The setting

KillUserProcesses=

in logind.conf controls whether all processes of a user's session should be killed when the user logs out of that session.

This was turned on by default in systemd version 230 (2016-05-21). Currently (August 2016), this is not yet shipped widely (Fedora Branched/25, Debian testing, stable-backports).

This basically means that if you start a background server process such as PostgreSQL from the shell, it will be killed when you log out, unless you take special care. This has no harmful impact on PostgreSQL server instances started through system service units. It only affects background processes started from a user shell. This could apply if you don't use systemd service units but instead orchestrate the PostgreSQL server (or any other server process) using traditional init scripts or similar scripts.

There are various ways to adjust that, including KillOnlyUsers=, KillExcludeUsers=, loginctl enable-linger, systemd-run. These are all explained on the logind.conf man page. Note that being a "system user" has no influence here.