在一个plpgsql函数中,我有这样的异常块:
exception when others then
txt := 'error text ';
perform record_error(id, table, txt);
当发生异常时,我想执行另一个功能,该功能将有关错误的信息添加到日志表中,如下所示:
CREATE OR REPLACE FUNCTION record_error(
id integer,
layer text,
message text)
RETURNS void AS
$BODY$
DECLARE
l_code integer:= 'sqlstate'
l_mesg varchar:= 'SQLERRM';
l_context text;
l_detail text;
BEGIN
GET STACKED DIAGNOSTICS l_context = PG_EXCEPTION_CONTEXT;
GET STACKED DIAGNOSTICS l_detail = PG_EXCEPTION_DETAIL;
INSERT INTO error_log ( error_code ,
error_message ,
backtrace ,
callstack ,
created_on ,
created_by ,
user_msg ,
etak_id ,
layer )
VALUES (l_code ,
l_mesg ,
l_context,
l_detail ,
current_timestamp ,
CURRENT_USER ,
message ,
ETAK_ID ,
Layer );
END;
$BODY$
LANGUAGE plpgsql VOLATILE
但是我收到一条错误消息:无法在异常处理程序之外使用获取堆叠诊断。
我可以在另一个函数中使用SQLSTATE和GET STACKED DIAGNOSTICS信息吗?
答案 0 :(得分:1)
不可能。出现此限制的原因是在SQL函数中实现了异常和异常处理。由于没有意义,因此在语法上是禁止的。