Commit 68dba97a authored by Tom Lane's avatar Tom Lane

Document psql's behavior of recalling the previously executed query.

Various psql slash commands that normally act on the current query buffer
will automatically recall and re-use the most recently executed SQL command
instead, if the current query buffer is empty.  Although this behavior is
ancient (dating apparently to commit 77a47299), it was documented nowhere
in the psql reference page.  For that matter, we'd never bothered to define
the concept of "current query buffer" explicitly.  Fix that.  Do some
wordsmithing on relevant command descriptions to improve clarity and
consistency.

Discussion: https://postgr.es/m/9b4ea968-753f-4b5f-b46c-d7d3bf7c8f90@manitou-mail.org
parent 5dbc5da1
...@@ -823,6 +823,14 @@ testdb=> ...@@ -823,6 +823,14 @@ testdb=>
continue beyond the end of the line. continue beyond the end of the line.
</para> </para>
<para>
Many of the meta-commands act on the <firstterm>current query buffer</>.
This is simply a buffer holding whatever SQL command text has been typed
but not yet sent to the server for execution. This will include previous
input lines as well as any text appearing before the meta-command on the
same line.
</para>
<para> <para>
The following meta-commands are defined: The following meta-commands are defined:
...@@ -1713,22 +1721,28 @@ testdb=&gt; ...@@ -1713,22 +1721,28 @@ testdb=&gt;
<listitem> <listitem>
<para> <para>
If <replaceable class="parameter">filename</replaceable> is If <replaceable class="parameter">filename</replaceable> is
specified, the file is edited; after the editor exits, its specified, the file is edited; after the editor exits, the file's
content is copied back to the query buffer. If no <replaceable content is copied into the current query buffer. If no <replaceable
class="parameter">filename</replaceable> is given, the current query class="parameter">filename</replaceable> is given, the current query
buffer is copied to a temporary file which is then edited in the same 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. fashion.
</para> </para>
<para> <para>
The new query buffer is then re-parsed according to the normal The new contents of the query buffer are then re-parsed according to
rules of <application>psql</application>, where the whole buffer the normal rules of <application>psql</application>, treating the
is treated as a single line. (Thus you cannot make scripts this whole buffer as a single line. Any complete queries are immediately
way. Use <command>\i</command> for that.) This means that executed; that is, if the query buffer contains or ends with a
if the query ends with (or contains) a semicolon, it is semicolon, everything up to that point is executed. Whatever remains
immediately executed. Otherwise it will merely wait in the will wait in the query buffer; type semicolon or <literal>\g</> to
query buffer; type semicolon or <literal>\g</> to send it, or send it, or <literal>\r</> to cancel it by clearing the query buffer.
<literal>\r</> to cancel. Treating the buffer as a single line primarily affects meta-commands:
whatever is in the buffer after a meta-command will be taken as
argument(s) to the meta-command, even if it spans multiple lines.
(Thus you cannot make meta-command-using scripts this way.
Use <command>\i</command> for that.)
</para> </para>
<para> <para>
...@@ -1888,16 +1902,17 @@ Tue Oct 26 21:40:57 CEST 1999 ...@@ -1888,16 +1902,17 @@ Tue Oct 26 21:40:57 CEST 1999
<term><literal>\g [ |<replaceable class="parameter">command</replaceable> ]</literal></term> <term><literal>\g [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
<listitem> <listitem>
<para> <para>
Sends the current query input buffer to the server, and Sends the current query buffer to the server for execution.
optionally stores the query's output in <replaceable If an argument is given, the query's output is written to the named
class="parameter">filename</replaceable> or pipes the output file or piped to the given shell command, instead of displaying it as
to the shell command <replaceable usual. The file or command is written to only if the query
class="parameter">command</replaceable>. The file or command is successfully returns zero or more tuples, not if the query fails or
written to only if the query successfully returns zero or more tuples, is a non-data-returning SQL command.
not if the query fails or is a non-data-returning SQL command.
</para> </para>
<para> <para>
A bare <literal>\g</literal> is essentially equivalent to a semicolon. If the current query buffer is empty, the most recently sent query is
re-executed instead. Except for that behavior, <literal>\g</literal>
without an argument is essentially equivalent to a semicolon.
A <literal>\g</literal> with argument is a <quote>one-shot</quote> A <literal>\g</literal> with argument is a <quote>one-shot</quote>
alternative to the <command>\o</command> command. alternative to the <command>\o</command> command.
</para> </para>
...@@ -1922,7 +1937,7 @@ Tue Oct 26 21:40:57 CEST 1999 ...@@ -1922,7 +1937,7 @@ Tue Oct 26 21:40:57 CEST 1999
<listitem> <listitem>
<para> <para>
Sends the current query input buffer to the server, then treats Sends the current query buffer to the server, then treats
each column of each row of the query's output (if any) as a SQL each column of each row of the query's output (if any) as a SQL
statement to be executed. For example, to create an index on each statement to be executed. For example, to create an index on each
column of <structname>my_table</>: column of <structname>my_table</>:
...@@ -1955,6 +1970,10 @@ CREATE INDEX ...@@ -1955,6 +1970,10 @@ CREATE INDEX
timing, and other query execution features apply to each generated timing, and other query execution features apply to each generated
query as well. query as well.
</para> </para>
<para>
If the current query buffer is empty, the most recently sent query
is re-executed instead.
</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -1964,7 +1983,7 @@ CREATE INDEX ...@@ -1964,7 +1983,7 @@ CREATE INDEX
<listitem> <listitem>
<para> <para>
Sends the current query input buffer to the server and stores the Sends the current query buffer to the server and stores the
query's output into <application>psql</> variables (see <xref query's output into <application>psql</> variables (see <xref
linkend="APP-PSQL-variables" endterm="APP-PSQL-variables-title">). linkend="APP-PSQL-variables" endterm="APP-PSQL-variables-title">).
The query to be executed must return exactly one row. Each column of The query to be executed must return exactly one row. Each column of
...@@ -1996,6 +2015,10 @@ hello 10 ...@@ -1996,6 +2015,10 @@ hello 10
If the query fails or does not return one row, If the query fails or does not return one row,
no variables are changed. no variables are changed.
</para> </para>
<para>
If the current query buffer is empty, the most recently sent query
is re-executed instead.
</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -2302,6 +2325,8 @@ lo_import 152801 ...@@ -2302,6 +2325,8 @@ lo_import 152801
<listitem> <listitem>
<para> <para>
Print the current query buffer to the standard output. Print the current query buffer to the standard output.
If the current query buffer is empty, the most recently executed query
is printed instead.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -2970,9 +2995,11 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput> ...@@ -2970,9 +2995,11 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
<term><literal>\w</literal> or <literal>\write</literal> <literal>|</><replaceable class="parameter">command</replaceable></term> <term><literal>\w</literal> or <literal>\write</literal> <literal>|</><replaceable class="parameter">command</replaceable></term>
<listitem> <listitem>
<para> <para>
Outputs the current query buffer to the file <replaceable Writes the current query buffer to the file <replaceable
class="parameter">filename</replaceable> or pipes it to the shell class="parameter">filename</replaceable> or pipes it to the shell
command <replaceable class="parameter">command</replaceable>. command <replaceable class="parameter">command</replaceable>.
If the current query buffer is empty, the most recently executed query
is written instead.
</para> </para>
</listitem> </listitem>
</varlistentry> </varlistentry>
...@@ -2988,6 +3015,10 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput> ...@@ -2988,6 +3015,10 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
displayed with a header that includes the <literal>\pset title</> displayed with a header that includes the <literal>\pset title</>
string (if any), the time as of query start, and the delay interval. string (if any), the time as of query start, and the delay interval.
</para> </para>
<para>
If the current query buffer is empty, the most recently sent query
is re-executed instead.
</para>
</listitem> </listitem>
</varlistentry> </varlistentry>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment