Last active
December 16, 2015 11:49
-
-
Save adunstan/5430395 to your computer and use it in GitHub Desktop.
exlain_url sql function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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