Re: rollback in C functions

From: Juan Pablo L <jpablolorenzetti(at)gmail(dot)com>
To: John R Pierce <pierce(at)hogranch(dot)com>
Cc: PostgreSQL mailing lists <pgsql-general(at)postgresql(dot)org>
Subject: Re: rollback in C functions
Date: 2015-02-20 16:27:03
Message-ID: CABSYDaYVD2sMat_fFzPq1064b-=QbYKbKOFOJF9pYHT82Kc3cg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Just an update, it was making a mistake, the execution of PQexecute, to
execute the query, was wrapped in a function called "PGresult
*db_execute(....)" that was returning the PGresult as NULL, i completely
forgot this, inside that function the exception was being caught and
discarded so by the time i got the PGresult pointer back in the caller it
contained nothing so trying to read any error from it at that point was
useless.

Just to clarify and for future reference, the proposal to use ereport
worked perfectly as proposed by the all members.

thank you very much!!

On 19 February 2015 at 16:42, Juan Pablo L <jpablolorenzetti(at)gmail(dot)com>
wrote:

> i tried this but the call to PQresultErrorField(PGresult,
> PG_DIAG_SQLSTATE) is returning NULL, this is what trigger the exception in
> the function code:
>
> ereport(ERROR,(errcode(ERRCODE_SQL_ROUTINE_EXCEPTION),errmsg("Plan with id
> %s does not allow balance with id %s",plan_id,in_balanceid)));
>
> and this is the caller code:
>
> if(PQresultStatus(pg_res) == PGRES_FATAL_ERROR)
> {
> char *t = PQresultErrorField(pg_res,PG_DIAG_SQLSTATE);
> log_debug("[C%03dH%03d] PQres returned NULL:
> %s",handler->my_connection->id,handler->id,t);
> }
>
> the above call log_debug show that t is NULL. am i doing something wrong
> ? thanks!
>
> On 19 February 2015 at 16:27, Juan Pablo L <jpablolorenzetti(at)gmail(dot)com>
> wrote:
>
>> Thank you, i will try this, honestly i was checking if PGResult is NULL,
>> when i trigger the exception i always get NULL so i did not any further but
>> i will try this .....
>>
>> On 19 February 2015 at 16:22, John R Pierce <pierce(at)hogranch(dot)com> wrote:
>>
>>> On 2/19/2015 2:02 PM, Juan Pablo L wrote:
>>>
>>>> Thank you Alvaro, i m afraid ereport seems to be the way, that it is
>>>> complicated to catch this error code in the code of the caller. cause you
>>>> have to use a callback etc etc
>>>>
>>>
>>> a query that triggers ereport(ERROR,....) should return a PGresult* that
>>> you pass to PQresultStatus(), which should indicate PGRES_FATAL_ERROR, so
>>> you then call PQresultErrorField(PGresult, PG_DIAG_SQLSTATE) to get back
>>> the SQLSTATE code.
>>>
>>>
>>>
>>> --
>>> john r pierce 37N 122W
>>> somewhere on the middle of the left coast
>>>
>>>
>>>
>>> --
>>> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-general
>>>
>>
>>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Melvin Davidson 2015-02-20 19:22:10 Re: PL/PgSQL and pg_temp pseudo-schema
Previous Message sridhar bamandlapally 2015-02-20 15:26:04 Oracle to PostgreSQL migration