Skip to content

Instantly share code, notes, and snippets.

@elhoyos
Created May 8, 2020 15:26
Show Gist options
  • Save elhoyos/9193ba59cb8cf93296cb3181bb8f7130 to your computer and use it in GitHub Desktop.
Save elhoyos/9193ba59cb8cf93296cb3181bb8f7130 to your computer and use it in GitHub Desktop.
-- Count the number of rows for all tables in a schema
-- From: https://stackoverflow.com/a/38684225/638425
select table_schema,
table_name,
(xpath('/row/cnt/text()', xml_count))[1]::text::bigint as row_count
from (
select table_name, table_schema,
query_to_xml(format('select count(*) as cnt from %I.%I', table_schema, table_name), false, true, '') as xml_count
from information_schema.tables
where table_schema = 'public' --<< change here for the schema you want
) t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment