Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 468868 - dev-db/postgresql-server: add systemd unit
Summary: dev-db/postgresql-server: add systemd unit
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Development (show other bugs)
Hardware: All Linux
: Normal normal with 1 vote (vote)
Assignee: PgSQL Bugs
URL:
Whiteboard:
Keywords: NeedPatch
: 491418 (view as bug list)
Depends on:
Blocks: install-systemd-unit
  Show dependency tree
 
Reported: 2013-05-07 13:14 UTC by Alexander Tsoy
Modified: 2013-11-17 12:32 UTC (History)
12 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
postgresql-server-9.2.4-r1.ebuild.patch (postgresql-server-9.2.4-r1.ebuild.patch,1.55 KB, patch)
2013-05-07 13:15 UTC, Alexander Tsoy
Details | Diff
postgresql.service (postgresql.service,1.66 KB, text/plain)
2013-05-07 13:15 UTC, Alexander Tsoy
Details
postgresql.tmpfilesd (postgresql.tmpfilesd,47 bytes, text/plain)
2013-05-07 13:15 UTC, Alexander Tsoy
Details
postgresql.tmpfilesd (postgresql.tmpfilesd,43 bytes, text/plain)
2013-05-07 13:19 UTC, Alexander Tsoy
Details
postgresql.service (postgresql.service,1.65 KB, text/plain)
2013-07-23 23:50 UTC, Alexander Tsoy
Details
postgresql-check-db-dir (postgresql-check-db-dir,1.01 KB, text/plain)
2013-07-23 23:51 UTC, Alexander Tsoy
Details
postgresql-server-9.2.4-r1.ebuild.patch (postgresql-server-9.2.4-r1.ebuild.patch,1.91 KB, patch)
2013-07-23 23:53 UTC, Alexander Tsoy
Details | Diff
postgresql-check-db-dir (postgresql-check-db-dir,1.01 KB, text/plain)
2013-07-24 00:59 UTC, Alexander Tsoy
Details
postgresql.service (postgresql.service,1.61 KB, text/plain)
2013-07-24 22:11 UTC, Alexander Tsoy
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Tsoy 2013-05-07 13:14:29 UTC
Please provide systemd unit file.

Reproducible: Always
Comment 1 Alexander Tsoy 2013-05-07 13:15:03 UTC
Created attachment 347598 [details, diff]
postgresql-server-9.2.4-r1.ebuild.patch
Comment 2 Alexander Tsoy 2013-05-07 13:15:23 UTC
Created attachment 347600 [details]
postgresql.service
Comment 3 Alexander Tsoy 2013-05-07 13:15:39 UTC
Created attachment 347602 [details]
postgresql.tmpfilesd
Comment 4 Alexander Tsoy 2013-05-07 13:19:53 UTC
Created attachment 347604 [details]
postgresql.tmpfilesd

