January 30th, 2013 by depesz | Tags: , , | 5 comments »
Did it help? If yes - maybe you can help me?

So, lately I have been thinking about integrating explain.depesz.com with psql. Currently, you have to run explain, copy the output, switch to webbrowser, go to explain.depesz.coma> site, paste output, click submit. It's not that it's a lot of work, but it can be simplified.

To simplify it, we need just 2 minutes.

First, you need to create /usr/local/bin/explain.depesz.com script (path and name is irrelevant, but /usr/local/bin/ is usually in $PATH, and explain.depesz.com name will be pretty self-explanatory when using it.

Content of this file should be:

#!/usr/bin/env bash
url="$( curl --silent --include --data-urlencode "plan@-" "${service_url}/" | perl -ne 'print if s/\ALocation:\s*(\S+)\s*\z/$1/' )"
echo "${service_url}${url}"
firefox "${service_url}${url}"

Instead of “firefox" you can use any webbrowser you want, though you might need to play with options to it.

Anyway. When you'll do it, and the script will have 0755 privileges, then in your psql, you run something along the lines of:

$ \o |explain.depesz.com
$ explain analyze select * from pg_class where relname = 'YEAH';
$ \o
Created new window in existing browser session.

That is, you run: “\o |explain.depesz.com", which redirects output of all commands to this script (it should be started by now, by psql).

Then you run your explain command.

And afterwards – you let psql know that you're done with output redirection – with \o.

At this time (at the end), explain.depesz.com, will get all the data, post it to explain.depesz.com, and open explain page in your browser (but only after final \o). You can also put the final \o at the end of explain line (after semicolon).

If this is too much for you to remember (switching \o on and off), you can get the same results by doing:

$ explain analyze select * from pg_class where relname = 'YEAH' \g | explain.depesz.com

that is – replacing “;" with “\g | …".

Hope if will help you, a bit 🙂

  1. 5 comments

  2. # Andrew Dunstan
    Feb 1, 2013

    Nice. Unfortunately I’m mostly remotely logged in to a client’s site when I need this, so I created a version that just echoes the URL rather than trying to load it into a browser. My terminal program is smart enough to recognize a URL and make it clickable.

    It’s a pity we can’t easily include the query with the explain.

  3. Feb 1, 2013

    well, re: query – I need help with yacc-style conflcts in grammar. once it will be done, there will be proper sql paste site, and it will be part of explain.depesz.com. The problem – I can’t seem to find anyone to look with me at the conflicts, and help me :/

  4. # Matt K
    Apr 5, 2013

    Thanks for this. I, like @Andrew, preferred a textual display of the URL. I combined this with my existing shortcuts for explain and explain analyze.

    Here’s my setup:
    \set exp ‘explain (verbose on)’
    \set expan ‘explain (analyze on, buffers on, verbose on, timing on)’
    \set webexp ‘\\g | explain.depesz.com


    curl –silent –include –data-urlencode “plan=$(cat -)” https://explain.depesz.com/ | \
    perl -ne ‘print if s/\ALocation:\s*(\S+)\s*\z/$1\n/’

    Then when in a psql shell:
    foo=> :expan select random() from generate_series(1,1000) :webexp;

  5. Sep 12, 2017

    Thanks to MatheusOl from IRC for update for the script – it didn’t work when I migrated to https, but the new version (updated in post body) works great.

  6. Sep 12, 2017

    And thanks for RhodiumToad for the @- trick 🙂

Sorry, comments for this post are disabled.