From a88771fadf727c6483a32b8b7da07ebcc45650d3 Mon Sep 17 00:00:00 2001 From: nnuanyang Date: Thu, 21 Sep 2023 02:58:08 -0700 Subject: [PATCH] get stacked diagnostic error --- src/common/pl/plpgsql/src/pl_exec.cpp | 3 ++ src/test/regress/expected/mysql_condition.out | 52 +++++++++++++++++++ src/test/regress/sql/mysql_condition.sql | 40 ++++++++++++++ 3 files changed, 95 insertions(+) diff --git a/src/common/pl/plpgsql/src/pl_exec.cpp b/src/common/pl/plpgsql/src/pl_exec.cpp index fea566fbff..a9c47a7e6a 100644 --- a/src/common/pl/plpgsql/src/pl_exec.cpp +++ b/src/common/pl/plpgsql/src/pl_exec.cpp @@ -2657,6 +2657,9 @@ PLpgSQL_datum* copy_plpgsql_datum(PLpgSQL_datum* datum) static bool exception_matches_conditions(ErrorData* edata, PLpgSQL_condition* cond) { + if (DB_IS_CMPT(B_FORMAT) && edata->sqlerrcode == ERRCODE_STACKED_DIAGNOSTICS_ACCESSED_WITHOUT_ACTIVE_HANDLER) + return false; + for (; cond != NULL; cond = cond->next) { int sqlerrstate = cond->sqlerrstate; char *condname = cond->condname; diff --git a/src/test/regress/expected/mysql_condition.out b/src/test/regress/expected/mysql_condition.out index 48f639dcac..1fe74d15b5 100644 --- a/src/test/regress/expected/mysql_condition.out +++ b/src/test/regress/expected/mysql_condition.out @@ -1742,6 +1742,58 @@ ERROR: table "t1" does not exist CONTEXT: SQL statement "drop table t1" PL/pgSQL function bug6900_9074(integer) line 20 at SQL statement drop procedure bug6900_9074; +CREATE TABLE t1(c1 TEXT NOT NULL); +CREATE OR REPLACE PROCEDURE p_1145188() +AS +BEGIN +DECLARE CONTINUE HANDLER FOR SQLEXCEPTION +BEGIN +-- Here the current DA is nonempty because no prior statements +-- executing within the handler have cleared it +GET CURRENT DIAGNOSTICS @num=NUMBER,@rowcount=ROW_COUNT; +RAISE NOTICE 'NUMBER=%,ROW_COUNT=%',@num,@rowcount; +GET CURRENT DIAGNOSTICS CONDITION 1 @luser = MYSQL_ERRNO, @msg = MESSAGE_TEXT; +RAISE NOTICE 'current DA before mapped insert , error = % , msg = %', @errno, @msg; +GET STACKED DIAGNOSTICS @num=NUMBER,@rowcount=ROW_COUNT; +RAISE NOTICE 'NUMBER=%,ROW_COUNT=%',@num,@rowcount; +GET STACKED DIAGNOSTICS CONDITION 1 @errno1 = MYSQL_ERRNO, @mingshigang2 = MESSAGE_TEXT; +RAISE NOTICE 'stacked DA before mapped insert , error = % , msg = %', @errno1, @msg1; + +INSERT INTO t1 (c1) VALUES(0),(1),(2); +GET CURRENT DIAGNOSTICS @num=NUMBER,@rowcount=ROW_COUNT; +RAISE NOTICE 'NUMBER=%,ROW_COUNT=%',@num,@rowcount; +GET CURRENT DIAGNOSTICS CONDITION 1 @luser = MYSQL_ERRNO, @msg = MESSAGE_TEXT; +RAISE NOTICE 'current DA before mapped insert , error = % , msg = %', @errno, @msg; +GET STACKED DIAGNOSTICS @num=NUMBER,@rowcount=ROW_COUNT; +RAISE NOTICE 'NUMBER=%,ROW_COUNT=%',@num,@rowcount; +GET STACKED DIAGNOSTICS CONDITION 1 @errno1 = MYSQL_ERRNO, @mingshigang2 = MESSAGE_TEXT; +RAISE NOTICE 'stacked DA before mapped insert , error = % , msg = %', @errno1, @msg1; +END; + +GET CURRENT DIAGNOSTICS @num=NUMBER,@errcount= ROW_COUNT; +RAISE NOTICE 'current DA before mapped insert , num = % , errcount = %', @num, @errcount; + +INSERT INTO t1 (c1) VALUES(NULL); + +GET CURRENT DIAGNOSTICS @num=NUMBER,@errcount= ROW_COUNT; +RAISE NOTICE 'current DA before mapped insert , num = % , errcount = %', @num, @errcount; +GET STACKED DIAGNOSTICS @num=NUMBER,@errcount= ROW_COUNT; + +END; +/ +call p_1145188(); +NOTICE: current DA before mapped insert , num = 0 , errcount = -1 +NOTICE: NUMBER=1,ROW_COUNT=-1 +NOTICE: current DA before mapped insert , error = 23502 , msg = NUMBER=1,ROW_COUNT=-1 +NOTICE: NUMBER=1,ROW_COUNT=-1 +NOTICE: stacked DA before mapped insert , error = 23502 , msg = Unhandled user-defined warning condition +NOTICE: NUMBER=0,ROW_COUNT=3 +NOTICE: current DA before mapped insert , error = 23502 , msg = NUMBER=0,ROW_COUNT=3 +NOTICE: NUMBER=1,ROW_COUNT=-1 +NOTICE: stacked DA before mapped insert , error = 23502 , msg = Unhandled user-defined warning condition +NOTICE: current DA before mapped insert , num = 1 , errcount = -1 +ERROR: GET STACKED DIAGNOSTICS cannot be used outside an exception handler +CONTEXT: PL/pgSQL function p_1145188() line 33 at GET DIAGNOSTICS \c regression -- test access to exception data create function zero_divide() returns int as $$ diff --git a/src/test/regress/sql/mysql_condition.sql b/src/test/regress/sql/mysql_condition.sql index 333e389458..2bc4c9516c 100644 --- a/src/test/regress/sql/mysql_condition.sql +++ b/src/test/regress/sql/mysql_condition.sql @@ -1141,6 +1141,46 @@ call bug6900_9074(0); call bug6900_9074(1); call bug6900_9074(2); drop procedure bug6900_9074; +CREATE TABLE t1(c1 TEXT NOT NULL); +CREATE OR REPLACE PROCEDURE p_1145188() +AS +BEGIN +DECLARE CONTINUE HANDLER FOR SQLEXCEPTION +BEGIN +-- Here the current DA is nonempty because no prior statements +-- executing within the handler have cleared it +GET CURRENT DIAGNOSTICS @num=NUMBER,@rowcount=ROW_COUNT; +RAISE NOTICE 'NUMBER=%,ROW_COUNT=%',@num,@rowcount; +GET CURRENT DIAGNOSTICS CONDITION 1 @luser = MYSQL_ERRNO, @msg = MESSAGE_TEXT; +RAISE NOTICE 'current DA before mapped insert , error = % , msg = %', @errno, @msg; +GET STACKED DIAGNOSTICS @num=NUMBER,@rowcount=ROW_COUNT; +RAISE NOTICE 'NUMBER=%,ROW_COUNT=%',@num,@rowcount; +GET STACKED DIAGNOSTICS CONDITION 1 @errno1 = MYSQL_ERRNO, @mingshigang2 = MESSAGE_TEXT; +RAISE NOTICE 'stacked DA before mapped insert , error = % , msg = %', @errno1, @msg1; + +INSERT INTO t1 (c1) VALUES(0),(1),(2); +GET CURRENT DIAGNOSTICS @num=NUMBER,@rowcount=ROW_COUNT; +RAISE NOTICE 'NUMBER=%,ROW_COUNT=%',@num,@rowcount; +GET CURRENT DIAGNOSTICS CONDITION 1 @luser = MYSQL_ERRNO, @msg = MESSAGE_TEXT; +RAISE NOTICE 'current DA before mapped insert , error = % , msg = %', @errno, @msg; +GET STACKED DIAGNOSTICS @num=NUMBER,@rowcount=ROW_COUNT; +RAISE NOTICE 'NUMBER=%,ROW_COUNT=%',@num,@rowcount; +GET STACKED DIAGNOSTICS CONDITION 1 @errno1 = MYSQL_ERRNO, @mingshigang2 = MESSAGE_TEXT; +RAISE NOTICE 'stacked DA before mapped insert , error = % , msg = %', @errno1, @msg1; +END; + +GET CURRENT DIAGNOSTICS @num=NUMBER,@errcount= ROW_COUNT; +RAISE NOTICE 'current DA before mapped insert , num = % , errcount = %', @num, @errcount; + +INSERT INTO t1 (c1) VALUES(NULL); + +GET CURRENT DIAGNOSTICS @num=NUMBER,@errcount= ROW_COUNT; +RAISE NOTICE 'current DA before mapped insert , num = % , errcount = %', @num, @errcount; +GET STACKED DIAGNOSTICS @num=NUMBER,@errcount= ROW_COUNT; + +END; +/ +call p_1145188(); \c regression -- test access to exception data create function zero_divide() returns int as $$ -- Gitee