Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Command-line reference

Everything ExplainSQL accepts, in one place. explainsql --help and explainsql COMMAND --help print the same information in your terminal.

explainsql [OPTIONS] [FILE]       read a plan, or run a statement with -d
explainsql diff BEFORE [AFTER]    compare two plans
explainsql check PATHS…           check plans in CI
explainsql logs FILES…            plan changes in auto_explain logs
explainsql top -d DATABASE        the costliest statements from pg_stat_statements
explainsql requests FILES…        N+1 loops in statement logs
explainsql anonymize [FILE]       a plan with names and values replaced

explainsql

Reads a plan from FILE, or from standard input when FILE is missing or -, and opens it in the viewer or prints a report. With -d and -f or -c, it runs a statement itself instead (connected mode).

Reading and output

OptionWhat it does
FILEThe plan file: JSON or text, as EXPLAIN printed it or wrapped in psql output, a log entry, a GUI client’s cell or a Markdown fence. Standard input when missing or -.
--printPrint a report instead of opening the viewer. This happens anyway when the output is not a terminal.
--format text|md|jsonThe report’s format. Default: text.
--color auto|always|neverWhen to color the text report. auto (the default) colors when the output is a terminal and NO_COLOR is not set.
--theme dark|lightThe terminal’s background, for the viewer’s colors. Default: dark.
--demoShow the bundled sample plan instead of reading one.
--pagerAct as psql’s pager: open plans in the viewer, pass any other output to $EXPLAINSQL_PAGER, $PAGER or less -S.
--debug-parsePrint what the parser understood instead of the analysis. With --format json, the parsed plan as JSON.
--fail-on low|medium|highWith a printed report, exit with 1 when a finding is at least this severe.

Connected mode

OptionWhat it does
-d, --dbname DATABASEThe database: a URL, key=value settings or a name. PG* variables, the service file and ~/.pgpass apply as in psql.
-f, --query-file FILERun the statement in this file.
-c, --command SQLRun this statement.
--no-analyzeShow the estimated plan only, without running the statement.
--timeout SECONDSStop a run after this many seconds. Default: 30.
--allow-dmlAlso run statements that modify data or lock rows, still in a transaction that is rolled back, and report what their writes cost.
--allow-ddlTo test an index without HypoPG, build it in a transaction that is rolled back. With --prove, also drop the indexes that keep updates from being HOT. Both block the table while they run.
--proveWith --print: test each suggested index, and with --allow-ddl, rerun a non-HOT update without its blocking indexes.
--why-not [TABLE]With --print: ask the planner why it chose its plan for the slowest nodes, or for the scans of TABLE (a table or index name).
--measureMeasure the alternatives of --why-not and y, and the differing plans of --params, with EXPLAIN ANALYZE instead of only estimating them.
--runs NHow many measured runs to compare for --prove and --measure, each side after one warm-up run. The median counts. Default: 1.
--locksReport the locks the statement takes.
--paramsThe statement takes parameters ($1, or ? as in JDBC): compare the plans their values get with the generic plan.
--bind N=VALUEThe value of $N, tried instead of values from the statistics. Repeat for each parameter. Implies --params.

explainsql diff

explainsql diff [OPTIONS] BEFORE [AFTER]

Compares two plans of the same statement, node by node. Without AFTER, BEFORE must hold both plans, one after the other.

OptionWhat it does
BEFOREThe plan before: a file, or - for standard input.
AFTERThe plan after.
--format text|md|jsonDefault: text.
--color auto|always|neverDefault: auto.

explainsql check

explainsql check [OPTIONS] PATHS…

Checks plans in CI: each against its findings and against the plan locked for it. Exits with 0 when every plan passed, 1 when one failed, 2 on an error.

OptionWhat it does
PATHSPlan files, or with -d, SQL files. Directories are searched for *.json and *.txt plans, or *.sql statements.
-d, --dbname DATABASERun the SQL files against this database.
--fail-on low|medium|highAlso fail a plan with a finding at least this severe.
--strictAlso fail a plan whose shape changed, even when it is not worse.
--lock FILEThe file of locked plans. Default: explainsql.lock.
--updateLock the plans as they are now instead of checking them. Other plans in the file are kept.
--proveWith -d: test the suggested indexes of each plan that failed, with HypoPG.
--no-analyzeWith -d: plan the statements without running them.
--allow-dmlWith -d: also run statements that modify data or lock rows, rolled back.
--timeout SECONDSWith -d: stop a statement after this many seconds. Default: 30.
--format text|md|json|sarifDefault: text.
--sarif FILEAlso write the SARIF report to this file.
--color auto|always|neverDefault: auto.

