Re: do I need a rollback() after commit that fails?

From: APseudoUtopia <apseudoutopia(at)gmail(dot)com>
To: Vick Khera <vivek(at)khera(dot)org>
Cc: Postgres General Postgres General <pgsql-general(at)postgresql(dot)org>
Subject: Re: do I need a rollback() after commit that fails?
Date: 2009-09-29 19:19:13
Message-ID: 27ade5280909291219v5ce78747pcb426e5444cb506a@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Sep 29, 2009 at 1:26 PM, Vick Khera <vivek(at)khera(dot)org> wrote:
> I'm running Pg 8.3.7 on FreeBSD 7.2.
>
> I have some code in Perl that does a bunch of inserts and updates with
> all constraints deferred.  On occasion, one of the FK's gets violated
> and the transaction commit fails.
>
> I trap this with code like this:
>
>    unless ($dbh->commit()) {
>      warn "commit failure ".$dbh->errstr;
>      $dbh->rollback();
>      return 'failed';
>    }
>
> The DBI is telling me that the rollback() is useless with AutoCommit
> is on (which it is).
>
> I did some direct testing with psql and it seems that this is not Perl
> DBI specific behavior.
>
> So, it seems that if commit fails, I don't need to issue a rollback.
> Is this portable to other databases, or is this Postgres specific?
>
> I also note that if I do not defer the constraints, and issue the
> commit even after the INSERT reports error, that the statement result
> printed by commit is instead "ROLLBACK".  If I have constraints
> deferred, the commit output is just the "ERROR" statement, without any
> indication of ROLLBACK.
>

If a query within a transaction has errors, COMMIT at the end of the
transaction is automatically turned into a ROLLBACK.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message CG 2009-09-29 19:21:16 anyelement and anynonarray inclusion
Previous Message Amitabh Kant 2009-09-29 19:00:47 Re: Performance evaluation of PostgreSQL's historic releases