Skip to content

Commit 9520f8d

Browse files
committed
psql: Update \timing also in case of an error
The changes to show all query results (7844c99) broke \timing output in case of an error; it didn't update the timing result and showed 0.000 ms. Fix by updating the timing result also in the error case. Also, for robustness, update the timing result any time a result is obtained, not only for the last, so a sensible value is always available. Reported-by: Tom Lane <tgl@sss.pgh.pa.us> Author: Richard Guo <guofenglinux@gmail.com> Author: Fabien COELHO <coelho@cri.ensmp.fr> Discussion: https://www.postgresql.org/message-id/3813350.1652111765%40sss.pgh.pa.us
1 parent 6e647ef commit 9520f8d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/bin/psql/common.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,16 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
15661566
else
15671567
result = PQgetResult(pset.db);
15681568

1569+
/*
1570+
* Get current timing measure in case an error occurs
1571+
*/
1572+
if (timing)
1573+
{
1574+
INSTR_TIME_SET_CURRENT(after);
1575+
INSTR_TIME_SUBTRACT(after, before);
1576+
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
1577+
}
1578+
15691579
continue;
15701580
}
15711581
else if (svpt_gone_p && !*svpt_gone_p)
@@ -1619,7 +1629,7 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
16191629
last = (next_result == NULL);
16201630

16211631
/*
1622-
* Get timing measure before printing the last result.
1632+
* Update current timing measure.
16231633
*
16241634
* It will include the display of previous results, if any. This
16251635
* cannot be helped because the server goes on processing further
@@ -1630,7 +1640,7 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
16301640
* With combined queries, timing must be understood as an upper bound
16311641
* of the time spent processing them.
16321642
*/
1633-
if (last && timing)
1643+
if (timing)
16341644
{
16351645
INSTR_TIME_SET_CURRENT(after);
16361646
INSTR_TIME_SUBTRACT(after, before);

src/bin/psql/t/001_basic.pl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,16 @@ sub psql_fails_like
8585
'\timing on
8686
SELECT 1',
8787
qr/^1$
88-
^Time: \d+.\d\d\d ms/m,
89-
'\timing');
88+
^Time: \d+\.\d\d\d ms/m,
89+
'\timing with successful query');
90+
91+
# test \timing with query that fails
92+
{
93+
my ($ret, $stdout, $stderr) = $node->psql('postgres', "\\timing on\nSELECT error");
94+
isnt($ret, 0, '\timing with query error: query failed');
95+
like($stdout, qr/^Time: \d+\.\d\d\d ms/m, '\timing with query error: timing output appears');
96+
unlike($stdout, qr/^Time: 0\.000 ms/m, '\timing with query error: timing was updated');
97+
}
9098

9199
# test that ENCODING variable is set and that it is updated when
92100
# client encoding is changed

0 commit comments

Comments
 (0)