-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtemp_files_summary.sql
More file actions
54 lines (54 loc) · 4.03 KB
/
Copy pathtemp_files_summary.sql
File metadata and controls
54 lines (54 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
WITH RECURSIVE
tablespace_dirs AS (
SELECT
dirname,
'pg_tblspc/' || dirname || '/' AS path,
1 AS depth
FROM
pg_catalog.pg_ls_dir('pg_tblspc/', true, false) AS dirname
UNION ALL
SELECT
subdir,
td.path || subdir || '/',
td.depth + 1
FROM
tablespace_dirs AS td,
pg_catalog.pg_ls_dir(td.path, true, false) AS subdir
WHERE
td.depth < 3
),
temp_dirs AS (
SELECT
td.path,
ts.spcname AS tablespace
FROM
tablespace_dirs AS td
INNER JOIN pg_catalog.pg_tablespace AS ts ON (ts.oid = substring(td.path FROM 'pg_tblspc/(\d+)')::int)
WHERE
td.depth = 3
AND
td.dirname = 'pgsql_tmp'
UNION ALL
VALUES
('base/pgsql_tmp/', 'pg_default')
),
temp_files AS (
SELECT
substring(filename FROM 'pgsql_tmp(\d+)')::int AS pid,
td.tablespace,
pg_stat_file(td.path || '/' || filename, true) AS file_stat
FROM
temp_dirs AS td,
pg_catalog.pg_ls_dir(td.path, true, false) AS filename
)
SELECT
tablespace,
count((file_stat).size) AS total_files,
pg_size_pretty(sum((file_stat).size)::BIGINT) AS total_size,
now() - min((file_stat).modification) AS oldest_modified
FROM
temp_files
GROUP BY
1
HAVING
count((file_stat).size) > 0;