Skip to content

Instantly share code, notes, and snippets.

@snelson82
Last active November 1, 2023 22:32
Show Gist options
  • Save snelson82/a072eabd1158f39511df29e75deadd73 to your computer and use it in GitHub Desktop.
Save snelson82/a072eabd1158f39511df29e75deadd73 to your computer and use it in GitHub Desktop.
PostgreSQL - All tables and record count
SELECT
table_schema,
table_name,
(xpath('/row/cnt/text()', xml_count))[1]::text::int 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') t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment