Skip to content

Instantly share code, notes, and snippets.

@revmischa
Created August 31, 2016 01:23
Show Gist options
  • Save revmischa/86c0ee0da810fbe526021581be679d0f to your computer and use it in GitHub Desktop.
Save revmischa/86c0ee0da810fbe526021581be679d0f to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# convert postgres slow query log query w/ separate 'parameters:' line into an executable EXPLAIN ANALYZE query
use strict;
use warnings;
use Data::Dump qw/ddx/;
my $qfile = shift @ARGV or die "query file required";
my $qf_contents;
my $fh;
open($fh, $qfile) or die $!;
{
local $/;
$qf_contents = <$fh>;
}
close($fh);
my %matches = $qf_contents =~ m/\$(\d+) = '([^']+)/msg;
$qf_contents =~ s/^.*parameters: .+$//m;
my ($k, $v);
while (($k, $v) = each %matches) {
$qf_contents =~ s/\$${k}/$v/e;
}
chomp $qf_contents;
print "EXPLAIN ANALYZE $qf_contents;\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment