एक्सेप्शन पकड़ना

PostgreSQL में Transactions और Error Handling

Jason Myers

Principal Engineer

एरर देने वाले स्टेटमेंट्स

INSERT INTO sales (name, quantity, cost) 
VALUES 
  ('chocolate chip', 6, null);
ERROR:  null value in column "cost" violates not-null constraint
DETAIL:  Failing row contains 
(1, "chocolate chip", 6, null, 2020-04-28 19:58:55.715886).
PostgreSQL में Transactions और Error Handling

जेनेरिक एक्सेप्शन कैप्चर

R

tryCatch(
  sqrt("a"), 
  error=function(e) 
    print("Boom!")
)

Python

try:
   math.sqrt("a")
except Exception as e:
   print("Boom!")

PL/pgSQL

BEGIN 
SELECT 
  SQRT("a");
EXCEPTION WHEN others THEN RAISE INFO 'Boom!';
END;

परिणाम

     R: Boom!
Python: Boom!
   SQL: INFO: Boom!
PostgreSQL में Transactions और Error Handling

PL/pgSQL DO कमांड्स (अनाम फ़ंक्शंस)

DO $$

DECLARE some_variable text;
BEGIN SELECT text from a table; END;
$$ language 'plpgsql';
PostgreSQL में Transactions और Error Handling

एक्सेप्शन हैंडलिंग फ़ंक्शन

DO $$
BEGIN
    SELECT SQRT("a");
EXCEPTION
    WHEN others THEN
       INSERT INTO errors (msg) VALUES ('Can not take the square root of a string.');
       RAISE INFO 'Can not take the square root of a string.';
END; 
$$ language 'plpgsql';
PostgreSQL में Transactions और Error Handling

सोच-समझकर एक्सेप्शन हैंडलिंग करें

  • EXCEPTION क्लॉज जोड़ने से पर्याप्त ओवरहेड आता है
  • Python या R की एक्सेप्शन हैंडलिंग ज़्यादा कुशल है
  • सही कॉन्टेक्स्ट पाए बिना एक्सेप्शन सुलझाने की जल्दी न करें
  • एक्सेप्शंस समझे बिना ऑप्टिमाइज़ न करें।
1 https://www.postgresql.org/docs/12/plpgsql-control-structures.html {{1}}
PostgreSQL में Transactions और Error Handling

डेटासेट बदलना

patients

column type
patient_id integer
a1c double (float)
glucose integer
fasting boolean
created_on timestamp

errors

column type
error_id integer
state string
msg string
detail string
context string
PostgreSQL में Transactions और Error Handling

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

PostgreSQL में Transactions और Error Handling

Preparing Video For Download...