From f7f9ac6eb875c20f160844634633b4c92650c8f3 Mon Sep 17 00:00:00 2001 From: Rayid Ghani Date: Thu, 5 May 2016 21:25:31 -0500 Subject: [PATCH] Create queries.sql --- queries.sql | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 queries.sql diff --git a/queries.sql b/queries.sql new file mode 100644 index 0000000..30a9f07 --- /dev/null +++ b/queries.sql @@ -0,0 +1,28 @@ +# get largest dbs in posrgres server +SELECT d.datname AS Name, pg_catalog.pg_get_userbyid(d.datdba) AS Owner, + CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') + THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname)) + ELSE 'No Access' + END AS SIZE +FROM pg_catalog.pg_database d + ORDER BY + CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') + THEN pg_catalog.pg_database_size(d.datname) + ELSE NULL + END DESC -- nulls first + LIMIT 10 + + + +# get largest tables in postgres db +SELECT nspname || '.' || relname AS "relation", + pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size" + FROM pg_class C + LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace) + WHERE nspname NOT IN ('pg_catalog', 'information_schema') + AND C.relkind <> 'i' + AND nspname !~ '^pg_toast' + ORDER BY pg_total_relation_size(C.oid) DESC + LIMIT 30; + +