{"id":552,"date":"2017-05-30T16:27:44","date_gmt":"2017-05-30T20:27:44","guid":{"rendered":"http:\/\/jasonralph.org\/?p=552"},"modified":"2017-05-30T16:35:47","modified_gmt":"2017-05-30T20:35:47","slug":"postgres-top-100-tables-in-tablespace","status":"publish","type":"post","link":"https:\/\/jasonralph.org\/?p=552","title":{"rendered":"POSTGRES &#8211; Top 100 Tables In Tablespace"},"content":{"rendered":"<p>I had a situation where I needed to find the top 100 largest tables in a certain tablespace on a postgres 9 database, in my case we archive tables into an archive1 tablespace. This query will find all the largest relations in the archive1 tablespace. Its important to swap out &#8216;archive1&#8217; with whatever tablespace you are trying to list.<\/p>\n<pre class=\"theme:solarized-dark lang:default decode:true \" >\r\n\r\n\r\nSELECT N.nspname || '.' || C.relname AS \"relation\",\r\n    CASE WHEN reltype = 0\r\n        THEN pg_size_pretty(pg_total_relation_size(C.oid)) || ' (index)'\r\n        ELSE pg_size_pretty(pg_total_relation_size(C.oid)) || ' (' ||  pg_size_pretty(pg_relation_size(C.oid)) || ' data)'\r\n    END AS \"size (data)\",\r\n    COALESCE(T.tablespace, I.tablespace, '') AS \"tablespace\"\r\nFROM pg_class C\r\nLEFT JOIN pg_namespace N ON  (N.oid = C.relnamespace)\r\nLEFT JOIN pg_tables T ON (T.tablename = C.relname)\r\nLEFT JOIN pg_indexes I ON (I.indexname = C.relname)\r\nLEFT JOIN pg_tablespace TS ON TS.spcname = T.tablespace\r\nLEFT JOIN pg_tablespace XS ON XS.spcname = I.tablespace\r\nWHERE nspname NOT IN ('pg_catalog','pg_toast','information_schema') \r\nAND COALESCE(T.tablespace, I.tablespace, '') = 'archive1'\r\nORDER BY pg_total_relation_size(C.oid) DESC LIMIT 100;\r\n                          relation                          |         size (data)          | tablespace\r\n------------------------------------------------------------+------------------------------+------------\r\n public.table1                                              | 172 GB (148 GB data)         | archive1\r\n public.table2                                              | 171 GB (147 GB data)         | archive1\r\n public.table3                                              | 32 GB (32 GB data)           | archive1\r\n\r\n--SNIP--\r\n\r\n<\/pre>\n<p>Hope this helps you out, took some time to get it to work. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a situation where I needed to find the top 100 largest tables in a certain tablespace on a postgres 9 database, in my case we archive tables into an archive1 tablespace. This query will find all the largest relations in the archive1 tablespace. Its important to swap out &#8216;archive1&#8217; with whatever tablespace you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,1],"tags":[51,52],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-coding-thoughts","category-general-code","tag-postgres","tag-sql"],"_links":{"self":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=552"}],"version-history":[{"count":5,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":557,"href":"https:\/\/jasonralph.org\/index.php?rest_route=\/wp\/v2\/posts\/552\/revisions\/557"}],"wp:attachment":[{"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jasonralph.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}