On Wed, 2024-02-28 at 12:16 +0100, Dimitrios Apostolou wrote: > yesterday I was doing: > > ALTER TABLE x ADD UNIQUE ... , ADD PRIMARY KEY ...; > > The table is almost a billion rows long but lies in its own TABLESPACE > that has plenty of space. But apparently the ALTER TABLE command is > writing a lot to the default tablespace (not the temp_tablespace, that is > already moved to a different partition). > > That quickly filled up the 50GB free space in my root partition: > > > 20:18:04.222 UTC [94144] PANIC: could not write to file "pg_wal/xlogtemp.94144": No space left on device > [...] > 20:19:11.578 UTC [94140] LOG: WAL writer process (PID 94144) was terminated by signal 6: Aborted > 20:19:11.578 UTC [94140] LOG: terminating any other active server processes > > > After postgresql crashed and restarted, the disk space in the root > partition was still not freed! I believe this is because of "orphaned > files" as discussed in mailing list thread [1]. > > [1] https://www.postgresql.org/message-id/CAN-RpxDBA7HbTsJPq4t4VznmRFJkssP2SNEMuG%3DoNJ%2B%3DsxLQew%40mail.gmail.com > > I ended up doing some risky actions to remediate the problem: Find the > filenames that have no identically named "oid" in pg_class, and delete > (move to backup) the biggest ones while the database is stopped. > Fortunately the database started up fine after that! Lucky you. It should have been "relfilenode" rather than "oid", and some catalog tables don't have their files listed in the catalog, because they are needed *before* the database can access tables. > So what is the moral of the story? How to guard against this? Monitor disk usage ... The root of the problem is that you created the index in the default tablespace. You should have ALTER TABLE x ADD UNIQUE ... USING INDEX TABLESPACE bigtblspc; > Needless to say, I would have hoped the database cleaned-up after itself > even after an uncontrolled crash, or that it provided some tools for the > job. (I tried VACUUM FULL on the table, but the orphaned files did not go > away). That is not so simple... Also, it would slow down crash recovery. But I agree that it would be nice to have a tool that reports or cleans up orphaned files. Yours, Laurenz Albe