psql
13.2 executes the query buffer no matter what when exiting the editor, even after quitting it – in which case the buffer may hold the most recently executed SQL query now.
To reproduce:
- Start editor (
vim
in my case) with\e
or\e filename
from thepsql
console. - Type something or not, doesn't make a difference.
- Cancel edit with
:q
(or:q!
to force it)
Expected behavior: go back to the state before starting the editor. (I cancelled the edit!)
Observed behavior: re-execute the last executed SQL query.
What seems to happen with \e
:
-
it executes (the equivalent of)
\p
, so the current query buffer contains the previous command when it was empty. (Which is not visible to the user when editing a file!)
The manual on\p
:\p
or\print
Print the current query buffer to the standard output. If the current query buffer is empty, the most recently executed query is printed instead.
-
Start up the editor. If a filename was provided, load the file, else, load the query buffer.
-
If the editor ends with a write, copy the result to the query buffer.
If the edit is cancelled (:q
in vim), don't overwrite the query buffer. (It holds the previous command now!) -
Execute the query buffer. Or, to be precise, do this:
The behavior seems ok for \p
(just copy the query buffer, no harm done), but not for \e
, which can execute a command I didn't even get to see.
Is it a bug?
Either way, I would at least like to disable auto-execute when leaving the editor. But that doesn't seem to be possible. Or am I missing something?
Best Answer
This has been fixed in v14 as a reaction to this question. The rest of the answer refers to the pre-v14 behavior:
This is (sort of) documented:
If you quit the editor without saving, it is the current query buffer or the most recently executed query that are executed.
I agree with you that this is annoying, and you are not the first person I heard complain.
When editing a script, the behavior clearly violates the “principle of least astonishment”: