Skip to content

Instantly share code, notes, and snippets.

@ArtemGr
Created January 15, 2014 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArtemGr/8434901 to your computer and use it in GitHub Desktop.
Save ArtemGr/8434901 to your computer and use it in GitHub Desktop.
The largestTables PostgreSQL function.
CREATE OR REPLACE FUNCTION public."largestTables" (
)
RETURNS TABLE (
relation text,
size text
) AS
$body$
BEGIN
-- http://wiki.postgresql.org/wiki/Disk_Usage
RETURN QUERY SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_relation_size(C.oid) DESC
LIMIT 20;
END;
$body$
LANGUAGE 'plpgsql'
STABLE
CALLED ON NULL INPUT
SECURITY INVOKER
COST 10 ROWS 20;
COMMENT ON FUNCTION public."largestTables"()
IS 'Run a query returning the size of the largest tables and indexes.
See http://wiki.postgresql.org/wiki/Disk_Usage.';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment