Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch all error psql function exception

I am writing an function with exception catching and ignoring. I want to catch all the exceptions and just ignore it. Is there anyway to catch all the exceptions and not individually?

CREATE OR REPLACE FUNCTION ADD_TABLE_TO_ARCHIVE (a TEXT, b TEXT) RETURNS INTEGER AS $SUCCESS$ DECLARE SUCCESS INTEGER; BEGIN     SUCCESS = 0;     BEGIN         UPDATE ARCHIVE_STATUS         SET *****         WHERE ***;         SUCCESS = 1;     EXCEPTION         WHEN UNIQUE_VIOLATION          SUCCESS = 0;     END;     RETURN SUCCESS; END; $SUCCESS$ LANGUAGE plpgsql; 

In place of unique exception, it should be any exception...

like image 673
Pavan Ebbadi Avatar asked Feb 02 '17 15:02

Pavan Ebbadi


People also ask

What is PSQL exception?

PostgreSQL raises an exception is used to raise the statement for reporting the warnings, errors and other type of reported message within a function or stored procedure.

How do you handle user defined exceptions in PostgreSQL?

if it is an user defined exception, delete the corresponding declaration and specify unique error code via ERRCODE in a USING clause. in catch-block replace SQLCODE by SQLSTATE.

What is raise notice in PostgreSQL?

RAISE is used to raise errors and report messages, PostgreSQL provides various parameters to report an error, warning, and information at a detailed level.


1 Answers

You can use EXCEPTION WHEN OTHERS clause:

BEGIN   do something EXCEPTION WHEN OTHERS THEN   handle any exception END; 

Without some exception a using of this clause is not good idea. The debugging, issue diagnostics can be terrible when you use this pattern. It is strong feature (sometimes necessary), but dangerous!

Attention - the entry to, end leave of protected section has significant load. The overhead of savepoint and releasing of savepoint is not small. The overhead of this construct is significantly higher than on Oracle (although there is visual similarity, it does different things). Although this code looks like code in PL/SQL, the implementation is absolutely different. If you expect higher load of your application, use it carefully (not inside cycles, ...)

like image 163
Pavel Stehule Avatar answered Oct 07 '22 02:10

Pavel Stehule