Why PostgreSQL shutdown drags
A clean shutdown ends with a checkpoint that writes every dirty buffer from shared memory to disk, so the time it takes scales with shared_buffers and with how many pages are currently modified. The shutdown mode is chosen by the signal sent to the postmaster:
SIGTERM— smart shutdown: refuse new connections, wait for the last client to leave, then shut down cleanlySIGINT— fast shutdown: drop all existing connections and shut down cleanlySIGQUIT— immediate shutdown: kill all processes without a clean shutdown
Smart mode is useless when you want a quick restart. Immediate mode is a trap: the server exits at once, but crash recovery on the next start can be just as slow. Oracle administrators should note that PostgreSQL's "fast" shutdown corresponds to what Oracle calls IMMEDIATE.
A checkpoint right before shutdown
The fewest dirty buffers exist immediately after a checkpoint, so the cheapest restart is a shutdown performed right after one — and that checkpoint should be a fast one rather than the default spread variant. Running the SQL statement CHECKPOINT produces exactly that:
# could take long, but the database is still up psql -c CHECKPOINT # should be fast pg_ctl restart
The UNLOGGED table problem
The trick above fails when large UNLOGGED tables are present. Those tables skip WAL for data modifications, which is why they are emptied by crash recovery but survive a clean shutdown. PostgreSQL therefore omits their dirty pages when checkpointing normally, yet the shutdown checkpoint must flush them — so the stop is slow anyway. An experiment assuming at least 2GB of shared_buffers shows the effect:
CREATE UNLOGGED TABLE large (
id bigint GENERATED ALWAYS AS IDENTITY,
s text STORAGE EXTERNAL
);
INSERT INTO large (s)
SELECT repeat('0123456789', 100)
FROM generate_series(1, 1000000);
SELECT pg_size_pretty(pg_table_size('large'));
pg_size_pretty
════════════════
1116 MB
With log_checkpoints = on, the log from a shutdown carried out as described above reads:
LOG: checkpoint starting: immediate force wait LOG: checkpoint complete: wrote 1 buffers (0.0%); [...] LOG: received fast shutdown request LOG: aborting any active transactions LOG: background worker "logical replication launcher" (PID 38137) exited with exit code 1 LOG: shutting down LOG: checkpoint starting: shutdown immediate LOG: checkpoint complete: wrote 142902 buffers (54.5%); [...] LOG: database system is shut down
The manual checkpoint did nothing, and the shutdown stayed slow.
Forcing unlogged data out of shared buffers
Looking at the source shows that dirty pages of UNLOGGED tables are flushed in four situations: during shutdown; from PostgreSQL v19 on, when CHECKPOINT (FLUSH_UNLOGGED) is executed; at the end of recovery, when the database is promoted to read-write; at the start of CREATE DATABASE with the traditional FILE_COPY strategy; and at the start of ALTER DATABASE ... SET TABLESPACE.
For v19 and later the answer is simply the explicit checkpoint with FLUSH_UNLOGGED. Older releases need another route. The first case is what we want to avoid, and recovery cannot be entered without a restart. The copy or move operations need current data files on disk, so one of them can be harnessed; CREATE DATABASE is the more convenient choice since it needs no tablespace:
CREATE DATABASE dummy TEMPLATE template0 STRATEGY FILE_COPY; DROP DATABASE dummy;
STRATEGY exists from PostgreSQL v15, the version that made WAL_LOG the default strategy. Below v15, omit that option. template0 serves as template because it is always empty, keeping CREATE DATABASE and DROP DATABASE as quick as possible.
Verifying the workaround
After a restart, the test table has to sit in shared buffers with all of its buffers dirty. The simplest way to achieve that is a row lock on every row:
SELECT id FROM large FOR NO KEY UPDATE;
Then perform a fast shutdown, using createdb and dropdb for convenience:
# omit the --strategy option before v15 createdb --strategy=file_copy dummy dropdb dummy pg_ctl stop
The log confirms the trick worked:
LOG: checkpoint starting: immediate force wait flush-all LOG: checkpoint complete: wrote 142864 buffers (54.5%); [...] LOG: checkpoint starting: immediate force wait LOG: checkpoint complete: wrote 0 buffers (0.0%); [...] LOG: checkpoint starting: immediate force wait LOG: checkpoint complete: wrote 3 buffers (0.0%); [...] LOG: received fast shutdown request LOG: aborting any active transactions LOG: background worker "logical replication launcher" (PID 1572) exited with exit code 1 LOG: shutting down LOG: checkpoint starting: shutdown immediate LOG: checkpoint complete: wrote 2 buffers (0.0%); [...] LOG: database system is shut down
Two extra checkpoints appear — one from the end of the CREATE DATABASE statement, one from DROP TABLE — but apart from the first, all of them had almost nothing to do and finished quickly.
Takeaway
An explicit checkpoint just before shutting down removes the usual cause of a slow stop. Where large unlogged tables exist, that is insufficient and a stronger measure is needed to make PostgreSQL write all dirty buffers to disk.



