testing needed: systemd unit files for PostgreSQL

Jacek Konieczny jajcus at jajcus.net
Sun Mar 9 16:51:21 CET 2014


Hi,

I found the init script for our postgresql a bit unreliable when used
under systemd. Additionally, most systemd process monitoring features
are lost when managing a service through the legacy init scripts.

I decided to add native systemd units for PostgreSQL, but that was not
trivial, as our init script supports multiple PostgreSQL instances, in
arbitrary locations (defined by $PG_DB_CLUSTERS in
/etc/sysconfig/postgresql).

Ideally, 'service postgresql start/stop/reload' should work as it used
to – start/stop/reload all the instances. But, at the same time, we want
every instance being separately managed by systemd.

I have implemented this in postgresql-9.3.3 by making
'postgresql.service' a dummy service, which just pulls
'postgresql.target', which Requires each individual instance defined
through 'postgresql at .service'.

That is how it works (with two clusters configured: /var/lib/pgsql and
/var/lib/pgsql-1):

Starting:

> [root at lolek ~]# service postgresql start                                       
> Redirecting to /bin/systemctl --output=cat start postgresql.service 
> [root at lolek ~]# service postgresql status                                      
> Redirecting to /bin/systemctl --output=cat status postgresql.service 
> postgresql.service - PostgreSQL server
>    Loaded: loaded (/lib/systemd/system/postgresql.service; enabled)
>    Active: active (exited) since Sun 2014-03-09 16:45:22 CET; 4s ago
>   Process: 11825 ExecStart=/bin/systemctl list-dependencies postgresql.target (code=exited, status=0/SUCCESS)
>   Process: 11823 ExecStart=/bin/systemctl is-active -q postgresql.target (code=exited, status=0/SUCCESS)
>  Main PID: 11825 (code=exited, status=0/SUCCESS)
> 
> Starting PostgreSQL server...
> Started PostgreSQL server.
> [root at lolek ~]# service postgresql at var-lib-pgsql status                        
> Redirecting to /bin/systemctl --output=cat status postgresql at var-lib-pgsql.service 
> postgresql at var-lib-pgsql.service - PostgreSQL database server at /var/lib/pgsql
>    Loaded: loaded (/lib/systemd/system/postgresql at .service; enabled)
>    Active: active (running) since Sun 2014-03-09 16:45:22 CET; 15s ago
>   Process: 11757 ExecStop=/usr/bin/pg_ctl -D %f stop -w -s -m fast (code=exited, status=0/SUCCESS)
>   Process: 11799 ExecStart=/usr/bin/pg_ctl -D %f start -w -s (code=exited, status=0/SUCCESS)
>  Main PID: 11807 (postgres)
>    CGroup: /system.slice/system-postgresql.slice/postgresql at var-lib-pgsql.service
>            ├─11807 /usr/bin/postgres -D /var/lib/pgsql
>            ├─11811 postgres: checkpointer process   
>            ├─11812 postgres: writer process   
>            ├─11813 postgres: wal writer process   
>            ├─11814 postgres: autovacuum launcher process   
>            └─11815 postgres: stats collector process   
> 
> [1-1] LOG:  ending log output to stderr
> [1-2] HINT:  Future log output will go to log destination "syslog".
> LOG:  ending log output to stderr
> HINT:  Future log output will go to log destination "syslog".
> [2-1] LOG:  database system was shut down at 2014-03-09 15:44:19 GMT
> [2-1] LOG:  database system is ready to accept connections
> [2-1] LOG:  autovacuum launcher started
> [3-1] LOG:  connection received: host=[local]
> [4-1] LOG:  connection authorized: user=postgres database=postgres
> Started PostgreSQL database server at /var/lib/pgsql.
> [root at lolek ~]# service postgresql at var-lib-pgsql\\x2d1 status                  
> Redirecting to /bin/systemctl --output=cat status postgresql at var-lib-pgsql-1.service 
> postgresql at var-lib-pgsql\x2d1.service - PostgreSQL database server at /var/lib/pgsql-1
>    Loaded: loaded (/lib/systemd/system/postgresql at .service; enabled)
>    Active: active (running) since Sun 2014-03-09 16:45:22 CET; 27s ago
>   Process: 11756 ExecStop=/usr/bin/pg_ctl -D %f stop -w -s -m fast (code=exited, status=0/SUCCESS)
>   Process: 11800 ExecStart=/usr/bin/pg_ctl -D %f start -w -s (code=exited, status=0/SUCCESS)
>  Main PID: 11808 (postgres)
>    CGroup: /system.slice/system-postgresql.slice/postgresql at var-lib-pgsql\x2d1.service
>            ├─11808 /usr/bin/postgres -D /var/lib/pgsql-1
>            ├─11816 postgres: checkpointer process   
>            ├─11817 postgres: writer process   
>            ├─11818 postgres: wal writer process   
>            ├─11819 postgres: autovacuum launcher process   
>            └─11820 postgres: stats collector process   
> 
> [1-1] LOG:  ending log output to stderr
> [1-2] HINT:  Future log output will go to log destination "syslog".
> [2-1] LOG:  database system was shut down at 2014-03-09 16:44:19 CET
> LOG:  ending log output to stderr
> HINT:  Future log output will go to log destination "syslog".
> [2-1] LOG:  database system is ready to accept connections
> [2-1] LOG:  autovacuum launcher started
> [3-1] LOG:  connection received: host=[local]
> [4-1] LOG:  connection authorized: user=postgres database=postgres
> Started PostgreSQL database server at /var/lib/pgsql-1.

Reloading:

