After a PostgreSQL server stops uncleanly, through pg_ctl stop -m immediate or a kill -9, what happens at the next start, and what should you check?
answer
- the lock file comes first
- the control file still says running
- replay from the last checkpoint
- some things do not come back
basics
~10 sStartup first validates postmaster.pid, then runs crash recovery, replaying WAL from the last checkpoint before accepting connections. Synchronously committed data survives, but unlogged tables are emptied and cumulative statistics counters are reset.
solid answer
~50 sAt the next start the postmaster first checks `postmaster.pid`: if the PID in it is dead and no shared memory is still attached, it removes the stale file itself; if orphaned backends from a `kill -9` still hold shared memory, it refuses with `pre-existing shared memory block ... is still in use`. Then the startup process sees the control file still in the `in production` state, logs `database system was not properly shut down; automatic recovery in progress` and replays WAL from the last checkpoint, refusing clients with `the database system is starting up` until it finishes. Commits acknowledged under `synchronous_commit = on` survive. **Unlogged tables are truncated** and the `pg_stat_*` counters reset, so autovacuum's triggers start from zero. Check the log for `redo done` and `ready to accept connections`, do not kill it again if `pg_ctl` times out, and run `ANALYZE` on busy tables.
code
console · 12 lines$ pg_controldata -D /srv/pg/inventory | grep 'cluster state'
Database cluster state: in production
$ pg_ctl start -D /srv/pg/inventory -l /var/log/pg/inventory.log
waiting for server to start............................................................ stopped waiting
pg_ctl: server did not start in time
$ grep -E 'interrupted|recovery|redo|ready' /var/log/pg/inventory.log
2026-10-04 02:31:07.412 UTC [41872] LOG: database system was interrupted; last known up at 2026-10-04 02:10:11 UTC
2026-10-04 02:31:07.530 UTC [41872] LOG: database system was not properly shut down; automatic recovery in progress
2026-10-04 02:31:07.538 UTC [41872] LOG: redo starts at 0/3A000028
2026-10-04 02:32:43.854 UTC [41872] LOG: redo done at 0/5F1C2D40 system usage: CPU: user: 41.20 s, system: 9.87 s, elapsed: 96.31 s
2026-10-04 02:32:43.861 UTC [41870] LOG: checkpoint starting: end-of-recovery immediate wait
2026-10-04 02:32:51.207 UTC [41869] LOG: database system is ready to accept connectionsgo deeper
Know that an immediate stop or crash makes the next start replay WAL before accepting connections, and that committed data survives under default settings.
Explain how startup treats a stale postmaster.pid, which log lines mark crash recovery, and why unlogged tables come back empty.
Show the operational checks: do not kill a slow recovery, handle orphaned backends after kill -9, refresh statistics for autovacuum, and rebuild unlogged data.
Treat recovery time as a design input: checkpoint spacing, unlogged-table use and monitoring of statistics resets should match what the business can tolerate after an unplanned restart.
## Step 1: the data-directory lock Every PostgreSQL server holds `postmaster.pid` in its data directory while it runs. Its lines are the postmaster's PID, the data directory, the start time, the port, the socket directory, the first listen address, the shared memory key and a status word (`starting`, `ready`, `standby`, `stopping`). After an unclean stop the file is usually still there, and startup handles it like this: 1. If the recorded PID belongs to **no live process**, the old server is gone. Before reusing the directory it checks whether the recorded **shared memory segment** is still attached by any process. 2. If processes are still attached, typically backends orphaned when someone ran `kill -9` on the postmaster, it stops with `FATAL: pre-existing shared memory block (key …, ID …) is still in use` and the hint to terminate old server processes for that directory. 3. Otherwise it **deletes the stale file itself** and starts. No manual cleanup is needed in the normal case. 4. If the recorded PID now belongs to **some other live process** (PID reuse can happen after a host or container restart, when PID numbering starts over), it stops with `FATAL: lock file "postmaster.pid" already exists` and `Is another postmaster (PID …) running in data directory …?`. Confirm with `ps` that no `postgres` process uses this directory, then remove the file by hand. Never delete `postmaster.pid` under a running server: the postmaster rechecks it once a minute and performs an immediate shutdown if it has been removed or overwritten. ## Step 2: crash recovery A clean shutdown leaves the control file in state `shut down`. After an immediate stop or a crash it still says `in production`, which `pg_controldata` shows as `Database cluster state`. The startup process then: - logs `database system was interrupted; last known up at …` and `database system was not properly shut down; automatic recovery in progress`; - logs `redo starts at <LSN>` and replays WAL from the redo point of the last completed checkpoint; - logs `redo done at <LSN> system usage: …`, requests an **end-of-recovery checkpoint**, and finally logs `database system is ready to accept connections`. While this runs, clients get `FATAL: the database system is starting up` (57P03). Recovery time is roughly proportional to the WAL written since the last checkpoint, so it depends on how far apart checkpoints are; the replay mechanics themselves belong to the WAL topic. `pg_ctl start` waits only 60 seconds by default and may print `server did not start in time` while recovery is still progressing. That message does not mean recovery failed: watch the log, and do not kill and restart it, which only begins recovery again. ## What survives and what does not | Item | After an unclean stop | |---|---| | Transactions acknowledged under `synchronous_commit = on` | kept: their WAL was flushed before the commit returned | | Commits made with `synchronous_commit = off` | the most recent ones can be lost | | Unlogged tables and their indexes | **truncated** at startup | | Cumulative statistics (`pg_stat_*` counters) | **reset to zero** | | Planner statistics in `pg_statistic` | kept, they are ordinary catalog data | | Data in logged tables | consistent after WAL replay | The statistics reset matters more than it looks: autovacuum decides when to vacuum and analyse from counters such as the dead and modified tuple counts, so right after recovery it believes nothing has changed. An `ANALYZE` of the busiest tables refreshes their live and dead tuple estimates and gets autovacuum back on track. ## What to check after the start 1. The log shows `redo done` and `database system is ready to accept connections`, with no `PANIC` or repeated restart cycle. 2. `pg_controldata` reports `in production` while running and `shut down` after the next clean stop. 3. Applications that use unlogged tables, such as staging or cache tables, reload or rebuild them. 4. Busy tables get a manual `ANALYZE` so autovacuum's counters are meaningful again. 5. Standbys reconnect and resume streaming. 6. The reason for the unclean stop is understood; repeated immediate stops in a runbook are a smell. ## Immediate shutdown versus kill -9 An immediate shutdown is orderly in its own way: the postmaster sends `SIGQUIT` to every child, escalates to `SIGKILL` after five seconds and exits only when they are gone, so the next start finds no orphans. `kill -9` on the postmaster gives it no chance to do that, which is how the shared-memory refusal in step 1 arises. Separately, when a single backend crashes while the server runs, the postmaster terminates the other server processes and, with `restart_after_crash = on` (the default), reinitialises and runs the same crash recovery without anyone restarting it.
- Why does an unlogged table come back empty after an immediate shutdown but not after a fast one?Unlogged tables write no WAL, so crash recovery has nothing to rebuild them from. After any unclean stop the server resets them to empty at startup. A fast shutdown writes a shutdown checkpoint that flushes their pages, so the next start needs no recovery and the data stays.
- The server will not start and reports that a pre-existing shared memory block is still in use. What happened and what do you do?The postmaster was killed with `kill -9` (or crashed) while its backends kept running and stayed attached to shared memory. Find the leftover `postgres` processes for that data directory with `ps`, terminate them, and start again; startup then removes the stale `postmaster.pid` itself.
- A single backend segfaults on a running server. Do you need to restart PostgreSQL by hand?Usually not. The postmaster logs `terminating any other active server processes`, waits for them to exit, logs `all server processes terminated; reinitializing` and runs crash recovery, because `restart_after_crash` defaults to `on`. Every client is disconnected, so applications must reconnect.
- After any crash you must delete postmaster.pid by hand before starting.
- An immediate shutdown resets planner statistics, so plans go bad.
- pg_ctl saying the server did not start in time means recovery failed.
- Unlogged tables survive an immediate shutdown because nothing was rolled back.
- Deleting postmaster.pid lets you safely start a second server for a quick check.