diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2020-04-07 17:46:29 -0400 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2020-04-07 17:46:29 -0400 |
commit | b63c293bcbd7439f883cd4cf748f6755df0fbb3c (patch) | |
tree | 0852b2e4c0850d0bdd51507f931475e834feadb5 /src/bin/psql/common.c | |
parent | 0f5ca02f53ac2b211d8518f0882c49284c0c9610 (diff) |
Allow psql's \g and \gx commands to transiently change \pset options.
We invented \gx to allow the "\pset expanded" flag to be forced on
for the duration of one command output, but that turns out to not
be nearly enough to satisfy the demand for variant output formats.
Hence, make it possible to change any pset option(s) for the duration
of a single command output, by writing "option=value ..." inside
parentheses, for example
\g (format=csv csv_fieldsep='\t') somefile
\gx can now be understood as a shorthand for including expanded=on
inside the parentheses.
Patch by me, expanding on a proposal by Pavel Stehule
Discussion: https://postgr.es/m/CAFj8pRBx9OnBPRJVtfA5ycUpySge-XootAXAsv_4rrkHxJ8eRg@mail.gmail.com
Diffstat (limited to 'src/bin/psql/common.c')
-rw-r--r-- | src/bin/psql/common.c | 21 |
1 files changed, 8 insertions, 13 deletions
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index 396a40089ce..621a33f7e83 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -707,13 +707,8 @@ PrintNotifications(void) static bool PrintQueryTuples(const PGresult *results) { - printQueryOpt my_popt = pset.popt; bool result = true; - /* one-shot expanded output requested via \gx */ - if (pset.g_expanded) - my_popt.topt.expanded = 1; - /* write output to \g argument, if any */ if (pset.gfname) { @@ -725,7 +720,7 @@ PrintQueryTuples(const PGresult *results) if (is_pipe) disable_sigpipe_trap(); - printQuery(results, &my_popt, fout, false, pset.logfile); + printQuery(results, &pset.popt, fout, false, pset.logfile); if (ferror(fout)) { pg_log_error("could not print result table: %m"); @@ -742,7 +737,7 @@ PrintQueryTuples(const PGresult *results) } else { - printQuery(results, &my_popt, pset.queryFout, false, pset.logfile); + printQuery(results, &pset.popt, pset.queryFout, false, pset.logfile); if (ferror(pset.queryFout)) { pg_log_error("could not print result table: %m"); @@ -1418,8 +1413,12 @@ sendquery_cleanup: pset.gfname = NULL; } - /* reset \gx's expanded-mode flag */ - pset.g_expanded = false; + /* restore print settings if \g changed them */ + if (pset.gsavepopt) + { + restorePsetInfo(&pset.popt, pset.gsavepopt); + pset.gsavepopt = NULL; + } /* reset \gset trigger */ if (pset.gset_prefix) @@ -1646,10 +1645,6 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) "FETCH FORWARD %d FROM _psql_cursor", fetch_count); - /* one-shot expanded output requested via \gx */ - if (pset.g_expanded) - my_popt.topt.expanded = 1; - /* prepare to write output to \g argument, if any */ if (pset.gfname) { |