스택드 다이어그노스틱스 가져오기
Stacked diagnostics를 사용하면 PostgreSQL 내부 오류 메시지와 예외 세부 정보를 가져올 수 있어요. 환자 테이블로 돌아가서 검사 한계를 초과하는 A1C 값을 추가해 보세요. 그러면 체크 제약 조건 예외가 발생하고, 이를 포착할 수 있어요. 예외 처리기에서 stacked diagnostics를 사용해 오류 기록을 더 풍부하게 만들겠습니다.
이 연습은 강의의 일부입니다
PostgreSQL에서의 트랜잭션과 오류 처리
연습 안내
DECLARE블록에서 텍스트 타입 변수exc_message와exc_detail두 개를 선언하세요.- 진단 스택을 가져와
exc_message에는MESSAGE_TEXT를,exc_detail에는PG_EXCEPTION_DETAIL을 설정하세요. exc_message와exc_detail을 errors 테이블의msg및detail필드에 삽입하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
DO $$
-- Declare our text variables: exc_message and exc_detail
___
___ text;
exc_detail ___;
BEGIN
INSERT INTO patients (a1c, glucose, fasting)
values (20, 89, TRUE);
EXCEPTION
WHEN others THEN
-- Get the exception message and detail via stacked diagnostics
___ ___ ___
exc_message = ___,
exc_detail = ___;
-- Record the exception message and detail in the errors table
INSERT INTO errors (___, detail) VALUES (exc_message, ___);
END;
$$ language 'plpgsql';
-- Select all the errors recorded
SELECT * FROM errors;