问题
In one plpgsql function I have exception block like this:
exception when others then
txt := 'error text ';
perform record_error(id, table, txt);
And when exception occurs I want to perform another function, which adds information about error to log table like this:
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
But I get a message ERROR: GET STACKED DIAGNOSTICS cannot be used outside an exception handler.
Can I use SQLSTATE and GET STACKED DIAGNOSTICS information in another function?
回答1:
It is not possible. Reason for this limit is implementation of exceptions and exception handling in SQL functions. Because there is not sense to do it, it is prohibited by syntax.
来源:https://stackoverflow.com/questions/56471876/sqlstate-and-get-stacked-diagnostics-information-in-another-function