explainsql logs

explainsql logs [OPTIONS] FILES…

Reads auto_explain plans from server logs (stderr, csvlog or jsonlog; - for standard input) and reports when each statement’s plan changed.

OptionWhat it does
--since TIMEOnly entries from this time on: as the log prints times (2026-10-06 06:00), or 30m, 24h, 7d back from the last entry.
--until TIMEOnly entries up to this time.
--query ID|TEXTOnly the statement with this query identifier, or whose text, prepared name or tags contain this.
--trace TRACE_IDOnly statements that ran in this trace (sqlcommenter traceparent).
--changedOnly statements whose plan changed.
--format text|md|jsonDefault: text.
--color auto|always|neverDefault: auto.

explainsql top

explainsql top [OPTIONS]

Lists a database’s costliest statements from pg_stat_statements, and plans the one you pick.

OptionWhat it does
-d, --dbname DATABASEThe database, as in connected mode.
--limit NHow many statements to list. Default: 20.
--printPrint the list instead of opening it.
--format text|md|jsonThe printed list’s format. Default: text.
--color auto|always|neverDefault: auto.
--theme dark|lightDefault: dark.
--measureWhen trying parameter values, measure the plans where they differ.
--allow-dmlWith --measure: also run statements that write, rolled back.
--timeout SECONDSStop each query after this many seconds. Default: 30.

In the list: Enter plans the selected statement, p tries values for its parameters, q goes back or quits.

explainsql requests

explainsql requests [OPTIONS] FILES…

Groups the statements of server logs into requests and finds the loops (N+1), with the batched statement for each.

OptionWhat it does
-d, --dbname DATABASEMeasure each loop’s batched statement against its runs, and look up the foreign key behind it.
--min-runs NThe fewest runs of a statement in one request that make a loop. Default: 3.
--gap MSStatements of a session without a trace or a transaction stay in one request while it is idle no longer than this. Default: 50.
--limit NHow many loops to show, and with -d, to measure. Default: 10.
--runs NWith -d: the median of this many measured runs of the batched statement. Default: 1.
--allow-dmlWith -d: also measure loops of statements that write, rolled back.
--timeout SECONDSWith -d: stop a statement after this many seconds. Default: 30.
--format text|md|jsonDefault: text.
--color auto|always|neverDefault: auto.

explainsql anonymize

explainsql anonymize [OPTIONS] [FILE]

Prints the plans of FILE (or standard input) with names and literal values replaced.

OptionWhat it does
--keep-namesKeep the names of tables, columns and other objects; replace only literal values.
--map FILEWrite what each name and value became to this JSON file.

Environment variables

VariableUsed for
PGHOST, PGHOSTADDR, PGPORT, PGUSER, PGPASSWORD, PGDATABASE, PGAPPNAME, PGSSLMODE, PGSSLROOTCERT, PGSERVICEConnection settings, as libpq reads them.
PGPASSFILE, PGSERVICEFILE, PGSYSCONFDIRWhere the password file, the service file and the system-wide service file are, when not in their default places.
EXPLAINSQL_PAGER, PAGERIn --pager mode, the pager for output that is not a plan. Default: less -S.
VISUAL, EDITORThe editor e opens in the viewer.
NO_COLORTurns colors off in the viewer and in reports.
COLORTERM, TERMHow many colors the terminal supports. TERM=dumb disables the viewer.
EXPLAINSQL_TEST_DATABASE_URLFor development only: the database the connected-mode tests run against.

Files

FileWhat it is
explainsql.lockThe locked plans of explainsql check, meant to be committed.
~/.pgpass (%APPDATA%\postgresql\pgpass.conf on Windows)Passwords, as for psql. Ignored when other users can read it.
~/.pg_service.confConnection services, as for psql.

Exit codes

Command012
explainsql, diff, logs, top, requests, anonymizeSuccessAn error, or with --fail-on, a finding at least that severe
checkEvery plan passedA plan failedThe check could not run