Viewing temporary file usage with Performance Insights
You can use Performance Insights to view temporary file usage by turning on the metrics
temp_bytes and temp_files. The view in
Performance Insights doesn't show the specific queries that generate temporary files, however, when
you combine Performance Insights with the query shown for pg_ls_tmpdir
, you can
troubleshoot, analyze, and determine the changes in your query workload.
-
In the Performance Insights dashboard, choose Manage Metrics.
-
Choose Database metrics, and select the temp_bytes and temp_files metrics as shown in the following image.
-
In the Top SQL tab, choose the Preferences icon.
-
In the Preferences window, turn on the following statistics to appear in the Top SQLtab and choose Continue.
-
Temp writes/sec
-
Temp reads/sec
-
Tmp blk write/call
-
Tmp blk read/call
-
-
The temporary file is broken out when combined with the query shown for
pg_ls_tmpdir
, as shown in the following example.
The IO:BufFileRead
and IO:BufFileWrite
events occur when the
top queries in your workload often create temporary files. You can use Performance
Insights to identify top queries waiting on IO:BufFileRead
and
IO:BufFileWrite
by reviewing Average Active Session (AAS) in Database
Load and Top SQL sections.

For more information on how to analyze top queries and load by wait event with Performance Insights, see Overview of the Top SQL tab. You should identify and tune the queries that cause increase in temporary file usage and related wait events. For more information on these wait events and remediation, see IO:BufFileRead and IO:BufFileWrite.
Note
The work_mem
As a best practice, when you have a large report with multiple joins and sorts,
set this parameter at the session level by using the SET work_mem
command. Then the change is only applied to the current session and doesn't
change the value globally.