Skip to content

Instantly share code, notes, and snippets.

@casperbrike
Created September 28, 2023 12:13
Show Gist options
  • Save casperbrike/5bba377d83aabac6bbbe748a24019e42 to your computer and use it in GitHub Desktop.
Save casperbrike/5bba377d83aabac6bbbe748a24019e42 to your computer and use it in GitHub Desktop.
Show list of tables and their rows count (PostgreSQL)
WITH tbl AS
(SELECT table_schema,
TABLE_NAME
FROM information_schema.tables
WHERE TABLE_NAME not like 'pg_%'
AND table_schema in ('public'))
SELECT table_schema,
TABLE_NAME,
(xpath('/row/c/text()', query_to_xml(format('select count(*) as c from %I.%I', table_schema, TABLE_NAME), FALSE, TRUE, '')))[1]::text::int AS rows_n
FROM tbl
ORDER BY rows_n DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment