Hello Aya-san,
Thanks for this review.
There is space (+--' '). Please delete it. It is cause of regression test failed.
Indeed, unsure how I could do that. Fixed.
IMHO this new setting should be on by default: few people know about \; so it would not change anything for most, and I do not see why those who use it would not be interested by the results of all the queries they asked for.I agree with your opinion.
Ok. I did not yet change the default in the attached version, though.
I test some query combination case. And I found when warning happen, the message is printed in head of results. I think it is not clear in which query the warning occurred.
Indeed.
How about print warning message before the query that warning occurred?
Sure. It happened to be trickier than I thought to achieve this, because there is a callback hook to send notifications.
This attached version does: - ensure that warnings appear just before its - add the entry in psql's help - redefine the function boundary so that timing is cleaner - include somehow improved tests -- Fabien.
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index b86764003d..de2b56cf88 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -3918,6 +3918,17 @@ bar </listitem> </varlistentry> + <varlistentry> + <term><varname>SHOW_ALL_RESULTS</varname></term> + <listitem> + <para> + When this variable is set to <literal>on</literal>, all results of a combined + (<literal>\;</literal>) query are shown instead of just the last one. + Default is <literal>off</literal>. + </para> + </listitem> + </varlistentry> + <varlistentry> <term><varname>SHOW_CONTEXT</varname></term> <listitem> diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c index bd284446f8..19bfce9610 100644 --- a/src/bin/psql/common.c +++ b/src/bin/psql/common.c @@ -472,6 +472,16 @@ ResetCancelConn(void) #endif } +static void +ShowErrorMessage(const PGresult *result) +{ + const char *error = PQerrorMessage(pset.db); + + if (strlen(error)) + pg_log_info("%s", error); + + CheckConnection(); +} /* * AcceptResult @@ -482,7 +492,7 @@ ResetCancelConn(void) * Returns true for valid result, false for error state. */ static bool -AcceptResult(const PGresult *result) +AcceptResult(const PGresult *result, bool show_error) { bool OK; @@ -513,15 +523,8 @@ AcceptResult(const PGresult *result) break; } - if (!OK) - { - const char *error = PQerrorMessage(pset.db); - - if (strlen(error)) - pg_log_info("%s", error); - - CheckConnection(); - } + if (!OK && show_error) + ShowErrorMessage(result); return OK; } @@ -701,7 +704,7 @@ PSQLexec(const char *query) ResetCancelConn(); - if (!AcceptResult(res)) + if (!AcceptResult(res, true)) { ClearOrSaveResult(res); res = NULL; @@ -743,7 +746,7 @@ PSQLexecWatch(const char *query, const printQueryOpt *opt) ResetCancelConn(); - if (!AcceptResult(res)) + if (!AcceptResult(res, true)) { ClearOrSaveResult(res); return 0; @@ -999,199 +1002,113 @@ loop_exit: return success; } - /* - * ProcessResult: utility function for use by SendQuery() only - * - * When our command string contained a COPY FROM STDIN or COPY TO STDOUT, - * PQexec() has stopped at the PGresult associated with the first such - * command. In that event, we'll marshal data for the COPY and then cycle - * through any subsequent PGresult objects. - * - * When the command string contained no such COPY command, this function - * degenerates to an AcceptResult() call. - * - * Changes its argument to point to the last PGresult of the command string, - * or NULL if that result was for a COPY TO STDOUT. (Returning NULL prevents - * the command status from being printed, which we want in that case so that - * the status line doesn't get taken as part of the COPY data.) - * - * Returns true on complete success, false otherwise. Possible failure modes - * include purely client-side problems; check the transaction status for the - * server-side opinion. + * Marshal the COPY data. Either subroutine will get the + * connection out of its COPY state, then call PQresultStatus() + * once and report any error. + * + * For COPY OUT, direct the output to pset.copyStream if it's set, + * otherwise to pset.gfname if it's set, otherwise to queryFout. + * For COPY IN, use pset.copyStream as data source if it's set, + * otherwise cur_cmd_source. + * + * Update result if further processing is necessary. (Returning NULL + * prevents the command status from being printed, which we want in that + * case so that the status line doesn't get taken as part of the COPY data.) */ static bool -ProcessResult(PGresult **results) +HandleCopyResult(PGresult **result) { bool success = true; - bool first_cycle = true; + FILE *copystream; + PGresult *copy_result; + ExecStatusType result_status = PQresultStatus(*result); - for (;;) + Assert(result_status == PGRES_COPY_OUT || + result_status == PGRES_COPY_IN); + + SetCancelConn(); + if (result_status == PGRES_COPY_OUT) { - ExecStatusType result_status; - bool is_copy; - PGresult *next_result; + bool need_close = false; + bool is_pipe = false; - if (!AcceptResult(*results)) + if (pset.copyStream) { - /* - * Failure at this point is always a server-side failure or a - * failure to submit the command string. Either way, we're - * finished with this command string. - */ - success = false; - break; + /* invoked by \copy */ + copystream = pset.copyStream; } - - result_status = PQresultStatus(*results); - switch (result_status) + else if (pset.gfname) { - case PGRES_EMPTY_QUERY: - case PGRES_COMMAND_OK: - case PGRES_TUPLES_OK: - is_copy = false; - break; - - case PGRES_COPY_OUT: - case PGRES_COPY_IN: - is_copy = true; - break; - - default: - /* AcceptResult() should have caught anything else. */ - is_copy = false; - pg_log_error("unexpected PQresultStatus: %d", result_status); - break; - } - - if (is_copy) - { - /* - * Marshal the COPY data. Either subroutine will get the - * connection out of its COPY state, then call PQresultStatus() - * once and report any error. - * - * For COPY OUT, direct the output to pset.copyStream if it's set, - * otherwise to pset.gfname if it's set, otherwise to queryFout. - * For COPY IN, use pset.copyStream as data source if it's set, - * otherwise cur_cmd_source. - */ - FILE *copystream; - PGresult *copy_result; - - SetCancelConn(); - if (result_status == PGRES_COPY_OUT) + /* invoked by \g */ + if (openQueryOutputFile(pset.gfname, + ©stream, &is_pipe)) { - bool need_close = false; - bool is_pipe = false; - - if (pset.copyStream) - { - /* invoked by \copy */ - copystream = pset.copyStream; - } - else if (pset.gfname) - { - /* invoked by \g */ - if (openQueryOutputFile(pset.gfname, - ©stream, &is_pipe)) - { - need_close = true; - if (is_pipe) - disable_sigpipe_trap(); - } - else - copystream = NULL; /* discard COPY data entirely */ - } - else - { - /* fall back to the generic query output stream */ - copystream = pset.queryFout; - } - - success = handleCopyOut(pset.db, - copystream, - ©_result) - && success - && (copystream != NULL); - - /* - * Suppress status printing if the report would go to the same - * place as the COPY data just went. Note this doesn't - * prevent error reporting, since handleCopyOut did that. - */ - if (copystream == pset.queryFout) - { - PQclear(copy_result); - copy_result = NULL; - } - - if (need_close) - { - /* close \g argument file/pipe */ - if (is_pipe) - { - pclose(copystream); - restore_sigpipe_trap(); - } - else - { - fclose(copystream); - } - } + need_close = true; + if (is_pipe) + disable_sigpipe_trap(); } else - { - /* COPY IN */ - copystream = pset.copyStream ? pset.copyStream : pset.cur_cmd_source; - success = handleCopyIn(pset.db, - copystream, - PQbinaryTuples(*results), - ©_result) && success; - } - ResetCancelConn(); - - /* - * Replace the PGRES_COPY_OUT/IN result with COPY command's exit - * status, or with NULL if we want to suppress printing anything. - */ - PQclear(*results); - *results = copy_result; + copystream = NULL; /* discard COPY data entirely */ } - else if (first_cycle) + else { - /* fast path: no COPY commands; PQexec visited all results */ - break; + /* fall back to the generic query output stream */ + copystream = pset.queryFout; } + success = handleCopyOut(pset.db, + copystream, + ©_result) + && success + && (copystream != NULL); + /* - * Check PQgetResult() again. In the typical case of a single-command - * string, it will return NULL. Otherwise, we'll have other results - * to process that may include other COPYs. We keep the last result. + * Suppress status printing if the report would go to the same + * place as the COPY data just went. Note this doesn't + * prevent error reporting, since handleCopyOut did that. */ - next_result = PQgetResult(pset.db); - if (!next_result) - break; + if (copystream == pset.queryFout) + { + PQclear(copy_result); + copy_result = NULL; + } - PQclear(*results); - *results = next_result; - first_cycle = false; + if (need_close) + { + /* close \g argument file/pipe */ + if (is_pipe) + { + pclose(copystream); + restore_sigpipe_trap(); + } + else + { + fclose(copystream); + } + } } + else + { + /* COPY IN */ + copystream = pset.copyStream ? pset.copyStream : pset.cur_cmd_source; + success = handleCopyIn(pset.db, + copystream, + PQbinaryTuples(*result), + ©_result) && success; + } + ResetCancelConn(); - SetResultVariables(*results, success); - - /* may need this to recover from conn loss during COPY */ - if (!first_cycle && !CheckConnection()) - return false; + PQclear(*result); + *result = copy_result; return success; } - /* * PrintQueryStatus: report command status as required * - * Note: Utility function for use by PrintQueryResults() only. + * Note: Utility function for use by HandleQueryResult() only. */ static void PrintQueryStatus(PGresult *results) @@ -1219,43 +1136,49 @@ PrintQueryStatus(PGresult *results) /* - * PrintQueryResults: print out (or store or execute) query results as required - * - * Note: Utility function for use by SendQuery() only. + * HandleQueryResult: print out, store or execute one query result + * as required. * * Returns true if the query executed successfully, false otherwise. */ static bool -PrintQueryResults(PGresult *results) +HandleQueryResult(PGresult *result, bool last) { bool success; const char *cmdstatus; - if (!results) + if (result == NULL) return false; - switch (PQresultStatus(results)) + switch (PQresultStatus(result)) { case PGRES_TUPLES_OK: /* store or execute or print the data ... */ - if (pset.gset_prefix) - success = StoreQueryTuple(results); - else if (pset.gexec_flag) - success = ExecQueryTuples(results); - else if (pset.crosstab_flag) - success = PrintResultsInCrosstab(results); + if (last && pset.gset_prefix) + success = StoreQueryTuple(result); + else if (last && pset.gexec_flag) + success = ExecQueryTuples(result); + else if (last && pset.crosstab_flag) + success = PrintResultsInCrosstab(result); + else if (last || pset.show_all_results) + success = PrintQueryTuples(result); + /* if it's INSERT/UPDATE/DELETE RETURNING, also print status */ else - success = PrintQueryTuples(results); - /* if it's INSERT/UPDATE/DELETE RETURNING, also print status */ - cmdstatus = PQcmdStatus(results); - if (strncmp(cmdstatus, "INSERT", 6) == 0 || - strncmp(cmdstatus, "UPDATE", 6) == 0 || - strncmp(cmdstatus, "DELETE", 6) == 0) - PrintQueryStatus(results); + success = true; + + if (last || pset.show_all_results) + { + cmdstatus = PQcmdStatus(result); + if (strncmp(cmdstatus, "INSERT", 6) == 0 || + strncmp(cmdstatus, "UPDATE", 6) == 0 || + strncmp(cmdstatus, "DELETE", 6) == 0) + PrintQueryStatus(result); + } break; case PGRES_COMMAND_OK: - PrintQueryStatus(results); + if (last || pset.show_all_results) + PrintQueryStatus(result); success = true; break; @@ -1265,7 +1188,7 @@ PrintQueryResults(PGresult *results) case PGRES_COPY_OUT: case PGRES_COPY_IN: - /* nothing to do here */ + /* nothing to do here: already processed */ success = true; break; @@ -1278,7 +1201,7 @@ PrintQueryResults(PGresult *results) default: success = false; pg_log_error("unexpected PQresultStatus: %d", - PQresultStatus(results)); + PQresultStatus(result)); break; } @@ -1287,6 +1210,153 @@ PrintQueryResults(PGresult *results) return success; } +/* + * Data structure and functions to record notices while they are + * emitted, so that they can be shown later. + * + * We need to know which result is last, which requires to extract + * one result in advance, hence two buffers are needed. + */ +typedef struct { + bool in_flip; + PQExpBufferData flip; + PQExpBufferData flop; +} t_notice_messages; + +static void +AppendNoticeMessage(void *arg, const char *msg) +{ + t_notice_messages *notes = (t_notice_messages*) arg; + appendPQExpBufferStr(notes->in_flip ? ¬es->flip : ¬es->flop, msg); +} + +static void +ShowNoticeMessage(t_notice_messages *notes) +{ + PQExpBufferData *current = notes->in_flip ? ¬es->flip : ¬es->flop; + if (current->data != NULL && *current->data != '\0') + pg_log_info("%s", current->data); + resetPQExpBuffer(current); +} + +/* + * SendQueryAndProcessResults: utility function for use by SendQuery() only + * + * Sends query and cycles through PGresult objects. + * + * When our command string contained a COPY FROM STDIN or COPY TO STDOUT, + * the PGresult associated with these commands must be processed. In that + * event, we'll marshal data for the COPY. + * + * Returns true on complete success, false otherwise. Possible failure modes + * include purely client-side problems; check the transaction status for the + * server-side opinion. + * + * Note that on a combined query, failure does not mean that nothing was + * committed. + */ +static bool +SendQueryAndProcessResults(const char *query, double *pelapsed_msec) +{ + bool success; + instr_time before; + PGresult *result; + t_notice_messages notes; + + if (pset.timing) + INSTR_TIME_SET_CURRENT(before); + + success = PQsendQuery(pset.db, query); + ResetCancelConn(); + + if (!success) + return false; + + /* intercept notices */ + notes.in_flip = true; + initPQExpBuffer(¬es.flip); + initPQExpBuffer(¬es.flop); + PQsetNoticeProcessor(pset.db, AppendNoticeMessage, ¬es); + + /* first result */ + result = PQgetResult(pset.db); + + while (result != NULL) + { + ExecStatusType result_status; + PGresult *next_result; + bool last; + + if (!AcceptResult(result, false)) + { + /* some error occured, record that */ + ShowNoticeMessage(¬es); + ShowErrorMessage(result); + SetResultVariables(result, false); + PQclear(result); + success = false; + + /* and switch to next result */ + result = PQgetResult(pset.db); + continue; + } + + /* must handle COPY before changing the current result */ + result_status = PQresultStatus(result); + if (result_status == PGRES_COPY_IN || + result_status == PGRES_COPY_OUT) + { + ShowNoticeMessage(¬es); + HandleCopyResult(&result); + } + + /* + * Check PQgetResult() again. In the typical case of a single-command + * string, it will return NULL. Otherwise, we'll have other results + * to process that may include other COPYs. + */ + notes.in_flip = !notes.in_flip; + next_result = PQgetResult(pset.db); + notes.in_flip = !notes.in_flip; + last = (next_result == NULL); + + /* timing measure before printing the last result */ + if (last && pset.timing) + { + instr_time now; + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, before); + *pelapsed_msec = INSTR_TIME_GET_MILLISEC(now); + } + + /* notices already show above for copy */ + ShowNoticeMessage(¬es); + + /* this may or may not print something depending on settings */ + if (result != NULL) + success &= HandleQueryResult(result, last); + + /* set variables on last result if all went well */ + if (last && success) + SetResultVariables(result, true); + + PQclear(result); + notes.in_flip = !notes.in_flip; + result = next_result; + } + + /* reset notice hook */ + PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL); + termPQExpBuffer(¬es.flip); + termPQExpBuffer(¬es.flop); + + /* may need this to recover from conn loss during COPY */ + if (!CheckConnection()) + return false; + + return success; +} + /* * SendQuery: send the query string to the backend @@ -1303,7 +1373,7 @@ PrintQueryResults(PGresult *results) bool SendQuery(const char *query) { - PGresult *results; + PGresult *results = NULL; PGTransactionStatusType transaction_status; double elapsed_msec = 0; bool OK = false; @@ -1408,28 +1478,7 @@ SendQuery(const char *query) pset.crosstab_flag || !is_select_command(query)) { /* Default fetch-it-all-and-print mode */ - instr_time before, - after; - - if (pset.timing) - INSTR_TIME_SET_CURRENT(before); - - results = PQexec(pset.db, query); - - /* these operations are included in the timing result: */ - ResetCancelConn(); - OK = ProcessResult(&results); - - if (pset.timing) - { - INSTR_TIME_SET_CURRENT(after); - INSTR_TIME_SUBTRACT(after, before); - elapsed_msec = INSTR_TIME_GET_MILLISEC(after); - } - - /* but printing results isn't: */ - if (OK && results) - OK = PrintQueryResults(results); + OK = SendQueryAndProcessResults(query, &elapsed_msec); } else { @@ -1606,7 +1655,7 @@ DescribeQuery(const char *query, double *elapsed_msec) PQclear(results); results = PQdescribePrepared(pset.db, ""); - OK = AcceptResult(results) && + OK = AcceptResult(results, true) && (PQresultStatus(results) == PGRES_COMMAND_OK); if (OK && results) { @@ -1654,7 +1703,7 @@ DescribeQuery(const char *query, double *elapsed_msec) PQclear(results); results = PQexec(pset.db, buf.data); - OK = AcceptResult(results); + OK = AcceptResult(results, true); if (pset.timing) { @@ -1664,7 +1713,7 @@ DescribeQuery(const char *query, double *elapsed_msec) } if (OK && results) - OK = PrintQueryResults(results); + OK = HandleQueryResult(results, true); termPQExpBuffer(&buf); } @@ -1723,7 +1772,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) if (PQtransactionStatus(pset.db) == PQTRANS_IDLE) { results = PQexec(pset.db, "BEGIN"); - OK = AcceptResult(results) && + OK = AcceptResult(results, true) && (PQresultStatus(results) == PGRES_COMMAND_OK); ClearOrSaveResult(results); if (!OK) @@ -1737,7 +1786,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) query); results = PQexec(pset.db, buf.data); - OK = AcceptResult(results) && + OK = AcceptResult(results, true) && (PQresultStatus(results) == PGRES_COMMAND_OK); if (!OK) SetResultVariables(results, OK); @@ -1814,7 +1863,7 @@ ExecQueryUsingCursor(const char *query, double *elapsed_msec) is_pager = false; } - OK = AcceptResult(results); + OK = AcceptResult(results, true); Assert(!OK); SetResultVariables(results, OK); ClearOrSaveResult(results); @@ -1923,7 +1972,7 @@ cleanup: results = PQexec(pset.db, "CLOSE _psql_cursor"); if (OK) { - OK = AcceptResult(results) && + OK = AcceptResult(results, true) && (PQresultStatus(results) == PGRES_COMMAND_OK); ClearOrSaveResult(results); } @@ -1933,7 +1982,7 @@ cleanup: if (started_txn) { results = PQexec(pset.db, OK ? "COMMIT" : "ROLLBACK"); - OK &= AcceptResult(results) && + OK &= AcceptResult(results, true) && (PQresultStatus(results) == PGRES_COMMAND_OK); ClearOrSaveResult(results); } diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index d6d41b51d5..3cc71731b3 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -404,6 +404,8 @@ helpVariables(unsigned short int pager) fprintf(output, _(" SERVER_VERSION_NAME\n" " SERVER_VERSION_NUM\n" " server's version (in short string or numeric format)\n")); + fprintf(output, _(" SHOW_ALL_RESULTS\n" + " show all results of a combined query (\\;) instead of only the last\n")); fprintf(output, _(" SHOW_CONTEXT\n" " controls display of message context fields [never, errors, always]\n")); fprintf(output, _(" SINGLELINE\n" diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index 5be5091f0e..f26bf7fc6b 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -127,6 +127,7 @@ typedef struct _psqlSettings bool quiet; bool singleline; bool singlestep; + bool show_all_results; bool hide_tableam; int fetch_count; int histsize; diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 855133bbcb..24b2fc8c28 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -882,6 +882,12 @@ singlestep_hook(const char *newval) return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep); } +static bool +show_all_results_hook(const char *newval) +{ + return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results); +} + static char * fetch_count_substitute_hook(char *newval) { @@ -1182,6 +1188,9 @@ EstablishVariableSpace(void) SetVariableHooks(pset.vars, "SINGLESTEP", bool_substitute_hook, singlestep_hook); + SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS", + bool_substitute_hook, + show_all_results_hook); SetVariableHooks(pset.vars, "FETCH_COUNT", fetch_count_substitute_hook, fetch_count_hook); diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c index bcddc7601e..b66e5db9e2 100644 --- a/src/bin/psql/tab-complete.c +++ b/src/bin/psql/tab-complete.c @@ -3662,7 +3662,7 @@ psql_completion(const char *text, int start, int end) else if (TailMatchesCS("\\set", MatchAny)) { if (TailMatchesCS("AUTOCOMMIT|ON_ERROR_STOP|QUIET|" - "SINGLELINE|SINGLESTEP")) + "SINGLELINE|SINGLESTEP|SHOW_ALL_RESULTS")) COMPLETE_WITH_CS("on", "off"); else if (TailMatchesCS("COMP_KEYWORD_CASE")) COMPLETE_WITH_CS("lower", "upper", diff --git a/src/test/regress/expected/copy2.out b/src/test/regress/expected/copy2.out index 75d4119eaa..4fa6343795 100644 --- a/src/test/regress/expected/copy2.out +++ b/src/test/regress/expected/copy2.out @@ -475,10 +475,10 @@ copy check_con_tbl from stdin; NOTICE: input = {"f1":1} NOTICE: input = {"f1":null} copy check_con_tbl from stdin; -NOTICE: input = {"f1":0} ERROR: new row for relation "check_con_tbl" violates check constraint "check_con_tbl_check" DETAIL: Failing row contains (0). CONTEXT: COPY check_con_tbl, line 1: "0" +NOTICE: input = {"f1":0} select * from check_con_tbl; f1 ---- diff --git a/src/test/regress/expected/copydml.out b/src/test/regress/expected/copydml.out index 1b533962c6..b5a225628f 100644 --- a/src/test/regress/expected/copydml.out +++ b/src/test/regress/expected/copydml.out @@ -84,10 +84,10 @@ drop rule qqq on copydml_test; create function qqq_trig() returns trigger as $$ begin if tg_op in ('INSERT', 'UPDATE') then - raise notice '% %', tg_op, new.id; + raise notice '% % %', tg_when, tg_op, new.id; return new; else - raise notice '% %', tg_op, old.id; + raise notice '% % %', tg_when, tg_op, old.id; return old; end if; end @@ -97,16 +97,16 @@ create trigger qqqbef before insert or update or delete on copydml_test create trigger qqqaf after insert or update or delete on copydml_test for each row execute procedure qqq_trig(); copy (insert into copydml_test (t) values ('f') returning id) to stdout; -NOTICE: INSERT 8 +NOTICE: BEFORE INSERT 8 8 -NOTICE: INSERT 8 +NOTICE: AFTER INSERT 8 copy (update copydml_test set t = 'g' where t = 'f' returning id) to stdout; -NOTICE: UPDATE 8 +NOTICE: BEFORE UPDATE 8 8 -NOTICE: UPDATE 8 +NOTICE: AFTER UPDATE 8 copy (delete from copydml_test where t = 'g' returning id) to stdout; -NOTICE: DELETE 8 +NOTICE: BEFORE DELETE 8 8 -NOTICE: DELETE 8 +NOTICE: AFTER DELETE 8 drop table copydml_test; drop function qqq_trig(); diff --git a/src/test/regress/expected/psql.out b/src/test/regress/expected/psql.out index 35856bffdd..bca5bf2fe7 100644 --- a/src/test/regress/expected/psql.out +++ b/src/test/regress/expected/psql.out @@ -4729,3 +4729,89 @@ drop schema testpart; set search_path to default; set role to default; drop role testrole_partitioning; +-- +-- combined queries & SHOW_ALL_RESULTS option +-- +\echo '# SHOW_ALL_RESULTS tests' +# SHOW_ALL_RESULTS tests +CREATE FUNCTION warn(msg TEXT) RETURNS BOOLEAN AS $$ + BEGIN RAISE NOTICE 'warn %', msg ; RETURN TRUE ; END +$$ LANGUAGE plpgsql; +\set SHOW_ALL_RESULTS on +-- show both +SELECT 1 AS one \; SELECT warn('1.5') \; SELECT 2 AS two ; + one +----- + 1 +(1 row) + +NOTICE: warn 1.5 +CONTEXT: PL/pgSQL function warn(text) line 2 at RAISE + warn +------ + t +(1 row) + + two +----- + 2 +(1 row) + +-- \gset applies to last query only +SELECT 3 AS three \; SELECT warn('3.5') \; SELECT 4 AS four \gset + three +------- + 3 +(1 row) + +NOTICE: warn 3.5 +CONTEXT: PL/pgSQL function warn(text) line 2 at RAISE + warn +------ + t +(1 row) + +\echo :three :four +:three 4 +-- syntax error stops all processing +SELECT 5 \; SELECT 6 + \; SELECT warn('6.5') \; SELECT 7 ; +ERROR: syntax error at or near ";" +LINE 1: SELECT 5 ; SELECT 6 + ; SELECT warn('6.5') ; SELECT 7 ; + ^ +-- with aborted transaction, stop on first error +BEGIN \; SELECT 8 AS eight \; SELECT 9/0 AS nine \; ROLLBACK \; SELECT 10 AS ten ; + eight +------- + 8 +(1 row) + +ERROR: division by zero +-- close previously aborted transaction +ROLLBACK; +-- misc SQL commands +-- (non SELECT output is sent to stderr, thus is not shown in expected results) +SELECT 'ok' AS "begin" \; +CREATE TABLE psql_comics(s TEXT) \; +INSERT INTO psql_comics VALUES ('Calvin'), ('hobbes') \; +COPY psql_comics FROM STDIN \; +UPDATE psql_comics SET s = 'Hobbes' WHERE s = 'hobbes' \; +DELETE FROM psql_comics WHERE s = 'Moe' \; +COPY psql_comics TO STDOUT \; +TRUNCATE psql_comics \; +DROP TABLE psql_comics \; +SELECT 'ok' AS "done" ; + begin +------- + ok +(1 row) + +Calvin +Susie +Hobbes + done +------ + ok +(1 row) + +\set SHOW_ALL_RESULTS off +DROP FUNCTION warn(TEXT); diff --git a/src/test/regress/sql/copydml.sql b/src/test/regress/sql/copydml.sql index 9a29f9c9ac..4578342253 100644 --- a/src/test/regress/sql/copydml.sql +++ b/src/test/regress/sql/copydml.sql @@ -70,10 +70,10 @@ drop rule qqq on copydml_test; create function qqq_trig() returns trigger as $$ begin if tg_op in ('INSERT', 'UPDATE') then - raise notice '% %', tg_op, new.id; + raise notice '% % %', tg_when, tg_op, new.id; return new; else - raise notice '% %', tg_op, old.id; + raise notice '% % %', tg_when, tg_op, old.id; return old; end if; end diff --git a/src/test/regress/sql/psql.sql b/src/test/regress/sql/psql.sql index 78f4b5d7d5..03a378de3d 100644 --- a/src/test/regress/sql/psql.sql +++ b/src/test/regress/sql/psql.sql @@ -1115,3 +1115,40 @@ set search_path to default; set role to default; drop role testrole_partitioning; + +-- +-- combined queries & SHOW_ALL_RESULTS option +-- +\echo '# SHOW_ALL_RESULTS tests' +CREATE FUNCTION warn(msg TEXT) RETURNS BOOLEAN AS $$ + BEGIN RAISE NOTICE 'warn %', msg ; RETURN TRUE ; END +$$ LANGUAGE plpgsql; +\set SHOW_ALL_RESULTS on +-- show both +SELECT 1 AS one \; SELECT warn('1.5') \; SELECT 2 AS two ; +-- \gset applies to last query only +SELECT 3 AS three \; SELECT warn('3.5') \; SELECT 4 AS four \gset +\echo :three :four +-- syntax error stops all processing +SELECT 5 \; SELECT 6 + \; SELECT warn('6.5') \; SELECT 7 ; +-- with aborted transaction, stop on first error +BEGIN \; SELECT 8 AS eight \; SELECT 9/0 AS nine \; ROLLBACK \; SELECT 10 AS ten ; +-- close previously aborted transaction +ROLLBACK; +-- misc SQL commands +-- (non SELECT output is sent to stderr, thus is not shown in expected results) +SELECT 'ok' AS "begin" \; +CREATE TABLE psql_comics(s TEXT) \; +INSERT INTO psql_comics VALUES ('Calvin'), ('hobbes') \; +COPY psql_comics FROM STDIN \; +UPDATE psql_comics SET s = 'Hobbes' WHERE s = 'hobbes' \; +DELETE FROM psql_comics WHERE s = 'Moe' \; +COPY psql_comics TO STDOUT \; +TRUNCATE psql_comics \; +DROP TABLE psql_comics \; +SELECT 'ok' AS "done" ; +Moe +Susie +\. +\set SHOW_ALL_RESULTS off +DROP FUNCTION warn(TEXT);