Skip to content

Commit f36efc7

Browse files
authored
add table_disk_usage
1 parent 536e90c commit f36efc7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/pubLib03-admin.sql

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,36 @@ CREATE or replace FUNCTION doc_UDF_show_simple(
275275
INNER JOIN doc_UDF_show($1,$2,$3,$4) u ON s.oid=u.oid::text
276276
$f$ LANGUAGE SQL IMMUTABLE;
277277
-- SELECT * FROM doc_UDF_show_simple('','%geohash%','st_%');
278+
279+
-------
280+
281+
CREATE or replace FUNCTION table_disk_usage(
282+
p_schema_name text DEFAULT 'public', -- NULL is ALL; exact name.
283+
p_name_like text DEFAULT NULL -- NULL is ALL; used as '%name%'.
284+
) RETURNS TABLE (
285+
schema_name text, relname text,
286+
relkind char,
287+
"size" text,
288+
size_bytes bigint
289+
) AS $f$
290+
SELECT
291+
schema_name, relname,relkind,
292+
pg_size_pretty(table_size) AS size,
293+
table_size as size_bytes
294+
FROM (
295+
SELECT
296+
pg_catalog.pg_namespace.nspname AS schema_name,
297+
relname,
298+
pg_relation_size(pg_catalog.pg_class.oid) AS table_size,
299+
relkind
300+
FROM pg_catalog.pg_class
301+
JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
302+
WHERE relkind IN ('r','i','t','m','f','p','I') -- exclude view and sequences
303+
) t
304+
WHERE (p_schema_name IS NULL OR schema_name=p_schema_name)
305+
AND (p_name_like IS NULL OR relname LIKE ('%'||p_name_like||'%'))
306+
ORDER BY table_size DESC
307+
$f$ LANGUAGE SQL IMMUTABLE;
308+
-- SELECT * FROM table_disk_usage();
309+
-- SELECT * FROM table_disk_usage(null,'tmp') WHERE schema_name IN ('public','test');
310+

0 commit comments

Comments
 (0)