Stacked diagnostics के साथ exception handling बेहतर करें

PostgreSQL में Transactions और Error Handling

Jason Myers

Principal Engineer

और ज़्यादा error जानकारी कैप्चर करना

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 में Transactions और Error Handling

Stacked diagnostics का उपयोग

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 में Transactions और Error Handling

उदाहरण: diagnostic आउटपुट

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 में Transactions और Error Handling

तो क्या-क्या मिल सकता है?

Name Description
RETURNED_SQLSTATE exception का SQLSTATE error code
COLUMN_NAME exception से जुड़ा column नाम
CONSTRAINT_NAME exception से जुड़ा constraint नाम
MESSAGE_TEXT exception के primary message का text
PG_EXCEPTION_DETAIL exception के detail message का text (यदि हो)
1 https://www.postgresql.org/docs/12/plpgsql-control-structures.html
PostgreSQL में Transactions और Error Handling

और diagnostic datapoints

Name Description
PG_DATATYPE_NAME exception से जुड़ा data type नाम
TABLE_NAME exception से जुड़ा table नाम
SCHEMA_NAME exception से जुड़ा schema नाम
PG_EXCEPTION_HINT exception के hint message का text (यदि हो)
PG_EXCEPTION_CONTEXT exception के समय call stack की line(s) का text
PostgreSQL में Transactions और Error Handling

अभ्यास करते हैं!

PostgreSQL में Transactions और Error Handling

Preparing Video For Download...