#!/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 ""; for my $schema (sort { $du->{$b}{".s"} <=> $du->{$a}{".s"} } keys %$du) { say ""; say ""; say ""; for my $table (sort { $du->{$schema}{$b}{".s"} <=> $du->{$schema}{$a}{".s"} } grep !/^\./, keys %{$du->{$schema}}) { say ""; say ""; say ""; say ""; if ($du->{$schema}{$table}{".ts"} != $du->{$schema}{$table}{".s"}) { say ""; say ""; say ""; say ""; say ""; } for my $index (sort { $du->{$schema}{$table}{$b}{".s"} <=> $du->{$schema}{$table}{$a}{".s"} } grep !/^\./, keys %{$du->{$schema}{$table}}) { say ""; say ""; say ""; say ""; say ""; } } } say "
", pretty($du->{$schema}{".s"}), "", escape($schema), "
", pretty($du->{$schema}{$table}{".s"}), "", escape($table), "
", pretty($du->{$schema}{$table}{".ts"}), "", "(table)", "
", pretty($du->{$schema}{$table}{$index}{".s"}), "", escape($index), "
"; sub pretty { my ($n) = @_; while ($n =~ /[0-9]{4}/) { $n =~ s/([0-9]+)([0-9]{3})/$1\N{NARROW NO-BREAK SPACE}$2/; } return $n; } sub escape { my ($s) = @_; $s =~ s/[<>&'"]/sprintf("&#%d;", ord($1))/eg; return $s; } # vim: sw=4 tw=132 expandtab