Suggerimenti per gestire eccezioni annidate

Transazioni e gestione degli errori in PostgreSQL

Jason Myers

Principal Engineer

Emulare i savepoint con blocchi annidati

DO $$
BEGIN
    -- Block 1
    BEGIN
        UPDATE inventory SET cost = 2.33 WHERE name = 'Linga';
        UPDATE inventory SET cost = 2.33 WHERE name = 'Petit-Beurre';
    EXCEPTION
    WHEN others THEN
       INSERT INTO errors (msg) VALUES ('Max cost is 10!');
       RAISE INFO 'Max cost is 10!';
    END; 
Transazioni e gestione degli errori in PostgreSQL

Emulare savepoint Blocco 2

-- Block 2
    BEGIN
        UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron';
    EXCEPTION
    WHEN others THEN
       INSERT INTO errors (msg) VALUES ('Max cost is 10!');
       RAISE INFO 'Max cost is 10!';
    END; 
END;
$$ language 'plpgsql';
Transazioni e gestione degli errori in PostgreSQL

Blocchi annidati con stacked diagnostics

DO $$
DECLARE
   exc_message text;
   exc_detail text;
   exc_context text;
BEGIN
    -- Block 1
    BEGIN
        UPDATE inventory SET cost = 2.33 WHERE name = 'Linga';
        UPDATE inventory SET cost = 2.33 WHERE name = 'Petit-Beurre';
    EXCEPTION
    WHEN others THEN
        GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT,
                                exc_detail = PG_EXCEPTION_DETAIL,
                                exc_context = PG_EXCEPTION_CONTEXT;
       INSERT INTO errors (msg,detail, context) 
           VALUES (exc_message, exc_detail, exc_context);
    END; 
    -- Block 2
    BEGIN
        UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron';
    EXCEPTION
    WHEN others THEN
       GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT,
                               exc_detail = PG_EXCEPTION_DETAIL,
                               exc_context = PG_EXCEPTION_CONTEXT;
       INSERT INTO errors (msg,detail, context) 
           VALUES (exc_message, exc_detail, exc_context);
    END; 
END$$;
Transazioni e gestione degli errori in PostgreSQL

Blocchi annidati con stacked diagnostics

DO $$
DECLARE
   exc_message text;
   exc_detail text;
   exc_context text;
BEGIN
Transazioni e gestione degli errori in PostgreSQL

Blocchi annidati con stacked diagnostics: blocco 2

    -- Block 1
    BEGIN
        UPDATE inventory SET cost = 2.33 WHERE name = 'Linga';
        UPDATE inventory SET cost = 2.33 WHERE name = 'Petit-Beurre';
    EXCEPTION
    WHEN others THEN
        GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT,
                                exc_detail = PG_EXCEPTION_DETAIL,
                                exc_context = PG_EXCEPTION_CONTEXT;
       INSERT INTO errors (msg,detail, context) 
           VALUES (exc_message, exc_detail, exc_context);
    END; 
Transazioni e gestione degli errori in PostgreSQL

Blocchi annidati con stacked diagnostics: blocco 2

-- Block 2
    BEGIN
        UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron';
    EXCEPTION
    WHEN others THEN
       GET STACKED DIAGNOSTICS exc_message = MESSAGE_TEXT,
                               exc_detail = PG_EXCEPTION_DETAIL,
                               exc_context = PG_EXCEPTION_CONTEXT;
       INSERT INTO errors (msg,detail, context) 
           VALUES (exc_message, exc_detail, exc_context);
    END; 
END$$;
Transazioni e gestione degli errori in PostgreSQL

Risultati

INFO:  Messaggio: la nuova riga per la relazione "inventory" viola il vincolo di check 
"cost_check" | Dettagli: La riga non valida contiene (7, 35, Macaron). | Contesto 
istruzione SQL "UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron'"
Funzione PL/pgSQL inline_code_block riga 23 all'istruzione SQL
DO
postgres=# \x on
Visualizzazione espansa attiva.
postgres=# select * from errors;
-[ RECORD 1 ]---------------------------------------------------------------------
error_id | 15
state    |
msg      | la nuova riga per la relazione "inventory" viola il vincolo di check "cost_check"
detail   | La riga non valida contiene (7, 35, Macaron).
context  | istruzione SQL "UPDATE inventory SET cost = 35.0 WHERE name = 'Macaron'"+
         | Funzione PL/pgSQL inline_code_block riga 23 all'istruzione SQL
Transazioni e gestione degli errori in PostgreSQL

Gestione eccezioni personalizzata vs stacked diagnostics

Personalizzata
  • Contesto dell'errore chiaro
  • Condizione d'errore attesa
  • Messaggio standard troppo generico
Stacked diagnostics
  • Serve più contesto sull'errore
  • Molte possibili condizioni d'errore
  • Debug
  • Generalizzare la gestione delle eccezioni
Transazioni e gestione degli errori in PostgreSQL

Esercitiamoci!

Transazioni e gestione degli errori in PostgreSQL

Preparing Video For Download...