Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete temporary files in postgresql

I have a huge database of about 800GB. When I tried to run a query which groups certain variables and aggregates the result, it was stopping after running for a couple of hours. Postgres was throwing a message that disk space is full. After looking at the statistics I realized that the dB has about 400GB of temporary files. I believe these temp files where created while I was running the query. My question is how do I delete these temp files. Also, how do I avoid such problems - use cursors or for-loops to not process all the data at once? Thanks.

I'm using Postgres 9.2

like image 340
let_there_be_light Avatar asked Aug 29 '16 04:08

let_there_be_light


People also ask

How do I find temp files in PostgreSQL?

SELECT temp_files AS "Temporary files" , temp_bytes AS "Size of temporary files" FROM pg_stat_database db; And the Postgres manual has details for pg_stat_database : tmp_files bigint Number of temporary files created by queries in this database.

How do I delete everything in PostgreSQL?

First, specify the table from which you want to delete data in the DELETE FROM clause. Second, specify which rows to delete by using the condition in the WHERE clause. The WHERE clause is optional. However, if you omit it, the DELETE statement will delete all rows in the table.

How do I reclaim space in PostgreSQL?

In order to reclaim disk space and reduce the disk usage of the database, it may help perform a VACUUM FULL on the bigiq_db database to bring the size down. This should be done on recommendation from F5 support. The default post autovacuum settings may not prevent database bloat in some environments.


1 Answers

The temporary files that get created in base/pgsql_tmp during query execution will get deleted when the query is done. You should not delete them by hand.
These files have nothing to do with temporary tables, they are use to store data for large hash or sort operations that would not fit in work_mem.

Make sure that the query is finished or canceled, try running CHECKPOINT twice in a row and see if the files are still there. If yes, that's a bug; did the PostgreSQL server crash when it ran out of disk space?

If you really have old files in base/pgsql_tmp that do not get deleted automatically, I think it is safe to delete them manually. But I'd file a bug with PostgreSQL in that case.

There is no way to avoid large temporary files if your execution plan needs to sort large result sets or needs to create large hashes. Cursors won't help you there. I guess that with for-loops you mean moving processing from the database to application code – doing that is usually a mistake and will only move the problem from the database to another place where processing is less efficient.

Change your query so that it doesn't have to sort or hash large result sets (check with EXPLAIN). I know that does not sound very helpful, but there's no better way. You'll probably have to do that anyway, or is a runtime of several hours acceptable for you?

like image 130
Laurenz Albe Avatar answered Sep 22 '22 08:09

Laurenz Albe