PostgreSQL में Transactions और Error Handling
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';
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$$;
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).
| 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 (यदि हो) |
| 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