Thread: psql _ and \e -- what does clearing the query buffer actually do?

psql _ and \e -- what does clearing the query buffer actually do?

From
Merlin Moncure
Date:
Hello all,

Couple things -- after unintentionally running a query for the (what feels like-) millionth time, it suggested a review of psql query editing behaviors. Starting with,

\r (clear 'query buffer'):  Other than dutifully reporting that the query buffer has been cleared, I cannot for the life of me detect any observable behavior.  If there is in fact one, the results are, with some forbearance, unintuitive.  Consider:
postgres=# \p
select 0;
postgres=# \r
Query buffer reset (cleared).
postgres=# \p
select 0;

Perhaps, it is reset, only to re-reset to last known prompt when prompt returned, but,
\r\e 
...claims to rest but the query buffer still contains the last executed command, which will then run with :q! in vim.

Regarding \e, things mostly work sanely, except that there is no way to not run a query except to explicitly write a blank buffer back, say with vim :wq after deleting all the lines in the buffer..  However, if you do so, the previously run query shows up again, for the next \e, which is odd.  Is this behavior intentional?  Is there something I'm missing?  Apologies if this has been discussed, cursory search did not turn up results, psql, query, buffer, etc not being selective terms.

merlin

Re: psql _ and \e -- what does clearing the query buffer actually do?

From
Tom Lane
Date:
Merlin Moncure <mmoncure@gmail.com> writes:
> \r (clear 'query buffer'):  Other than dutifully reporting that the query
> buffer has been cleared, I cannot for the life of me detect any observable
> behavior.

Uh ... surely there's a lot.  For example:

regression=# foo
regression-# bar;
ERROR:  syntax error at or near "foo"
LINE 1: foo
        ^
regression=# foo
regression-# \r
Query buffer reset (cleared).
regression=# bar;
ERROR:  syntax error at or near "bar"
LINE 1: bar;
        ^
regression=# 

> Perhaps, it is reset, only to re-reset to last known prompt when prompt
> returned, but,
> \r\e
> ...claims to rest but the query buffer still contains the last executed
> command, which will then run with :q! in vim.

What \r clears is the current (active) input buffer.  \e is documented
thus:

    If no filename is given, the current query buffer is copied to a
    temporary file which is then edited in the same fashion. Or, if the
    current query buffer is empty, the most recently executed query is
    copied to a temporary file and edited in the same fashion.

So \r\e will indeed result in editing the previous query.

> Regarding \e, things mostly work sanely, except that there is no way to not
> run a query except to explicitly write a blank buffer back, say with vim
> :wq after deleting all the lines in the buffer..

It's sufficient to get rid of any trailing semicolon in the editor.
Again, the man page's discussion of \e seems to me to cover this.
If you want to suggest some adjustments to make it clearer, we
could discuss that.

            regards, tom lane



Re: psql _ and \e -- what does clearing the query buffer actually do?

From
Merlin Moncure
Date:


On Mon, Oct 23, 2023 at 1:50 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Merlin Moncure <mmoncure@gmail.com> writes:
> \r (clear 'query buffer'):  Other than dutifully reporting that the query
> buffer has been cleared, I cannot for the life of me detect any observable
> behavior.

Uh ... surely there's a lot.  For example:

regression=# foo
regression-# bar;
ERROR:  syntax error at or near "foo"
LINE 1: foo
        ^
regression=# foo
regression-# \r
Query buffer reset (cleared).
regression=# bar;
ERROR:  syntax error at or near "bar"
LINE 1: bar;
        ^
regression=#

> Perhaps, it is reset, only to re-reset to last known prompt when prompt
> returned, but,
> \r\e
> ...claims to rest but the query buffer still contains the last executed
> command, which will then run with :q! in vim.

What \r clears is the current (active) input buffer.  \e is documented
thus:

    If no filename is given, the current query buffer is copied to a
    temporary file which is then edited in the same fashion. Or, if the
    current query buffer is empty, the most recently executed query is
    copied to a temporary file and edited in the same fashion.

So \r\e will indeed result in editing the previous query.

> Regarding \e, things mostly work sanely, except that there is no way to not
> run a query except to explicitly write a blank buffer back, say with vim
> :wq after deleting all the lines in the buffer..

It's sufficient to get rid of any trailing semicolon in the editor.
Again, the man page's discussion of \e seems to me to cover this.
If you want to suggest some adjustments to make it clearer, we
could discuss that.

Hm, well that explains my misunderstanding, TIL.  The man page for \e at least is very clear both in terms of behavior and intent.  FWIW The finer details are not in \?, which i did check (vs the man page, which you correctly guessed I did not check).  It reports:

Query Buffer
  \e [FILE] [LINE]       edit the query buffer (or file) with external editor
  \ef [FUNCNAME [LINE]]  edit function definition with external editor
  \ev [VIEWNAME [LINE]]  edit view definition with external editor
  \p                    show the contents of the query buffer
  \r                     reset (clear) the query buffer
  \s [FILE]              display history or save it to file
  \w FILE                write query buffer to file

The basic problem is that the query buffer is used both to represent the unsent query text and the query to be sent.  

Maybe after the 'Query Buffer' line, something like,
  (An empty query buffer will to refer the last executed query, if any)

...don't know if it's worth adjusting in hindsight, but the \e\p mechanics are odd per the docs, oh well.

merlin