• Tom Lane's avatar
    Fix "pg_ctl start -w" to test child process status directly. · 6bcce258
    Tom Lane authored
    pg_ctl start with -w previously relied on a heuristic that the postmaster
    would surely always manage to create postmaster.pid within five seconds.
    Unfortunately, that fails much more often than we would like on some of the
    slower, more heavily loaded buildfarm members.
    
    We have known for quite some time that we could remove the need for that
    heuristic on Unix by using fork/exec instead of system() to launch the
    postmaster.  This allows us to know the exact PID of the postmaster, which
    allows near-certain verification that the postmaster.pid file is the one
    we want and not a leftover, and it also lets us use waitpid() to detect
    reliably whether the child postmaster has exited or not.
    
    What was blocking this change was not wanting to rewrite the Windows
    version of start_postmaster() to avoid use of CMD.EXE.  That's doable
    in theory but would require fooling about with stdout/stderr redirection,
    and getting the handling of quote-containing postmaster switches to
    stay the same might be rather ticklish.  However, we realized that
    we don't have to do that to fix the problem, because we can test
    whether the shell process has exited as a proxy for whether the
    postmaster is still alive.  That doesn't allow an exact check of the
    PID in postmaster.pid, but we're no worse off than before in that
    respect; and we do get to get rid of the heuristic about how long the
    postmaster might take to create postmaster.pid.
    
    On Unix, this change means that a second "pg_ctl start -w" immediately
    after another such command will now reliably fail, whereas previously
    it would succeed if done within two seconds of the earlier command.
    Since that's a saner behavior anyway, it's fine.  On Windows, the case can
    still succeed within the same time window, since pg_ctl can't tell that the
    earlier postmaster's postmaster.pid isn't the pidfile it is looking for.
    To ensure stable test results on Windows, we can insert a short sleep into
    the test script for pg_ctl, ensuring that the existing pidfile looks stale.
    This hack can be removed if we ever do rewrite start_postmaster(), but that
    no longer seems like a high-priority thing to do.
    
    Back-patch to all supported versions, both because the current behavior
    is buggy and because we must do that if we want the buildfarm failures
    to go away.
    
    Tom Lane and Michael Paquier
    6bcce258
pg_ctl.c 62.4 KB