| 1 |
#!/usr/bin/env perl |
| 2 |
#------------------------------------------------------------------------------ |
| 3 |
# |
| 4 |
# pgbadger_tools - Tools based on pgBadger binary files |
| 5 |
# |
| 6 |
# This program is open source, licensed under the PostgreSQL Licence. |
| 7 |
# For license terms, see the LICENSE file. |
| 8 |
#------------------------------------------------------------------------------ |
| 9 |
# |
| 10 |
# This program is first used to demonstrate how to deal with pgBadger binary |
| 11 |
# files to build your own tool. |
| 12 |
# |
| 13 |
# See README.tools for more explanations. |
| 14 |
# |
| 15 |
#------------------------------------------------------------------------------ |
| 16 |
use vars qw($VERSION); |
| 17 |
|
| 18 |
use strict; |
| 19 |
|
| 20 |
use Getopt::Long qw(:config no_ignore_case bundling); |
| 21 |
use File::Spec qw/ tmpdir /; |
| 22 |
use File::Temp qw/ tempfile /; |
| 23 |
use IO::File; |
| 24 |
use IO::Handle; |
| 25 |
use Storable qw(store_fd fd_retrieve); |
| 26 |
|
| 27 |
$VERSION = '2.0'; |
| 28 |
|
| 29 |
my $PSQL_BIN = 'psql'; |
| 30 |
my $TMP_DIR = File::Spec->tmpdir() || '/tmp'; |
| 31 |
|
| 32 |
my @SQL_ACTION = ('SELECT', 'INSERT', 'UPDATE', 'DELETE', 'COPY FROM', 'COPY TO', 'CTE', 'DDL', 'TCL'); |
| 33 |
|
| 34 |
# Where statistics are stored in pgbadger binary files |
| 35 |
my %overall_stat = (); |
| 36 |
my %pgb_overall_stat = (); |
| 37 |
my %overall_checkpoint = (); |
| 38 |
my %top_slowest = (); |
| 39 |
my %normalyzed_info = (); |
| 40 |
my %error_info = (); |
| 41 |
my %pgb_error_info = (); |
| 42 |
my %logs_type = (); |
| 43 |
my %per_minute_info = (); |
| 44 |
my %pgb_per_minute_info = (); |
| 45 |
my %pgb_pool_info = (); |
| 46 |
my %lock_info = (); |
| 47 |
my %tempfile_info = (); |
| 48 |
my %connection_info = (); |
| 49 |
my %pgb_connection_info = (); |
| 50 |
my %database_info = (); |
| 51 |
my %application_info = (); |
| 52 |
my %user_info = (); |
| 53 |
my %host_info = (); |
| 54 |
my %session_info = (); |
| 55 |
my %pgb_session_info = (); |
| 56 |
my %conn_received = (); |
| 57 |
my %checkpoint_info = (); |
| 58 |
my %autovacuum_info = (); |
| 59 |
my %autoanalyze_info = (); |
| 60 |
my %cur_info = (); |
| 61 |
my %cur_temp_info = (); |
| 62 |
my %cur_lock_info = (); |
| 63 |
my %tsung_session = (); |
| 64 |
my %errors_code = (); |
| 65 |
my @top_locked_info = (); |
| 66 |
my @top_tempfile_info = (); |
| 67 |
my @log_files = (); |
| 68 |
my $nlines = 0; |
| 69 |
my $sample = 3; |
| 70 |
my $top = 20; |
| 71 |
my $anonymize = 0; |
| 72 |
|
| 73 |
# Let's say --explode was not used to generate the binary files |
| 74 |
# in this case all data are stored under the postgres database. |
| 75 |
my $def_db = 'postgres'; |
| 76 |
|
| 77 |
################################################################## |
| 78 |
# Get the command line parameters |
| 79 |
# Add your own option |
| 80 |
################################################################## |
| 81 |
# General options |
| 82 |
my $help = 0; |
| 83 |
my $quiet = 0; |
| 84 |
my $debug = 0; |
| 85 |
my $pghost = ''; |
| 86 |
my $pgport = ''; |
| 87 |
my $pguser = ''; |
| 88 |
my $pgdb = ''; |
| 89 |
|
| 90 |
# Tools related option |
| 91 |
my $explain_slowest = 0; |
| 92 |
my $analyze = 0; |
| 93 |
my $max_duration = 0; |
| 94 |
my $file_per_query = 0; |
| 95 |
my $format_query = 0; |
| 96 |
my $explain_time_consuming = 0; |
| 97 |
my $explain_normalized = 0; |
| 98 |
my $csv_time_consuming = 0; |
| 99 |
my $csv_slowest = 0; |
| 100 |
my $csv_normalized = 0; |
| 101 |
my $csv_filename = 'out.csv'; |
| 102 |
|
| 103 |
my $result = GetOptions( |
| 104 |
'h|host=s' => \$pghost, |
| 105 |
'p|port=i' => \$pgport, |
| 106 |
'U|username=s' => \$pguser, |
| 107 |
'd|dbname=s' => \$pgdb, |
| 108 |
'q|quiet!' => \$quiet, |
| 109 |
'v|verbose!' => \$debug, |
| 110 |
'help!' => \$help, |
| 111 |
# Explain slowest query tool |
| 112 |
'explain-slowest!' => \$explain_slowest, |
| 113 |
'explain-time-consuming!' => \$explain_time_consuming, |
| 114 |
'explain-normalized!' => \$explain_normalized, |
| 115 |
'analyze!' => \$analyze, |
| 116 |
'max-duration=i' => \$max_duration, |
| 117 |
'file-per-query!' => \$file_per_query, |
| 118 |
'format-query!' => \$format_query, |
| 119 |
'top=i' => $top, |
| 120 |
# CSV output of top queries tool |
| 121 |
'csv-time-consuming!' => \$csv_time_consuming, |
| 122 |
'csv-slowest!' => \$csv_slowest, |
| 123 |
'csv-normalized!' => \$csv_normalized, |
| 124 |
'csv-filename=s' => \$csv_filename, |
| 125 |
); |
| 126 |
|
| 127 |
# Show help an exit |
| 128 |
if ($help) { |
| 129 |
&usage(); |
| 130 |
} |
| 131 |
|
| 132 |
# Lookup for binary files to load from command line |
| 133 |
my @file_list = (); |
| 134 |
foreach my $f (@ARGV) { |
| 135 |
push(@file_list, $f) if (-e $f && ($f =~ /\.bin$/)); |
| 136 |
} |
| 137 |
|
| 138 |
die "FATAL: no binary file found, see usage (--help).\n" if ($#file_list == -1); |
| 139 |
|
| 140 |
# Load all data gathered by all the different processes. This function |
| 141 |
# is responsible of loading pgbadger statistics from binary files |
| 142 |
foreach my $f (@file_list) { |
| 143 |
next if (-z "$f"); |
| 144 |
my $fht = new IO::File; |
| 145 |
$fht->open("< $f") or die "FATAL: can't open temp file $f, $!\n"; |
| 146 |
&load_stats($fht); |
| 147 |
$fht->close(); |
| 148 |
} |
| 149 |
|
| 150 |
# Set the psql command follwing the option |
| 151 |
my $psql_cmd = ''; |
| 152 |
$psql_cmd .= " -h $pghost" if ($pghost); |
| 153 |
$psql_cmd .= " -p $pgport" if ($pgport); |
| 154 |
$psql_cmd .= " -U $pguser" if ($pguser); |
| 155 |
$psql_cmd .= " -d $pgdb" if ($pgdb ); |
| 156 |
if ($psql_cmd) { |
| 157 |
$psql_cmd = $PSQL_BIN . ' ' . $psql_cmd; |
| 158 |
} |
| 159 |
|
| 160 |
#################################################################### |
| 161 |
# 1srt tool: Dump top slowest queries inside explain statement. Will |
| 162 |
# be executed when option --explain-slowest is set at command line |
| 163 |
# Add your own condition. See also TOOL FUNCTIONS bellow |
| 164 |
################################################################## |
| 165 |
if ($explain_slowest) { |
| 166 |
&logmsg('LOG', "Output each slowest queries within explain statement."); |
| 167 |
&dump_slowest_queries($def_db); |
| 168 |
} |
| 169 |
# when option --explain-time-consuming is set at command line |
| 170 |
if ($explain_time_consuming) { |
| 171 |
&logmsg('LOG', "Output each most times consuming queries within explain statement."); |
| 172 |
&dump_times_consuming_queries($def_db); |
| 173 |
} |
| 174 |
# when option --explain-normalized is set at command line |
| 175 |
if ($explain_normalized) { |
| 176 |
&logmsg('LOG', "Output each slowest normalized queries within explain statement."); |
| 177 |
&dump_normalized_slowest_queries($def_db); |
| 178 |
} |
| 179 |
|
| 180 |
#################################################################### |
| 181 |
# 2nd tool: Dump top queries into CSV file. Will be executed when |
| 182 |
# option --csv-time_consuming or --csv-slowest or --csv-normalized |
| 183 |
# are enabled. |
| 184 |
################################################################## |
| 185 |
if ($csv_time_consuming || $csv_slowest || $csv_normalized) { |
| 186 |
# Check that Text::CSV is available |
| 187 |
if (eval {require Text::CSV;1;} ne 1) { |
| 188 |
die("Can not save output in csv format, please install Perl module Text::CSV first.\n"); |
| 189 |
} else { |
| 190 |
Text::CSV->import(); |
| 191 |
} |
| 192 |
if ($csv_time_consuming) { |
| 193 |
&csv_times_consuming_queries($def_db); |
| 194 |
} elsif ($csv_slowest) { |
| 195 |
&csv_slowest_queries($def_db); |
| 196 |
} elsif ($csv_normalized) { |
| 197 |
&csv_normalized_queries($def_db); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
################################################################## |
| 203 |
# Add your own bellow |
| 204 |
################################################################## |
| 205 |
|
| 206 |
exit 0; |
| 207 |
|
| 208 |
################################################################## |
| 209 |
# Display pgbadger_tools usage with the list of options |
| 210 |
# Add your own in the Tools section with a sample. |
| 211 |
################################################################## |
| 212 |
sub usage |
| 213 |
{ |
| 214 |
|
| 215 |
print qq{ |
| 216 |
Usage: pgbadger_tools [options] [options tools] BINARY_FILE |
| 217 |
|
| 218 |
Options: |
| 219 |
|
| 220 |
-d | --dbname DBNAME : same as in psql command, see psql --help |
| 221 |
-h | --host HOST : same as in psql command, see psql --help |
| 222 |
-p | --port PORT : same as in psql command, see psql --help |
| 223 |
-q | --quiet : do not print any information |
| 224 |
-U | --username NAME : same as in psql command, see psql --help |
| 225 |
-v | --verbose : show debug information |
| 226 |
--help : Show this message |
| 227 |
|
| 228 |
Note: option -d, -h, -p and -U are passed directly to the psql command. |
| 229 |
The psql command must be in the PATH environment variable. If you have |
| 230 |
authentication for the connection, use .pgpass. This allow to execute |
| 231 |
queries to a PostgreSQL backend and get the output. |
| 232 |
|
| 233 |
Options Tools: |
| 234 |
|
| 235 |
Generate EXPLAIN statements |
| 236 |
--------------------------- |
| 237 |
|
| 238 |
This tool allow to generate EXPLAIN statements with the top slowest queries |
| 239 |
reported by pgBadger. Here are the supported options, only the first one is |
| 240 |
mandatory: |
| 241 |
|
| 242 |
--explain-slowest : generate explain statements of slowest queries |
| 243 |
|
| 244 |
./pgbadger_tools --explain-slowest out.bin |
| 245 |
|
| 246 |
--explain-time-consuming : generate explain statements of time consuming queries |
| 247 |
|
| 248 |
./pgbadger_tools --explain-time-consuming out.bin |
| 249 |
|
| 250 |
--explain-normalized : generate explain statements of normalized slowest queries |
| 251 |
|
| 252 |
./pgbadger_tools --explain-normalized out.bin |
| 253 |
|
| 254 |
--analyze : generate explain analyze statements of slowest queries |
| 255 |
|
| 256 |
./pgbadger_tools --explain-slowest --analyze out.bin |
| 257 |
|
| 258 |
--max-duration MS : set the number of milliseconds above which queries |
| 259 |
will not be reported. Use it if you want to auto |
| 260 |
execute explain statements. |
| 261 |
|
| 262 |
--file-per-query : will dump each query into a separate file named qryXXX.sql |
| 263 |
|
| 264 |
--format-query : do simple query formating |
| 265 |
|
| 266 |
To automatically execute those EXPLAIN statements and get the results with |
| 267 |
the queries, you just have to set at least one of the -d, -h, -p or -U |
| 268 |
command. For example, if the PostgreSQL instance is local and use peer as |
| 269 |
authent method for the postgres user and listen on default port: |
| 270 |
|
| 271 |
./pgbadger_tools --explain-slowest --analyze -d postgres out.bin |
| 272 |
|
| 273 |
Default is to generate EXPLAIN statement for the top 20 of queries, you can |
| 274 |
change this value using --top N. |
| 275 |
|
| 276 |
Options --explain-slowest, --explain-time-consuming and --explain-normalized |
| 277 |
can be use together in the same pgbadger_tools command. |
| 278 |
|
| 279 |
Generate CSV file with top queries |
| 280 |
---------------------------------- |
| 281 |
|
| 282 |
This tool allow to dump most time consuming statements reported by pgBadger. |
| 283 |
Here are the supported options, only one of the following is mandatory: |
| 284 |
|
| 285 |
--csv-time-consuming : generate a CSV file with top time consuming queries |
| 286 |
--csv-slowest : generate a CSV file with top slowest queries |
| 287 |
--csv-normalized : generate a CSV file with top normalized queries |
| 288 |
|
| 289 |
./pgbadger_tools --csv-time-consuming out.bin |
| 290 |
|
| 291 |
Those options can not be used together. By default the output file is named |
| 292 |
out.csv, you can use the --csv-filename option to renamed this file. Ex: |
| 293 |
|
| 294 |
./pgbadger_tools --csv-slowest --csv-filename slowest.csv out.bin |
| 295 |
|
| 296 |
Option to limit top queries to minimum duration: |
| 297 |
|
| 298 |
--max-duration MS : set the number of milliseconds above which queries |
| 299 |
will not be reported. Use it if you want to auto |
| 300 |
execute explain statements. |
| 301 |
|
| 302 |
|
| 303 |
}; |
| 304 |
exit 0; |
| 305 |
} |
| 306 |
|
| 307 |
################################################################## |
| 308 |
# Internal method, do not edit unless you know what you are doing |
| 309 |
################################################################## |
| 310 |
|
| 311 |
#### |
| 312 |
# Display message following the log level |
| 313 |
#### |
| 314 |
sub logmsg |
| 315 |
{ |
| 316 |
my ($level, $str) = @_; |
| 317 |
|
| 318 |
return if ($quiet && ($level ne 'FATAL')); |
| 319 |
return if (!$debug && ($level eq 'DEBUG')); |
| 320 |
|
| 321 |
if ($level =~ /(\d+)/) { |
| 322 |
print STDERR "\t" x $1; |
| 323 |
} |
| 324 |
|
| 325 |
print STDERR "$level: $str\n"; |
| 326 |
} |
| 327 |
|
| 328 |
#### |
| 329 |
# Stores top N error sample queries, taken from pgbadger script |
| 330 |
#### |
| 331 |
sub set_top_error_sample |
| 332 |
{ |
| 333 |
my ($curdb, $q, $date, $real_error, $detail, $context, $statement, $hint, $db, $user, $app, $remote, $sqlstate) = @_; |
| 334 |
|
| 335 |
$errors_code{$curdb}{$sqlstate}++ if ($sqlstate); |
| 336 |
|
| 337 |
# Stop when we have our number of samples |
| 338 |
if (!exists $error_info{$curdb}{$q}{date} || ($#{$error_info{$curdb}{$q}{date}}+1 < $sample)) { |
| 339 |
if ( ($q =~ /deadlock detected/) || ($real_error && !grep(/^\Q$real_error\E$/, @{$error_info{$curdb}{$q}{error}})) ) { |
| 340 |
if ($anonymize) { |
| 341 |
$context = &anonymize_query($context); |
| 342 |
$statement = &anonymize_query($statement); |
| 343 |
$detail = &anonymize_query($detail); |
| 344 |
} |
| 345 |
|
| 346 |
push(@{$error_info{$curdb}{$q}{date}}, $date); |
| 347 |
push(@{$error_info{$curdb}{$q}{detail}}, $detail); |
| 348 |
push(@{$error_info{$curdb}{$q}{context}}, $context); |
| 349 |
push(@{$error_info{$curdb}{$q}{statement}}, $statement); |
| 350 |
push(@{$error_info{$curdb}{$q}{hint}}, $hint); |
| 351 |
push(@{$error_info{$curdb}{$q}{error}}, $real_error); |
| 352 |
push(@{$error_info{$curdb}{$q}{db}}, $db); |
| 353 |
push(@{$error_info{$curdb}{$q}{user}}, $user); |
| 354 |
push(@{$error_info{$curdb}{$q}{app}}, $app); |
| 355 |
push(@{$error_info{$curdb}{$q}{remote}}, $remote); |
| 356 |
push(@{$error_info{$curdb}{$q}{sqlstate}}, $sqlstate); |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
#### |
| 362 |
# Load statistics from binary file into memory, the function is copied from pgbadger script |
| 363 |
#### |
| 364 |
sub load_stats |
| 365 |
{ |
| 366 |
|
| 367 |
my $fd = shift; |
| 368 |
|
| 369 |
no strict; |
| 370 |
|
| 371 |
my %stats = %{ fd_retrieve($fd) }; |
| 372 |
my %_overall_stat = %{$stats{overall_stat}}; |
| 373 |
my %_pgb_overall_stat = %{$stats{pgb_overall_stat}}; |
| 374 |
my %_overall_checkpoint = %{$stats{overall_checkpoint}}; |
| 375 |
my %_normalyzed_info = %{$stats{normalyzed_info}}; |
| 376 |
my %_error_info = %{$stats{error_info}}; |
| 377 |
my %_pgb_error_info = %{$stats{pgb_error_info}}; |
| 378 |
my %_pgb_pool_info = %{$stats{pgb_pool_info}}; |
| 379 |
my %_connection_info = %{$stats{connection_info}}; |
| 380 |
my %_pgb_connection_info = %{$stats{pgb_connection_info}}; |
| 381 |
my %_database_info = %{$stats{database_info}}; |
| 382 |
my %_application_info = %{$stats{application_info}}; |
| 383 |
my %_user_info = %{$stats{user_info}}; |
| 384 |
my %_host_info = %{$stats{host_info}}; |
| 385 |
my %_checkpoint_info = %{$stats{checkpoint_info}}; |
| 386 |
my %_session_info = %{$stats{session_info}}; |
| 387 |
my %_pgb_session_info = %{$stats{pgb_session_info}}; |
| 388 |
my %_tempfile_info = %{$stats{tempfile_info}}; |
| 389 |
my %_cancelled_info = %{$stats{cancelled_info}}; |
| 390 |
my %_logs_type = %{$stats{logs_type}}; |
| 391 |
my %_errors_code = %{$stats{errors_code}}; |
| 392 |
my %_lock_info = %{$stats{lock_info}}; |
| 393 |
my %_per_minute_info = %{$stats{per_minute_info}}; |
| 394 |
my %_pgb_per_minute_info = %{$stats{pgb_per_minute_info}}; |
| 395 |
my %_top_slowest = %{$stats{top_slowest}}; |
| 396 |
my $_nlines = $stats{nlines}; |
| 397 |
my $_first_log_timestamp = $stats{first_log_timestamp}; |
| 398 |
my $_last_log_timestamp = $stats{last_log_timestamp}; |
| 399 |
my @_log_files = @{$stats{log_files}}; |
| 400 |
my %_autovacuum_info = %{$stats{autovacuum_info}}; |
| 401 |
my %_autoanalyze_info = %{$stats{autoanalyze_info}}; |
| 402 |
my %_top_locked_info = %{$stats{top_locked_info}}; |
| 403 |
my %_top_tempfile_info = %{$stats{top_tempfile_info}}; |
| 404 |
my %_top_cancelled_info = %{$stats{top_cancelled_info}}; |
| 405 |
|
| 406 |
### overall_stat ### |
| 407 |
|
| 408 |
# Those a used to update the progress bar and are not related to any database |
| 409 |
$overall_stat{'queries_number'} += $_overall_stat{'queries_number'}; |
| 410 |
$overall_stat{'errors_number'} += $_overall_stat{'errors_number'}; |
| 411 |
$pgb_overall_stat{'queries_number'} += $_pgb_overall_stat{'queries_number'}; |
| 412 |
$pgb_overall_stat{'errors_number'} += $_pgb_overall_stat{'errors_number'}; |
| 413 |
foreach my $d (keys %{ $_overall_stat{nlines} }) |
| 414 |
{ |
| 415 |
$overall_stat{'nlines'}{$d} += $_overall_stat{'nlines'}{$d}; |
| 416 |
} |
| 417 |
|
| 418 |
foreach my $curdb (keys %_overall_stat) |
| 419 |
{ |
| 420 |
next if (grep(/^$curdb$/, 'queries_number', 'errors_number', 'queries_number', 'errors_number', 'nlines')); |
| 421 |
if ($_overall_stat{$curdb}{'first_log_ts'}) { |
| 422 |
$overall_stat{$curdb}{'first_log_ts'} = $_overall_stat{$curdb}{'first_log_ts'} |
| 423 |
if (!$overall_stat{$curdb}{'first_log_ts'} || |
| 424 |
($overall_stat{$curdb}{'first_log_ts'} gt $_overall_stat{$curdb}{'first_log_ts'})); |
| 425 |
} |
| 426 |
|
| 427 |
$overall_stat{$curdb}{'last_log_ts'} = $_overall_stat{$curdb}{'last_log_ts'} |
| 428 |
if not $overall_stat{$curdb}{'last_log_ts'} |
| 429 |
or $overall_stat{$curdb}{'last_log_ts'} lt $_overall_stat{$curdb}{'last_log_ts'}; |
| 430 |
|
| 431 |
$overall_stat{$curdb}{'queries_number'} += $_overall_stat{$curdb}{'queries_number'}; |
| 432 |
$overall_stat{$curdb}{'errors_number'} += $_overall_stat{$curdb}{'errors_number'}; |
| 433 |
$pgb_overall_stat{$curdb}{'queries_number'} += $_pgb_overall_stat{$curdb}{'queries_number'}; |
| 434 |
$pgb_overall_stat{$curdb}{'errors_number'} += $_pgb_overall_stat{$curdb}{'errors_number'}; |
| 435 |
|
| 436 |
if ($_overall_stat{$curdb}{'first_query_ts'}) { |
| 437 |
$overall_stat{$curdb}{'first_query_ts'} = $_overall_stat{$curdb}{'first_query_ts'} |
| 438 |
if (!$overall_stat{$curdb}{'first_query_ts'} || |
| 439 |
($overall_stat{$curdb}{'first_query_ts'} gt $_overall_stat{$curdb}{'first_query_ts'})); |
| 440 |
} |
| 441 |
|
| 442 |
$overall_stat{$curdb}{'last_query_ts'} = $_overall_stat{$curdb}{'last_query_ts'} |
| 443 |
if not $overall_stat{$curdb}{'last_query_ts'} |
| 444 |
or $overall_stat{$curdb}{'last_query_ts'} lt $_overall_stat{$curdb}{'last_query_ts'}; |
| 445 |
|
| 446 |
$overall_stat{$curdb}{queries_duration} += $_overall_stat{$curdb}{queries_duration}; |
| 447 |
|
| 448 |
foreach my $a (@SQL_ACTION) { |
| 449 |
$overall_stat{$curdb}{$a} += $_overall_stat{$curdb}{$a} |
| 450 |
if exists $_overall_stat{$curdb}{$a}; |
| 451 |
} |
| 452 |
|
| 453 |
foreach my $k (keys %{$_overall_stat{$curdb}{peak}}) { |
| 454 |
$overall_stat{$curdb}{peak}{$k}{query} += $_overall_stat{$curdb}{peak}{$k}{query}; |
| 455 |
$overall_stat{$curdb}{peak}{$k}{select} += $_overall_stat{$curdb}{peak}{$k}{select}; |
| 456 |
$overall_stat{$curdb}{peak}{$k}{write} += $_overall_stat{$curdb}{peak}{$k}{write}; |
| 457 |
$overall_stat{$curdb}{peak}{$k}{connection} += $_overall_stat{$curdb}{peak}{$k}{connection}; |
| 458 |
$overall_stat{$curdb}{peak}{$k}{session} += $_overall_stat{$curdb}{peak}{$k}{session}; |
| 459 |
$overall_stat{$curdb}{peak}{$k}{tempfile_size} += $_overall_stat{$curdb}{peak}{$k}{tempfile_size}; |
| 460 |
$overall_stat{$curdb}{peak}{$k}{tempfile_count} += $_overall_stat{$curdb}{peak}{$k}{tempfile_count}; |
| 461 |
$overall_stat{$curdb}{peak}{$k}{cancelled_size} += $_overall_stat{$curdb}{peak}{$k}{cancelled_size}; |
| 462 |
$overall_stat{$curdb}{peak}{$k}{cancelled_count} += $_overall_stat{$curdb}{peak}{$k}{cancelled_count}; |
| 463 |
} |
| 464 |
|
| 465 |
foreach my $k (keys %{$_overall_stat{$curdb}{histogram}{query_time}}) { |
| 466 |
$overall_stat{$curdb}{histogram}{query_time}{$k} += $_overall_stat{$curdb}{histogram}{query_time}{$k}; |
| 467 |
} |
| 468 |
$overall_stat{$curdb}{histogram}{query_total} += $_overall_stat{$curdb}{histogram}{total}; |
| 469 |
$overall_stat{$curdb}{histogram}{query_total} += $_overall_stat{$curdb}{histogram}{query_total}; |
| 470 |
foreach my $k (keys %{$_overall_stat{$curdb}{histogram}{session_time}}) { |
| 471 |
$overall_stat{$curdb}{histogram}{session_time}{$k} += $_overall_stat{$curdb}{histogram}{session_time}{$k}; |
| 472 |
} |
| 473 |
$overall_stat{$curdb}{histogram}{session_total} += $_overall_stat{$curdb}{histogram}{session_total}; |
| 474 |
|
| 475 |
foreach my $k ('prepare', 'bind','execute') { |
| 476 |
$overall_stat{$curdb}{$k} += $_overall_stat{$curdb}{$k}; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
$overall_checkpoint{checkpoint_warning} += $_overall_checkpoint{checkpoint_warning}; |
| 481 |
$overall_checkpoint{checkpoint_write} = $_overall_checkpoint{checkpoint_write} |
| 482 |
if ($_overall_checkpoint{checkpoint_write} > $overall_checkpoint{checkpoint_write}); |
| 483 |
$overall_checkpoint{checkpoint_sync} = $_overall_checkpoint{checkpoint_sync} |
| 484 |
if ($_overall_checkpoint{checkpoint_sync} > $overall_checkpoint{checkpoint_sync}); |
| 485 |
foreach my $k (keys %{$_overall_checkpoint{peak}}) { |
| 486 |
$overall_checkpoint{peak}{$k}{checkpoint_wbuffer} += $_overall_checkpoint{peak}{$k}{checkpoint_wbuffer}; |
| 487 |
$overall_checkpoint{peak}{$k}{walfile_usage} += $_overall_checkpoint{peak}{$k}{walfile_usage}; |
| 488 |
$overall_checkpoint{peak}{$k}{distance} += $_overall_checkpoint{peak}{$k}{distance}; |
| 489 |
$overall_checkpoint{peak}{$k}{estimate} += $_overall_checkpoint{peak}{$k}{estimate}; |
| 490 |
} |
| 491 |
|
| 492 |
### pgbouncer related overall stats ### |
| 493 |
foreach my $k (keys %{$_pgb_overall_stat{peak}}) { |
| 494 |
$pgb_overall_stat{peak}{$k}{connection} += $_pgb_overall_stat{peak}{$k}{connection}; |
| 495 |
$pgb_overall_stat{peak}{$k}{session} += $_pgb_overall_stat{peak}{$k}{session}; |
| 496 |
$pgb_overall_stat{peak}{$k}{t_req} += $_pgb_overall_stat{peak}{$k}{t_req}; |
| 497 |
$pgb_overall_stat{peak}{$k}{t_inbytes} += $_pgb_overall_stat{peak}{$k}{t_inbytes}; |
| 498 |
$pgb_overall_stat{peak}{$k}{t_outbytes} += $_pgb_overall_stat{peak}{$k}{t_outbytes}; |
| 499 |
$pgb_overall_stat{peak}{$k}{t_avgduration} += $_pgb_overall_stat{peak}{$k}{t_avgduration}; |
| 500 |
} |
| 501 |
|
| 502 |
foreach my $k (keys %{$_pgb_overall_stat{histogram}{session_time}}) { |
| 503 |
$pgb_overall_stat{histogram}{session_time}{$k} += $_pgb_overall_stat{histogram}{session_time}{$k}; |
| 504 |
} |
| 505 |
$pgb_overall_stat{histogram}{session_total} += $_pgb_overall_stat{histogram}{session_total}; |
| 506 |
|
| 507 |
### Logs level ### |
| 508 |
|
| 509 |
foreach my $l (qw(LOG WARNING ERROR FATAL PANIC DETAIL HINT STATEMENT CONTEXT)) |
| 510 |
{ |
| 511 |
foreach my $curdb (keys %_logs_type) |
| 512 |
{ |
| 513 |
$logs_type{$curdb}{$l} += $_logs_type{$curdb}{$l} if exists $_logs_type{$curdb}{$l}; |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
### Errors code ### |
| 518 |
|
| 519 |
foreach my $curdb (keys %_errors_code) |
| 520 |
{ |
| 521 |
foreach my $c (keys %{$_errors_code{$curdb}}) { |
| 522 |
$errors_code{$curdb}{$c} += $_errors_code{$curdb}{$c}; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
### database_info ### |
| 527 |
|
| 528 |
foreach my $curdb (keys %_database_info) |
| 529 |
{ |
| 530 |
foreach my $db (keys %{$_database_info{$curdb}}) { |
| 531 |
foreach my $k (keys %{ $_database_info{$curdb}{$db} }) { |
| 532 |
$database_info{$curdb}{$db}{$k} += $_database_info{$curdb}{$db}{$k}; |
| 533 |
} |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
### application_info ### |
| 538 |
|
| 539 |
foreach my $curdb (keys %_application_info) |
| 540 |
{ |
| 541 |
foreach my $app (keys %{$_application_info{$curdb}}) { |
| 542 |
foreach my $k (keys %{ $_application_info{$curdb}{$app} }) { |
| 543 |
$application_info{$curdb}{$app}{$k} += $_application_info{$curdb}{$app}{$k}; |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
### user_info ### |
| 549 |
|
| 550 |
foreach my $curdb (keys %_user_info) |
| 551 |
{ |
| 552 |
foreach my $u (keys %{$_user_info{$curdb}}) { |
| 553 |
foreach my $k (keys %{ $_user_info{$curdb}{$u} }) { |
| 554 |
$user_info{$curdb}{$u}{$k} += $_user_info{$curdb}{$u}{$k}; |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
### host_info ### |
| 560 |
|
| 561 |
foreach my $curdb (keys %_host_info) |
| 562 |
{ |
| 563 |
foreach my $h (keys %{$_host_info{$curdb}}) { |
| 564 |
foreach my $k (keys %{ $_host_info{$curdb}{$h} }) { |
| 565 |
$host_info{$curdb}{$h}{$k} += $_host_info{$curdb}{$h}{$k}; |
| 566 |
} |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
### connection_info ### |
| 572 |
|
| 573 |
foreach my $curdb (keys %_connection_info) |
| 574 |
{ |
| 575 |
foreach my $db (keys %{ $_connection_info{$curdb}{database} }) { |
| 576 |
$connection_info{$curdb}{database}{$db} += $_connection_info{$curdb}{database}{$db}; |
| 577 |
} |
| 578 |
|
| 579 |
foreach my $db (keys %{ $_connection_info{$curdb}{database_user} }) { |
| 580 |
foreach my $user (keys %{ $_connection_info{$curdb}{database_user}{$db} }) { |
| 581 |
$connection_info{$curdb}{database_user}{$db}{$user} += $_connection_info{$curdb}{database_user}{$db}{$user}; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
foreach my $db (keys %{ $_connection_info{$curdb}{database_host} }) { |
| 586 |
foreach my $user (keys %{ $_connection_info{$curdb}{database_host}{$db} }) { |
| 587 |
$connection_info{$curdb}{database_host}{$db}{$user} += $_connection_info{$curdb}{database_host}{$db}{$user}; |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
foreach my $user (keys %{ $_connection_info{$curdb}{user} }) { |
| 592 |
$connection_info{$curdb}{user}{$user} += $_connection_info{$curdb}{user}{$user}; |
| 593 |
} |
| 594 |
|
| 595 |
foreach my $host (keys %{ $_connection_info{$curdb}{host} }) { |
| 596 |
$connection_info{$curdb}{host}{$host} += $_connection_info{$curdb}{host}{$host}; |
| 597 |
} |
| 598 |
|
| 599 |
$connection_info{$curdb}{count} += $_connection_info{$curdb}{count}; |
| 600 |
|
| 601 |
foreach my $day (keys %{ $_connection_info{$curdb}{chronos} }) |
| 602 |
{ |
| 603 |
foreach my $hour (keys %{ $_connection_info{$curdb}{chronos}{$day} }) { |
| 604 |
|
| 605 |
$connection_info{$curdb}{chronos}{$day}{$hour}{count} += $_connection_info{$curdb}{chronos}{$day}{$hour}{count}; |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
### pgbouncer connection_info ### |
| 611 |
|
| 612 |
foreach my $db (keys %{ $_pgb_connection_info{database} }) { |
| 613 |
$pgb_connection_info{database}{$db} += $_pgb_connection_info{database}{$db}; |
| 614 |
} |
| 615 |
|
| 616 |
foreach my $db (keys %{ $_pgb_connection_info{database_user} }) { |
| 617 |
foreach my $user (keys %{ $_pgb_connection_info{database_user}{$db} }) { |
| 618 |
$pgb_connection_info{database_user}{$db}{$user} += $_pgb_connection_info{database_user}{$db}{$user}; |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
foreach my $db (keys %{ $_pgb_connection_info{database_host} }) { |
| 623 |
foreach my $user (keys %{ $_pgb_connection_info{database_host}{$db} }) { |
| 624 |
$pgb_connection_info{database_host}{$db}{$user} += $_pgb_connection_info{database_host}{$db}{$user}; |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
foreach my $user (keys %{ $_pgb_connection_info{user} }) { |
| 629 |
$pgb_connection_info{user}{$user} += $_pgb_connection_info{user}{$user}; |
| 630 |
} |
| 631 |
|
| 632 |
foreach my $host (keys %{ $_pgb_connection_info{host} }) { |
| 633 |
$pgb_connection_info{host}{$host} += $_pgb_connection_info{host}{$host}; |
| 634 |
} |
| 635 |
|
| 636 |
$pgb_connection_info{count} += $_pgb_connection_info{count}; |
| 637 |
|
| 638 |
foreach my $day (keys %{ $_pgb_connection_info{chronos} }) { |
| 639 |
foreach my $hour (keys %{ $_pgb_connection_info{chronos}{$day} }) { |
| 640 |
$pgb_connection_info{chronos}{$day}{$hour}{count} += $_pgb_connection_info{chronos}{$day}{$hour}{count} |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
### log_files ### |
| 645 |
|
| 646 |
foreach my $f (@_log_files) { |
| 647 |
push(@log_files, $f) if (!grep(m#^$f$#, @_log_files)); |
| 648 |
} |
| 649 |
|
| 650 |
### error_info ### |
| 651 |
|
| 652 |
foreach my $curdb (keys %_error_info) |
| 653 |
{ |
| 654 |
foreach my $q (keys %{$_error_info{$curdb}}) { |
| 655 |
$error_info{$curdb}{$q}{count} += $_error_info{$curdb}{$q}{count}; |
| 656 |
foreach my $day (keys %{ $_error_info{$curdb}{$q}{chronos} }) { |
| 657 |
foreach my $hour (keys %{$_error_info{$curdb}{$q}{chronos}{$day}}) { |
| 658 |
$error_info{$curdb}{$q}{chronos}{$day}{$hour}{count} += $_error_info{$curdb}{$q}{chronos}{$day}{$hour}{count}; |
| 659 |
foreach my $min (keys %{$_error_info{$curdb}{$q}{chronos}{$day}{$hour}{min}}) { |
| 660 |
$error_info{$curdb}{$q}{chronos}{$day}{$hour}{min}{$min} += $_error_info{$curdb}{$q}{chronos}{$day}{$hour}{min}{$min}; |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
for (my $i = 0; $i <= $#{$_error_info{$curdb}{$q}{date}}; $i++) { |
| 665 |
&set_top_error_sample( $curdb, $q, |
| 666 |
$_error_info{$curdb}{$q}{date}[$i], |
| 667 |
$_error_info{$curdb}{$q}{error}[$i], |
| 668 |
$_error_info{$curdb}{$q}{detail}[$i], |
| 669 |
$_error_info{$curdb}{$q}{context}[$i], |
| 670 |
$_error_info{$curdb}{$q}{statement}[$i], |
| 671 |
$_error_info{$curdb}{$q}{hint}[$i], |
| 672 |
$_error_info{$curdb}{$q}{db}[$i], |
| 673 |
$_error_info{$curdb}{$q}{user}[$i], |
| 674 |
$_error_info{$curdb}{$q}{app}[$i], |
| 675 |
$_error_info{$curdb}{$q}{remote}[$i], |
| 676 |
$_error_info{$curdb}{$q}{sqlstate}[$i] |
| 677 |
); |
| 678 |
} |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
### pgbouncer error_info ### |
| 683 |
|
| 684 |
foreach my $q (keys %_pgb_error_info) |
| 685 |
{ |
| 686 |
$pgb_error_info{$q}{count} += $_pgb_error_info{$q}{count}; |
| 687 |
foreach my $day (keys %{ $_pgb_error_info{$q}{chronos} }) { |
| 688 |
foreach my $hour (keys %{$_pgb_error_info{$q}{chronos}{$day}}) { |
| 689 |
$pgb_error_info{$q}{chronos}{$day}{$hour}{count} += $_pgb_error_info{$q}{chronos}{$day}{$hour}{count}; |
| 690 |
foreach my $min (keys %{$_pgb_error_info{$q}{chronos}{$day}{$hour}{min}}) { |
| 691 |
$pgb_error_info{$q}{chronos}{$day}{$hour}{min}{$min} += $_pgb_error_info{$q}{chronos}{$day}{$hour}{min}{$min}; |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
for (my $i = 0; $i <= $#{$_pgb_error_info{$q}{date}}; $i++) { |
| 696 |
&pgb_set_top_error_sample($q, |
| 697 |
$_pgb_error_info{$q}{date}[$i], |
| 698 |
$_pgb_error_info{$q}{error}[$i], |
| 699 |
$_pgb_error_info{$q}{db}[$i], |
| 700 |
$_pgb_error_info{$q}{user}[$i], |
| 701 |
$_pgb_error_info{$q}{remote}[$i] |
| 702 |
); |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
### pgbouncer pool_info ### |
| 707 |
|
| 708 |
foreach my $q (keys %_pgb_pool_info) { |
| 709 |
$pgb_pool_info{$q}{count} += $_pgb_pool_info{$q}{count}; |
| 710 |
foreach my $day (keys %{ $_pgb_pool_info{$q}{chronos} }) { |
| 711 |
foreach my $hour (keys %{$_pgb_pool_info{$q}{chronos}{$day}}) { |
| 712 |
$pgb_pool_info{$q}{chronos}{$day}{$hour}{count} += $_pgb_pool_info{$q}{chronos}{$day}{$hour}{count}; |
| 713 |
foreach my $min (keys %{$_pgb_pool_info{$q}{chronos}{$day}{$hour}{min}}) { |
| 714 |
$pgb_pool_info{$q}{chronos}{$day}{$hour}{min}{$min} += $_pgb_pool_info{$q}{chronos}{$day}{$hour}{min}{$min}; |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
### per_minute_info ### |
| 721 |
|
| 722 |
foreach my $curdb (keys %_per_minute_info) { |
| 723 |
foreach my $day (keys %{$_per_minute_info{$curdb}}) { |
| 724 |
foreach my $hour (keys %{ $_per_minute_info{$curdb}{$day} }) { |
| 725 |
foreach my $min (keys %{ $_per_minute_info{$curdb}{$day}{$hour} }) { |
| 726 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{connection}{count} += |
| 727 |
($_per_minute_info{$curdb}{$day}{$hour}{$min}{connection}{count} || 0); |
| 728 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{session}{count} += |
| 729 |
($_per_minute_info{$curdb}{$day}{$hour}{$min}{session}{count} || 0); |
| 730 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{query}{count} += |
| 731 |
($_per_minute_info{$curdb}{$day}{$hour}{$min}{query}{count} || 0); |
| 732 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{query}{duration} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{query}{duration}; |
| 733 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{query}{min} = $_per_minute_info{$curdb}{$day}{$hour}{$min}{query}{min} if (!exists $per_minute_info{$curdb}{$day}{$hour}{$min}{query}{min} || ($per_minute_info{$curdb}{$day}{$hour}{$min}{query}{min} > $_per_minute_info{$curdb}{$day}{$hour}{$min}{query}{min})); |
| 734 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{query}{max} = $_per_minute_info{$curdb}{$day}{$hour}{$min}{query}{max} if (!exists $per_minute_info{$curdb}{$day}{$hour}{$min}{query}{max} || ($per_minute_info{$curdb}{$day}{$hour}{$min}{query}{max} < $_per_minute_info{$curdb}{$day}{$hour}{$min}{query}{max})); |
| 735 |
|
| 736 |
foreach my $sec (keys %{ $_per_minute_info{$curdb}{$day}{$hour}{$min}{connection}{second} }) { |
| 737 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{connection}{second}{$sec} += |
| 738 |
($_per_minute_info{$curdb}{$day}{$hour}{$min}{connection}{second}{$sec} || 0); |
| 739 |
} |
| 740 |
foreach my $sec (keys %{ $_per_minute_info{$curdb}{$day}{$hour}{$min}{session}{second} }) { |
| 741 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{session}{second}{$sec} += |
| 742 |
($_per_minute_info{$curdb}{$day}{$hour}{$min}{session}{second}{$sec} || 0); |
| 743 |
} |
| 744 |
foreach my $sec (keys %{ $_per_minute_info{$curdb}{$day}{$hour}{$min}{query}{second} }) { |
| 745 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{query}{second}{$sec} += |
| 746 |
($_per_minute_info{$curdb}{$day}{$hour}{$min}{query}{second}{$sec} || 0); |
| 747 |
} |
| 748 |
foreach my $action (@SQL_ACTION) { |
| 749 |
next if (!exists $_per_minute_info{$curdb}{$day}{$hour}{$min}{$action}); |
| 750 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{$action}{count} += ($_per_minute_info{$curdb}{$day}{$hour}{$min}{$action}{count} || 0); |
| 751 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{$action}{duration} += ($_per_minute_info{$curdb}{$day}{$hour}{$min}{$action}{duration} || 0); |
| 752 |
foreach my $sec (keys %{ $_per_minute_info{$curdb}{$day}{$hour}{$min}{$action}{second} }) { |
| 753 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{$action}{second}{$sec} += |
| 754 |
($_per_minute_info{$curdb}{$day}{$hour}{$min}{$action}{second}{$sec} || 0); |
| 755 |
} |
| 756 |
} |
| 757 |
foreach my $k ('prepare', 'bind','execute') { |
| 758 |
if (exists $_per_minute_info{$curdb}{$day}{$hour}{$min}{$k}) { |
| 759 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{$k} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{$k}; |
| 760 |
} |
| 761 |
} |
| 762 |
foreach my $log (keys %{ $_per_minute_info{$curdb}{$day}{$hour}{$min}{log_level} }) { |
| 763 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{log_level}{$log} += |
| 764 |
($_per_minute_info{$curdb}{$day}{$hour}{$min}{log_level}{$log} || 0); |
| 765 |
} |
| 766 |
|
| 767 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{cancelled}{count} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{cancelled}{count} |
| 768 |
if defined $_per_minute_info{$curdb}{$day}{$hour}{$min}{cancelled}{count}; |
| 769 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{'tempfile'}{count} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{'tempfile'}{count} |
| 770 |
if defined $_per_minute_info{$curdb}{$day}{$hour}{$min}{'tempfile'}{count}; |
| 771 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{'tempfile'}{size} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{'tempfile'}{size} |
| 772 |
if defined $_per_minute_info{$curdb}{$day}{$hour}{$min}{'tempfile'}{size}; |
| 773 |
|
| 774 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{file_removed} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{file_removed}; |
| 775 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync}; |
| 776 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{wbuffer} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{wbuffer}; |
| 777 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{file_recycled} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{file_recycled}; |
| 778 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{total} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{total}; |
| 779 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{file_added} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{file_added}; |
| 780 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{write} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{write}; |
| 781 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{autovacuum}{count} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{autovacuum}{count}; |
| 782 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{autoanalyze}{count} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{autoanalyze}{count}; |
| 783 |
|
| 784 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync_files} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync_files}; |
| 785 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync_avg} += $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync_avg}; |
| 786 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync_longest} = $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync_longest} |
| 787 |
if ($_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync_longest} > $per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{sync_longest}); |
| 788 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{distance} = $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{distance}; |
| 789 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{distance_count} = $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{distance_count}; |
| 790 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{estimate} = $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{estimate}; |
| 791 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{warnning} = $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{warning}; |
| 792 |
$per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{warning_seconds} = $_per_minute_info{$curdb}{$day}{$hour}{$min}{checkpoint}{warning_seconds}; |
| 793 |
} |
| 794 |
} |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
### pgbouncer per_minute_info ### |
| 799 |
|
| 800 |
foreach my $day (keys %_pgb_per_minute_info) { |
| 801 |
foreach my $hour (keys %{ $_pgb_per_minute_info{$day} }) { |
| 802 |
foreach my $min (keys %{ $_pgb_per_minute_info{$day}{$hour} }) { |
| 803 |
$pgb_per_minute_info{$day}{$hour}{$min}{connection}{count} += |
| 804 |
($_pgb_per_minute_info{$day}{$hour}{$min}{connection}{count} || 0); |
| 805 |
$pgb_per_minute_info{$day}{$hour}{$min}{session}{count} += |
| 806 |
($_pgb_per_minute_info{$day}{$hour}{$min}{session}{count} || 0); |
| 807 |
$pgb_per_minute_info{$day}{$hour}{$min}{t_req} += |
| 808 |
($_pgb_per_minute_info{$day}{$hour}{$min}{t_req} || 0); |
| 809 |
$pgb_per_minute_info{$day}{$hour}{$min}{t_inbytes} += |
| 810 |
($_pgb_per_minute_info{$day}{$hour}{$min}{t_inbytes} || 0); |
| 811 |
$pgb_per_minute_info{$day}{$hour}{$min}{t_outbytes} += |
| 812 |
($_pgb_per_minute_info{$day}{$hour}{$min}{t_outbytes} || 0); |
| 813 |
$pgb_per_minute_info{$day}{$hour}{$min}{t_avgduration} += |
| 814 |
($_pgb_per_minute_info{$day}{$hour}{$min}{t_avgduration} || 0); |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
### lock_info ### |
| 820 |
|
| 821 |
foreach my $curdb (keys %_lock_info) |
| 822 |
{ |
| 823 |
foreach my $lock (keys %{$_lock_info{$curdb}}) { |
| 824 |
$lock_info{$curdb}{$lock}{count} += $_lock_info{$curdb}{$lock}{count}; |
| 825 |
|
| 826 |
foreach my $day (keys %{ $_lock_info{$curdb}{chronos} }) { |
| 827 |
foreach my $hour (keys %{ $_lock_info{$curdb}{chronos}{$day} }) { |
| 828 |
$lock_info{$curdb}{chronos}{$day}{$hour}{count} += $_lock_info{$curdb}{chronos}{$day}{$hour}{count}; |
| 829 |
$lock_info{$curdb}{chronos}{$day}{$hour}{duration} += $_lock_info{$curdb}{chronos}{$day}{$hour}{duration}; |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
$lock_info{$curdb}{$lock}{duration} += $_lock_info{$curdb}{$lock}{duration}; |
| 834 |
|
| 835 |
foreach my $type (keys %{$_lock_info{$curdb}{$lock}}) { |
| 836 |
next if $type =~ /^(count|chronos|duration)$/; |
| 837 |
|
| 838 |
$lock_info{$curdb}{$lock}{$type}{count} += $_lock_info{$curdb}{$lock}{$type}{count}; |
| 839 |
$lock_info{$curdb}{$lock}{$type}{duration} += $_lock_info{$curdb}{$lock}{$type}{duration}; |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
|
| 844 |
### nlines ### |
| 845 |
|
| 846 |
$nlines += $_nlines; |
| 847 |
|
| 848 |
### normalyzed_info ### |
| 849 |
|
| 850 |
foreach my $curdb (keys %_normalyzed_info) { |
| 851 |
foreach my $stmt (keys %{$_normalyzed_info{$curdb}}) { |
| 852 |
|
| 853 |
foreach my $dt (keys %{$_normalyzed_info{$curdb}{$stmt}{samples}} ) { |
| 854 |
foreach my $k (keys %{$_normalyzed_info{$curdb}{$stmt}{samples}{$dt}} ) { |
| 855 |
$normalyzed_info{$curdb}{$stmt}{samples}{$dt}{$k} = $_normalyzed_info{$curdb}{$stmt}{samples}{$dt}{$k}; |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
# Keep only the top N samples |
| 860 |
my $i = 1; |
| 861 |
foreach my $k (sort {$b <=> $a} keys %{$normalyzed_info{$curdb}{$stmt}{samples}}) { |
| 862 |
if ($i > $sample) { |
| 863 |
delete $normalyzed_info{$curdb}{$stmt}{samples}{$k}; |
| 864 |
} |
| 865 |
$i++; |
| 866 |
} |
| 867 |
|
| 868 |
$normalyzed_info{$curdb}{$stmt}{count} += $_normalyzed_info{$curdb}{$stmt}{count}; |
| 869 |
|
| 870 |
# Set min / max duration for this query |
| 871 |
if (!exists $normalyzed_info{$curdb}{$stmt}{min} || ($normalyzed_info{$curdb}{$stmt}{min} > $_normalyzed_info{$curdb}{$stmt}{min})) { |
| 872 |
$normalyzed_info{$curdb}{$stmt}{min} = $_normalyzed_info{$curdb}{$stmt}{min}; |
| 873 |
} |
| 874 |
if (!exists $normalyzed_info{$curdb}{$stmt}{max} || ($normalyzed_info{$curdb}{$stmt}{max} < $_normalyzed_info{$curdb}{$stmt}{max})) { |
| 875 |
$normalyzed_info{$curdb}{$stmt}{max} = $_normalyzed_info{$curdb}{$stmt}{max}; |
| 876 |
} |
| 877 |
|
| 878 |
foreach my $day (keys %{$_normalyzed_info{$curdb}{$stmt}{chronos}} ) { |
| 879 |
foreach my $hour (keys %{$_normalyzed_info{$curdb}{$stmt}{chronos}{$day}} ) { |
| 880 |
$normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{count} += |
| 881 |
$_normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{count}; |
| 882 |
$normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{duration} += |
| 883 |
$_normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{duration}; |
| 884 |
foreach my $min (keys %{$_normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{min}} ) { |
| 885 |
$normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{min}{$min} += |
| 886 |
$_normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{min}{$min}; |
| 887 |
} |
| 888 |
foreach my $min (keys %{$_normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{min_duration}} ) { |
| 889 |
$normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{min_duration}{$min} += |
| 890 |
$_normalyzed_info{$curdb}{$stmt}{chronos}{$day}{$hour}{min_duration}{$min}; |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
$normalyzed_info{$curdb}{$stmt}{duration} += $_normalyzed_info{$curdb}{$stmt}{duration}; |
| 896 |
|
| 897 |
if (exists $_normalyzed_info{$curdb}{$stmt}{locks}) { |
| 898 |
$normalyzed_info{$curdb}{$stmt}{locks}{count} += $_normalyzed_info{$curdb}{$stmt}{locks}{count}; |
| 899 |
$normalyzed_info{$curdb}{$stmt}{locks}{wait} += $_normalyzed_info{$curdb}{$stmt}{locks}{wait}; |
| 900 |
if (!exists $normalyzed_info{$curdb}{$stmt}{locks}{minwait} || ($normalyzed_info{$curdb}{$stmt}{locks}{minwait} > $_normalyzed_info{$stmt}{locks}{minwait})) { |
| 901 |
$normalyzed_info{$curdb}{$stmt}{locks}{minwait} = $_normalyzed_info{$curdb}{$stmt}{locks}{minwait}; |
| 902 |
} |
| 903 |
if (!exists $normalyzed_info{$curdb}{$stmt}{locks}{maxwait} || ($normalyzed_info{$curdb}{$stmt}{locks}{maxwait} < $_normalyzed_info{$curdb}{$stmt}{locks}{maxwait})) { |
| 904 |
$normalyzed_info{$curdb}{$stmt}{locks}{maxwait} = $_normalyzed_info{$curdb}{$stmt}{locks}{maxwait}; |
| 905 |
} |
| 906 |
} |
| 907 |
|
| 908 |
if (exists $_normalyzed_info{$curdb}{$stmt}{tempfiles}) { |
| 909 |
$normalyzed_info{$curdb}{$stmt}{tempfiles}{count} += $_normalyzed_info{$curdb}{$stmt}{tempfiles}{count}; |
| 910 |
$normalyzed_info{$curdb}{$stmt}{tempfiles}{size} += $_normalyzed_info{$curdb}{$stmt}{tempfiles}{size}; |
| 911 |
if (!exists $normalyzed_info{$curdb}{$stmt}{tempfiles}{minsize} || ($normalyzed_info{$curdb}{$stmt}{tempfiles}{minsize} > $_normalyzed_info{$curdb}{$stmt}{tempfiles}{minsize})) { |
| 912 |
$normalyzed_info{$curdb}{$stmt}{tempfiles}{minsize} = $_normalyzed_info{$curdb}{$stmt}{tempfiles}{minsize}; |
| 913 |
} |
| 914 |
if (!exists $normalyzed_info{$curdb}{$stmt}{tempfiles}{maxsize} || ($normalyzed_info{$curdb}{$stmt}{tempfiles}{maxsize} < $_normalyzed_info{$curdb}{$stmt}{tempfiles}{maxsize})) { |
| 915 |
$normalyzed_info{$curdb}{$stmt}{tempfiles}{maxsize} = $_normalyzed_info{$curdb}{$stmt}{tempfiles}{maxsize}; |
| 916 |
} |
| 917 |
} |
| 918 |
|
| 919 |
if (exists $_normalyzed_info{$curdb}{$stmt}{cancelled}) { |
| 920 |
$normalyzed_info{$curdb}{$stmt}{cancelled}{count} += $_normalyzed_info{$curdb}{$stmt}{cancelled}{count}; |
| 921 |
} |
| 922 |
|
| 923 |
foreach my $u (keys %{$_normalyzed_info{$curdb}{$stmt}{users}} ) { |
| 924 |
foreach my $k (keys %{$_normalyzed_info{$curdb}{$stmt}{users}{$u}} ) { |
| 925 |
$normalyzed_info{$curdb}{$stmt}{users}{$u}{$k} += $_normalyzed_info{$curdb}{$stmt}{users}{$u}{$k}; |
| 926 |
} |
| 927 |
} |
| 928 |
foreach my $u (keys %{$_normalyzed_info{$curdb}{$stmt}{apps}} ) { |
| 929 |
foreach my $k (keys %{$_normalyzed_info{$curdb}{$stmt}{apps}{$u}} ) { |
| 930 |
$normalyzed_info{$curdb}{$stmt}{apps}{$u}{$k} += $_normalyzed_info{$curdb}{$stmt}{apps}{$u}{$k}; |
| 931 |
} |
| 932 |
} |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
### session_info ### |
| 937 |
|
| 938 |
foreach my $curdb (keys %_session_info) { |
| 939 |
foreach my $db (keys %{ $_session_info{$curdb}{database}}) { |
| 940 |
$session_info{$curdb}{database}{$db}{count} += $_session_info{$curdb}{database}{$db}{count}; |
| 941 |
$session_info{$curdb}{database}{$db}{duration} += $_session_info{$curdb}{database}{$db}{duration}; |
| 942 |
} |
| 943 |
|
| 944 |
$session_info{$curdb}{count} += $_session_info{$curdb}{count}; |
| 945 |
|
| 946 |
foreach my $day (keys %{ $_session_info{$curdb}{chronos}}) { |
| 947 |
foreach my $hour (keys %{ $_session_info{$curdb}{chronos}{$day}}) { |
| 948 |
$session_info{$curdb}{chronos}{$day}{$hour}{count} += $_session_info{$curdb}{chronos}{$day}{$hour}{count}; |
| 949 |
$session_info{$curdb}{chronos}{$day}{$hour}{duration} += $_session_info{$curdb}{chronos}{$day}{$hour}{duration}; |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
foreach my $user (keys %{ $_session_info{$curdb}{user}}) { |
| 954 |
$session_info{$curdb}{user}{$user}{count} += $_session_info{$curdb}{user}{$user}{count}; |
| 955 |
$session_info{$curdb}{user}{$user}{duration} += $_session_info{$curdb}{user}{$user}{duration}; |
| 956 |
} |
| 957 |
|
| 958 |
$session_info{$curdb}{duration} += $_session_info{$curdb}{duration}; |
| 959 |
|
| 960 |
foreach my $host (keys %{ $_session_info{$curdb}{host}}) { |
| 961 |
$session_info{$curdb}{host}{$host}{count} += $_session_info{$curdb}{host}{$host}{count}; |
| 962 |
$session_info{$curdb}{host}{$host}{duration} += $_session_info{$curdb}{host}{$host}{duration}; |
| 963 |
} |
| 964 |
|
| 965 |
foreach my $app (keys %{ $_session_info{$curdb}{app}}) { |
| 966 |
$session_info{$curdb}{app}{$app}{count} += $_session_info{$curdb}{app}{$app}{count}; |
| 967 |
$session_info{$curdb}{app}{$app}{duration} += $_session_info{$curdb}{app}{$app}{duration}; |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
### pgbouncer session_info ### |
| 972 |
|
| 973 |
foreach my $db (keys %{ $_pgb_session_info{database}}) { |
| 974 |
$pgb_session_info{database}{$db}{count} += $_pgb_session_info{database}{$db}{count}; |
| 975 |
$pgb_session_info{database}{$db}{duration} += $_pgb_session_info{database}{$db}{duration}; |
| 976 |
} |
| 977 |
|
| 978 |
$pgb_session_info{count} += $_pgb_session_info{count}; |
| 979 |
|
| 980 |
foreach my $day (keys %{ $_pgb_session_info{chronos}}) { |
| 981 |
foreach my $hour (keys %{ $_pgb_session_info{chronos}{$day}}) { |
| 982 |
$pgb_session_info{chronos}{$day}{$hour}{count} += $_pgb_session_info{chronos}{$day}{$hour}{count}; |
| 983 |
$pgb_session_info{chronos}{$day}{$hour}{duration} += $_pgb_session_info{chronos}{$day}{$hour}{duration}; |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
foreach my $user (keys %{ $_pgb_session_info{user}}) { |
| 988 |
$pgb_session_info{user}{$user}{count} += $_pgb_session_info{user}{$user}{count}; |
| 989 |
$pgb_session_info{user}{$user}{duration} += $_pgb_session_info{user}{$user}{duration}; |
| 990 |
} |
| 991 |
|
| 992 |
$pgb_session_info{duration} += $_pgb_session_info{duration}; |
| 993 |
|
| 994 |
foreach my $host (keys %{ $_pgb_session_info{host}}) { |
| 995 |
$pgb_session_info{host}{$host}{count} += $_pgb_session_info{host}{$host}{count}; |
| 996 |
$pgb_session_info{host}{$host}{duration} += $_pgb_session_info{host}{$host}{duration}; |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
### tempfile_info ### |
| 1001 |
|
| 1002 |
foreach my $curdb (keys %_tempfile_info) |
| 1003 |
{ |
| 1004 |
$tempfile_info{$curdb}{count} += $_tempfile_info{$curdb}{count} |
| 1005 |
if defined $_tempfile_info{$curdb}{count}; |
| 1006 |
$tempfile_info{$curdb}{size} += $_tempfile_info{$curdb}{size} |
| 1007 |
if defined $_tempfile_info{$curdb}{size}; |
| 1008 |
$tempfile_info{$curdb}{maxsize} = $_tempfile_info{$curdb}{maxsize} |
| 1009 |
if defined $_tempfile_info{$curdb}{maxsize} and ( not defined $tempfile_info{$curdb}{maxsize} |
| 1010 |
or $tempfile_info{$curdb}{maxsize} < $_tempfile_info{$curdb}{maxsize} ); |
| 1011 |
} |
| 1012 |
|
| 1013 |
### top_slowest ### |
| 1014 |
foreach my $curdb (keys %_top_slowest) |
| 1015 |
{ |
| 1016 |
my @tmp_top_slowest = sort {$b->[0] <=> $a->[0]} (@{$top_slowest{$curdb}}, @{$_top_slowest{$curdb}}); |
| 1017 |
@{$top_slowest{$curdb}} = (); |
| 1018 |
for (my $i = 0; $i <= $#tmp_top_slowest; $i++) { |
| 1019 |
push(@{$top_slowest{$curdb}}, $tmp_top_slowest[$i]); |
| 1020 |
last if ($i == $end_top); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
### top_locked ### |
| 1025 |
foreach my $curdb (keys %_top_locked_info) |
| 1026 |
{ |
| 1027 |
my @tmp_top_locked_info = sort {$b->[0] <=> $a->[0]} (@{$top_locked_info{$curdb}}, @{$_top_locked_info{$curdb}}); |
| 1028 |
@{$top_locked_info{$curdb}} = (); |
| 1029 |
for (my $i = 0; $i <= $#tmp_top_locked_info; $i++) { |
| 1030 |
push(@{$top_locked_info{$curdb}}, $tmp_top_locked_info[$i]); |
| 1031 |
last if ($i == $end_top); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
### top_tempfile ### |
| 1036 |
foreach my $curdb (keys %_top_tempfile_info) |
| 1037 |
{ |
| 1038 |
my @tmp_top_tempfile_info = sort {$b->[0] <=> $a->[0]} (@{$top_tempfile_info{$curdb}}, @{$_top_tempfile_info{$curdb}}); |
| 1039 |
@{$top_tempfile_info{$curdb}} = (); |
| 1040 |
for (my $i = 0; $i <= $#tmp_top_tempfile_info; $i++) { |
| 1041 |
push(@{$top_tempfile_info{$curdb}}, $tmp_top_tempfile_info[$i]); |
| 1042 |
last if ($i == $end_top); |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|
| 1046 |
### checkpoint_info ### |
| 1047 |
$checkpoint_info{file_removed} += $_checkpoint_info{file_removed}; |
| 1048 |
$checkpoint_info{sync} += $_checkpoint_info{sync}; |
| 1049 |
$checkpoint_info{wbuffer} += $_checkpoint_info{wbuffer}; |
| 1050 |
$checkpoint_info{file_recycled} += $_checkpoint_info{file_recycled}; |
| 1051 |
$checkpoint_info{total} += $_checkpoint_info{total}; |
| 1052 |
$checkpoint_info{file_added} += $_checkpoint_info{file_added}; |
| 1053 |
$checkpoint_info{write} += $_checkpoint_info{write}; |
| 1054 |
$checkpoint_info{distance} += $_checkpoint_info{distance}; |
| 1055 |
$checkpoint_info{estimate} += $_checkpoint_info{estimate}; |
| 1056 |
$checkpoint_info{warning} += $_checkpoint_info{warning}; |
| 1057 |
$checkpoint_info{warning_seconds} += $_checkpoint_info{warning_seconds}; |
| 1058 |
|
| 1059 |
#### Autovacuum info #### |
| 1060 |
|
| 1061 |
foreach my $curdb (keys %_autovacuum_info) |
| 1062 |
{ |
| 1063 |
$autovacuum_info{$curdb}{count} += $_autovacuum_info{$curdb}{count}; |
| 1064 |
|
| 1065 |
foreach my $day (keys %{ $_autovacuum_info{$curdb}{chronos} }) { |
| 1066 |
foreach my $hour (keys %{ $_autovacuum_info{$curdb}{chronos}{$day} }) { |
| 1067 |
$autovacuum_info{$curdb}{chronos}{$day}{$hour}{count} += $_autovacuum_info{$curdb}{chronos}{$day}{$hour}{count}; |
| 1068 |
} |
| 1069 |
} |
| 1070 |
foreach my $table (keys %{ $_autovacuum_info{$curdb}{tables} }) { |
| 1071 |
$autovacuum_info{$curdb}{tables}{$table}{vacuums} += $_autovacuum_info{$curdb}{tables}{$table}{vacuums}; |
| 1072 |
$autovacuum_info{$curdb}{tables}{$table}{idxscans} += $_autovacuum_info{$curdb}{tables}{$table}{idxscans}; |
| 1073 |
$autovacuum_info{$curdb}{tables}{$table}{tuples}{removed} += $_autovacuum_info{$curdb}{tables}{$table}{tuples}{removed}; |
| 1074 |
$autovacuum_info{$curdb}{tables}{$table}{pages}{removed} += $_autovacuum_info{$curdb}{tables}{$table}{pages}{removed}; |
| 1075 |
} |
| 1076 |
if ($_autovacuum_info{$curdb}{peak}{system_usage}{elapsed} > $autovacuum_info{$curdb}{peak}{system_usage}{elapsed}) { |
| 1077 |
$autovacuum_info{$curdb}{peak}{system_usage}{elapsed} = $_autovacuum_info{$curdb}{peak}{system_usage}{elapsed}; |
| 1078 |
$autovacuum_info{$curdb}{peak}{system_usage}{table} = $_autovacuum_info{$curdb}{peak}{system_usage}{table}; |
| 1079 |
$autovacuum_info{$curdb}{peak}{system_usage}{date} = $_autovacuum_info{$curdb}{peak}{system_usage}{date}; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
#### Autoanalyze info #### |
| 1084 |
|
| 1085 |
foreach my $curdb (keys %_autoanalyze_info) |
| 1086 |
{ |
| 1087 |
$autoanalyze_info{$curdb}{count} += $_autoanalyze_info{$curdb}{count}; |
| 1088 |
|
| 1089 |
foreach my $day (keys %{ $_autoanalyze_info{$curdb}{chronos} }) { |
| 1090 |
foreach my $hour (keys %{ $_autoanalyze_info{$curdb}{chronos}{$day} }) { |
| 1091 |
$autoanalyze_info{$curdb}{chronos}{$day}{$hour}{count} += $_autoanalyze_info{$curdb}{chronos}{$day}{$hour}{count}; |
| 1092 |
} |
| 1093 |
} |
| 1094 |
foreach my $table (keys %{ $_autoanalyze_info{$curdb}{tables} }) { |
| 1095 |
$autoanalyze_info{$curdb}{tables}{$table}{analyzes} += $_autoanalyze_info{$curdb}{tables}{$table}{analyzes}; |
| 1096 |
} |
| 1097 |
if ($_autoanalyze_info{$curdb}{peak}{system_usage}{elapsed} > $autoanalyze_info{$curdb}{peak}{system_usage}{elapsed}) { |
| 1098 |
$autoanalyze_info{$curdb}{peak}{system_usage}{elapsed} = $_autoanalyze_info{$curdb}{peak}{system_usage}{elapsed}; |
| 1099 |
$autoanalyze_info{$curdb}{peak}{system_usage}{table} = $_autoanalyze_info{$curdb}{peak}{system_usage}{table}; |
| 1100 |
$autoanalyze_info{$curdb}{peak}{system_usage}{date} = $_autoanalyze_info{$curdb}{peak}{system_usage}{date}; |
| 1101 |
} |
| 1102 |
} |
| 1103 |
|
| 1104 |
use strict; |
| 1105 |
|
| 1106 |
return; |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
# Do minimal query formatting. |
| 1111 |
# Add carriage return to be more human readable |
| 1112 |
sub fmtquery |
| 1113 |
{ |
| 1114 |
my $qry = shift; |
| 1115 |
|
| 1116 |
return $qry if (!$format_query); |
| 1117 |
|
| 1118 |
my @KEYWORDS = qw( FROM INNER WHERE AND OR ORDER RETURNING ); |
| 1119 |
|
| 1120 |
foreach my $key (@KEYWORDS) { |
| 1121 |
$qry =~ s/\s+($key\s+)/\n$1/ig; |
| 1122 |
} |
| 1123 |
return $qry; |
| 1124 |
} |
| 1125 |
|
| 1126 |
############################################################################### |
| 1127 |
# TOOL FUNCTIONS |
| 1128 |
# Add your own below |
| 1129 |
############################################################################### |
| 1130 |
|
| 1131 |
# Dump top slowest queries as EXPLAIN statement. |
| 1132 |
sub dump_slowest_queries |
| 1133 |
{ |
| 1134 |
my $curdb = shift; |
| 1135 |
|
| 1136 |
# Global information |
| 1137 |
my $curdate = localtime(time); |
| 1138 |
my $logfile_str = join(',', @log_files); |
| 1139 |
|
| 1140 |
for (my $i = 0 ; $i <= $#{$top_slowest{$curdb}} ; $i++) { |
| 1141 |
|
| 1142 |
# Do not process request that are slower than $max_duration |
| 1143 |
next if ( $max_duration && ($top_slowest{$curdb}[$i]->[0] > $max_duration) ); |
| 1144 |
|
| 1145 |
my $head = qq{-- Generated on $curdate |
| 1146 |
-- Log file: $logfile_str |
| 1147 |
-- Log start from $overall_stat{'first_log_ts'} to $overall_stat{'last_log_ts'} |
| 1148 |
-- |
| 1149 |
}; |
| 1150 |
$head .= "-- duration: $top_slowest{$curdb}[$i]->[0]\n" if ($top_slowest{$curdb}[$i]->[0]); |
| 1151 |
$head .= "-- database: $top_slowest{$curdb}[$i]->[3]\n" if ($top_slowest{$curdb}[$i]->[3]); |
| 1152 |
$head .= "-- user: $top_slowest{$curdb}[$i]->[4]\n" if ($top_slowest{$curdb}[$i]->[4]); |
| 1153 |
$head .= "-- remote: $top_slowest{$curdb}[$i]->[5]\n" if ($top_slowest{$curdb}[$i]->[5]); |
| 1154 |
$head .= "-- app: $top_slowest{$curdb}[$i]->[6]\n" if ($top_slowest{$curdb}[$i]->[6]); |
| 1155 |
$head .= "--, bind query: yes\n" if ($top_slowest{$curdb}[$i]->[7]); |
| 1156 |
$head =~ s/^, //; |
| 1157 |
|
| 1158 |
my $explain = "EXPLAIN\n"; |
| 1159 |
if ($analyze) { |
| 1160 |
$explain = "BEGIN;\nEXPLAIN (ANALYZE, VERBOSE, BUFFERS)\n"; |
| 1161 |
} |
| 1162 |
$explain .= "$top_slowest{$curdb}[$i]->[2]\n"; |
| 1163 |
$explain .= "ROLLBACK;\n" if ($analyze); |
| 1164 |
if ($file_per_query) { |
| 1165 |
my $fhqr = new IO::File; |
| 1166 |
my $fname = sprintf("qry%03d.sql", $i); |
| 1167 |
$fhqr->open("> $fname") or die "FATAL: can't open temp file $fname, $!\n"; |
| 1168 |
$fhqr->print($head); |
| 1169 |
$fhqr->print(&fmtquery($explain)); |
| 1170 |
$fhqr->close(); |
| 1171 |
} else { |
| 1172 |
print $head; |
| 1173 |
print $explain; |
| 1174 |
} |
| 1175 |
if ($psql_cmd) { |
| 1176 |
my @tmpfile = tempfile('tmp_pgbadger_tools_XXXX', SUFFIX => '.txt', DIR => $TMP_DIR, UNLINK => 1 ); |
| 1177 |
my $fht = new IO::File; |
| 1178 |
$fht->open("> $tmpfile[1]") or die "FATAL: can't open temp file $tmpfile[1], $!\n"; |
| 1179 |
$fht->print("$explain"); |
| 1180 |
$fht->close(); |
| 1181 |
print `$psql_cmd -f $tmpfile[1]`; |
| 1182 |
unlink($tmpfile[1]); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
# Dump top time consuming queries as EXPLAIN statement. |
| 1188 |
sub dump_times_consuming_queries |
| 1189 |
{ |
| 1190 |
my $curdb = shift; |
| 1191 |
|
| 1192 |
# Global information |
| 1193 |
my $curdate = localtime(time); |
| 1194 |
my $logfile_str = join(',', @log_files); |
| 1195 |
|
| 1196 |
my $rank = 1; |
| 1197 |
foreach my $k (sort {$normalyzed_info{$curdb}{$b}{count} <=> $normalyzed_info{$curdb}{$a}{count}} keys %{$normalyzed_info{$curdb}}) { |
| 1198 |
next if (!$normalyzed_info{$curdb}{$k}{count}); |
| 1199 |
foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$curdb}{$k}{samples}}) { |
| 1200 |
# Do not process request that are slower than $max_duration |
| 1201 |
next if ( $max_duration && ( $d > $max_duration) ); |
| 1202 |
|
| 1203 |
my $head = qq{-- Generated on $curdate |
| 1204 |
-- Log file: $logfile_str |
| 1205 |
-- Log start from $overall_stat{'first_log_ts'} to $overall_stat{'last_log_ts'} |
| 1206 |
-- |
| 1207 |
}; |
| 1208 |
$head .= "-- duration: $d\n" if ($d); |
| 1209 |
$head .= "-- database: $normalyzed_info{$curdb}{$k}{samples}{$d}{db}\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{db}); |
| 1210 |
$head .= "-- user: $normalyzed_info{$curdb}{$k}{samples}{$d}{user}\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{user}); |
| 1211 |
$head .= "-- remote: $normalyzed_info{$curdb}{$k}{samples}{$d}{remote}\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{remote}); |
| 1212 |
$head .= "-- app: $normalyzed_info{$curdb}{$k}{samples}{$d}{app}\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{app}); |
| 1213 |
$head .= "--, bind query: yes\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{bind}); |
| 1214 |
$head =~ s/^, //; |
| 1215 |
|
| 1216 |
my $explain = "EXPLAIN\n"; |
| 1217 |
if ($analyze) { |
| 1218 |
$explain = "BEGIN;\nEXPLAIN (ANALYZE, VERBOSE, BUFFERS)\n"; |
| 1219 |
} |
| 1220 |
$explain .= "$normalyzed_info{$curdb}{$k}{samples}{$d}{query}\n"; |
| 1221 |
$explain .= "ROLLBACK;\n" if ($analyze); |
| 1222 |
if ($file_per_query) { |
| 1223 |
my $fhqr = new IO::File; |
| 1224 |
my $fname = sprintf("time_consuming_query%03d.sql", $rank); |
| 1225 |
$fhqr->open("> $fname") or die "FATAL: can't open temp file $fname, $!\n"; |
| 1226 |
$fhqr->print($head); |
| 1227 |
$fhqr->print(&fmtquery($explain)); |
| 1228 |
$fhqr->close(); |
| 1229 |
} else { |
| 1230 |
print $head; |
| 1231 |
print $explain; |
| 1232 |
} |
| 1233 |
if ($psql_cmd) { |
| 1234 |
my @tmpfile = tempfile('tmp_pgbadger_tools_XXXX', SUFFIX => '.txt', DIR => $TMP_DIR, UNLINK => 1 ); |
| 1235 |
my $fht = new IO::File; |
| 1236 |
$fht->open("> $tmpfile[1]") or die "FATAL: can't open temp file $tmpfile[1], $!\n"; |
| 1237 |
$fht->print("$explain"); |
| 1238 |
$fht->close(); |
| 1239 |
print `$psql_cmd -f $tmpfile[1]`; |
| 1240 |
unlink($tmpfile[1]); |
| 1241 |
} |
| 1242 |
last; # Just take one sample |
| 1243 |
} |
| 1244 |
last if ($rank > $top); |
| 1245 |
$rank++; |
| 1246 |
} |
| 1247 |
|
| 1248 |
} |
| 1249 |
|
| 1250 |
# Dump top normalized slowest queries as EXPLAIN statement. |
| 1251 |
sub dump_normalized_slowest_queries |
| 1252 |
{ |
| 1253 |
my $curdb = shift; |
| 1254 |
|
| 1255 |
# Global information |
| 1256 |
my $curdate = localtime(time); |
| 1257 |
my $logfile_str = join(',', @log_files); |
| 1258 |
|
| 1259 |
my $rank = 1; |
| 1260 |
foreach my $k (sort {$normalyzed_info{$curdb}{$b}{average} <=> $normalyzed_info{$curdb}{$a}{average}} keys %{$normalyzed_info{$curdb}}) { |
| 1261 |
next if (!$normalyzed_info{$curdb}{$k}{count}); |
| 1262 |
foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$curdb}{$k}{samples}}) { |
| 1263 |
# Do not process request that are slower than $max_duration |
| 1264 |
next if ( $max_duration && ( $d > $max_duration) ); |
| 1265 |
|
| 1266 |
my $head = qq{-- Generated on $curdate |
| 1267 |
-- Log file: $logfile_str |
| 1268 |
-- Log start from $overall_stat{'first_log_ts'} to $overall_stat{'last_log_ts'} |
| 1269 |
-- |
| 1270 |
}; |
| 1271 |
$head .= "-- duration: $d\n" if ($d); |
| 1272 |
$head .= "-- database: $normalyzed_info{$curdb}{$k}{samples}{$d}{db}\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{db}); |
| 1273 |
$head .= "-- user: $normalyzed_info{$curdb}{$k}{samples}{$d}{user}\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{user}); |
| 1274 |
$head .= "-- remote: $normalyzed_info{$curdb}{$k}{samples}{$d}{remote}\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{remote}); |
| 1275 |
$head .= "-- app: $normalyzed_info{$curdb}{$k}{samples}{$d}{app}\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{app}); |
| 1276 |
$head .= "--, bind query: yes\n" if ($normalyzed_info{$curdb}{$k}{samples}{$d}{bind}); |
| 1277 |
$head =~ s/^, //; |
| 1278 |
|
| 1279 |
my $explain = "EXPLAIN\n"; |
| 1280 |
if ($analyze) { |
| 1281 |
$explain = "BEGIN;\nEXPLAIN (ANALYZE, VERBOSE, BUFFERS)\n"; |
| 1282 |
} |
| 1283 |
$explain .= "$normalyzed_info{$curdb}{$k}{samples}{$d}{query}\n"; |
| 1284 |
$explain .= "ROLLBACK;\n" if ($analyze); |
| 1285 |
if ($file_per_query) { |
| 1286 |
my $fhqr = new IO::File; |
| 1287 |
my $fname = sprintf("normalized_slowest_query%03d.sql", $rank); |
| 1288 |
$fhqr->open("> $fname") or die "FATAL: can't open temp file $fname, $!\n"; |
| 1289 |
$fhqr->print($head); |
| 1290 |
$fhqr->print(&fmtquery($explain)); |
| 1291 |
$fhqr->close(); |
| 1292 |
} else { |
| 1293 |
print $head; |
| 1294 |
print $explain; |
| 1295 |
} |
| 1296 |
if ($psql_cmd) { |
| 1297 |
my @tmpfile = tempfile('tmp_pgbadger_tools_XXXX', SUFFIX => '.txt', DIR => $TMP_DIR, UNLINK => 1 ); |
| 1298 |
my $fht = new IO::File; |
| 1299 |
$fht->open("> $tmpfile[1]") or die "FATAL: can't open temp file $tmpfile[1], $!\n"; |
| 1300 |
$fht->print("$explain"); |
| 1301 |
$fht->close(); |
| 1302 |
print `$psql_cmd -f $tmpfile[1]`; |
| 1303 |
unlink($tmpfile[1]); |
| 1304 |
} |
| 1305 |
last; # Just take one sample |
| 1306 |
} |
| 1307 |
last if ($rank > $top); |
| 1308 |
$rank++; |
| 1309 |
} |
| 1310 |
|
| 1311 |
} |
| 1312 |
|
| 1313 |
# Export time consuming queries as CSV |
| 1314 |
sub csv_times_consuming_queries |
| 1315 |
{ |
| 1316 |
my $curdb = shift; |
| 1317 |
|
| 1318 |
my $fhcsv = undef; |
| 1319 |
|
| 1320 |
my $csv = Text::CSV->new({ |
| 1321 |
binary => 1, # should set binary attribute. |
| 1322 |
eol => $/, # end of line character |
| 1323 |
}) or die "Cannot use CSV: ".Text::CSV->error_diag (); |
| 1324 |
open $fhcsv, ">", "$csv_filename" or die "FATAL: can not write to $csv_filename: $!\n"; |
| 1325 |
$csv->print($fhcsv, ['Database','Rank','Total_Duration','Times_Executed','Min','Max','Avg','Query']); |
| 1326 |
|
| 1327 |
my $rank = 1; |
| 1328 |
foreach my $k (sort {$normalyzed_info{$curdb}{$b}{count} <=> $normalyzed_info{$curdb}{$a}{count}} keys %{$normalyzed_info{$curdb}}) { |
| 1329 |
next if (!$normalyzed_info{$curdb}{$k}{count}); |
| 1330 |
foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$curdb}{$k}{samples}}) { |
| 1331 |
# Do not process request that are slower than $max_duration |
| 1332 |
next if ( $max_duration && ( $d > $max_duration) ); |
| 1333 |
|
| 1334 |
$normalyzed_info{$curdb}{$k}{average} = $normalyzed_info{$curdb}{$k}{duration} / $normalyzed_info{$curdb}{$k}{count}; |
| 1335 |
|
| 1336 |
$csv->print ($fhcsv, [ |
| 1337 |
$normalyzed_info{$curdb}{$k}{samples}{$d}{db}, |
| 1338 |
$rank, |
| 1339 |
$d, |
| 1340 |
$normalyzed_info{$curdb}{$k}{count}, |
| 1341 |
$normalyzed_info{$curdb}{$k}{min}, |
| 1342 |
$normalyzed_info{$curdb}{$k}{max}, |
| 1343 |
$normalyzed_info{$curdb}{$k}{average}, |
| 1344 |
$normalyzed_info{$curdb}{$k}{samples}{$d}{query} |
| 1345 |
]); |
| 1346 |
# Report only the first sample |
| 1347 |
last; |
| 1348 |
|
| 1349 |
} |
| 1350 |
last if ($rank > $top); |
| 1351 |
$rank++; |
| 1352 |
} |
| 1353 |
close $fhcsv if (defined $fhcsv); |
| 1354 |
|
| 1355 |
} |
| 1356 |
|
| 1357 |
# Export normalized slowest queries as CSV |
| 1358 |
sub csv_normalized_queries |
| 1359 |
{ |
| 1360 |
my $curdb = shift; |
| 1361 |
|
| 1362 |
my $fhcsv = undef; |
| 1363 |
|
| 1364 |
my $csv = Text::CSV->new({ |
| 1365 |
binary => 1, # should set binary attribute. |
| 1366 |
eol => $/, # end of line character |
| 1367 |
}) or die "Cannot use CSV: ".Text::CSV->error_diag (); |
| 1368 |
open $fhcsv, ">", "$csv_filename" or die "FATAL: can not write to $csv_filename: $!\n"; |
| 1369 |
$csv->print($fhcsv, ['Database','Rank','Total_Duration','Times_Executed','Min','Max','Avg','Query']); |
| 1370 |
|
| 1371 |
my $rank = 1; |
| 1372 |
foreach my $k (sort {$normalyzed_info{$curdb}{$b}{average} <=> $normalyzed_info{$curdb}{$a}{average}} keys %{$normalyzed_info{$curdb}}) { |
| 1373 |
next if (!$normalyzed_info{$curdb}{$k}{count}); |
| 1374 |
foreach my $d (sort {$b <=> $a} keys %{$normalyzed_info{$curdb}{$k}{samples}}) { |
| 1375 |
# Do not process request that are slower than $max_duration |
| 1376 |
next if ( $max_duration && ( $d > $max_duration) ); |
| 1377 |
|
| 1378 |
$csv->print ($fhcsv, [ |
| 1379 |
$normalyzed_info{$curdb}{$k}{samples}{$d}{db}, |
| 1380 |
$rank, |
| 1381 |
$d, |
| 1382 |
$normalyzed_info{$curdb}{$k}{count}, |
| 1383 |
$normalyzed_info{$curdb}{$k}{min}, |
| 1384 |
$normalyzed_info{$curdb}{$k}{max}, |
| 1385 |
$normalyzed_info{$curdb}{$k}{average}, |
| 1386 |
$normalyzed_info{$curdb}{$k}{samples}{$d}{query} |
| 1387 |
]); |
| 1388 |
# Report only the first sample |
| 1389 |
last; |
| 1390 |
|
| 1391 |
} |
| 1392 |
last if ($rank > $top); |
| 1393 |
$rank++; |
| 1394 |
} |
| 1395 |
close $fhcsv if (defined $fhcsv); |
| 1396 |
|
| 1397 |
} |
| 1398 |
|
| 1399 |
# Export slowest queries as CSV |
| 1400 |
sub csv_slowest_queries |
| 1401 |
{ |
| 1402 |
my $curdb = shift; |
| 1403 |
|
| 1404 |
my $fhcsv = undef; |
| 1405 |
|
| 1406 |
my $csv = Text::CSV->new({ |
| 1407 |
binary => 1, # should set binary attribute. |
| 1408 |
eol => $/, # end of line character |
| 1409 |
}) or die "Cannot use CSV: ".Text::CSV->error_diag (); |
| 1410 |
open $fhcsv, ">", "$csv_filename" or die "FATAL: can not write to $csv_filename: $!\n"; |
| 1411 |
$csv->print($fhcsv, ['Database','Rank','Duration','Query']); |
| 1412 |
|
| 1413 |
my $rank = 1; |
| 1414 |
for (my $i = 0 ; $i <= $#{$top_slowest{$curdb}} ; $i++) { |
| 1415 |
|
| 1416 |
# Do not process request that are slower than $max_duration |
| 1417 |
next if ( $max_duration && ($top_slowest{$curdb}[$i]->[0] > $max_duration) ); |
| 1418 |
|
| 1419 |
$csv->print ($fhcsv, [ |
| 1420 |
$top_slowest{$curdb}[$i]->[3], |
| 1421 |
$rank, |
| 1422 |
$top_slowest{$curdb}[$i]->[0], |
| 1423 |
$top_slowest{$curdb}[$i]->[2] |
| 1424 |
]); |
| 1425 |
last if ($rank > $top); |
| 1426 |
$rank++; |
| 1427 |
} |
| 1428 |
close $fhcsv if (defined $fhcsv); |
| 1429 |
} |
| 1430 |
|
| 1431 |
|