#!/usr/bin/perl use charnames ':full'; # only necessary before v5.16 use strict; use v5.10; use warnings; use DBIx::SimpleConnect; use Encode qw(:fallbacks encode); binmode STDOUT, ":encoding(UTF-8)"; my $db = shift // "default"; my $dbh = DBIx::SimpleConnect->connect($db, {RaiseError => 1}); my $table_usage = $dbh->selectall_arrayref(" select schemaname, tablename, pg_total_relation_size(quote_ident(schemaname) || '.' || quote_ident(tablename)) as s from pg_tables ", { Slice => {} } ); my $du; for my $tu (@$table_usage) { $du->{$tu->{schemaname}}{".s"} += $tu->{s}; $du->{$tu->{schemaname}}{$tu->{tablename}}{".s"} += $tu->{s}; $du->{$tu->{schemaname}}{$tu->{tablename}}{".ts"} += $tu->{s}; } my $index_usage = $dbh->selectall_arrayref(" select schemaname, tablename, indexname, pg_total_relation_size(quote_ident(schemaname) || '.' || quote_ident(indexname)) as s from pg_indexes ", { Slice => {} } ); for my $iu (@$index_usage) { $du->{$iu->{schemaname}}{".s"} += $iu->{s}; $du->{$iu->{schemaname}}{$iu->{tablename}}{".s"} += $iu->{s}; $du->{$iu->{schemaname}}{$iu->{tablename}}{$iu->{indexname}}{".s"} += $iu->{s}; } say ""; say ""; say "
", pretty($du->{$schema}{".s"}), " | "; say "", escape($schema), " | "; for my $table (sort { $du->{$schema}{$b}{".s"} <=> $du->{$schema}{$a}{".s"} } grep !/^\./, keys %{$du->{$schema}}) { say "||
", pretty($du->{$schema}{$table}{".s"}), " | "; say ""; say " | ", escape($table), " | "; if ($du->{$schema}{$table}{".ts"} != $du->{$schema}{$table}{".s"}) { say "|
", pretty($du->{$schema}{$table}{".ts"}), " | "; say ""; say " | "; say " | ", "(table)", " | "; } for my $index (sort { $du->{$schema}{$table}{$b}{".s"} <=> $du->{$schema}{$table}{$a}{".s"} } grep !/^\./, keys %{$du->{$schema}{$table}}) { say "
", pretty($du->{$schema}{$table}{$index}{".s"}), " | "; say ""; say " | "; say " | ", escape($index), " | "; } } } say "