]> granicus.if.org Git - postgresql/commit
Detach postmaster process from pg_ctl's session at server startup.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Mon, 14 Jan 2019 12:50:58 +0000 (14:50 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Mon, 14 Jan 2019 12:50:58 +0000 (14:50 +0200)
commitbb24439cefead34f195c78f400982f3834593df6
tree126461b2b4befa8e0463665277c5c5cee9b1ef91
parentd9cdb1ba9e66039d0f4c037a7ae6e2839e9652d6
Detach postmaster process from pg_ctl's session at server startup.

pg_ctl is supposed to daemonize the postmaster process, so that it's not
affected by signals to the launching process group.  Before this patch, if
you had a shell script that used "pg_ctl start", and you interrupted the
shell script after postmaster had been launched, postmaster was also
killed.  To fix, call setsid() after forking the postmaster process.

Long time ago, we had a 'silent_mode' option, which daemonized the
postmaster process by calling setsid(), but that was removed back in 2011
(commit f7ea6beaf4).  We discussed bringing that back in some form, but
pg_ctl is the documented way of launching postmaster to the background, so
putting the setsid() call in pg_ctl itself seems appropriate.

Just putting postmaster in a separate session would change the behavior
when you interrupt "pg_ctl -w start", e.g. with CTRL-C, while it's waiting
for postmaster to start.  The historical behavior has been that
interrupting pg_ctl aborts the server launch, which is handy if the server
is stuck in recovery, for example, and won't fully start up.  To keep that
behavior, install a signal handler in pg_ctl, to explicitly kill
postmaster, if pg_ctl is interrupted while it's waiting for the server to
start up.  This isn't 100% watertight, there is a small window after
forking the postmaster process, where the signal handler doesn't know the
postmaster's PID yet, but seems good enough.

Arguably this is a long-standing bug, but I refrained from back-batching,
out of fear of breaking someone's scripts that depended on the old
behavior.

Reviewed by Tom Lane.  Report and original patch by Paul Guo, with
feedback from Michael Paquier.

Discussion: https://www.postgresql.org/message-id/CAEET0ZH5Bf7dhZB3mYy8zZQttJrdZg_0Wwaj0o1PuuBny1JkEw%40mail.gmail.com
src/bin/pg_ctl/pg_ctl.c