1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Add a \gexec command to psql for evaluation of computed queries.

\gexec executes the just-entered query, like \g, but instead of printing
the results it takes each field as a SQL command to send to the server.
Computing a series of queries to be executed is a fairly common thing,
but up to now you always had to resort to kluges like writing the queries
to a file and then inputting the file.  Now it can be done with no
intermediate step.

The implementation is fairly straightforward except for its interaction
with FETCH_COUNT.  ExecQueryUsingCursor isn't capable of being called
recursively, and even if it were, its need to create a transaction
block interferes unpleasantly with the desired behavior of \gexec after
a failure of a generated query (i.e., that it can continue).  Therefore,
disable use of ExecQueryUsingCursor when doing the master \gexec query.
We can still apply it to individual generated queries, however, and there
might be some value in doing so.

While testing this feature's interaction with single-step mode, I (tgl) was
led to conclude that SendQuery needs to recognize SIGINT (cancel_pressed)
as a negative response to the single-step prompt.  Perhaps that's a
back-patchable bug fix, but for now I just included it here.

Corey Huinker, reviewed by Jim Nasby, Daniel Vérité, and myself
This commit is contained in:
Tom Lane
2016-04-04 15:25:16 -04:00
parent 66229ac004
commit 2bbe9112ae
8 changed files with 207 additions and 6 deletions

View File

@ -1766,6 +1766,49 @@ Tue Oct 26 21:40:57 CEST 1999
</listitem>
</varlistentry>
<varlistentry>
<term><literal>\gexec</literal></term>
<listitem>
<para>
Sends the current query input buffer to the server, then treats
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
column of <structname>my_table</>:
<programlisting>
=&gt; <userinput>SELECT format('create index on my_table(%I)', attname)</>
-&gt; <userinput>FROM pg_attribute</>
-&gt; <userinput>WHERE attrelid = 'my_table'::regclass AND attnum &gt; 0</>
-&gt; <userinput>ORDER BY attnum</>
-&gt; <userinput>\gexec</>
CREATE INDEX
CREATE INDEX
CREATE INDEX
CREATE INDEX
</programlisting>
</para>
<para>
The generated queries are executed in the order in which the rows
are returned, and left-to-right within each row if there is more
than one column. NULL fields are ignored. The generated queries
are sent literally to the server for processing, so they cannot be
<application>psql</> meta-commands nor contain <application>psql</>
variable references. If any individual query fails, execution of
the remaining queries continues
unless <varname>ON_ERROR_STOP</varname> is set. Execution of each
query is subject to <varname>ECHO</varname> processing.
(Setting <varname>ECHO</varname> to <literal>all</literal>
or <literal>queries</literal> is often advisable when
using <command>\gexec</>.) Query logging, single-step mode,
timing, and other query execution features apply to each generated
query as well.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>\gset [ <replaceable class="parameter">prefix</replaceable> ]</literal></term>