Skip to content

Commit ec73ae8

Browse files
authored
Merge branch 'reclaim-the-stack:master' into master
2 parents 683756a + 9ba5905 commit ec73ae8

File tree

1 file changed

+51
-12
lines changed

1 file changed

+51
-12
lines changed

k

Lines changed: 51 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ def pg
824824
puts "pg:cache <cluster-name>"
825825
puts "pg:index_usage <cluster-name>"
826826
puts "pg:seq_scans <cluster-name>"
827-
puts "pg:table_size <cluster-name>"
827+
puts "pg:table_size <cluster-name> [table-name]"
828828
puts "pg:unused_indexes <cluster-name>"
829829
end
830830

@@ -1052,6 +1052,7 @@ def pg_seq_scans
10521052
cluster = ARGV.delete_at(0)
10531053
abort "Must pass name of cluster, eg. pg:seq_scans <cluster-name>" unless cluster
10541054

1055+
puts
10551056
Pg.query_on_primary cluster, <<~SQL
10561057
SELECT relname AS name,
10571058
seq_scan as count
@@ -1063,18 +1064,56 @@ end
10631064

10641065
def pg_table_size
10651066
cluster = ARGV.delete_at(0)
1066-
abort "Must pass name of cluster, eg. pg:table_size <cluster-name>" unless cluster
1067+
abort "Must pass name of cluster, eg. pg:table_size <cluster-name> [table-name]" unless cluster
10671068

1068-
Pg.query_on_primary cluster, <<~SQL
1069-
SELECT c.relname AS name,
1070-
pg_size_pretty(pg_table_size(c.oid)) AS size
1071-
FROM pg_class c
1072-
LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
1073-
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
1074-
AND n.nspname !~ '^pg_toast'
1075-
AND c.relkind='r'
1076-
ORDER BY pg_table_size(c.oid) DESC
1077-
SQL
1069+
table = ARGV.delete_at(0)
1070+
1071+
puts
1072+
1073+
if table
1074+
# Output table name followed by columns and their approximate sizes
1075+
puts "#{gray('===')} #{bold(table)}"
1076+
1077+
Pg.query_on_primary cluster, <<~SQL
1078+
WITH
1079+
-- 1) get the avg_width for each column from the last ANALYZE
1080+
col_stats AS (
1081+
SELECT
1082+
attname AS column_name,
1083+
avg_width
1084+
FROM pg_stats
1085+
WHERE tablename = '#{table}'
1086+
),
1087+
1088+
-- 2) grab the planner’s row estimate for the table
1089+
est_rows AS (
1090+
SELECT
1091+
reltuples::bigint AS row_estimate
1092+
FROM pg_class
1093+
WHERE relname = '#{table}'
1094+
)
1095+
1096+
SELECT
1097+
c.column_name,
1098+
pg_size_pretty((c.avg_width * e.row_estimate)::bigint) AS estimated_size
1099+
FROM col_stats c
1100+
CROSS JOIN est_rows e
1101+
ORDER BY estimated_size DESC;
1102+
SQL
1103+
else
1104+
Pg.query_on_primary cluster, <<~SQL
1105+
SELECT
1106+
c.relname AS table_name,
1107+
pg_size_pretty(pg_table_size(c.oid)) AS table_size,
1108+
(c.reltuples::bigint) AS row_estimate
1109+
FROM pg_class c
1110+
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
1111+
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
1112+
AND n.nspname !~ '^pg_toast'
1113+
AND c.relkind = 'r'
1114+
ORDER BY pg_table_size(c.oid) DESC;
1115+
SQL
1116+
end
10781117
end
10791118

10801119
def pg_unused_indexes

0 commit comments

Comments
 (0)