Stacked diagnostics प्राप्त करना
Stacked diagnostics PostgreSQL के आंतरिक error message और exception विवरण ला सकता है. आइए patients टेबल पर फिर लौटें और एक ऐसा A1C जोड़ने की कोशिश करें जो testing limit से ऊपर हो. इससे check constraint exception उठेगा जिसे हम कैप्चर कर सकते हैं. Exception handler में हम stacked diagnostics का उपयोग करके अपनी error recording को और समृद्ध कर सकते हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
PostgreSQL में Transactions और Error Handling
अभ्यास निर्देश
DECLAREमें दो वैरिएबलexc_messageऔरexc_detailको text के रूप में घोषित करें.- Diagnostics stack प्राप्त करें और
exc_messageकोMESSAGE_TEXTतथाexc_detailकोPG_EXCEPTION_DETAILसे सेट करें. exc_messageऔरexc_detailको errors टेबल केmsgऔरdetailफ़ील्ड में insert करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
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;