From 2bbe9112aec60abc2d3b4c39e75d0cbdcaaa45e1 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Mon, 4 Apr 2016 15:25:16 -0400 Subject: Add a \gexec command to psql for evaluation of computed queries. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit \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 --- doc/src/sgml/ref/psql-ref.sgml | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'doc/src') diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index e8afc247afe..d8b9a03ee0e 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1766,6 +1766,49 @@ Tue Oct 26 21:40:57 CEST 1999 + + + \gexec + + + + 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 my_table: + +=> SELECT format('create index on my_table(%I)', attname) +-> FROM pg_attribute +-> WHERE attrelid = 'my_table'::regclass AND attnum > 0 +-> ORDER BY attnum +-> \gexec +CREATE INDEX +CREATE INDEX +CREATE INDEX +CREATE INDEX + + + + + 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 + psql meta-commands nor contain psql + variable references. If any individual query fails, execution of + the remaining queries continues + unless ON_ERROR_STOP is set. Execution of each + query is subject to ECHO processing. + (Setting ECHO to all + or queries is often advisable when + using \gexec.) Query logging, single-step mode, + timing, and other query execution features apply to each generated + query as well. + + + + + \gset [ prefix ] -- cgit v1.2.3