Thread: Re: [HACKERS] ECPG ignores SAVEPOINT if first statement of a transaction

Re: [HACKERS] ECPG ignores SAVEPOINT if first statement of a transaction

From
Bruce Momjian
Date:
Would someone on the interface list look for a fix to this?  Thanks.

---------------------------------------------------------------------------

Michael Fuhr wrote:
> ECPG ignores SAVEPOINT if it's the first statement of a transaction:
>
> % cat foo.pgc
> int
> main(void)
> {
>     EXEC SQL WHENEVER SQLERROR SQLPRINT;
>     EXEC SQL WHENEVER SQLWARNING SQLPRINT;
>
>     EXEC SQL CONNECT TO test;
>
>     EXEC SQL SAVEPOINT foo;
>     EXEC SQL DROP TABLE nosuch_1;
>     EXEC SQL ROLLBACK TO foo;
>     EXEC SQL DROP TABLE nosuch_2;
>
>     EXEC SQL COMMIT;
>     EXEC SQL DISCONNECT;
>
>     return 0;
> }
>
> % ./foo
> sql error 'table "nosuch_1" does not exist' in line 10.
> sql error 'current transaction is aborted, commands ignored until end of transa
>
> The SAVEPOINT code is generated but apparently ECPGtrans() doesn't
> execute it.  A sniff of the connection doesn't show it, and the
> sniff shows the ROLLBACK TO failing with "no such savepoint."
>
> If I execute a command before the SAVEPOINT then I get the following,
> which is what I was expecting:
>
> % ./foo
> sql error 'table "nosuch_1" does not exist' in line 11.
> sql error 'table "nosuch_2" does not exist' in line 13.
>
> A sniff of this connection shows both the SAVEPOINT and ROLLBACK TO
> being executed and succeeding.
>
> --
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: [HACKERS] ECPG ignores SAVEPOINT if first statement of a transaction

From
Michael Fuhr
Date:
On Fri, Aug 12, 2005 at 10:22:32PM -0400, Bruce Momjian wrote:
> Michael Fuhr wrote:
> > ECPG ignores SAVEPOINT if it's the first statement of a transaction:

ECPGtrans() ignores the statement because of this check:

  /*
   * if we are not in autocommit mode, already have committed the
   * transaction and get another commit, just ignore it
   */
  if (!con->committed || con->autocommit)
  {
          if ((res = PQexec(con->connection, transaction)) == NULL)
          {
                  ECPGraise(lineno, ECPG_TRANS, ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN, NULL);
                  return FALSE;
          }
          PQclear(res);
  }

If no statements have been executed in this transaction then committed
is true, so ECPGtrans() ignores the current statement.  It looks
like the code should make an extra consideration for SAVEPOINT in
case it's the first statement.

I'm also wondering if the check for a NULL return value from PQexec()
is sufficient.  Shouldn't it also check for a non-NULL result that's
anything other than PGRES_COMMAND_OK?

--
Michael Fuhr

Re: [HACKERS] ECPG ignores SAVEPOINT if first statement of a transaction

From
Michael Meskes
Date:
Am Samstag, 13. August 2005 17:01 schrieb Michael Fuhr:
> On Fri, Aug 12, 2005 at 10:22:32PM -0400, Bruce Momjian wrote:
> > Michael Fuhr wrote:
> > > ECPG ignores SAVEPOINT if it's the first statement of a transaction:
> 
> ECPGtrans() ignores the statement because of this check:
>
>   /*
>    * if we are not in autocommit mode, already have committed the
>    * transaction and get another commit, just ignore it
>    */
>   if (!con->committed || con->autocommit)
>   {
>           if ((res = PQexec(con->connection, transaction)) == NULL)
>           {
>                   ECPGraise(lineno, ECPG_TRANS,
> ECPG_SQLSTATE_TRANSACTION_RESOLUTION_UNKNOWN, NULL); return FALSE;
>           }
>           PQclear(res);
>   }
>
> If no statements have been executed in this transaction then committed
> is true, so ECPGtrans() ignores the current statement.  It looks

I have to admit that this code is so old I have to think quite a bit about why 
it is done this way.

> like the code should make an extra consideration for SAVEPOINT in
> case it's the first statement.

Yes, I agree.

> I'm also wondering if the check for a NULL return value from PQexec()
> is sufficient.  Shouldn't it also check for a non-NULL result that's
> anything other than PGRES_COMMAND_OK?

Seems to make sense too. The NULL return value would just mean that the 
statement wasn't even started. If it is but returns an error, it is 
considered valid here, right?

Michael
-- 
Michael Meskes
Email: Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
ICQ: 179140304, AIM/Yahoo: michaelmeskes, Jabber: meskes@jabber.org
Go SF 49ers! Go Rhein Fire! Use Debian GNU/Linux! Use PostgreSQL!


Re: [HACKERS] ECPG ignores SAVEPOINT if first statement of a transaction

From
Michael Meskes
Date:
On Sat, Aug 13, 2005 at 09:01:45AM -0600, Michael Fuhr wrote:
> If no statements have been executed in this transaction then committed
> is true, so ECPGtrans() ignores the current statement.  It looks
> like the code should make an extra consideration for SAVEPOINT in
> case it's the first statement.

I rearranged the whole code as it seemed to have more holes. This code
really was old. :-)

So could you please check with the lates CVS, either HEAD or 8.0?

> I'm also wondering if the check for a NULL return value from PQexec()
> is sufficient.  Shouldn't it also check for a non-NULL result that's
> anything other than PGRES_COMMAND_OK?

Yes, it should. I changed this as well.

Michael
--
Michael Meskes
Email: Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
ICQ: 179140304, AIM/Yahoo: michaelmeskes, Jabber: meskes@jabber.org
Go SF 49ers! Go Rhein Fire! Use Debian GNU/Linux! Use PostgreSQL!