Thread

Commits

  1. Add wal_sender_shutdown_timeout GUC to limit shutdown wait for replication

  1. hang during shutdown

    Jeremy Schneider <schneider@ardentperf.com> — 2026-07-05T21:54:42Z

    i recently saw a hung shutdown on a busy multi-TB production database. the
    hung shutdown had some unexpected downstream impacts unrelated to core
    postgres which turned a routine maintenance operation into a long dramatic
    overnight incident... blowing way past our planned maint window and
    spoiling my weekend... but that's another story and mainly just explains my
    stubborn persistence slowly following the dominoes backwards over this past
    month.  :)
    
    so far, it all started with this postgres hang during a shutdown. we didnt
    capture enough diagnostics to root-cause the domino before the hang, but
    the data i do have about this postgres hang is very peculiar. i'm working
    on some automation to gather more diagnostics if there is a recurrence. in
    the meantime i wanted to share what i do know, in case anyone else has seen
    something similar.
    
    postgres version 16.13
    Ubuntu 22.04.5 LTS
    kernel 5.15.0-1110-azure
    
    between log messages and wal contents, it appears PostmasterStateMachine
    transitioned into PM_WAIT_BACKENDS but never progressed beyond this for a
    full 30 minutes until external automation finally killed the processes. i
    think the evidence most strongly supports the idea that an autovacuum
    worker was somehow wedged, but no idea where it possibly could have been
    stuck. app was shut down before maint started, at hang time there were only
    three open "postgres" user connections from platform automation.
    
    here's what i have:
    
    04:24:07 wal checkpoint. two in-progress autovacs, one is writing
    FREEZE_PAGE and VISIBLE records, AI tells me this requires an xid.
    RUNNING_XACTS shows single open xact 2121426339 - this must belong to the
    autovac doing freeze & visibility.
    
    04:25:09.403 immediate checkpoint (explicit from automation), same running
    xid
    
    04:25:09.808 pg logs the fast shutdown req (pg_ctl stop -m fast from
    automation)
    
    04:25:09.815 both autovac workers log "terminating autovacuum process":
    die() -> ProcessInterrupts() -> ereport()
    
    04:30:09 XLOG_SWITCH and archiver uploads segment 0x30
    
    04:30:10 checkpooint_timeout=300s fires, 72 dirty buffers written.
    RUNNING_XACTS at this time shows xid 2121426339 is still open.
    
    04:35:10 XLOG_SWITCH and archiver uploads segment 0x31
    
    04:55:08 external automation SIGKILL
    
    
    based on the fact that we saw checkpoints and archivals a full 10 minutes
    later, that's why i think it never left PM_WAIT_BACKENDS. also, there was
    never any XLOG_CHECKPOINT_SHUTDOWN record or any XLOG_XACT_ABORT record
    written in the whole 30 minutes.
    
    AI tells me autovac process needs to call AbortTransaction() which calls
    ProcArrayEndTransaction() to remove xid from PGPROC and also calls
    RecordTransactionAbort() which should write the abort WAL record. neither
    of these happen, which suggests autovac was stuck after it logs its
    termination message and before it performs either of these actions. this is
    not a lot of code, and my AI couldnt find any obvious places where theres
    disk or network calls or anything else that could hang.
    
    successful checkpoints suggest that both the data and the wal disks were
    accepting writes, and IO was not hung on the whole. if there was a D-state
    hang then it would have been just one process, while others on same device
    did not hang.
    
    i'm pretty stumped!
    
    my plan right now is to work on the automation that fires the SIGKILL and
    just have it collect and log everything it can from /proc/pid/* before the
    kill. if this happens again we should get a little more info to go on.
    
    if anyone else has thoughts or ideas to add, lmk
    
    -Jeremy
    
    -- 
    http://about.me/jeremy_schneider
    
  2. Re: hang during shutdown

    Srinath Reddy Sadipiralla <srinath2133@gmail.com> — 2026-07-06T05:29:01Z

    Hi Jeremy,
    
    On Mon, Jul 6, 2026 at 3:25 AM Jeremy Schneider <schneider@ardentperf.com>
    wrote:
    
    > i recently saw a hung shutdown on a busy multi-TB production database. the
    > hung shutdown had some unexpected downstream impacts unrelated to core
    > postgres which turned a routine maintenance operation into a long dramatic
    > overnight incident... blowing way past our planned maint window and
    > spoiling my weekend... but that's another story and mainly just explains my
    > stubborn persistence slowly following the dominoes backwards over this past
    > month.  :)
    >
    
    sorry for ruining your weekend ;)
    
    
    >
    > so far, it all started with this postgres hang during a shutdown. we didnt
    > capture enough diagnostics to root-cause the domino before the hang, but
    > the data i do have about this postgres hang is very peculiar. i'm working
    > on some automation to gather more diagnostics if there is a recurrence. in
    > the meantime i wanted to share what i do know, in case anyone else has seen
    > something similar.
    >
    > postgres version 16.13
    > Ubuntu 22.04.5 LTS
    > kernel 5.15.0-1110-azure
    >
    > between log messages and wal contents, it appears PostmasterStateMachine
    > transitioned into PM_WAIT_BACKENDS but never progressed beyond this for a
    > full 30 minutes until external automation finally killed the processes. i
    > think the evidence most strongly supports the idea that an autovacuum
    > worker was somehow wedged, but no idea where it possibly could have been
    > stuck. app was shut down before maint started, at hang time there were only
    > three open "postgres" user connections from platform automation.
    >
    > here's what i have:
    >
    > 04:24:07 wal checkpoint. two in-progress autovacs, one is writing
    > FREEZE_PAGE and VISIBLE records, AI tells me this requires an xid.
    > RUNNING_XACTS shows single open xact 2121426339 - this must belong to the
    > autovac doing freeze & visibility.
    >
    > 04:25:09.403 immediate checkpoint (explicit from automation), same running
    > xid
    >
    > 04:25:09.808 pg logs the fast shutdown req (pg_ctl stop -m fast from
    > automation)
    >
    > 04:25:09.815 both autovac workers log "terminating autovacuum process":
    > die() -> ProcessInterrupts() -> ereport()
    >
    > 04:30:09 XLOG_SWITCH and archiver uploads segment 0x30
    >
    > 04:30:10 checkpooint_timeout=300s fires, 72 dirty buffers written.
    > RUNNING_XACTS at this time shows xid 2121426339 is still open.
    >
    > 04:35:10 XLOG_SWITCH and archiver uploads segment 0x31
    >
    > 04:55:08 external automation SIGKILL
    >
    >
    > based on the fact that we saw checkpoints and archivals a full 10 minutes
    > later, that's why i think it never left PM_WAIT_BACKENDS. also, there was
    > never any XLOG_CHECKPOINT_SHUTDOWN record or any XLOG_XACT_ABORT record
    > written in the whole 30 minutes.
    >
    > AI tells me autovac process needs to call AbortTransaction() which calls
    > ProcArrayEndTransaction() to remove xid from PGPROC and also calls
    > RecordTransactionAbort() which should write the abort WAL record. neither
    > of these happen, which suggests autovac was stuck after it logs its
    > termination message and before it performs either of these actions. this is
    > not a lot of code, and my AI couldnt find any obvious places where theres
    > disk or network calls or anything else that could hang.
    >
    > successful checkpoints suggest that both the data and the wal disks were
    > accepting writes, and IO was not hung on the whole. if there was a D-state
    > hang then it would have been just one process, while others on same device
    > did not hang.
    >
    >
    your autovac analysis makes sense, i think that after worker's fatal ->
    proc_exit(1)->
    proc_exit_prepare->calls a shmem_exit and stuck there, maybe because
    of any extension's shmem callback, idk, it would be awesome if you
    get the backtrace using gdb by connecting to the PID of autovacuum
    which got stuck , if it was not stuck then it goes to the ShutdownPostgres ,
    a InitPostgres before_shmem_exit callback and would have called
    AbortTransaction and write the WAL record, which is not the case as
    you mentioned.
    
    -- 
    Thanks :)
    Srinath Reddy Sadipiralla
    EDB: https://www.enterprisedb.com/
    
  3. Re: hang during shutdown

    Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com> — 2026-07-06T06:30:06Z

    Hi,
    
    On Sun, Jul 5, 2026 at 11:55 PM Jeremy Schneider
    <schneider@ardentperf.com> wrote:
    > so far, it all started with this postgres hang during a shutdown. we didnt capture enough diagnostics to root-cause the domino before the hang, but the data i do have about this postgres hang is very peculiar. i'm working on some automation to gather more diagnostics if there is a recurrence. in the meantime i wanted to share what i do know, in case anyone else has seen something similar.
    >
    > postgres version 16.13
    > Ubuntu 22.04.5 LTS
    > kernel 5.15.0-1110-azure
    >
    > between log messages and wal contents, it appears PostmasterStateMachine transitioned into PM_WAIT_BACKENDS but never progressed beyond this for a full 30 minutes until external automation finally killed the processes.
    
    Did you have an active logical replication stream at the time of the
    shutdown? If so, it's very likely due to a bug in the logical
    walsender that can be stuck in an infinite loop when the WAL is in a
    specific state (last record is a rollback crossing the WAL page
    boundary).
    
    A fix for that was released in the latest 16.14 minor release[0].
    
    [0]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=82935467a
    
    Regards,
    Anthonin Bonnefoy
    
    
    
    
  4. Re: hang during shutdown

    Jakub Wartak <jakub.wartak@enterprisedb.com> — 2026-07-06T12:42:10Z

    On Mon, Jul 6, 2026 at 8:30 AM Anthonin Bonnefoy
    <anthonin.bonnefoy@datadoghq.com> wrote:
    >
    > Hi,
    >
    > On Sun, Jul 5, 2026 at 11:55 PM Jeremy Schneider
    > <schneider@ardentperf.com> wrote:
    > > so far, it all started with this postgres hang during a shutdown. we didnt capture enough diagnostics to root-cause the domino before the hang, but the data i do have about this postgres hang is very peculiar. i'm working on some automation to gather more diagnostics if there is a recurrence. in the meantime i wanted to share what i do know, in case anyone else has seen something similar.
    > >
    > > postgres version 16.13
    > > Ubuntu 22.04.5 LTS
    > > kernel 5.15.0-1110-azure
    > >
    > > between log messages and wal contents, it appears PostmasterStateMachine transitioned into PM_WAIT_BACKENDS but never progressed beyond this for a full 30 minutes until external automation finally killed the processes.
    >
    > Did you have an active logical replication stream at the time of the
    > shutdown? If so, it's very likely due to a bug in the logical
    > walsender[..]
    
    Aren't walsenders in general problematic for shutdowns? (not just logical, of
    course it might be this). The PG 19 is going to have
    wal_sender_shutdown_timeout, and from description it seems to match [0] to
    something like that. 'netstat -ntpo' would help to see from such hanged
    system (if walsenders running there at this point at all).
    
    -J.
    
    [0] - https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=a8f45dee91768cf1447ffaf2527e499e75a194c3