Transacties en foutafhandeling in 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';
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).
| Naam | Beschrijving |
|---|---|
| RETURNED_SQLSTATE | de SQLSTATE-foutcode van de exceptie |
| COLUMN_NAME | de kolomnaam gerelateerd aan de exceptie |
| CONSTRAINT_NAME | de naam van de constraint gerelateerd aan de exceptie |
| MESSAGE_TEXT | de hoofdboodschap van de exceptie |
| PG_EXCEPTION_DETAIL | de detailboodschap van de exceptie, indien aanwezig |
| Naam | Beschrijving |
|---|---|
| PG_DATATYPE_NAME | de naam van het gegevenstype gerelateerd aan de exceptie |
| TABLE_NAME | de naam van de tabel gerelateerd aan de exceptie |
| SCHEMA_NAME | de naam van het schema gerelateerd aan de exceptie |
| PG_EXCEPTION_HINT | de tekstaanduiding van de hint van de exceptie, indien aanwezig |
| PG_EXCEPTION_CONTEXT | regel(s) met tekst van de callstack op het moment van de exceptie |
Transacties en foutafhandeling in PostgreSQL