Re: in failed sql transaction

Lists: pgsql-general
From: Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: in failed sql transaction
Date: 2006-09-24 10:03:59
Message-ID: 200609241203.59292.ralf.wiebicke@exedio.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Hi all!

I just realized the following behaviour in postgresql: when I violate any
constraint (unique constraint in my case) then the transaction is not usable
anymore. Any other sql command returns a "in failed sql transaction" error.
All other databases I used up to now just ignore the statement violating the
constraint, but leave the transaction intact.

Is this intended behaviour or rather a bug? Or is there any way to "switch on"
the behaviour I'd like to see?

Best regards,
Ralf.

--
Ralf Wiebicke
Softwareengineer

exedio GmbH
Am Fiebig 14
01561 Thiendorf
Deutschland

Telefon +49 (35248) 80-118
Fax +49 (35248) 80-199
ralf(dot)wiebicke(at)exedio(dot)com
www.exedio.com


From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-24 23:40:09
Message-ID: 20060924234009.GA43233@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Sun, Sep 24, 2006 at 12:03:59PM +0200, Ralf Wiebicke wrote:
> I just realized the following behaviour in postgresql: when I violate any
> constraint (unique constraint in my case) then the transaction is not usable
> anymore. Any other sql command returns a "in failed sql transaction" error.

Transactions are all-or-nothing: all statements must succeed or the
transaction fails (but see below regarding savepoints).

> All other databases I used up to now just ignore the statement violating the
> constraint, but leave the transaction intact.

Which databases behave that way? Does COMMIT succeed even if some
statements failed?

> Is this intended behaviour or rather a bug? Or is there any way to "switch on"
> the behaviour I'd like to see?

This is intended behavior. You can use savepoints to roll back
part of a transaction so the transaction can continue after an
error.

--
Michael Fuhr


From: Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 08:14:35
Message-ID: 200609251014.35990.ralf.wiebicke@exedio.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Sorry, I was a bit impatient and posted the same question in a newsgroup a few
days before. There is an answer now:

http://groups.google.de/group/comp.databases.postgresql/browse_thread/thread/36e5c65dd15b0388/1e5ff9b7e2c6863e?hl=de#1e5ff9b7e2c6863e

Of course, if anyone has an additional idea, i'd appreciate it.

Best regards,
Ralf.

--
Ralf Wiebicke
Software Engineer

exedio GmbH
Am Fiebig 14
01561 Thiendorf
Deutschland

Telefon +49 (35248) 80-118
Fax +49 (35248) 80-199
ralf(dot)wiebicke(at)exedio(dot)com
www.exedio.com


