Issue 5
Turning off temp file cleanup for speed only moves the stall
Turning off PostgreSQL's remove_temp_files_after_crash defers temp file cleanup to the next start, the step AWS saw delay recovery at 2.2 million files.
Mat Caniglia
Engineering leader
Why now
A new walk-through by Christophe Pettus, published October 24, explains that turning off remove_temp_files_after_crash leaves the cleanup stall for the next postmaster start. For a platform team that runs PostgreSQL for product teams, crash recovery time is incident time, and it lands on whoever holds the pager. The setting looks like a quick lever for slow restarts, so it is worth knowing what it moves before someone flips it during an outage.
1. One backend crash orphans every temporary file on the server
In PostgreSQL, the crash of one backend takes every other backend down with it, because the postmaster sends a quit signal to each child. Those children exit without running any cleanup, on purpose, because shared memory may be corrupt and the exit path cannot be trusted. Spill files and temporary table storage are left behind for every session, including sessions that did nothing wrong. Version 14 added remove_temp_files_after_crash, which this edition calls temp file cleanup, and with its default of on it deletes the files during reinitialization, before crash recovery begins. Before 14, with restart_after_crash on, they stayed until the next postmaster start, and in Pettus's example of a crash under heavy analytic load, a second crash doubled the pile until the disk filled.
2. Your temp file metrics cannot count what a crash leaves behind
The temp file counters in pg_stat_database and the log_temp_files line are written by the backend at the moment it deletes the file. A backend killed mid-query deletes nothing, so it reports nothing, and the dashboard stays quiet. Pettus adds that pg_ls_tmpdir() skips directories, which hides the filesets parallel queries leave, and temporary table storage sits outside pgsql_tmp entirely. A full count needs a shell on the data directory. The marker he gives is a temp file or fileset directory whose PID matches no session in pg_stat_activity.
3. Turning temp file cleanup off moves the stall to the next start
The deletion runs single-threaded in the postmaster while the server is down, so every file adds to the time nobody can connect. In his sandbox, 300,000 planted files on local ext4 cost the postmaster 2.6 seconds, and he notes those were empty files on local disk. AWS raised the concern on pgsql-hackers in September 2021, while 14 was in beta, after startup cleanup had already delayed recovery in its fleet. Switching remove_temp_files_after_crash off does make reinitialization fast again. The next postmaster start then deletes the same files unconditionally, with the same stall, because the startup path never reads this parameter.
4. The file count is the real fault, so leave temp file cleanup on
Pettus reads millions of temp files as an exploded hash join or a work_mem small enough that every connection spills. Off has one legitimate use in his post, dissecting a specific crash, where you copy the files out before any restart and then turn it back on. I would leave remove_temp_files_after_crash on and fix a slow reinitialization through work_mem and hash_mem_multiplier tuning. This week, pick your last crash on a version 14 or later server with remove_temp_files_after_crash and restart_after_crash both on. Read the log gap between the reinitializing line and the start of crash recovery, since that is where the cleanup runs.
One question