Re: Autocommit off - commits/rollbacks

Lists: pgsql-general
From: "Vogt, Michael" <Michael(dot)Vogt(at)united-security-providers(dot)ch>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Autocommit off - commits/rollbacks
Date: 2011-03-14 14:55:37
Message-ID: 98D8B015D09D1241A15FD54BA7EAFF150253C2E5@ttsrv02.tetrade.ch
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hey all

I have a question, using the autocommit off option in postgres.

As starting position I use a table called xxx.configuration using a
unique id constraint.

Why does postgres rollback the whole transaction after an error? I
compared the behavior with oracle/hsql - those dbms commit whats
possible. To illustrate my question, here are some examples:

Here is the postgres example
---
nasdb=# \set AUTOCOMMIT 'off'
nasdb=# insert into xxx.configuration(name, value, id) VALUES('aa',
'mm', 812); INSERT 0 1 nasdb=# insert into xxx.configuration(name,
value, id) VALUES('aa', 'mm', 813); INSERT 0 1 nasdb=# insert into
xxx.configuration(name, value, id) VALUES('aa', 'mm', 812);
ERROR: duplicate key value violates unique constraint
"configuration_pk"
nasdb=# commit;
ROLLBACK

>> value with id 812 and 813 are not stored in the table due the
>> rollback
---

Here is the same function using oracle:
---
SQL> set auto off;
SQL> insert into xxx.configuration(id,name,value) values(200,'aa','bb');
1 row created.
SQL> insert into xxx.configuration(id,name,value) values(201,'aa','bb');
1 row created.
SQL> insert into xxx.configuration(id,name,value) values(201,'aa','bb');
insert into xxx.configuration(id,name,value) values(201,'aa','bb')
*
ERROR at line 1:
ORA-00001: unique constraint (XXX.CONFIGURATION_PK) violated
SQL> commit;
Commit complete.

>> the first two inserts (id 200 and 201) are stored in the
xxx.configuration table.
---

Also hsqldb will insert all possible data into the db (like oracle).

Or can postgres behavior be changed to a "commit whatever is possible"
mode?

Regards
michu


From: Andrew Sullivan <ajs(at)crankycanuck(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Autocommit off - commits/rollbacks
Date: 2011-03-14 16:29:01
Message-ID: 20110314162901.GA60150@shinkuro.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, Mar 14, 2011 at 03:55:37PM +0100, Vogt, Michael wrote:
> Why does postgres rollback the whole transaction after an error? I
> compared the behavior with oracle/hsql - those dbms commit whats
> possible.

A transaction is supposed to commit or rollback. If you want to hold
on to something that you've done and then try something (and pursue
alternatives depending on whether you get an error), use a savepoint.
See
http://www.postgresql.org/docs/9.0/interactive/tutorial-transactions.html

A

--
Andrew Sullivan
ajs(at)crankycanuck(dot)ca


From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: "Vogt, Michael" <Michael(dot)Vogt(at)united-security-providers(dot)ch>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Autocommit off - commits/rollbacks
Date: 2011-03-29 02:25:56
Message-ID: 4D914334.5050203@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 03/14/2011 10:55 PM, Vogt, Michael wrote:
> Hey all
>
> I have a question, using the autocommit off option in postgres.
>
> As starting position I use a table called xxx.configuration using a
> unique id constraint.
>
> Why does postgres rollback the whole transaction after an error?

It's a PostgreSQL limitation (or, arguably, optimization). When a
statement fails, PostgreSQL doesn't have any way to return to the state
of the transaction just before that statement was executed. It aborts
the whole transaction. I usually find this is what I want anyway and
I'll be issuing an explicit ROLLBACK after the error anyway. I think
it's rather dangerous to allow a statement to execute successfully after
a prior one has failed, as to me it breaks the whole idea that a
transaction succeeds or fails as a whole.

If you want to handle errors, you can use a SAVEPOINT before a statement
that might fail, or you can use PL/PgSQL's EXCEPTION blocks.

--
Craig Ringer


From: Jerry Sievers <gsievers19(at)comcast(dot)net>
To:
Cc: "Vogt\, Michael" <Michael(dot)Vogt(at)united-security-providers(dot)ch>, pgsql-general(at)postgresql(dot)org
Subject: Re: Autocommit off - commits/rollbacks
Date: 2011-03-29 07:24:06
Message-ID: 871v1qs82x.fsf@comcast.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Craig Ringer <craig(at)postnewspapers(dot)com(dot)au> writes:

> On 03/14/2011 10:55 PM, Vogt, Michael wrote:
>
>> Hey all
>>
>> I have a question, using the autocommit off option in postgres.
>>
>> As starting position I use a table called xxx.configuration using a
>> unique id constraint.
>>
>> Why does postgres rollback the whole transaction after an error?
>
> It's a PostgreSQL limitation (or, arguably, optimization). When a

Well, any transactional RDBMS whatsoever should behave that way. Hardly
a PostgreSQL exclusive feature :-)

--
Jerry Sievers
e: gsievers19(at)comcast(dot)net
p: 305.321.1144


From: tushar nehete <tpnehete(at)gmail(dot)com>
To: Jerry Sievers <gsievers19(at)comcast(dot)net>
Cc: "Vogt, Michael" <Michael(dot)Vogt(at)united-security-providers(dot)ch>, pgsql-general(at)postgresql(dot)org
Subject: Re: Autocommit off - commits/rollbacks
Date: 2011-03-29 11:35:04
Message-ID: AANLkTikqL4dpK5q4hAXn7EhuAjAGHMkdsG4i-pC4hA_g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Yes We can use exception for each statement to restrict the rollback.
But how we can use SAVEPOINT and rollback to SAVEPOINT
in the stored function or procedure in POSTGRES?
We can only use the savepoints in transactions but not in stored functions.

Regards,
Tushar

On Tue, Mar 29, 2011 at 12:54 PM, Jerry Sievers <gsievers19(at)comcast(dot)net>wrote:

> Craig Ringer <craig(at)postnewspapers(dot)com(dot)au> writes:
>
> > On 03/14/2011 10:55 PM, Vogt, Michael wrote:
> >
> >> Hey all
> >>
> >> I have a question, using the autocommit off option in postgres.
> >>
> >> As starting position I use a table called xxx.configuration using a
> >> unique id constraint.
> >>
> >> Why does postgres rollback the whole transaction after an error?
> >
> > It's a PostgreSQL limitation (or, arguably, optimization). When a
>
> Well, any transactional RDBMS whatsoever should behave that way. Hardly
> a PostgreSQL exclusive feature :-)
>
> --
> Jerry Sievers
> e: gsievers19(at)comcast(dot)net
> p: 305.321.1144
>
> --
> 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
>


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Cc: tushar nehete <tpnehete(at)gmail(dot)com>, Jerry Sievers <gsievers19(at)comcast(dot)net>, "Vogt, Michael" <Michael(dot)Vogt(at)united-security-providers(dot)ch>
Subject: Re: Autocommit off - commits/rollbacks
Date: 2011-03-29 14:03:48
Message-ID: 201103290703.48991.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Tuesday, March 29, 2011 4:35:04 am tushar nehete wrote:
> Yes We can use exception for each statement to restrict the rollback.
> But how we can use SAVEPOINT and rollback to SAVEPOINT
> in the stored function or procedure in POSTGRES?
> We can only use the savepoints in transactions but not in stored functions.
>
> Regards,
> Tushar
>

See below for how to achieve the same effect using EXCEPTIONS in pl/pgsql:

http://www.postgresql.org/docs/9.0/interactive/plpgsql-control-
structures.html#PLPGSQL-ERROR-TRAPPING

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com