Skip to content

Commit 5b214c5

Browse files
committed
Add new ECHO mode 'errors' that displays only failed commands in psql.
When the psql variable ECHO is set to 'erros', only failed SQL commands are printed to standard error output. Also this patch adds -b option into psql. This is equivalent to setting the variable ECHO to 'errors'. Pavel Stehule, reviewed by Fabrízio de Royes Mello, Samrat Revagade, Kumar Rajeev Rastogi, Abhijit Menon-Sen, and me.
1 parent b043985 commit 5b214c5

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,18 @@ PostgreSQL documentation
7272
</listitem>
7373
</varlistentry>
7474

75+
<varlistentry>
76+
<term><option>-b</></term>
77+
<term><option>--echo-errors</></term>
78+
<listitem>
79+
<para>
80+
Print failed SQL commands to standard error output. This is
81+
equivalent to setting the variable <varname>ECHO</varname> to
82+
<literal>errors</literal>.
83+
</para>
84+
</listitem>
85+
</varlistentry>
86+
7587
<varlistentry>
7688
<term><option>-c <replaceable class="parameter">command</replaceable></></term>
7789
<term><option>--command=<replaceable class="parameter">command</replaceable></></term>
@@ -2812,7 +2824,9 @@ bar
28122824
<literal>queries</literal>,
28132825
<application>psql</application> merely prints all queries as
28142826
they are sent to the server. The switch for this is
2815-
<option>-e</option>.
2827+
<option>-e</option>. If set to <literal>errors</literal> then only
2828+
failed queries are displayed on standard error output. The switch
2829+
for this is <option>-b</option>.
28162830
</para>
28172831
</listitem>
28182832
</varlistentry>

src/bin/psql/common.c

+3
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,9 @@ SendQuery(const char *query)
995995
results = NULL; /* PQclear(NULL) does nothing */
996996
}
997997

998+
if (!OK && pset.echo == PSQL_ECHO_ERRORS)
999+
psql_error("STATEMENT: %s\n", query);
1000+
9981001
/* If we made a temporary savepoint, possibly release/rollback */
9991002
if (on_error_rollback_savepoint)
10001003
{

src/bin/psql/help.c

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ usage(void)
8787

8888
printf(_("\nInput and output options:\n"));
8989
printf(_(" -a, --echo-all echo all input from script\n"));
90+
printf(_(" -b, --echo-errors echo failed commands\n"));
9091
printf(_(" -e, --echo-queries echo commands sent to server\n"));
9192
printf(_(" -E, --echo-hidden display queries that internal commands generate\n"));
9293
printf(_(" -L, --log-file=FILENAME send session log to file\n"));

src/bin/psql/settings.h

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef enum
3131
{
3232
PSQL_ECHO_NONE,
3333
PSQL_ECHO_QUERIES,
34+
PSQL_ECHO_ERRORS,
3435
PSQL_ECHO_ALL
3536
} PSQL_ECHO;
3637

src/bin/psql/startup.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
354354
{"command", required_argument, NULL, 'c'},
355355
{"dbname", required_argument, NULL, 'd'},
356356
{"echo-queries", no_argument, NULL, 'e'},
357+
{"echo-errors", no_argument, NULL, 'b'},
357358
{"echo-hidden", no_argument, NULL, 'E'},
358359
{"file", required_argument, NULL, 'f'},
359360
{"field-separator", required_argument, NULL, 'F'},
@@ -391,7 +392,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
391392

392393
memset(options, 0, sizeof *options);
393394

394-
while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
395+
while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
395396
long_options, &optindex)) != -1)
396397
{
397398
switch (c)
@@ -402,6 +403,9 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
402403
case 'A':
403404
pset.popt.topt.format = PRINT_UNALIGNED;
404405
break;
406+
case 'b':
407+
SetVariable(pset.vars, "ECHO", "errors");
408+
break;
405409
case 'c':
406410
options->action_string = pg_strdup(optarg);
407411
if (optarg[0] == '\\')
@@ -720,6 +724,8 @@ echo_hook(const char *newval)
720724
pset.echo = PSQL_ECHO_NONE;
721725
else if (strcmp(newval, "queries") == 0)
722726
pset.echo = PSQL_ECHO_QUERIES;
727+
else if (strcmp(newval, "errors") == 0)
728+
pset.echo = PSQL_ECHO_ERRORS;
723729
else if (strcmp(newval, "all") == 0)
724730
pset.echo = PSQL_ECHO_ALL;
725731
else

0 commit comments

Comments
 (0)