Thread: How to handle error message in PG_CATCH

How to handle error message in PG_CATCH

From
Zdenek Kotala
Date:
I'm working on implementing pg_check functions. Because I want to test 
whole table I need catch a error and handle it myself. I use following 
construct:



PG_TRY();
{...ereport(ERROR, (errmsg("Error test")));...
}
PG_CATCH();
{errcontext("Context error");        EmitErrorReport();        FlushErrorState();
}
PG_END_TRY();

At the end I got following message:

ERROR:  Error test
CONTEXT:  Context error
server sent data ("D" message) without prior row description ("T" message)

and also nothing appears in a log file. Similar concept is used in
autovacuum.c.

Any idea what is wrong?
    Thanks for help Zdenek


Re: How to handle error message in PG_CATCH

From
Alvaro Herrera
Date:
Zdenek Kotala wrote:

> PG_TRY();
> {
>     ...
>     ereport(ERROR, (errmsg("Error test")));
>     ...
> }
> PG_CATCH();
> {
>     errcontext("Context error");
>         EmitErrorReport();
>         FlushErrorState();
> }
> PG_END_TRY();
>
> At the end I got following message:
>
> ERROR:  Error test
> CONTEXT:  Context error
> server sent data ("D" message) without prior row description ("T" message)

I don't see anything wrong with this code.  Perhaps the problem is
somewhere else?

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
The PostgreSQL Company - Command Prompt, Inc.


Re: How to handle error message in PG_CATCH

From
Zdenek Kotala
Date:
Alvaro Herrera napsal(a):
> Zdenek Kotala wrote:
> 
>> PG_TRY();
>> {
>>     ...
>>     ereport(ERROR, (errmsg("Error test")));
>>     ...
>> }
>> PG_CATCH();
>> {
>>     errcontext("Context error");
>>         EmitErrorReport();
>>         FlushErrorState();
>> }
>> PG_END_TRY();
>>
>> At the end I got following message:
>>
>> ERROR:  Error test
>> CONTEXT:  Context error
>> server sent data ("D" message) without prior row description ("T" message)
> 
> I don't see anything wrong with this code.  Perhaps the problem is
> somewhere else?
> 


There is whole test code. It is store procedure and there are nothing 
special.  The difference between this and autovacuum is that autovacuum 
works without client side.

Datum
pg_check(PG_FUNCTION_ARGS)
{PG_TRY();{            ereport(ERROR, (errmsg("Error test")));        }        PG_CATCH();        {
errcontext("Contexterror");            EmitErrorReport();            FlushErrorState();        }
PG_END_TRY();PG_RETURN_DATUM(0);
}

    Zdenek


Re: How to handle error message in PG_CATCH

From
Tom Lane
Date:
Zdenek Kotala <Zdenek.Kotala@Sun.COM> writes:
> Alvaro Herrera napsal(a):
>> Zdenek Kotala wrote:
>>> At the end I got following message:
>>> 
>>> ERROR:  Error test
>>> CONTEXT:  Context error
>>> server sent data ("D" message) without prior row description ("T" message)
>> 
>> I don't see anything wrong with this code.  Perhaps the problem is
>> somewhere else?

> There is whole test code. It is store procedure and there are nothing 
> special.  The difference between this and autovacuum is that autovacuum 
> works without client side.

The problem with this is it's violating the wire protocol.  Once you've
sent the client an ERROR message, it no longer expects to see any result
from the SELECT that called the function.
        regards, tom lane


Re: How to handle error message in PG_CATCH

From
Zdenek Kotala
Date:
Tom Lane napsal(a):
> Zdenek Kotala <Zdenek.Kotala@Sun.COM> writes:
>> Alvaro Herrera napsal(a):
>>> Zdenek Kotala wrote:
>>>> At the end I got following message:
>>>>
>>>> ERROR:  Error test
>>>> CONTEXT:  Context error
>>>> server sent data ("D" message) without prior row description ("T" message)
>>> I don't see anything wrong with this code.  Perhaps the problem is
>>> somewhere else?
> 
>> There is whole test code. It is store procedure and there are nothing 
>> special.  The difference between this and autovacuum is that autovacuum 
>> works without client side.
> 
> The problem with this is it's violating the wire protocol.  Once you've
> sent the client an ERROR message, it no longer expects to see any result
> from the SELECT that called the function.
> 

Thanks for explanation. I added extra error message at the end of 
function and it works now.

    Thanks Zdenek