From: Darold Gilles Date: Mon, 28 Apr 2014 18:29:39 +0000 (+0200) Subject: Add support to named PREPARE and EXECUTE queries. Calls to EXECUTE statements are... X-Git-Tag: v5.1~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f8dccadd9263c3e32f7ab8c9a2292f0d403bb390;p=pgbadger Add support to named PREPARE and EXECUTE queries. Calls to EXECUTE statements are now replaced by the prepared query and show samples with parameters. Thanks to Brian DeRocher for the feature request. --- diff --git a/pgbadger b/pgbadger index 4ff8043..d5274e2 100644 --- a/pgbadger +++ b/pgbadger @@ -8528,7 +8528,7 @@ sub parse_query return; } - # Look at bind parameters if any + # Look at bind/execute parameters if any if ($cur_info{$t_pid}{query}) { # Remove obsolete connection storage @@ -8542,6 +8542,19 @@ sub parse_query $cur_info{$t_pid}{parameters} = "$1"; # go look at other params return; + # replace the execute statements with the prepared query and set the parameters + } elsif (($prefix_vars{'t_loglevel'} eq 'DETAIL') && ($prefix_vars{'t_query'} =~ s/prepare: PREPARE[\s\t]+([^\s\t]+)[\s\t]+AS[\s\t]+(.*)//is)) { + my $q_name = $1; + my $real_query = $2; + if ($cur_info{$t_pid}{query} =~ /\b$q_name\b/) { + $cur_info{$t_pid}{query} =~ s/EXECUTE[\s\t]+$q_name([\s\t]+)\(//is; + $cur_info{$t_pid}{parameters} = $cur_info{$t_pid}{query}; + $cur_info{$t_pid}{parameters} =~ s/\)$//; + $cur_info{$t_pid}{query} = $real_query; + $cur_info{$t_pid}{'bind'} = 1; + } + # go look at other params + return; } } } @@ -8750,12 +8763,19 @@ sub store_queries $cur_info{$t_pid}{query} =~ s/[\t\s\r\n;]+$//s; # Replace bind parameters values in the query if any - if (exists $cur_info{$t_pid}{parameters}) { + if (exists $cur_info{$t_pid}{parameters} && ($cur_info{$t_pid}{parameters} =~ /[,\s]*\$(\d+)\s=\s/)) { my @t_res = split(/[,\s]*\$(\d+)\s=\s/, $cur_info{$t_pid}{parameters}); shift(@t_res); for (my $i = 0 ; $i < $#t_res ; $i += 2) { $cur_info{$t_pid}{query} =~ s/\$$t_res[$i]\b/$t_res[$i+1]/s; } + } else { + # parameters from EXECUTE statements + my @t_res = split(/[\s]*,[\s]*/, $cur_info{$t_pid}{parameters}); + for (my $i = 0 ; $i <= $#t_res ; $i++) { + my $num = $i + 1; + $cur_info{$t_pid}{query} =~ s/\$$num\b/$t_res[$i]/s; + } } # We only process stored object with query here