Thread: Error handling in C API function calls in a way that doesn't close client connection

Hello all,

This question refers to version 9.4 of Postgres. I have have a function

Datum do_something(PG_FUNCTION_ARGS)
{
    ...
    if(error_occured) {
        ereport(ERROR, (errmsg("some error occured")));
    }
    ...
}

When I call do_something in a way to deliberately cause the error condition, the client connection (psql) is terminated, however for example when you say (or many other function calls that cause ereport to be called):

psql > SELECT ST_IsSimple('POINT(1, 1)');

I simply get the message "ERROR:  parse error - invalid geometry" but the connection is not closed. 

The subqueries also know that the inner query had terminated with an error somehow, without terminating the connection:

psql> select * from (select st_issimple('point(0, 0)')) as a;

ERROR:  parse error - invalid geometry

I guess this could be done with surrounding the function call with PG_TRY/CATCH however these macros are not present in Postgis code. 

Can you please suggest how to implement this functionality in my code?

Thanks,
Igor

Igor Stassiy <istassiy@gmail.com> writes:
> This question refers to version 9.4 of Postgres. I have have a function

> Datum do_something(PG_FUNCTION_ARGS)
> {
>     ...
>     if(error_occured) {
>         ereport(ERROR, (errmsg("some error occured")));
>     }
>     ...
> }

> When I call do_something in a way to deliberately cause the error
> condition, the client connection (psql) is terminated,

[ shrug... ]  You did not show us whatever is causing that problem.

Given a self-contained example, it would be possible to offer some
useful advice.  On the basis of what you've said here, though, I can
only suggest that you're looking for the problem in the wrong place.

            regards, tom lane