Skip to content

Instantly share code, notes, and snippets.

@mccun934
Created December 11, 2018 15:44
Show Gist options
  • Save mccun934/32b3b76991b8c1036a7b2cbb67ce95cd to your computer and use it in GitHub Desktop.
Save mccun934/32b3b76991b8c1036a7b2cbb67ce95cd to your computer and use it in GitHub Desktop.
postgres tablesizes
#!/bin/bash
TABLEQUERY=$(cat <<-END
SELECT table_name, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
, pg_indexes_size(c.oid) AS index_bytes
, pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r'
) a
) a order by total_bytes DESC LIMIT 30;
END
)
# echo $TABLEQUERY
cd ~postgres
echo ""
echo "************* Candlepin Tablesizes *************"
echo ""
echo $TABLEQUERY | sudo -u postgres psql -d candlepin
echo ""
echo "************** Foreman Tablesizes **************"
echo ""
echo $TABLEQUERY | sudo -u postgres psql -d foreman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment