自冶事务与exception的限制

最近更新时间: 2024-06-12 15:06:00

postgres=# CREATE OR REPLACE PROCEDURE p_exception_error(a_id integer,a_nc text) AS
$$    
DECLARE    
    v_sqlstate text;
    v_context text;
    v_message_text text;
BEGIN           
    INSERT INTO t_exception VALUES(a_id,a_nc);
COMMIT;
    EXCEPTION WHEN OTHERS THEN 
    GET STACKED DIAGNOSTICS v_sqlstate = RETURNED_SQLSTATE,
                                 v_message_text = MESSAGE_TEXT,
                                 v_context = PG_EXCEPTION_CONTEXT;     
    RAISE NOTICE '错误代码 : %',v_sqlstate;
    RAISE NOTICE '出错信息 : %',v_message_text;
    RAISE NOTICE '发生异常语句 : %',v_context; 
    raise notice '错误代码 : % \n出错信息 : % 发生异常语句 : %',v_sqlstate ,v_message_text,v_context;   
END;
$$
LANGUAGE plpgsql;
CREATE PROCEDURE
postgres=# call p_exception_error(1,'tbase');        
NOTICE:  错误代码 : 2D000
NOTICE:  出错信息 : cannot commit while a subtransaction is active
NOTICE:  发生异常语句 : PL/pgSQL function p_exception_error(integer,text) line 8 at COMMIT
NOTICE:  错误代码 : 2D000 \n出错信息 : cannot commit while a subtransaction is active 发生异常语句 : PL/pgSQL function p_exception_error(integer,text) line 8 at COMMIT
CALL
postgres=#

如果存储过程中使用了exception,就不能再使用自冶事务,在5.06版本中兼容这个问题。