From: "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>
To: "Michael Fuhr" <mike(at)fuhr(dot)org>
Cc: "Ralf Wiebicke" <ralf(dot)wiebicke(at)exedio(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 09:46:07
Message-ID: 65937bea0609250246p109ce574j79d0c883b54a170f@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I too have been bothered about this behaviour in the past.

On 9/25/06, Michael Fuhr <mike(at)fuhr(dot)org> wrote:
>
> Transactions are all-or-nothing: all statements must succeed or the

Correct.

> All other databases I used up to now just ignore the statement violating
> the
> > constraint, but leave the transaction intact.
>
> Which databases behave that way? Does COMMIT succeed even if some
> statements failed?

Oracle, for one, behaves that way... Yes, COMMIT does succeed even if some
statement(s) threw errors.

This is intended behavior. You can use savepoints to roll back
> part of a transaction so the transaction can continue after an
> error.

Probably, the 'other' DBs have implemented that by an implicit savepoint
just before a command, and rollong back to it automatically, if the
transaction fails.

This is quite a desirable feature...

--
gurjeet(at)EnterpriseDB(dot)com
singh(dot)gurjeet(at){ gmail | hotmail | yahoo }.com


From: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 10:04:57
Message-ID: 20060925100457.GB5461@merkur.hilbert.loc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, Sep 25, 2006 at 03:16:07PM +0530, Gurjeet Singh wrote:

> >All other databases I used up to now just ignore the statement violating
> >the
> >> constraint, but leave the transaction intact.
> >
> >Which databases behave that way? Does COMMIT succeed even if some
> >statements failed?
>
> Oracle, for one, behaves that way... Yes, COMMIT does succeed even if some
> statement(s) threw errors.
>
> Probably, the 'other' DBs have implemented that by an implicit savepoint
> just before a command, and rollong back to it automatically, if the
> transaction fails.
>
> This is quite a desirable feature...

Why bother with transactions at all if autocommit is enabled ??

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346


From: Alban Hertroys <alban(at)magproductions(dot)nl>
To: Gurjeet Singh <singh(dot)gurjeet(at)gmail(dot)com>
Cc: Michael Fuhr <mike(at)fuhr(dot)org>, Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 11:43:28
Message-ID: 4517C0E0.6080406@magproductions.nl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Gurjeet Singh wrote:
> > All other databases I used up to now just ignore the statement
> violating the
> > constraint, but leave the transaction intact.
>
> Which databases behave that way? Does COMMIT succeed even if some
> statements failed?
>
>
> Oracle, for one, behaves that way... Yes, COMMIT does succeed even if
> some statement(s) threw errors.

Actually, Oracle implicitly COMMIT's all open transactions if someone
performs a DDL statement on the table (or even the same schema?).

What other databases do is not necessarily correct[1]. In this case
PostgreSQL does the right thing; something went wrong, queries after the
error may very well depend on that data - you can't rely on the current
state. And it's what the SQL specs say too, of course...

[1] I'm not trying to imply that what PostgreSQL does is (in general).
--
Alban Hertroys
alban(at)magproductions(dot)nl

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
7500 AK Enschede

// Integrate Your World //


From: "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>
To: "Alban Hertroys" <alban(at)magproductions(dot)nl>
Cc: "Michael Fuhr" <mike(at)fuhr(dot)org>, "Ralf Wiebicke" <ralf(dot)wiebicke(at)exedio(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 12:10:56
Message-ID: 65937bea0609250510p4397ad93r8f1a46a93477e315@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 9/25/06, Alban Hertroys <alban(at)magproductions(dot)nl> wrote:
>
> In this case
> PostgreSQL does the right thing; something went wrong, queries after the
> error may very well depend on that data - you can't rely on the current
> state. And it's what the SQL specs say too, of course...
>
> [1] I'm not trying to imply that what PostgreSQL does is (in general).
> --
>

In an automated/programmatic access to the database, this might be
desirable; but when there's someone manually doing some activity, it sure
does get to one's nerves if the transaction till now was a long one.
Instead, the operator would love to edit just that one query and fire again!

Also, in automated/programmatic access, the programs are supposed to
catch the error and rollback/correct on their own.

I sure like PG's following of the standards, but usability should not be
lost sight of.

Best regards,

--
gurjeet(at)EnterpriseDB(dot)com
singh(dot)gurjeet(at){ gmail | hotmail | yahoo }.com


From: Karsten Hilbert <Karsten(dot)Hilbert(at)gmx(dot)net>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 13:09:34
Message-ID: 20060925130934.GE5461@merkur.hilbert.loc
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, Sep 25, 2006 at 05:40:56PM +0530, Gurjeet Singh wrote:

> >In this case
> >PostgreSQL does the right thing; something went wrong, queries after the
> >error may very well depend on that data - you can't rely on the current
> >state. And it's what the SQL specs say too, of course...
>
> In an automated/programmatic access to the database, this might be
> desirable; but when there's someone manually doing some activity, it sure
> does get to one's nerves if the transaction till now was a long one.
> Instead, the operator would love to edit just that one query and fire again!
Well, psql does it just that way. It implements auto-commit
on behalf of the user unless a transaction is explicitely
started.

> Also, in automated/programmatic access, the programs are supposed to
> catch the error and rollback/correct on their own.
Sure but that of course does not relieve the database of
aborting the transacation on its own as soon as something
goes wrong. And for sake of efficiency the transaction
should be aborted right there and then and subsequent
queries can be ignored until the end of transaction. This is
easier on CPU cycles and memory consumption.

Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346


From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 14:36:25
Message-ID: 1159194986.4033.2.camel@localhost.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Sun, 2006-09-24 at 12:03 +0200, Ralf Wiebicke wrote:
> Hi all!
>
> I just realized the following behaviour in postgresql: when I violate any
> constraint (unique constraint in my case) then the transaction is not usable
> anymore. Any other sql command returns a "in failed sql transaction" error.
> All other databases I used up to now just ignore the statement violating the
> constraint, but leave the transaction intact.
>
> Is this intended behaviour or rather a bug? Or is there any way to "switch on"
> the behaviour I'd like to see?

Normal behaviour.

Have you read up on savepoints?

http://www.postgresql.org/docs/8.1/interactive/sql-savepoint.html

It allows you to set a point to rollback to should an error occur.


From: Andrew Sullivan <ajs(at)crankycanuck(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 18:23:26
Message-ID: 20060925182326.GH9685@phlogiston.dyndns.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, Sep 25, 2006 at 05:40:56PM +0530, Gurjeet Singh wrote:
> I sure like PG's following of the standards, but usability should not be
> lost sight of.

One man's meal is another man's poison. For me, with a small number
of exceptions, the standards conformance _is_ what makes PostgreSQL
so usable.

A

--
Andrew Sullivan | ajs(at)crankycanuck(dot)ca
A certain description of men are for getting out of debt, yet are
against all taxes for raising money to pay it off.
--Alexander Hamilton


From: Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 21:20:58
Message-ID: 200609252320.58253.ralf.wiebicke@exedio.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Hi!

Thanks for all the help.

I finally used savepoints to get what I want.

However I don't like this very much. I tried a few other databases (hsqldb,
mysql/innodb and oracle), and none of them made the transaction unusable
after violating the constraint.

Best regards,
Ralf.


From: Scott Marlowe <smarlowe(at)g2switchworks(dot)com>
To: Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>
Cc: pgsql general <pgsql-general(at)postgresql(dot)org>
Subject: Re: in failed sql transaction
Date: 2006-09-25 21:33:36
Message-ID: 1159220016.26848.42.camel@state.g2switchworks.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, 2006-09-25 at 16:20, Ralf Wiebicke wrote:
> Hi!
>
> Thanks for all the help.
>
> I finally used savepoints to get what I want.
>
> However I don't like this very much. I tried a few other databases (hsqldb,
> mysql/innodb and oracle), and none of them made the transaction unusable
> after violating the constraint.

I wouldn't hold MySQL as the standard of "the right way of doing
things." But I do take your point.

Having grown up with PostgreSQL, I much prefer the all or nothing
approach with explicit save pointing to make you do it right.

It's especially nice when you're trying to to an import. With oracle,
you HAVE to have sqlldr to get things done. With pgsql, you can just
try an import, and if one row is bad, the whole thing aborts, no half
finished import without knowing what did or didn't go in.

There's the right way, and the easy way, and sadly, seldom are they the
same.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-25 22:31:39
Message-ID: 16197.1159223499@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com> writes:
> I finally used savepoints to get what I want.
> However I don't like this very much.

Have you experimented with psql's ON_ERROR_ROLLBACK setting?

regards, tom lane


From: Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-26 08:32:05
Message-ID: 200609261032.05736.ralf.wiebicke@exedio.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


> Have you experimented with psql's ON_ERROR_ROLLBACK setting?

Thanks for the hint. Seems to be exactly what I want. But is not yet available
through JDBC, as far as I see:

http://archives.postgresql.org/pgsql-jdbc/2006-07/msg00092.php

I'm writing a java framework, so there is no way around JDBC for me.

Best regards,
Ralf.

--
Ralf Wiebicke
Software Engineer

exedio GmbH
Am Fiebig 14
01561 Thiendorf
Deutschland

Telefon +49 (35248) 80-118
Fax +49 (35248) 80-199
ralf(dot)wiebicke(at)exedio(dot)com
www.exedio.com


From: "Gurjeet Singh" <singh(dot)gurjeet(at)gmail(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: in failed sql transaction
Date: 2006-09-26 10:12:55
Message-ID: 65937bea0609260312j1aa71771nf5b6eb4b963d7b09@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thanks a lot for the pointer.... This is exactly what I have been looking
for.

<from_docs>
The on_error_rollback-on mode works by issuing an implicit SAVEPOINT for
you, just before each command that is in a transaction block, and rolls back
to the savepoint on error.
</from_docs>

On 9/26/06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> Ralf Wiebicke <ralf(dot)wiebicke(at)exedio(dot)com> writes:
> > I finally used savepoints to get what I want.
> > However I don't like this very much.
>
> Have you experimented with psql's ON_ERROR_ROLLBACK setting?
>
> regards, tom lane
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
> choose an index scan if your joining column's datatypes do not
> match
>

--
gurjeet(at)EnterpriseDB(dot)com
singh(dot)gurjeet(at){ gmail | hotmail | yahoo }.com