Skip to content

Commit b2678ef

Browse files
committed
psql: Add \gx command
It can often be useful to use expanded mode output (\x) for just a single query. Introduce a \gx which acts exactly like \g except that it will force expanded output mode for that one \gx call. This is simpler than having to use \x as a toggle and also means that the user doesn't have to worry about the current state of the expanded variable, or resetting it later, to ensure a given query is always returned in expanded mode. Primairly Christoph's patch, though I did tweak the documentation and help text a bit, and re-indented the tab completion section. Author: Christoph Berg Reviewed By: Daniel Verite Discussion: https://postgr.es/m/20170127132737.6skslelaf4txs6iw%40msg.credativ.de
1 parent 9a83d56 commit b2678ef

File tree

8 files changed

+64
-7
lines changed

8 files changed

+64
-7
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,6 +1890,18 @@ Tue Oct 26 21:40:57 CEST 1999
18901890
</varlistentry>
18911891

18921892

1893+
<varlistentry>
1894+
<term><literal>\gx [ <replaceable class="parameter">filename</replaceable> ]</literal></term>
1895+
<term><literal>\gx [ |<replaceable class="parameter">command</replaceable> ]</literal></term>
1896+
<listitem>
1897+
<para>
1898+
<literal>\gx</literal> is equivalent to <literal>\g</literal>, but
1899+
forces expanded output mode for this query. See <literal>\x</literal>.
1900+
</para>
1901+
</listitem>
1902+
</varlistentry>
1903+
1904+
18931905
<varlistentry>
18941906
<term><literal>\gexec</literal></term>
18951907

src/bin/psql/command.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,11 @@ exec_command(const char *cmd,
906906
free(fname);
907907
}
908908

909-
/* \g [filename] -- send query, optionally with output to file/pipe */
910-
else if (strcmp(cmd, "g") == 0)
909+
/*
910+
* \g [filename] -- send query, optionally with output to file/pipe
911+
* \gx [filename] -- same as \g, with expanded mode forced
912+
*/
913+
else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
911914
{
912915
char *fname = psql_scan_slash_option(scan_state,
913916
OT_FILEPIPE, NULL, false);
@@ -920,6 +923,8 @@ exec_command(const char *cmd,
920923
pset.gfname = pg_strdup(fname);
921924
}
922925
free(fname);
926+
if (strcmp(cmd, "gx") == 0)
927+
pset.g_expanded = true;
923928
status = PSQL_CMD_SEND;
924929
}
925930

src/bin/psql/common.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,10 @@ PrintQueryTuples(const PGresult *results)
770770
{
771771
printQueryOpt my_popt = pset.popt;
772772

773+
/* one-shot expanded output requested via \gx */
774+
if (pset.g_expanded)
775+
my_popt.topt.expanded = 1;
776+
773777
/* write output to \g argument, if any */
774778
if (pset.gfname)
775779
{
@@ -1410,6 +1414,9 @@ SendQuery(const char *query)
14101414
pset.gfname = NULL;
14111415
}
14121416

1417+
/* reset \gx's expanded-mode flag */
1418+
pset.g_expanded = false;
1419+
14131420
/* reset \gset trigger */
14141421
if (pset.gset_prefix)
14151422
{

src/bin/psql/help.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ slashUsage(unsigned short int pager)
173173
fprintf(output, _(" \\copyright show PostgreSQL usage and distribution terms\n"));
174174
fprintf(output, _(" \\errverbose show most recent error message at maximum verbosity\n"));
175175
fprintf(output, _(" \\g [FILE] or ; execute query (and send results to file or |pipe)\n"));
176+
fprintf(output, _(" \\gx [FILE] as \\g, but forces expanded output mode\n"));
176177
fprintf(output, _(" \\gexec execute query, then execute each value in its result\n"));
177178
fprintf(output, _(" \\gset [PREFIX] execute query and store results in psql variables\n"));
178179
fprintf(output, _(" \\q quit psql\n"));

src/bin/psql/settings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ typedef struct _psqlSettings
9191
printQueryOpt popt;
9292

9393
char *gfname; /* one-shot file output argument for \g */
94+
bool g_expanded; /* one-shot expanded output requested via \gx */
9495
char *gset_prefix; /* one-shot prefix argument for \gset */
9596
bool gexec_flag; /* one-shot flag to execute query's results */
9697
bool crosstab_flag; /* one-shot request to crosstab results */

src/bin/psql/tab-complete.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,11 +1375,12 @@ psql_completion(const char *text, int start, int end)
13751375
"\\dm", "\\dn", "\\do", "\\dO", "\\dp", "\\drds", "\\ds", "\\dS",
13761376
"\\dt", "\\dT", "\\dv", "\\du", "\\dx", "\\dy",
13771377
"\\e", "\\echo", "\\ef", "\\encoding", "\\errverbose", "\\ev",
1378-
"\\f", "\\g", "\\gexec", "\\gset", "\\h", "\\help", "\\H", "\\i", "\\ir", "\\l",
1379-
"\\lo_import", "\\lo_export", "\\lo_list", "\\lo_unlink",
1380-
"\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q", "\\qecho", "\\r",
1381-
"\\s", "\\set", "\\setenv", "\\sf", "\\sv", "\\t", "\\T",
1382-
"\\timing", "\\unset", "\\x", "\\w", "\\watch", "\\z", "\\!", NULL
1378+
"\\f", "\\g", "\\gexec", "\\gset", "\\gx", "\\h", "\\help", "\\H",
1379+
"\\i", "\\ir", "\\l", "\\lo_import", "\\lo_export", "\\lo_list",
1380+
"\\lo_unlink", "\\o", "\\p", "\\password", "\\prompt", "\\pset", "\\q",
1381+
"\\qecho", "\\r", "\\s", "\\set", "\\setenv", "\\sf", "\\sv", "\\t",
1382+
"\\T", "\\timing", "\\unset", "\\x", "\\w", "\\watch", "\\z", "\\!",
1383+
NULL
13831384
};
13841385

13851386
(void) end; /* "end" is not used */

src/test/regress/expected/psql.out

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,29 @@ on
2828
\unset ON_ERROR_ROLLBACK
2929
\echo :ON_ERROR_ROLLBACK
3030
off
31+
-- \g and \gx
32+
SELECT 1 as one, 2 as two \g
33+
one | two
34+
-----+-----
35+
1 | 2
36+
(1 row)
37+
38+
\gx
39+
-[ RECORD 1 ]
40+
one | 1
41+
two | 2
42+
43+
SELECT 3 as three, 4 as four \gx
44+
-[ RECORD 1 ]
45+
three | 3
46+
four | 4
47+
48+
\g
49+
three | four
50+
-------+------
51+
3 | 4
52+
(1 row)
53+
3154
-- \gset
3255
select 10 as test01, 20 as test02, 'Hello' as test03 \gset pref01_
3356
\echo :pref01_test01 :pref01_test02 :pref01_test03

src/test/regress/sql/psql.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
\unset ON_ERROR_ROLLBACK
2222
\echo :ON_ERROR_ROLLBACK
2323

24+
-- \g and \gx
25+
26+
SELECT 1 as one, 2 as two \g
27+
\gx
28+
SELECT 3 as three, 4 as four \gx
29+
\g
30+
2431
-- \gset
2532

2633
select 10 as test01, 20 as test02, 'Hello' as test03 \gset pref01_

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy