Skip to content

Commit e77de23

Browse files
committed
psql: Show notices immediately (again)
The new show-all-results feature in psql (7844c99) went out of its way to show notices next to the results of the statements (in a multi-statement string) that caused them. This also had the consequence that notices for a single statement were not shown until after the statement had executed, instead of right away. After some discussion, it seems very difficult to satisfy both of these goals, so here we are giving up on the first goal and just show the notices as we get them. This restores the pre-7844c9918 behavior for notices. Reported-by: Alastair McKinley <a.mckinley@analyticsengines.com> Author: Fabien COELHO <coelho@cri.ensmp.fr> Discussion: https://www.postgresql.org/message-id/flat/PAXPR02MB760039506C87A2083AD85575E3DA9%40PAXPR02MB7600.eurprd02.prod.outlook.com
1 parent 7ab5b4e commit e77de23

File tree

2 files changed

+4
-68
lines changed

2 files changed

+4
-68
lines changed

src/bin/psql/common.c

-64
Original file line numberDiff line numberDiff line change
@@ -1061,44 +1061,6 @@ PrintQueryResult(PGresult *result, bool last, bool is_watch, const printQueryOpt
10611061
return success;
10621062
}
10631063

1064-
/*
1065-
* Data structure and functions to record notices while they are
1066-
* emitted, so that they can be shown later.
1067-
*
1068-
* We need to know which result is last, which requires to extract
1069-
* one result in advance, hence two buffers are needed.
1070-
*/
1071-
struct t_notice_messages
1072-
{
1073-
PQExpBufferData messages[2];
1074-
int current;
1075-
};
1076-
1077-
/*
1078-
* Store notices in appropriate buffer, for later display.
1079-
*/
1080-
static void
1081-
AppendNoticeMessage(void *arg, const char *msg)
1082-
{
1083-
struct t_notice_messages *notices = arg;
1084-
1085-
appendPQExpBufferStr(&notices->messages[notices->current], msg);
1086-
}
1087-
1088-
/*
1089-
* Show notices stored in buffer, which is then reset.
1090-
*/
1091-
static void
1092-
ShowNoticeMessage(struct t_notice_messages *notices)
1093-
{
1094-
PQExpBufferData *current = &notices->messages[notices->current];
1095-
1096-
if (*current->data != '\0')
1097-
pg_log_info("%s", current->data);
1098-
resetPQExpBuffer(current);
1099-
}
1100-
1101-
11021064
/*
11031065
* SendQuery: send the query string to the backend
11041066
* (and print out result)
@@ -1483,7 +1445,6 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
14831445
instr_time before,
14841446
after;
14851447
PGresult *result;
1486-
struct t_notice_messages notices;
14871448

14881449
if (timing)
14891450
INSTR_TIME_SET_CURRENT(before);
@@ -1513,12 +1474,6 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
15131474
return 0;
15141475
}
15151476

1516-
/* intercept notices */
1517-
notices.current = 0;
1518-
initPQExpBuffer(&notices.messages[0]);
1519-
initPQExpBuffer(&notices.messages[1]);
1520-
PQsetNoticeProcessor(pset.db, AppendNoticeMessage, &notices);
1521-
15221477
/* first result */
15231478
result = PQgetResult(pset.db);
15241479

@@ -1536,7 +1491,6 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
15361491
*/
15371492
const char *error = PQresultErrorMessage(result);
15381493

1539-
ShowNoticeMessage(&notices);
15401494
if (strlen(error))
15411495
pg_log_info("%s", error);
15421496

@@ -1601,31 +1555,22 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
16011555
if (result_status == PGRES_COPY_IN ||
16021556
result_status == PGRES_COPY_OUT)
16031557
{
1604-
ShowNoticeMessage(&notices);
1605-
16061558
if (is_watch)
16071559
{
16081560
ClearOrSaveAllResults();
16091561
pg_log_error("\\watch cannot be used with COPY");
16101562
return -1;
16111563
}
16121564

1613-
/* use normal notice processor during COPY */
1614-
PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
1615-
16161565
success &= HandleCopyResult(&result);
1617-
1618-
PQsetNoticeProcessor(pset.db, AppendNoticeMessage, &notices);
16191566
}
16201567

16211568
/*
16221569
* Check PQgetResult() again. In the typical case of a single-command
16231570
* string, it will return NULL. Otherwise, we'll have other results
16241571
* to process. We need to do that to check whether this is the last.
16251572
*/
1626-
notices.current ^= 1;
16271573
next_result = PQgetResult(pset.db);
1628-
notices.current ^= 1;
16291574
last = (next_result == NULL);
16301575

16311576
/*
@@ -1647,9 +1592,6 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
16471592
*elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
16481593
}
16491594

1650-
/* notices already shown above for copy */
1651-
ShowNoticeMessage(&notices);
1652-
16531595
/* this may or may not print something depending on settings */
16541596
if (result != NULL)
16551597
success &= PrintQueryResult(result, last, false, opt, printQueryFout);
@@ -1659,7 +1601,6 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
16591601
SetResultVariables(result, true);
16601602

16611603
ClearOrSaveResult(result);
1662-
notices.current ^= 1;
16631604
result = next_result;
16641605

16651606
if (cancel_pressed)
@@ -1669,11 +1610,6 @@ ExecQueryAndProcessResults(const char *query, double *elapsed_msec, bool *svpt_g
16691610
}
16701611
}
16711612

1672-
/* reset notice hook */
1673-
PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
1674-
termPQExpBuffer(&notices.messages[0]);
1675-
termPQExpBuffer(&notices.messages[1]);
1676-
16771613
/* may need this to recover from conn loss during COPY */
16781614
if (!CheckConnection())
16791615
return -1;

src/test/regress/expected/psql.out

+4-4
Original file line numberDiff line numberDiff line change
@@ -5316,13 +5316,13 @@ AS $$
53165316
$$;
53175317
-- show both
53185318
SELECT 1 AS one \; SELECT warn('1.5') \; SELECT 2 AS two ;
5319+
NOTICE: warn 1.5
5320+
CONTEXT: PL/pgSQL function warn(text) line 2 at RAISE
53195321
one
53205322
-----
53215323
1
53225324
(1 row)
53235325

5324-
NOTICE: warn 1.5
5325-
CONTEXT: PL/pgSQL function warn(text) line 2 at RAISE
53265326
warn
53275327
------
53285328
t
@@ -5335,13 +5335,13 @@ CONTEXT: PL/pgSQL function warn(text) line 2 at RAISE
53355335

53365336
-- \gset applies to last query only
53375337
SELECT 3 AS three \; SELECT warn('3.5') \; SELECT 4 AS four \gset
5338+
NOTICE: warn 3.5
5339+
CONTEXT: PL/pgSQL function warn(text) line 2 at RAISE
53385340
three
53395341
-------
53405342
3
53415343
(1 row)
53425344

5343-
NOTICE: warn 3.5
5344-
CONTEXT: PL/pgSQL function warn(text) line 2 at RAISE
53455345
warn
53465346
------
53475347
t

0 commit comments

Comments
 (0)