> [root at lolek ~]# service postgresql reload                                      
> Redirecting to /bin/systemctl --output=cat reload postgresql.service 
> [root at lolek ~]# service postgresql at var-lib-pgsql status                        
> Redirecting to /bin/systemctl --output=cat status postgresql at var-lib-pgsql.service 
> postgresql at var-lib-pgsql.service - PostgreSQL database server at /var/lib/pgsql
>    Loaded: loaded (/lib/systemd/system/postgresql at .service; enabled)
>    Active: active (running) since Sun 2014-03-09 16:45:22 CET; 44s ago
>   Process: 11757 ExecStop=/usr/bin/pg_ctl -D %f stop -w -s -m fast (code=exited, status=0/SUCCESS)
>   Process: 11869 ExecReload=/usr/bin/pg_ctl -D %f reload -w -s (code=exited, status=0/SUCCESS)
>   Process: 11799 ExecStart=/usr/bin/pg_ctl -D %f start -w -s (code=exited, status=0/SUCCESS)
>  Main PID: 11807 (postgres)
>    CGroup: /system.slice/system-postgresql.slice/postgresql at var-lib-pgsql.service
>            ├─11807 /usr/bin/postgres -D /var/lib/pgsql
>            ├─11811 postgres: checkpointer process   
>            ├─11812 postgres: writer process   
>            ├─11813 postgres: wal writer process   
>            ├─11814 postgres: autovacuum launcher process   
>            └─11815 postgres: stats collector process   
> 
> HINT:  Future log output will go to log destination "syslog".
> [2-1] LOG:  database system was shut down at 2014-03-09 15:44:19 GMT
> [2-1] LOG:  database system is ready to accept connections
> [2-1] LOG:  autovacuum launcher started
> [3-1] LOG:  connection received: host=[local]
> [4-1] LOG:  connection authorized: user=postgres database=postgres
> Started PostgreSQL database server at /var/lib/pgsql.
> Reloading PostgreSQL database server at /var/lib/pgsql.
> [3-1] LOG:  received SIGHUP, reloading configuration files
> Reloaded PostgreSQL database server at /var/lib/pgsql.

Stopping:

> [root at lolek ~]# service postgresql stop                                        
> Redirecting to /bin/systemctl --output=cat stop postgresql.service 
> [root at lolek ~]# service postgresql at var-lib-pgsql status                        
> Redirecting to /bin/systemctl --output=cat status postgresql at var-lib-pgsql.service 
> postgresql at var-lib-pgsql.service - PostgreSQL database server at /var/lib/pgsql
>    Loaded: loaded (/lib/systemd/system/postgresql at .service; enabled)
>    Active: inactive (dead) since Sun 2014-03-09 16:46:11 CET; 1s ago
>   Process: 11891 ExecStop=/usr/bin/pg_ctl -D %f stop -w -s -m fast (code=exited, status=0/SUCCESS)
>   Process: 11869 ExecReload=/usr/bin/pg_ctl -D %f reload -w -s (code=exited, status=0/SUCCESS)
>   Process: 11799 ExecStart=/usr/bin/pg_ctl -D %f start -w -s (code=exited, status=0/SUCCESS)
>  Main PID: 11807 (code=exited, status=0/SUCCESS)
> 
> Reloading PostgreSQL database server at /var/lib/pgsql.
> [3-1] LOG:  received SIGHUP, reloading configuration files
> Reloaded PostgreSQL database server at /var/lib/pgsql.
> Stopping PostgreSQL database server at /var/lib/pgsql...
> [4-1] LOG:  received fast shutdown request
> [5-1] LOG:  aborting any active transactions
> [3-1] LOG:  autovacuum launcher shutting down
> [2-1] LOG:  shutting down
> [3-1] LOG:  database system is shut down
> Stopped PostgreSQL database server at /var/lib/pgsql.
> [root at lolek ~]# service postgresql at var-lib-pgsql\\x2d1 status                  
> Redirecting to /bin/systemctl --output=cat status postgresql at var-lib-pgsql-1.service 
> postgresql at var-lib-pgsql\x2d1.service - PostgreSQL database server at /var/lib/pgsql-1
>    Loaded: loaded (/lib/systemd/system/postgresql at .service; enabled)
>    Active: inactive (dead) since Sun 2014-03-09 16:46:11 CET; 8s ago
>   Process: 11890 ExecStop=/usr/bin/pg_ctl -D %f stop -w -s -m fast (code=exited, status=0/SUCCESS)
>   Process: 11868 ExecReload=/usr/bin/pg_ctl -D %f reload -w -s (code=exited, status=0/SUCCESS)
>   Process: 11800 ExecStart=/usr/bin/pg_ctl -D %f start -w -s (code=exited, status=0/SUCCESS)
>  Main PID: 11808 (code=exited, status=0/SUCCESS)
> 
> Started PostgreSQL database server at /var/lib/pgsql-1.
> Reloading PostgreSQL database server at /var/lib/pgsql-1.
> [3-1] LOG:  received SIGHUP, reloading configuration files
> Reloaded PostgreSQL database server at /var/lib/pgsql-1.
> Stopping PostgreSQL database server at /var/lib/pgsql-1...
> [4-1] LOG:  received fast shutdown request
> [5-1] LOG:  aborting any active transactions
> [2-1] LOG:  shutting down
> [3-1] LOG:  database system is shut down
> Stopped PostgreSQL database server at /var/lib/pgsql-1.

The problem is 'service postgresql status' gives little useful
information now (more information can be obtained when querying individual
instances), but except that I think the configuration works quite well.

One can also disable the 'postgresql.service' and manually manage the
'postgresql at something.service' units.

Please, check it out and tell if something is broken.

Greets,
	Jacek


More information about the pld-devel-en mailing list