Fix special commands (\q, \d, exit, ...) being broken in explain mode - #1620
Open
DiegoDAF wants to merge 1 commit into
Open
Fix special commands (\q, \d, exit, ...) being broken in explain mode#1620DiegoDAF wants to merge 1 commit into
DiegoDAF wants to merge 1 commit into
Conversation
In PGExecute.run() the explain prefix was applied in an if/elif with the
pgspecial branch:
if explain_mode:
sql = self.explain_prefix() + sql
elif pgspecial:
...detect and run special commands...
So with explain mode (F5) on, the pgspecial branch was never reached and every
input was prefixed with EXPLAIN (...) and sent to the server as SQL. \q, \d,
\i, named queries and the bare words exit/quit all came back with
`syntax error at or near "\"`, leaving no way to turn explain mode off or to
quit the session.
Special commands are not SQL, so they are now detected first, regardless of
explain mode, and the EXPLAIN prefix is applied further down to statements that
fall through as normal SQL. This also fixes `select ... \G` in explain mode:
the \G is stripped by the pgspecial branch, which previously never ran.
Adds four tests: a special command is dispatched (and never sent to the
server), a describe command runs as special, normal SQL is still prefixed, and
\G is stripped.
DiegoDAF
added a commit
to DiegoDAF/pgcli.daf
that referenced
this pull request
Aug 18, 2026
Contributor
Author
|
The red CI here is not this change, and it is a nice demonstration of why On this same commit:
Same code, one Python version errors and another does not, and the errored #1619 raises those timeouts and should make this stop happening across the (The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
With explain mode (F5) turned on, no special command works, and there is no way
out of the session short of killing it.
PGExecute.run()applies the explain prefix in anif/elifwith thepgspecial branch:
Because it is an
elif, the pgspecial branch is never reached whileexplain_modeis true, so every input is prefixed and shipped to the server asSQL:
\q,exit/quit,\d,\i, named queries and the rest all fail the sameway. F5 does not help either, since toggling it back is a key binding but the
session is already stuck for anything typed.
This has been there since the explain visualizer was added in #1279.
Change
Special commands are not SQL, so detect them first regardless of explain mode,
and apply the EXPLAIN prefix further down, to statements that fall through as
normal SQL.
Two side effects worth mentioning:
select ... \Gnow works in explain mode too. The\Gstripping lives inthe pgspecial branch, so previously the literal
\Gwas sent to the server.reconnect path) is reachable in explain mode again.
Validation
Four new tests in
tests/test_pgexecute.py: a special command is dispatchedand never reaches
execute_normal_sql, a describe command runs as special,normal SQL is still prefixed with
explain_prefix(), and\Gis stripped.Three of the four fail on current main.
Full suite green locally (2734 passed).
Checklist
changelog.rst.Not a feature from my list in discussion #1603: this is one of the upstream bugs I ran into while maintaining the fork, listed in the status section at the bottom of that discussion.