• Tom Lane's avatar
    Improve autoprewarm's handling of early-shutdown scenarios. · ff769831
    Tom Lane authored
    Bad things happen if the DBA issues "pg_ctl stop -m fast" before
    autoprewarm finishes loading its list of blocks to prewarm.
    The current worker process successfully terminates early, but
    (if this wasn't the last database with blocks to prewarm) the
    leader process will just try to launch another worker for the
    next database.  Since the postmaster is now in PM_WAIT_BACKENDS
    state, it ignores the launch request, and the leader just sits
    until it's killed manually.
    
    This is mostly the fault of our half-baked design for launching
    background workers, but a proper fix for that is likely to be
    too invasive to be back-patchable.  To ameliorate the situation,
    fix apw_load_buffers() to check whether SIGTERM has arrived
    just before trying to launch another worker.  That leaves us with
    only a very narrow window in each worker launch where SIGTERM
    could occur between the launch request and successful worker start.
    
    Another issue is that if the leader process does manage to exit,
    it unconditionally rewrites autoprewarm.blocks with only the
    blocks currently in shared buffers, thus forgetting any blocks
    that we hadn't reached yet while prewarming.  This seems quite
    unhelpful, since the next database start will then not have the
    expected prewarming benefit.  Fix it to not modify the file if
    we shut down before the initial load attempt is complete.
    
    Per bug #16785 from John Thompson.  Back-patch to v11 where
    the autoprewarm code was introduced.
    
    Discussion: https://postgr.es/m/16785-c0207d8c67fb5f25@postgresql.org
    ff769831
autoprewarm.c 24.5 KB