My database is suddenly hanging on some queries that join many tables and I see:
temporary file size exceeds temp_file_limit (1025563kB)
A sample query and query plan can be seen here:
http://www.sharecsv.com/s/a6333a7f610e049dd81ebcfc19a4c02f/temp_file_limit_exceeded.csv
This query normally takes less than 100ms but hangs when hit temporary file size limit.
When I run:
SELECT temp_files AS "Temporary files", temp_bytes AS "Size of temporary files"
FROM pg_stat_database db;
I see:
Temporary files Size of temporary files
---
22550 10651900248
How do I resolve this?
I was able to enable more of the problem. Say a user lists they can speak 2 languages - the query runs ok. Then they edit their profile to say they know 20 languages. The query ends up exceeding the temp_file_limit and hanging.
You are joining around 40 tables to retrieve around 200 columns. I did not dig in but, typically, such a query is a misunderstanding.Yes, I am! The use case is a detailed user profile, where most fields many-to-many to lookup tables. I am not sure how to optimize that query, so I will create a separate question for that if that's the issue. But increasing mytemp_file_limithelped (on Google Cloud SQL it was capped).work_memwas at 4GB and I increased to 64GB, but didn't notice much difference here. Google Cloud SQL is on Postgres 9.6. – atkayla Mar 24 '19 at 14:00work_memtoo high can have adverse effects. It very much depends on available RAM, number of concurrent sessions and the complete workload, but 4 GB is already an exceptionally high setting and 64 GB seems way out of proportion except for specialized warehouse systems. – Erwin Brandstetter Mar 24 '19 at 23:04work_memon a 3.75GB RAM database. :P – atkayla Mar 24 '19 at 23:23temp_file_limitshowed me that the real problem might just be that my schema is just poor, specifically all of those surrogate key joins to lookup tables, when I've always heard that surrogate keys are recommended and that relational databases are good at joins. I'm in the process of refactoring to remove those surrogate keys and instead using natural keys as per https://dba.stackexchange.com/a/232955/133732, therefore eliminating those 40 joins. I really hope that will help this situation. – atkayla Mar 25 '19 at 00:19enumtypes, too - sometimes a reasonable middle ground. And consider upgrading to Postgres 11 if possible. – Erwin Brandstetter Mar 25 '19 at 01:44