pvsbandi
asked on
Error handling in PostgreSQL
Hi All,
I'm new to PostgreSQL.
Have a requirement to write a stored procedure, where the error code and description should be inserted into an error log table and the handler shall continue.
Can someone kindly give me a code example which satisfies my scenario?
Thanks in advance!
I'm new to PostgreSQL.
Have a requirement to write a stored procedure, where the error code and description should be inserted into an error log table and the handler shall continue.
Can someone kindly give me a code example which satisfies my scenario?
Thanks in advance!
ASKER
Thank you.
I need to capture any exception and insert into a error log table. So, I need something more generic.
Please advice
I need to capture any exception and insert into a error log table. So, I need something more generic.
Please advice
You need to use BEGIN END, EXCEPTION..
CREATE OR REPLACE FUNCTION YourfunctionOrProcName (ParameterName1 Datatype , ParameterName2 Datatype ..etc)
RETURNS INT AS $$
BEGIN
raise notice 'Hello';
---Your Statements
RETURN 1
EXCEPTION
WHEN OTHERS THEN
BEGIN
raise notice 'exception';
RETURN 0;
END;
END;
$$ LANGUAGE plpgsql;
ASKER
Hi Pawan,
What i'm expecting is to capture the error code, error description, and insert them into a table.
The stored procedure shall continue processing after the error is handled.
Do you have an example of this?
What i'm expecting is to capture the error code, error description, and insert them into a table.
The stored procedure shall continue processing after the error is handled.
Do you have an example of this?
This is well documented https://www.postgresql.org/docs/9.6/static/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING
special variable SQLSTATE contains the error code of the raised exception Table A-1 gives a list of error codes.
special variable SQLERRM contains the error message associated with the exception.
These variables are undefined outside exception handlers.
Beyond these two simple variables there is the pl/pgsql construct
GET STACKED DIAGNOSTICS to get more information like what table was being abused.
special variable SQLSTATE contains the error code of the raised exception Table A-1 gives a list of error codes.
special variable SQLERRM contains the error message associated with the exception.
These variables are undefined outside exception handlers.
Beyond these two simple variables there is the pl/pgsql construct
GET STACKED DIAGNOSTICS to get more information like what table was being abused.
ASKER
Thank you!
Can you please tell me if this is fine?
Can you please tell me if this is fine?
EXCEPTION
WHEN others THEN
LV_ERRCODE = sqlstate,
LV_MESSAGE = COALESCE(sqlerrm,'')||'::'
||TO_CHAR(current_timestamp, 'yyyy-mm-dd hh12:mi:ss AM')
||COALESCE(LV_PROCEDURE_NM,'')
||'Placement id/ Payment detail id'||' :: '
||COALESCE(CAST(LV_PLACEMENT_ID AS VARCHAR(10)),'')||' / '
||COALESCE(CAST(LV_SEQ_PMNT_DETAIL_ID AS VARCHAR(10)),'');
INSERT INTO TBL_PGPS_LOG(PROGRAM_NAME,
ERROR_SQLCODE,
LOG_DESC_TX,
CREATED_BY,
CREATE_TS
)
SELECT LV_PROCEDURE_NM,
LV_ERRCODE,
LV_MESSAGE,
'PGPSUSER',
TO_CHAR(current_timestamp, 'yyyy-mm-dd hh12:mi:ss AM');
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot!
Open in new window
Here you have to create function as they do not have proc. Exception can be done using RAISE EXCEPTION.