Skip to content

Instantly share code, notes, and snippets.

@padak
Created July 30, 2014 21:18
Show Gist options
  • Save padak/393f50f1899b87f7bc12 to your computer and use it in GitHub Desktop.
Save padak/393f50f1899b87f7bc12 to your computer and use it in GitHub Desktop.
SELECT c.oid AS "Table ID",
s.oid AS "Schema ID",
trim(c.relname) AS "Name",
trim(s.nspname) AS "Schema",
trim(u.usename) AS "Owner",
coalesce(c.reltuples, 0)::bigint AS "Rows",
coalesce(t.rows, 0) AS "Rows with deleted",
coalesce(t.sorted_rows, 0) AS "Sorted Rows",
NULL::double PRECISION AS "Total GB",
coalesce(t.blocks * 1048576, 0)::bigint AS "Total Bytes",
coalesce(t.blocks, 0) AS "Total Blocks",
CASE
WHEN t.max_rows > t.min_rows THEN (t.max_rows - t.min_rows)::numeric(18,5) / t.max_rows
WHEN t.max_rows > t.min_rows
AND t.min_rows = 0 THEN 1::numeric(18,5)
ELSE 0
END::numeric(18,5) AS "Data Skew",
coalesce(t.min_rows, 0) AS "Min Rows",
coalesce(t.max_rows, 0) AS "Max Rows",
coalesce(t.temp, 0) AS "Temp Space",
c.relnatts AS "Columns",
'lg' AS db
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace s ON s.oid = c.relnamespace
AND s.nspname NOT IN ('pg_catalog',
'information_schema',
'pg_toast',
'pg_bitmapindex',
'pg_internal',
'pg_aoseg')
JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner
LEFT JOIN
(SELECT p.id,
p.db_id,
sum(coalesce(p.rows, 0)) AS ROWS,
sum(coalesce(p.sorted_rows, 0)) AS sorted_rows,
sum(coalesce(p.temp, 0)) AS TEMP,
min(coalesce(p.ROWS, 0)) AS min_rows,
max(coalesce(p.ROWS, 0)) AS max_rows,
sum(b.blocknum) AS blocks
FROM
(SELECT DISTINCT slice
FROM pg_catalog.stv_blocklist) bl
LEFT JOIN pg_catalog.stv_tbl_perm p ON p.slice = bl.slice
LEFT JOIN pg_catalog.pg_database d ON d.datname = 'lg'
LEFT JOIN
(SELECT bl.tbl,
bl.slice,
count(bl.blocknum) AS blocknum
FROM pg_catalog.stv_blocklist bl
GROUP BY bl.tbl,
bl.slice) b ON b.tbl = p.id
AND b.slice = p.slice
GROUP BY p.id,
p.db_id) t ON t.id = c.oid
WHERE c.relkind = 'r'
ORDER BY c.relname,
s.nspname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment