dalibo / explain.dalibo.com

PEV2 Flask service. Visualizing and understanding PostgreSQL EXPLAIN plans made easy.
PostgreSQL License
40 stars 15 forks source link

Script to create a plan from query #18

Open piranha opened 8 months ago

piranha commented 8 months ago

I'm writing a shell script to create a plan from query and got to the following curl command:

echo "$PLAN" | curl -s -w '%{redirect_url}' --data-urlencode plan@- -X POST 'https://explain.dalibo.com/new'

It returns an url to the plan, but then the url returns error 500. It's https://explain.dalibo.com/plan/0424...hacb (I can post full URL if you want, but I suppose you can see that in logs somewhere). Any pointers on what's wrong the the query? $PLAN in question is a JSON from postgres.

pgiraud commented 8 months ago

Thanks for the report. Currently, the POST is requiring title and query to be set to at least an empty string.

The following curl command works.

curl -Ls -w %{url_effective} -d '{"plan":"Seq Scan on tenk1 (cost=0.00..333.00 rows=10000 width=148)", "title": "", "query": ""}' -H "Content-Type: application/json" -X POST https://explain.dalibo.com/new -o /dev/null

I'll try to fix the code so that those parameters are not required.

piranha commented 8 months ago

Thanks, it works now! If you've interested in adding that to a site, I've got a script to execute a query in Postgres and then POST it to explain.dalibo.com:

#!/bin/sh

set -euo pipefail

if [ -z "${1:-}" ]; then
    echo "Usage: $0 <name from pg_service.conf>"
    echo ""
    echo "Gets SQL query from clipboard, sends it to Postgres you supply as"
    echo "a first argument and posts it to explain.dalibo.com"
    exit 1
fi

SERVICE="$1"
QUERY="EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON) $(pbpaste)"

PLAN=$(psql "service=$SERVICE" -qXAt -c "$QUERY")
URL=$(echo "$PLAN" | curl -s -w '%{redirect_url}' --data-urlencode "query=$QUERY" --data-urlencode title= --data-urlencode plan@- -X POST 'https://explain.dalibo.com/new' -o /dev/null)
echo "$URL" | pbcopy
echo "URL copied to clipboard: $URL"