스택 진단으로 예외 처리 강화

PostgreSQL에서의 트랜잭션과 오류 처리

Jason Myers

Principal Engineer

더 많은 오류 정보 캡처

DO $$
BEGIN
   UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron';
   UPDATE inventory SET cost = 3.50 WHERE name = 'Panellets';
EXCEPTION
   WHEN others THEN
       INSERT INTO errors (msg) VALUES ('Max cost is 10!');
       RAISE INFO 'Max cost is 10!';
END; 
$$ language 'plpgsql';
PostgreSQL에서의 트랜잭션과 오류 처리

스택 진단 사용하기

DO $$
DECLARE
   exc_message text;
   exc_detail text;

BEGIN UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron'; UPDATE inventory SET cost = 3.50 WHERE name = 'Panellets';
EXCEPTION WHEN others THEN GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT, exc_detail = PG_EXCEPTION_DETAIL; INSERT INTO errors (msg, detail) VALUES (exc_message, exc_detail); RAISE INFO 'Exception Messaage: % | Exception Details: %', exc_message, exc_detail; END$$;
PostgreSQL에서의 트랜잭션과 오류 처리

진단 출력 예시

INFO:  Exception Messaage: new row for relation "inventory" violates check constraint 
"cost_check" | Exception Details: Failing row contains (7, 35, Macaron).
DO

postgres=# \x on
Expanded display is on.
postgres=# select msg, detail from errors;
-[ RECORD 1 ]-------------------------------------------------------------------
msg    | new row for relation "inventory" violates check constraint "cost_check"
detail | Failing row contains (7, 35, Macaron).
PostgreSQL에서의 트랜잭션과 오류 처리

무엇을 얻을 수 있나요?

Name Description
RETURNED_SQLSTATE 예외의 SQLSTATE 오류 코드
COLUMN_NAME 예외와 관련된 컬럼 이름
CONSTRAINT_NAME 예외와 관련된 제약 조건 이름
MESSAGE_TEXT 예외 기본 메시지 텍스트
PG_EXCEPTION_DETAIL 예외 상세 메시지 텍스트(있는 경우)
1 https://www.postgresql.org/docs/12/plpgsql-control-structures.html
PostgreSQL에서의 트랜잭션과 오류 처리

더 많은 진단 데이터 포인트

Name Description
PG_DATATYPE_NAME 예외와 관련된 데이터 타입 이름
TABLE_NAME 예외와 관련된 테이블 이름
SCHEMA_NAME 예외와 관련된 스키마 이름
PG_EXCEPTION_HINT 예외 힌트 메시지(있는 경우)
PG_EXCEPTION_CONTEXT 예외 시점의 호출 스택 텍스트 줄
PostgreSQL에서의 트랜잭션과 오류 처리

연습해 봅시다!

PostgreSQL에서의 트랜잭션과 오류 처리

Preparing Video For Download...