Skip to content
CYBERTEC PostgreSQL Blog· Laurenz Albe·· 9d ago

How to Deal with Slow Shutdown in PostgreSQL

How to deal with slow shutdown in PostgreSQL

Key Takeaway

Laurenz Albe explains why PostgreSQL shutdown can be slow: a clean shutdown runs a checkpoint that flushes dirty shared buffers to disk, so larger shared_buffers and more dirty pages mean longer shutdown.

Why It Matters

For PostgreSQL instances needing fast restarts, an explicit CHECKPOINT before shutdown shortens downtime; with large UNLOGGED tables you must use FLUSH_UNLOGGED (v19+) or the FILE_COPY database trick, or shutdown stays slow.

Technical Breakdown · Distilled from the original source

Shutdown Modes and the Root Cause

  • Smart shutdown (SIGTERM) waits for clients; immediate shutdown (SIGQUIT) is fast but forces crash recovery on next start. Oracle users beware: Oracle's IMMEDIATE equals PostgreSQL's fast shutdown.
  • Fast shutdown (SIGINT) terminates connections and runs a checkpoint flushing dirty shared buffers to disk; larger shared_buffers and more dirty pages mean slower shutdown.
  • Running psql -c CHECKPOINT before pg_ctl restart triggers a fast checkpoint so the shutdown checkpoint has little to do.

Why UNLOGGED Tables Still Slow Shutdown

  • UNLOGGED tables skip WAL, so normal checkpoints don't flush their dirty pages (they'd be empty after crash recovery), but a clean shutdown must persist them.
  • Experiment with shared_buffers ≥2GB and a 1116 MB UNLOGGED table: after a manual CHECKPOINT, the shutdown checkpoint still wrote 142902 buffers (54.5%).
  • Source shows four flush cases: during shutdown; CHECKPOINT (FLUSH_UNLOGGED) from v19; end of recovery; start of CREATE DATABASE with FILE_COPY and ALTER DATABASE ... SET TABLESPACE.

Workarounds and Verification

  • On v19+, use CHECKPOINT (FLUSH_UNLOGGED). On older versions, CREATE DATABASE dummy TEMPLATE template0 STRATEGY FILE_COPY; DROP DATABASE dummy; — STRATEGY arrived in v15 (default changed to WAL_LOG); omit it before v15.
  • Dirty the buffers via SELECT id FROM large FOR NO KEY UPDATE;, then createdb --strategy=file_copy dummy, dropdb dummy, pg_ctl stop; the shutdown checkpoint wrote only 2 buffers.
Source: CYBERTEC PostgreSQL Blog· cybertec-postgresql.com

PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.

Read Official Source

Related PostgreSQL Reference