#!/usr/bin/perl use charnames ':full'; # only necessary before v5.16 use strict; use v5.24; use warnings; use autodie; use DBIx::SimpleConnect; use Encode qw(:fallbacks encode); use Getopt::Long; use Pod::Usage; use POSIX qw(strftime); my $db = "default"; my $dir = "."; GetOptions( 'dbname=s' => \$db, 'directory=s' => \$dir, ) or pod2usage(1); binmode STDOUT, ":encoding(UTF-8)"; { no autodie; unless (chdir($dir)) { # if at first we don't succeed ... mkdir $dir or die "cannot create $dir: $!"; chdir $dir or die "cannot chdir to $dir: $!"; } } my $errorfile = strftime("%Y-%m-%d %H:%M:%S%z.err", localtime); open STDERR, ">:encoding(UTF-8)", $errorfile; my $dbh = DBIx::SimpleConnect->connect($db, {RaiseError => 1, PrintError => 0}); # this is intended to run from cron, so limit the time it can run. $dbh->do("set statement_timeout to 30000"); my $overview = $dbh->selectall_hashref(" select schemaname, tablename, sum(pg_total_relation_size(quote_ident(schemaname) || '.' || quote_ident(tablename))) as total_size, sum(pg_table_size(quote_ident(schemaname) || '.' || quote_ident(tablename))) as table_size, sum(pg_indexes_size(quote_ident(schemaname) || '.' || quote_ident(tablename))) as indexes_size from pg_tables group by rollup (schemaname, tablename) ", ['schemaname', 'tablename'] ); open(my $fh, ">:encoding(UTF-8)", "index.html.$$"); print_header($fh); say $fh "
", "Schema", " | "; say $fh "", "Table", " | "; say $fh "", "Total size (bytes)", " | "; say $fh "", "Table size (bytes)", " | "; say $fh "", "Indexes size (bytes)", " | "; say $fh "
---|---|---|---|---|
", escape($s), " | "; say $fh "", '', " | "; say $fh "", pretty($overview->{$s}{''}{total_size}), " | "; say $fh "", pretty($overview->{$s}{''}{table_size}), " | "; say $fh "", pretty($overview->{$s}{''}{indexes_size}), " | "; say $fh "
", escape($s), " | "; say $fh "", escape($t), " | "; say $fh "", pretty($overview->{$s}{$t}{total_size}), " | "; say $fh "", pretty($overview->{$s}{$t}{table_size}), " | "; say $fh "", pretty($overview->{$s}{$t}{indexes_size}), " | "; say $fh "