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

add option to pass through psql flags #2

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ Usage:
pev2 <path-to-query-file>
Notes:
This tool will run the query using the \`psql\` command-line tool.

Prefix your SQL query with the following line to get a detailed analysis:
EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS, FORMAT JSON)
Options:
--open -o Open the URL in the browser.
--quiet -q Don't report what's going on.
--once -1 Stop serving after pev2 has received the data.
--open -o Open the URL in the browser.
--quiet -q Don't report what's going on.
--once -1 Stop serving after pev2 has received the data.
--psql-opts -p Options to pass into psql.
Examples:
pev2 --open -q path/to/some-explain-query.sql
pev2 --open -q -p '-v enable_seqscan=off' path/to/some-explain-query.sql
\n`)
process.exit(0)
}
Expand All @@ -47,6 +51,10 @@ const showError = (err) => {
}

;(async () => {
console.error('process.argv', process.argv)
console.error('argv', argv)
process.exit(1)

const pathToQuery = argv._[0]
if (!pathToQuery) {
showError('Missing 1st argument: path to EXPLAIN query file.')
Expand All @@ -55,12 +63,14 @@ const showError = (err) => {

const quiet = !!(argv.quiet || argv.q)
const once = !!(argv.once || argv['1'])
const psqlOpts = argv['psql-opts'] || argv.p || ''

if (!quiet) console.info(`running psql with ${pathToQuery}`)
const {stdout: explainResult} = await execa('psql', [
'-XqAt', // from pev2 instructions
'-v', 'ON_ERROR_STOP=1', // stop & exit non-zero on errors
'-f', pathToQuery,
psqlOpts,
])

const {url} = await visualizeExplainFile(explainResult, query, {
Expand Down