Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 68dba97

Browse files
committed
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 77a4729), 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
1 parent 5dbc5da commit 68dba97

File tree

1 file changed

+52
-21
lines changed

1 file changed

+52
-21
lines changed

doc/src/sgml/ref/psql-ref.sgml

+52-21
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,14 @@ testdb=>
823823
continue beyond the end of the line.
824824
</para>
825825

826+
<para>
827+
Many of the meta-commands act on the <firstterm>current query buffer</>.
828+
This is simply a buffer holding whatever SQL command text has been typed
829+
but not yet sent to the server for execution. This will include previous
830+
input lines as well as any text appearing before the meta-command on the
831+
same line.
832+
</para>
833+
826834
<para>
827835
The following meta-commands are defined:
828836

@@ -1713,22 +1721,28 @@ testdb=&gt;
17131721
<listitem>
17141722
<para>
17151723
If <replaceable class="parameter">filename</replaceable> is
1716-
specified, the file is edited; after the editor exits, its
1717-
content is copied back to the query buffer. If no <replaceable
1724+
specified, the file is edited; after the editor exits, the file's
1725+
content is copied into the current query buffer. If no <replaceable
17181726
class="parameter">filename</replaceable> is given, the current query
17191727
buffer is copied to a temporary file which is then edited in the same
1728+
fashion. Or, if the current query buffer is empty, the most recently
1729+
executed query is copied to a temporary file and edited in the same
17201730
fashion.
17211731
</para>
17221732

17231733
<para>
1724-
The new query buffer is then re-parsed according to the normal
1725-
rules of <application>psql</application>, where the whole buffer
1726-
is treated as a single line. (Thus you cannot make scripts this
1727-
way. Use <command>\i</command> for that.) This means that
1728-
if the query ends with (or contains) a semicolon, it is
1729-
immediately executed. Otherwise it will merely wait in the
1730-
query buffer; type semicolon or <literal>\g</> to send it, or
1731-
<literal>\r</> to cancel.
1734+
The new contents of the query buffer are then re-parsed according to
1735+
the normal rules of <application>psql</application>, treating the
1736+
whole buffer as a single line. Any complete queries are immediately
1737+
executed; that is, if the query buffer contains or ends with a
1738+
semicolon, everything up to that point is executed. Whatever remains
1739+
will wait in the query buffer; type semicolon or <literal>\g</> to
1740+
send it, or <literal>\r</> to cancel it by clearing the query buffer.
1741+
Treating the buffer as a single line primarily affects meta-commands:
1742+
whatever is in the buffer after a meta-command will be taken as
1743+
argument(s) to the meta-command, even if it spans multiple lines.
1744+
(Thus you cannot make meta-command-using scripts this way.
1745+
Use <command>\i</command> for that.)
17321746
</para>
17331747

17341748
<para>
@@ -1888,16 +1902,17 @@ Tue Oct 26 21:40:57 CEST 1999
18881902
<term><literal>\g [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
18891903
<listitem>
18901904
<para>
1891-
Sends the current query input buffer to the server, and
1892-
optionally stores the query's output in <replaceable
1893-
class="parameter">filename</replaceable> or pipes the output
1894-
to the shell command <replaceable
1895-
class="parameter">command</replaceable>. The file or command is
1896-
written to only if the query successfully returns zero or more tuples,
1897-
not if the query fails or is a non-data-returning SQL command.
1905+
Sends the current query buffer to the server for execution.
1906+
If an argument is given, the query's output is written to the named
1907+
file or piped to the given shell command, instead of displaying it as
1908+
usual. The file or command is written to only if the query
1909+
successfully returns zero or more tuples, not if the query fails or
1910+
is a non-data-returning SQL command.
18981911
</para>
18991912
<para>
1900-
A bare <literal>\g</literal> is essentially equivalent to a semicolon.
1913+
If the current query buffer is empty, the most recently sent query is
1914+
re-executed instead. Except for that behavior, <literal>\g</literal>
1915+
without an argument is essentially equivalent to a semicolon.
19011916
A <literal>\g</literal> with argument is a <quote>one-shot</quote>
19021917
alternative to the <command>\o</command> command.
19031918
</para>
@@ -1922,7 +1937,7 @@ Tue Oct 26 21:40:57 CEST 1999
19221937

19231938
<listitem>
19241939
<para>
1925-
Sends the current query input buffer to the server, then treats
1940+
Sends the current query buffer to the server, then treats
19261941
each column of each row of the query's output (if any) as a SQL
19271942
statement to be executed. For example, to create an index on each
19281943
column of <structname>my_table</>:
@@ -1955,6 +1970,10 @@ CREATE INDEX
19551970
timing, and other query execution features apply to each generated
19561971
query as well.
19571972
</para>
1973+
<para>
1974+
If the current query buffer is empty, the most recently sent query
1975+
is re-executed instead.
1976+
</para>
19581977
</listitem>
19591978
</varlistentry>
19601979

@@ -1964,7 +1983,7 @@ CREATE INDEX
19641983

19651984
<listitem>
19661985
<para>
1967-
Sends the current query input buffer to the server and stores the
1986+
Sends the current query buffer to the server and stores the
19681987
query's output into <application>psql</> variables (see <xref
19691988
linkend="APP-PSQL-variables" endterm="APP-PSQL-variables-title">).
19701989
The query to be executed must return exactly one row. Each column of
@@ -1996,6 +2015,10 @@ hello 10
19962015
If the query fails or does not return one row,
19972016
no variables are changed.
19982017
</para>
2018+
<para>
2019+
If the current query buffer is empty, the most recently sent query
2020+
is re-executed instead.
2021+
</para>
19992022
</listitem>
20002023
</varlistentry>
20012024

@@ -2302,6 +2325,8 @@ lo_import 152801
23022325
<listitem>
23032326
<para>
23042327
Print the current query buffer to the standard output.
2328+
If the current query buffer is empty, the most recently executed query
2329+
is printed instead.
23052330
</para>
23062331
</listitem>
23072332
</varlistentry>
@@ -2970,9 +2995,11 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
29702995
<term><literal>\w</literal> or <literal>\write</literal> <literal>|</><replaceable class="parameter">command</replaceable></term>
29712996
<listitem>
29722997
<para>
2973-
Outputs the current query buffer to the file <replaceable
2998+
Writes the current query buffer to the file <replaceable
29742999
class="parameter">filename</replaceable> or pipes it to the shell
29753000
command <replaceable class="parameter">command</replaceable>.
3001+
If the current query buffer is empty, the most recently executed query
3002+
is written instead.
29763003
</para>
29773004
</listitem>
29783005
</varlistentry>
@@ -2988,6 +3015,10 @@ testdb=&gt; <userinput>\setenv LESS -imx4F</userinput>
29883015
displayed with a header that includes the <literal>\pset title</>
29893016
string (if any), the time as of query start, and the delay interval.
29903017
</para>
3018+
<para>
3019+
If the current query buffer is empty, the most recently sent query
3020+
is re-executed instead.
3021+
</para>
29913022
</listitem>
29923023
</varlistentry>
29933024

0 commit comments

Comments
 (0)