Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script to create a plan from query #18

Open
piranha opened this issue Dec 7, 2023 · 2 comments
Open

Script to create a plan from query #18

piranha opened this issue Dec 7, 2023 · 2 comments

Comments

@piranha
Copy link

piranha commented Dec 7, 2023

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
Copy link
Member

pgiraud commented Dec 7, 2023

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
Copy link
Author

piranha commented Dec 7, 2023

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants