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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Add a \gexec command to psql for evaluation of computed queries.
Date: 2016-04-04 19:25:21
Message-ID: E1anA7l-00063G-Og@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

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

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/2bbe9112aec60abc2d3b4c39e75d0cbdcaaa45e1

Modified Files
--------------
doc/src/sgml/ref/psql-ref.sgml | 43 ++++++++++++++++++
src/bin/psql/command.c | 7 +++
src/bin/psql/common.c | 90 ++++++++++++++++++++++++++++++++++++--
src/bin/psql/help.c | 3 +-
src/bin/psql/settings.h | 1 +
src/bin/psql/tab-complete.c | 2 +-
src/test/regress/expected/psql.out | 45 +++++++++++++++++++
src/test/regress/sql/psql.sql | 22 ++++++++++
8 files changed, 207 insertions(+), 6 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Alvaro Herrera 2016-04-04 20:14:45 pgsql: Silence compiler warning
Previous Message Tom Lane 2016-04-04 16:32:47 pgsql: Introduce a LOG_SERVER_ONLY ereport level, which is never sent t