• Heikki Linnakangas's avatar
    Detach postmaster process from pg_ctl's session at server startup. · bb24439c
    Heikki Linnakangas authored
    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 f7ea6bea).  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
    bb24439c
pg_ctl.c 65.5 KB