Skip to content

Instantly share code, notes, and snippets.

@CHERTS
Created March 27, 2024 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 CHERTS/ca9aec32d3cd4871c8882f91d5d71113 to your computer and use it in GitHub Desktop.
Save CHERTS/ca9aec32d3cd4871c8882f91d5d71113 to your computer and use it in GitHub Desktop.
Getting the number of records in all tables in PostgreSQL (use count)
WITH tbl AS (
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_name NOT LIKE 'pg_%' AND table_schema NOT IN ('information_schema')
)
SELECT table_schema, table_name,
(xpath('/row/c/text()', query_to_xml(format('SELECT count(1) AS c FROM "%I"."%I"', table_schema, table_name), FALSE, TRUE, '')))[1]::text::bigint AS table_count
FROM tbl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment