Skip to content

Commit

Permalink
Add new option -Q | --query-numbering used to add numbering of queries
Browse files Browse the repository at this point in the history
to the output when using options --dump-all-queries or --normalized-only.
This can be useful to extract multiline queries in the output file from
an external script. Thanks to Shantanu Oak for the feature request.
  • Loading branch information
Gilles Darold committed May 19, 2020
1 parent 1de1505 commit dd4b134
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README
Expand Up @@ -73,6 +73,8 @@ SYNOPSIS
-P | --no-prettify : disable SQL queries prettify formatter.
-q | --quiet : don't print anything to stdout, not even a progress
bar.
-Q | --query-numbering : add numbering of queries to the output when using
options --dump-all-queries or --normalized-only.
-r | --remote-host ip : set the host where to execute the cat command on
remote logfile to parse locally the file.
-R | --retention N : number of weeks to keep in incremental mode. Default
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -92,6 +92,8 @@ Options:
-P | --no-prettify : disable SQL queries prettify formatter.
-q | --quiet : don't print anything to stdout, not even a progress
bar.
-Q | --query-numbering : add numbering of queries to the output when using
options --dump-all-queries or --normalized-only.
-r | --remote-host ip : set the host where to execute the cat command on
remote logfile to parse locally the file.
-R | --retention N : number of weeks to keep in incremental mode. Default
Expand Down
2 changes: 2 additions & 0 deletions doc/pgBadger.pod
Expand Up @@ -75,6 +75,8 @@ Options:
-P | --no-prettify : disable SQL queries prettify formatter.
-q | --quiet : don't print anything to stdout, not even a progress
bar.
-Q | --query-numbering : add numbering of queries to the output when using
options --dump-all-queries or --normalized-only.
-r | --remote-host ip : set the host where to execute the cat command on
remote logfile to parse locally the file.
-R | --retention N : number of weeks to keep in incremental mode. Default
Expand Down
20 changes: 16 additions & 4 deletions pgbadger
Expand Up @@ -360,6 +360,7 @@ my $noexplain = 0;
my $log_command = '';
my $wide_char = 0;
my $noweekreport = 0;
my $query_numbering = 0;

my $NUMPROGRESS = 10;
my @DIMENSIONS = (800, 300);
Expand Down Expand Up @@ -476,6 +477,7 @@ my $result = GetOptions(
"p|prefix=s" => \$log_line_prefix,
"P|no-prettify!" => \$noprettify,
"q|quiet!" => \$quiet,
"Q|query-numbering!" => \$query_numbering,
"r|remote-host=s" => \$remote_host,
'R|retention=i' => \$retention,
"s|sample=i" => \$sample,
Expand Down Expand Up @@ -2043,6 +2045,8 @@ Options:
-P | --no-prettify : disable SQL queries prettify formatter.
-q | --quiet : don't print anything to stdout, not even a progress
bar.
-Q | --query-numbering : add numbering of queries to the output when using
options --dump-all-queries or --normalized-only.
-r | --remote-host ip : set the host where to execute the cat command on
remote logfile to parse locally the file.
-R | --retention N : number of weeks to keep in incremental mode. Default
Expand Down Expand Up @@ -5055,11 +5059,18 @@ Log start from $t_log_min to $t_log_max
};

# Dump normalized queries only if requested
if ($dump_normalized_only) {
print $fh "Count\t\tQuery\n";
print $fh '-'x70,"\n";
if ($dump_normalized_only)
{
if (!$query_numbering) {
print $fh "Count\t\tQuery\n";
print $fh '-'x70,"\n";
} else {
print $fh "#Num\tCount\t\tQuery\n";
print $fh '-'x80,"\n";
}
foreach my $q (sort { $normalyzed_info{$curdb}{$b}{count} <=> $normalyzed_info{$curdb}{$a}{count} } keys %{$normalyzed_info{$curdb}}) {
print $fh "$normalyzed_info{$curdb}{$q}{count} $q\n";
print $fh $query_numbering++, "\t" if ($query_numbering);
print $fh "$normalyzed_info{$curdb}{$q}{count}\t$q\n";
}
print $fh "\n\n";
print $fh "Report generated by pgBadger $VERSION ($project_url).\n";
Expand Down Expand Up @@ -15640,6 +15651,7 @@ sub store_queries
}
}
return if ($cur_info{$t_pid}{query} =~ /^parameters:/);
print $fh "# Query #", $query_numbering++, "\n" if ($query_numbering);
print $fh "$cur_info{$t_pid}{query}\n";
$overall_stat{'queries_number'}++;
return 1;
Expand Down

0 comments on commit dd4b134

Please sign in to comment.