Create dir in /run instead of /var/run
Comment 5 Alexander Tsoy 2013-05-07 13:31:48 UTC
(In reply to comment #1)
> Created attachment 347598 [details, diff] [details, diff]
> postgresql-server-9.2.4-r1.ebuild.patch

If you put postgresql.service and postgresql.tmpfilesd in postgresql-initscript-*.tbz2, then of course some changes should be made to this patch.
Comment 6 Alexander Tsoy 2013-05-07 20:37:11 UTC
(In reply to comment #2)
> Created attachment 347600 [details]
> postgresql.service

This file is a modified unit taken from fedora package. "postgresql-check-db-dir" is a script that checks that db is initialized and does not need an upgrade. I leave ExecStartPre commented for now.
Comment 7 Aaron W. Swenson gentoo-dev 2013-05-10 10:35:38 UTC
I'll need a postgresql.service file that's suitable for pre-9.2 servers as well before I can include it in Portage.
Comment 8 Alexander Tsoy 2013-07-23 23:50:29 UTC
Created attachment 354046 [details]
postgresql.service

This unit is suitable for all postgresql versions. I tested it with 8.4, 9.1 and 9.2.

- fixed ExecStart, ExecStop and ExecReload options
- added PIDFile option
- uncommented ExecStartPre
Comment 9 Alexander Tsoy 2013-07-23 23:51:01 UTC
Created attachment 354048 [details]
postgresql-check-db-dir

Script for ExecStartPre
Comment 10 Alexander Tsoy 2013-07-23 23:53:03 UTC
Created attachment 354050 [details, diff]
postgresql-server-9.2.4-r1.ebuild.patch

- install postgresql-check-db-dir script
- move sed commands in src_prepare()
Comment 11 Alexander Tsoy 2013-07-23 23:57:37 UTC
(In reply to Alexander Tsoy from comment #9)
> Created attachment 354048 [details]
> postgresql-check-db-dir
> 
> Script for ExecStartPre

It is based on checkconfig() from init script.
Comment 12 Alexander Tsoy 2013-07-24 00:59:25 UTC
Created attachment 354056 [details]
postgresql-check-db-dir

s/return/exit/
Comment 13 Alexander Tsoy 2013-07-24 22:11:18 UTC
Created attachment 354132 [details]
postgresql.service

Drop PIDFile. Variables not allowed in this option and unit works fine whithout it.
Comment 14 Constantine E. Kozlov 2013-08-02 23:25:54 UTC
Hmmm... sorry for messing all up.
Maybe doing something wrong, but attached files doesn't work.
Steps performed:
1. Copy ebuild of 9.2.4 to local overlay and rename to 9.2.4-r1;
2. download all files to "files" subfolder;
3. ebuild digest;
3. eix-update;
4. emerge -u postgresql-server;
5. emerge --config =dev-db/postgresql-server;

Actual result:
parmasse OhtarGentoo # systemctl start postgresql
Job for postgresql.service failed. See 'systemctl status postgresql.service' and 'journalctl -xn' for details.
parmasse OhtarGentoo # journalctl -xn
-- 
-- Unit postgresql.service has begun starting up.
авг 03 02:17:28 parmasse systemd[24084]: Failed at step EXEC spawning /usr/bin/postgresql-@SLOT@-check-db-dir: No such file or directory
-- Subject: Process /usr/bin/postgresql-@SLOT@-check-db-dir could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- Documentation: http://www.freedesktop.org/wiki/Software/systemd/catalog/641257651c1b4ec9a8624d7a40a9e1e7
-- 
-- The process /usr/bin/postgresql-@SLOT@-check-db-dir could not be executed and failed.
-- 
-- The error number returned while executing this process is 2.
авг 03 02:17:28 parmasse systemd[1]: postgresql.service: control process exited, code=exited status=203
авг 03 02:17:28 parmasse systemd[1]: Failed to start PostgreSQL database server.
-- Subject: Unit postgresql.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- Documentation: http://www.freedesktop.org/wiki/Software/systemd/catalog/be02cf6855d2428ba40df7e9d022f03d
-- 
-- Unit postgresql.service has failed.
-- 
-- The result is failed.
авг 03 02:17:28 parmasse systemd[1]: Unit postgresql.service entered failed state.


However, the file exists:
parmasse OhtarGentoo # ls -la /usr/bin/postgresql-9.2-check-db-dir 
-rwxr-xr-x 1 root root 1031 авг  3 02:15 /usr/bin/postgresql-9.2-check-db-dir


What am I missing?
Comment 15 Constantine E. Kozlov 2013-08-02 23:27:52 UTC
Damn, it's to late for gentoo-ing...
Forgot mentioning, that applied patch for ebuild
Comment 16 Emanuel Dávila 2013-08-03 20:27:10 UTC
(In reply to Constantine E. Kozlov from comment #14)
> Hmmm... sorry for messing all up.
> Maybe doing something wrong, but attached files doesn't work.
> Steps performed:
> 1. Copy ebuild of 9.2.4 to local overlay and rename to 9.2.4-r1;
> 2. download all files to "files" subfolder;
> 3. ebuild digest;
> 3. eix-update;
> 4. emerge -u postgresql-server;
> 5. emerge --config =dev-db/postgresql-server;
> 
> Actual result:
> parmasse OhtarGentoo # systemctl start postgresql
> Job for postgresql.service failed. See 'systemctl status postgresql.service'
> and 'journalctl -xn' for details.
> parmasse OhtarGentoo # journalctl -xn
> -- 
> -- Unit postgresql.service has begun starting up.
> авг 03 02:17:28 parmasse systemd[24084]: Failed at step EXEC spawning
> /usr/bin/postgresql-@SLOT@-check-db-dir: No such file or directory
> -- Subject: Process /usr/bin/postgresql-@SLOT@-check-db-dir could not be
> executed
> -- Defined-By: systemd
> -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
> -- Documentation:
> http://www.freedesktop.org/wiki/Software/systemd/catalog/
> 641257651c1b4ec9a8624d7a40a9e1e7
> -- 
> -- The process /usr/bin/postgresql-@SLOT@-check-db-dir could not be executed
> and failed.
> -- 
> -- The error number returned while executing this process is 2.
> авг 03 02:17:28 parmasse systemd[1]: postgresql.service: control process
> exited, code=exited status=203
> авг 03 02:17:28 parmasse systemd[1]: Failed to start PostgreSQL database
> server.
> -- Subject: Unit postgresql.service has failed
> -- Defined-By: systemd
> -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
> -- Documentation:
> http://www.freedesktop.org/wiki/Software/systemd/catalog/
> be02cf6855d2428ba40df7e9d022f03d
> -- 
> -- Unit postgresql.service has failed.
> -- 
> -- The result is failed.
> авг 03 02:17:28 parmasse systemd[1]: Unit postgresql.service entered failed
> state.
> 
> 
> However, the file exists:
> parmasse OhtarGentoo # ls -la /usr/bin/postgresql-9.2-check-db-dir 
> -rwxr-xr-x 1 root root 1031 авг  3 02:15 /usr/bin/postgresql-9.2-check-db-dir
> 
> 
> What am I missing?

I start it with...

systemctl start postgresql-9.2

It runs properly
Comment 17 Constantine E. Kozlov 2013-08-03 23:26:52 UTC
(In reply to Emanuel Dávila from comment #16)
> 
> I start it with...
> 
> systemctl start postgresql-9.2
> 
> It runs properly

Daaaamn... Sure, it's pretty fine. Before this, I tried to create such service manually from ARCHlinux forum, I succeed, but going here to post bug made me come to this page. So I just tried to start own broken service (postgresql.service) instead of this (postgresql-9.2.service).

Anyway, still have question to this bugfix:
 How such service will be handled through eselect? As example, now 9.3 unmasked and it'll, therefore, install new such service. Yes, it will not be started automatically just after install, but isn't it possible to handle eselect-ed slot (eselect postgresql show) through service like just 'postgresql.service'? 

Sure, I can simply suggest such changed service and ebuild, but I dont know, does such changes needed - it wasn't made, and I'm pretty new to writing ebuilds, services and other to know, was this omitted accidentally or not.
Comment 18 Aaron W. Swenson gentoo-dev 2013-08-04 18:15:26 UTC
(In reply to Constantine E. Kozlov from comment #17)
> (In reply to Emanuel Dávila from comment #16)
> > 
> > I start it with...
> > 
> > systemctl start postgresql-9.2
> > 
> > It runs properly
> 
> Daaaamn... Sure, it's pretty fine. Before this, I tried to create such
> service manually from ARCHlinux forum, I succeed, but going here to post bug
> made me come to this page. So I just tried to start own broken service
> (postgresql.service) instead of this (postgresql-9.2.service).
> 
> Anyway, still have question to this bugfix:
>  How such service will be handled through eselect? As example, now 9.3
> unmasked and it'll, therefore, install new such service. Yes, it will not be
> started automatically just after install, but isn't it possible to handle
> eselect-ed slot (eselect postgresql show) through service like just
> 'postgresql.service'? 
> 
> Sure, I can simply suggest such changed service and ebuild, but I dont know,
> does such changes needed - it wasn't made, and I'm pretty new to writing
> ebuilds, services and other to know, was this omitted accidentally or not.

'postgresql-config' will not handle this. The service files will be installed with the slot appended and it is up to the administrator what they want the default to be.
Comment 19 Pacho Ramos gentoo-dev 2013-08-20 09:14:27 UTC
@Aaron, will you take care of this? I can also commit the service files when I have time if you prefer :)
Comment 20 Aaron W. Swenson gentoo-dev 2013-08-21 02:16:26 UTC
(In reply to Pacho Ramos from comment #19)
> @Aaron, will you take care of this? I can also commit the service files when
> I have time if you prefer :)

I'm not terribly interested in systemd so I'm a bit suspect of all this, which is why I've been slow in adding this feature.

It seems to be moving away from what users have expressed they wanted: Control through /etc/postgresql-${SLOT}/postgresql.conf. It doesn't look like there's a way to keep what we've been gaining in the initscripts with the systemd service file.

Yes, I'll add this as it is...but it isn't a very good replacement for the existing initscripts.
Comment 21 Michał Górny archtester Gentoo Infrastructure gentoo-dev Security 2013-08-21 08:40:47 UTC
(In reply to Aaron W. Swenson from comment #20)
> (In reply to Pacho Ramos from comment #19)
> > @Aaron, will you take care of this? I can also commit the service files when
> > I have time if you prefer :)
> 
> I'm not terribly interested in systemd so I'm a bit suspect of all this,
> which is why I've been slow in adding this feature.
> 
> It seems to be moving away from what users have expressed they wanted:
> Control through /etc/postgresql-${SLOT}/postgresql.conf. It doesn't look
> like there's a way to keep what we've been gaining in the initscripts with
> the systemd service file.

I don't see what do you mean. AFAICS the attached .service file actually uses all that mess.

Though I don't understand why it's explicitly specifying the port. Unless I'm missing something, you are supposed to set the port via the config file. Does that have any disadvantages over the ugly -o '-p ...'?
Comment 22 Aaron W. Swenson gentoo-dev 2013-08-21 16:09:40 UTC
(In reply to Michał Górny from comment #21)
> (In reply to Aaron W. Swenson from comment #20)
> > (In reply to Pacho Ramos from comment #19)
> > > @Aaron, will you take care of this? I can also commit the service files when
> > > I have time if you prefer :)
> > 
> > I'm not terribly interested in systemd so I'm a bit suspect of all this,
> > which is why I've been slow in adding this feature.
> > 
> > It seems to be moving away from what users have expressed they wanted:
> > Control through /etc/postgresql-${SLOT}/postgresql.conf. It doesn't look
> > like there's a way to keep what we've been gaining in the initscripts with
> > the systemd service file.
> 
> I don't see what do you mean. AFAICS the attached .service file actually
> uses all that mess.

The service file is incapable of progressively attempting more aggressive means of stopping PostgreSQL.

If I can be proven wrong here I'd be happier about this.

> Though I don't understand why it's explicitly specifying the port. Unless
> I'm missing something, you are supposed to set the port via the config file.
> Does that have any disadvantages over the ugly -o '-p ...'?

The PGPORT can be dropped, but the initscript pulls that from the config file if it's set. Not grabbing the port information leads to a longer wait on finding out that the server can't bind to a port. So, it's a convenience thing rather than a technical requirement.

The start command for 9.2+ is a bit messy and can be simplified so that it's easier to read. Something I was planning to do anyway.

Some of the mess is there because of legacy systems. I'll be removing some bits and pieces that would no longer be a concern after some time passes.

Several users, though, have expressed repeatedly that they want postgresql.conf to control the server, and not have to edit /etc/conf.d/postgresql-${SLOT}. Specifically the port, socket directory, and the data directory. That's what we lose with the service file.
Comment 23 Aaron W. Swenson gentoo-dev 2013-08-22 11:50:04 UTC
Almost ready [1] to commit to gentoo-x86. I can't test this, though, so if somebody would give it a go and make sure all the ebuilds are working as they should with systemd.

[1] https://github.com/titanofold/titanofold-gentoo-x86/tree/master/dev-db/postgresql-server
Comment 24 Alexander Tsoy 2013-08-23 14:08:42 UTC
(In reply to Aaron W. Swenson from comment #22) 
> The service file is incapable of progressively attempting more aggressive
> means of stopping PostgreSQL.

We can use multiple ExecStop commands in unit, something like this:

ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m smart -t $NICE_TIMEOUT
ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m fast -t $RUDE_TIMEOUT
ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m immediate -t $FORCE_TIMEOUT

I didn't test it yet. May be the first two commands should be prefixed with '-'.

(In reply to Aaron W. Swenson from comment #22)
> Several users, though, have expressed repeatedly that they want
> postgresql.conf to control the server, and not have to edit
> /etc/conf.d/postgresql-${SLOT}. Specifically the port, socket directory, and
> the data directory. That's what we lose with the service file.

I have a question about the data directory. Is it possible with pg_ctl? Seems pg_ctl expects that -D is pointing to the directory with pid file and probably postmaster.opts (${DATA_DIR}), so I have no idea how to define data directory only in postgresql.conf.
Comment 25 Aaron W. Swenson gentoo-dev 2013-08-26 00:38:54 UTC
(In reply to Alexander Tsoy from comment #24)
> (In reply to Aaron W. Swenson from comment #22) 
> > The service file is incapable of progressively attempting more aggressive
> > means of stopping PostgreSQL.
> 
> We can use multiple ExecStop commands in unit, something like this:
> 
> ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> smart -t $NICE_TIMEOUT
> ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> fast -t $RUDE_TIMEOUT
> ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> immediate -t $FORCE_TIMEOUT
> 
> I didn't test it yet. May be the first two commands should be prefixed with
> '-'.

If you could make that work, I'd be happy to commit it. In a properly working setup `pg_ctl stop ... immediate` should never be encountered, but `pg_ctl stop ... fast` might be regularly encountered because of long lived programs writing to the database, like rsyslog.

> I have a question about the data directory. Is it possible with pg_ctl?
> Seems pg_ctl expects that -D is pointing to the directory with pid file and
> probably postmaster.opts (${DATA_DIR}), so I have no idea how to define data
> directory only in postgresql.conf.

-D is supposed to point at the directory containing either the database files or configuration files. Apparently this isn't documented for pg_ctl, but that is the way it works for 9.2 and later.

However, we don't have the initscripts pulling the data directory from the configuration file anyway. (I thought it did, but I was mistaken.) Again, technically nothing prevents us from doing so now with 9.2 and later, but for simplicity we can wait until 9.1 and older reach end of life.
Comment 26 Aaron W. Swenson gentoo-dev 2013-08-28 23:58:26 UTC
(In reply to Aaron W. Swenson from comment #25)
> (In reply to Alexander Tsoy from comment #24)
> > (In reply to Aaron W. Swenson from comment #22) 
> > > The service file is incapable of progressively attempting more aggressive
> > > means of stopping PostgreSQL.
> > 
> > We can use multiple ExecStop commands in unit, something like this:
> > 
> > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > smart -t $NICE_TIMEOUT
> > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > fast -t $RUDE_TIMEOUT
> > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > immediate -t $FORCE_TIMEOUT
> > 
> > I didn't test it yet. May be the first two commands should be prefixed with
> > '-'.
> 
> If you could make that work, I'd be happy to commit it. In a properly
> working setup `pg_ctl stop ... immediate` should never be encountered, but
> `pg_ctl stop ... fast` might be regularly encountered because of long lived
> programs writing to the database, like rsyslog.

Or anyone for that matter since there seems to be quite a few followers on this now.
Comment 27 Elias Probst 2013-08-29 00:24:29 UTC
Did I miss something or is there a specific reason not to use service instances to refer to the different SLOTs?
Instead of creating a specific unit file for each SLOT, a single file could be enough to address all instances.

See also the unit for OpenVPN: http://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/net-misc/openvpn/files/openvpn.service?revision=1.1&view=markup

This would allow to refer to different SLOTs like this:
    systemctl start postgresql-server@9.2
whereas "9.2" would replace %i in the unit file.
Comment 28 Egor Y. Egorov 2013-08-29 02:27:21 UTC
(In reply to Elias Probst from comment #27)
> Did I miss something or is there a specific reason not to use service
> instances to refer to the different SLOTs?
> Instead of creating a specific unit file for each SLOT, a single file could
> be enough to address all instances.
> 
> See also the unit for OpenVPN:
> http://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/net-misc/openvpn/
> files/openvpn.service?revision=1.1&view=markup
> 
> This would allow to refer to different SLOTs like this:
>     systemctl start postgresql-server@9.2
> whereas "9.2" would replace %i in the unit file.

This is not good idea. Macros %I not available in ExecStart/ExecStop in path to binary. In addition to different versions of slots can support a variety of command line options. For exabple 8.4 support key -silent-mode=true, but 9.2 don't.
Comment 29 Elias Probst 2013-08-29 15:12:35 UTC
(In reply to Egor Y. Egorov from comment #28)
> This is not good idea. Macros %I not available in ExecStart/ExecStop in path
> to binary.
Do you have any sources for this? I can't find anything related, all that I could find looks like this is not a problem.
 
> In addition to different versions of slots can support a variety
> of command line options. For exabple 8.4 support key -silent-mode=true, but
> 9.2 don't.
Why not use SLOT specific conf.d/ files for this?
Comment 30 Alexander Tsoy 2013-08-29 22:51:22 UTC
(In reply to Elias Probst from comment #27)
> Did I miss something or is there a specific reason not to use service
> instances to refer to the different SLOTs?
> Instead of creating a specific unit file for each SLOT, a single file could
> be enough to address all instances.

postgresql-server ebuild is slotted. Unit installed by each slot should have an unique name.

> 
> See also the unit for OpenVPN:
> http://sources.gentoo.org/cgi-bin/viewvc.cgi/gentoo-x86/net-misc/openvpn/
> files/openvpn.service?revision=1.1&view=markup
> 
> This would allow to refer to different SLOTs like this:
>     systemctl start postgresql-server@9.2
> whereas "9.2" would replace %i in the unit file.

%i and other variables are not allowed in the path to command.
Comment 31 Alexander Tsoy 2013-08-29 23:06:16 UTC
(In reply to Aaron W. Swenson from comment #26)
> (In reply to Aaron W. Swenson from comment #25)
> > (In reply to Alexander Tsoy from comment #24)
> > > (In reply to Aaron W. Swenson from comment #22) 
> > > > The service file is incapable of progressively attempting more aggressive
> > > > means of stopping PostgreSQL.
> > > 
> > > We can use multiple ExecStop commands in unit, something like this:
> > > 
> > > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > > smart -t $NICE_TIMEOUT
> > > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > > fast -t $RUDE_TIMEOUT
> > > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > > immediate -t $FORCE_TIMEOUT
> > > 
> > > I didn't test it yet. May be the first two commands should be prefixed with
> > > '-'.
> > 
> > If you could make that work, I'd be happy to commit it. In a properly
> > working setup `pg_ctl stop ... immediate` should never be encountered, but
> > `pg_ctl stop ... fast` might be regularly encountered because of long lived
> > programs writing to the database, like rsyslog.
> 
> Or anyone for that matter since there seems to be quite a few followers on
> this now.

Most likely I'll have no time to do this until next week.

Also according to the recently published systemd/ebuild policy, using env variables in units is strongly discouraged. :(
https://wiki.gentoo.org/wiki/Systemd/Ebuild_policy
Comment 32 Egor Y. Egorov 2013-08-30 04:37:23 UTC
(In reply to Elias Probst from comment #29)
> Do you have any sources for this? I can't find anything related, all that I
> could find looks like this is not a problem.

The command line accepts "%" specifiers as described in systemd.unit(5). Note that the first argument of the command line (i.e. the program to          execute) may not include specifiers.

© man systemd.service
Comment 33 Pacho Ramos gentoo-dev 2013-09-11 11:59:19 UTC
systemctl start won't let you start services with "@" in their name
Comment 34 Pacho Ramos gentoo-dev 2013-09-11 12:55:54 UTC
(In reply to Pacho Ramos from comment #33)
> systemctl start won't let you start services with "@" in their name

Bleh, that is wrong, just tested :S
Comment 35 Alexander Tsoy 2013-09-15 19:28:16 UTC
(In reply to Aaron W. Swenson from comment #25)
> (In reply to Alexander Tsoy from comment #24)
> > (In reply to Aaron W. Swenson from comment #22) 
> > > The service file is incapable of progressively attempting more aggressive
> > > means of stopping PostgreSQL.
> > 
> > We can use multiple ExecStop commands in unit, something like this:
> > 
> > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > smart -t $NICE_TIMEOUT
> > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > fast -t $RUDE_TIMEOUT
> > ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > immediate -t $FORCE_TIMEOUT
> > 
> > I didn't test it yet. May be the first two commands should be prefixed with
> > '-'.
> 
> If you could make that work, I'd be happy to commit it. In a properly
> working setup `pg_ctl stop ... immediate` should never be encountered, but
> `pg_ctl stop ... fast` might be regularly encountered because of long lived
> programs writing to the database, like rsyslog.

This works:

ExecStop=-/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m smart -t $NICE_TIMEOUT
ExecStop=-/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m fast -t $RUDE_TIMEOUT

"stop -m immediate" is not needed - systemd kills all remaining processes. Exit status of both commands is ignored, and I dislike this solution. IMHO if something goes wrong, unit must enter failing state, that is not happen if we ignore exit status. Moreover, if other services use postgres (like rsyslog), then they unit files should be customized with "After=postgresql-SLOT". This way they will be started after postgres and will be stopped before it.
Comment 36 Aaron W. Swenson gentoo-dev 2013-09-15 21:07:12 UTC
(In reply to Alexander Tsoy from comment #35)
> This works:
> 
> ExecStop=-/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> smart -t $NICE_TIMEOUT
> ExecStop=-/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> fast -t $RUDE_TIMEOUT
> 
> "stop -m immediate" is not needed - systemd kills all remaining processes.
> Exit status of both commands is ignored, and I dislike this solution. IMHO
> if something goes wrong, unit must enter failing state, that is not happen
> if we ignore exit status. Moreover, if other services use postgres (like
> rsyslog), then they unit files should be customized with
> "After=postgresql-SLOT". This way they will be started after postgres and
> will be stopped before it.

It would be good to keep "immediate" mode in there as an example, commented out of course, as there may be a time other than machine shutdown that PostgreSQL will need to be stopped.

As for the command, couldn't we do:
 ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m smart -t $NICE_TIMEOUT || /usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m fast -t $RUDE_TIMEOUT

Then we'd get an appropriate exit status, right?
Comment 37 Alexander Tsoy 2013-09-15 21:30:03 UTC
(In reply to Aaron W. Swenson from comment #36)
> (In reply to Alexander Tsoy from comment #35)
> > This works:
> > 
> > ExecStop=-/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > smart -t $NICE_TIMEOUT
> > ExecStop=-/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> > fast -t $RUDE_TIMEOUT
> > 
> > "stop -m immediate" is not needed - systemd kills all remaining processes.
> > Exit status of both commands is ignored, and I dislike this solution. IMHO
> > if something goes wrong, unit must enter failing state, that is not happen
> > if we ignore exit status. Moreover, if other services use postgres (like
> > rsyslog), then they unit files should be customized with
> > "After=postgresql-SLOT". This way they will be started after postgres and
> > will be stopped before it.
> 
> It would be good to keep "immediate" mode in there as an example, commented
> out of course, as there may be a time other than machine shutdown that
> PostgreSQL will need to be stopped.
> 
> As for the command, couldn't we do:
>  ExecStop=/usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D ${DATA_DIR} -s -m
> smart -t $NICE_TIMEOUT || /usr/lib/postgresql-@SLOT@/bin/pg_ctl stop -D
> ${DATA_DIR} -s -m fast -t $RUDE_TIMEOUT
> 
> Then we'd get an appropriate exit status, right?

systemd doesn't support shell command lines. Following should work:
ExecStop=/bin/sh -c "... || ..."
but it's very ugly. :)
Comment 38 Mike Gilbert gentoo-dev 2013-09-16 00:54:23 UTC
If you want to do conditional stuff in ExecStop, it might be best to create an external shell script and call that. You could even share it with the openrc script.

However, given the behavior of postgres, the "smart" shutdown is the only one for which pg_ctl is actually needed.

pg_ctl -m fast sends SIGINT to the master, which then sends SIGTERM to the children. systemd will automatically send SIGTERM to all processes in the cgroup if ExecStop times out, so that's pretty much the same end result.

pg_ctl -m immediate sends SIGQUIT to the master, whereas the normal systemd fallback is SIGKILL. I'm not sure if you actually want to worry about this case.
Comment 39 Pacho Ramos gentoo-dev 2013-10-09 19:04:47 UTC
Exherbo is doing the stop in this way:
http://git.exherbo.org/arbor.git/tree/packages/dev-db/postgresql/files/systemd/9.2/postgresql.service
Comment 40 Alexander Tsoy 2013-10-10 09:14:28 UTC
(In reply to Pacho Ramos from comment #39)
> Exherbo is doing the stop in this way:
> http://git.exherbo.org/arbor.git/tree/packages/dev-db/postgresql/files/
> systemd/9.2/postgresql.service

This is the same idea, which Aaron proposed in comment 36. And imho "immediate" step is not needed here.
Comment 41 Pacho Ramos gentoo-dev 2013-10-11 17:27:24 UTC
+*postgresql-server-9.2.5 (11 Oct 2013)
+*postgresql-server-8.4.18 (11 Oct 2013)
+*postgresql-server-9.1.10 (11 Oct 2013)
+*postgresql-server-9.0.14 (11 Oct 2013)
+*postgresql-server-9.3.1 (11 Oct 2013)
+
+  11 Oct 2013; Patrick Lauer <patrick@gentoo.org>
+  +postgresql-server-8.4.18.ebuild, +postgresql-server-9.0.14.ebuild,
+  +postgresql-server-9.1.10.ebuild, +postgresql-server-9.2.5.ebuild,
+  +postgresql-server-9.3.1.ebuild:
+  Bump
 
This looks to include systemd unit files :O
Comment 42 Pacho Ramos gentoo-dev 2013-11-17 12:32:38 UTC
*** Bug 491418 has been marked as a duplicate of this bug. ***