Created
March 27, 2024 11:52
-
-
Save CHERTS/ca9aec32d3cd4871c8882f91d5d71113 to your computer and use it in GitHub Desktop.
Getting the number of records in all tables in PostgreSQL (use count)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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