Skip to content

Instantly share code, notes, and snippets.

@adunstan
Last active December 16, 2015 11:49
Show Gist options
  • Save adunstan/5430395 to your computer and use it in GitHub Desktop.
Save adunstan/5430395 to your computer and use it in GitHub Desktop.
exlain_url sql function
CREATE OR REPLACE FUNCTION public.explain_url(query text)
RETURNS text
LANGUAGE plperlu
AS $function$
my $query = shift;
elog(ERROR,"not an explain query") unless $query =~ /^\s*explain\s/i;
my $rv = spi_exec_query($query);
my $nrows = $rv->{processed};
my $text = $query . "\n QUERY PLAN\n-------------------\n";
foreach my $rn (0 .. $nrows-1)
{
$text = $text . "$rv->{rows}[$rn]->{'QUERY PLAN'}\n";
}
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $req = POST ' http://explain.depesz.com/',
[ plan => $text ];
my $resp = $ua->request($req);
my $loc = $resp->header('Location');
$loc =~ s!^/!http://explain.depesz.com/!;
return $loc;
$function$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment