Re: How to track exceptions in PL/pgSQL

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Florian Ledoux <florian(dot)ledoux(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: How to track exceptions in PL/pgSQL
Date: 2005-10-12 16:11:32
Message-ID: 20051012161132.GA35484@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Oct 12, 2005 at 12:40:29PM +0200, Florian Ledoux wrote:
> I would like to track (timestamp and error number) in the database or
> in text files the exceptions that occur in my PL/pgSQL functions.
>
> 1. How can I retrieve the SQLCODE in a 8.0.3 PG server ?

You can't, at least not that I'm aware of. In 8.1 you'll have
SQLSTATE and SQLERRM but they're not available in earlier versions.
I don't know if you could take the source code for 8.1's PL/pgSQL
and build it under 8.0; if not, then you might at least be able to
isolate the SQLSTATE/SQLERRM code and add it to 8.0's PL/pgSQL.
Perhaps one of the developers will comment about how feasible this
would be.

Without SQLSTATE/SQLERRM you could use several "WHEN some_exception
THEN" clauses to trap the most likely exceptions so at least you'd
know what kind of exception you were handling.

> 2. How can I trace the exception in a table although an implicit
> rollback is done when a exception occurs (the trace will be rollbacked
> too) ?

You should be able do inserts from the handler code. It worked for
me in simple tests, so apparently the rollback is done before the
handler code is executed.

--
Michael Fuhr

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Reid Thompson 2005-10-12 16:38:00 Postgresql Mention-- Logicworks' LogicOps Management and Monitoring Platform --Taking Linux, Apache, PostgreSQL, and PHP to their logical extreme
Previous Message codeWarrior 2005-10-12 15:39:09 Re: user privilages for executing pg_autovacuum?