Skip to content

Instantly share code, notes, and snippets.

@PostgreSqlStan
Last active February 19, 2022 03:20
Show Gist options
  • Save PostgreSqlStan/0737b8d2f1592d2eac68d0124d509610 to your computer and use it in GitHub Desktop.
Save PostgreSqlStan/0737b8d2f1592d2eac68d0124d509610 to your computer and use it in GitHub Desktop.
PostgreSQL view to list tables/views from all schemas
-- view: ls (tested on postgres 14.1)
CREATE OR REPLACE VIEW ls AS
SELECT
N.nspname AS "schema",
relname AS "relation",
CASE c.relkind
WHEN 'f' THEN 'fdw table'
WHEN 'r' THEN 'table'
WHEN 'v' THEN 'view'
WHEN 'm' THEN 'm view' END AS "type",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total size",
obj_description(C.oid) AS "comment"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND nspname !~ '^pg_toast'
ORDER BY 1, 2;
COMMENT ON VIEW ls IS 'list relations in all schemas';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment