dbTalk Databases Forums  

Continuing a transaction after INSERT failure

comp.databases.postgresql comp.databases.postgresql


Discuss Continuing a transaction after INSERT failure in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Michael Schuerig
 
Posts: n/a

Default Continuing a transaction after INSERT failure - 04-01-2009 , 11:12 AM







I want to give people with the same name consecutive serial numbers. The
way I'm trying to achieve this is to insert everyone with a serial
number of 1 at first and catch exceptions that may result from a
violated uniqueness constraint. Then I retry with an incremented serial
number.

I've used this strategy originally with sqlite3 where it works, now with
PostgreSQL (8.3) the transaction is aborted. Is there a way to recover
from the violated uniqueness constraint and continue the transaction?
Or is there a better strategy entirely?

Michael

--
Michael Schuerig
mailto:michael (AT) schuerig (DOT) de
http://www.schuerig.de/michael/

Reply With Quote
  #2  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Continuing a transaction after INSERT failure - 04-01-2009 , 12:53 PM








Michael Schuerig wrote on 01.04.2009 18:12:
Quote:
I want to give people with the same name consecutive serial numbers. The
way I'm trying to achieve this is to insert everyone with a serial
number of 1 at first and catch exceptions that may result from a
violated uniqueness constraint. Then I retry with an incremented serial
number.

I've used this strategy originally with sqlite3 where it works, now with
PostgreSQL (8.3) the transaction is aborted. Is there a way to recover
from the violated uniqueness constraint and continue the transaction?
Or is there a better strategy entirely?
You need to set a savepoint before you "try" to insert, then you can rollback to
the savepoint leaving the "overall" transaction valid.

But to me the whole concept does not sound that good.

Why not simply insert them with a generated key that might not be consecutive,
but generate the consecutive numbers during the display in the front end. Do you
have any real (e.g. legal) requirement that those numbers are consecutive?
Usually this is a very bad design and will eventually lead to a lot more
problems (e.g. what happens if you delete a row).

Thomas


Reply With Quote
  #3  
Old   
Michael Schuerig
 
Posts: n/a

Default Re: Continuing a transaction after INSERT failure - 04-01-2009 , 01:26 PM



Thomas Kellerer wrote:

Quote:

Michael Schuerig wrote on 01.04.2009 18:12:
I want to give people with the same name consecutive serial numbers.
The way I'm trying to achieve this is to insert everyone with a
serial number of 1 at first and catch exceptions that may result from
a violated uniqueness constraint. Then I retry with an incremented
serial number.

I've used this strategy originally with sqlite3 where it works, now
with PostgreSQL (8.3) the transaction is aborted. Is there a way to
recover from the violated uniqueness constraint and continue the
transaction? Or is there a better strategy entirely?

You need to set a savepoint before you "try" to insert, then you can
rollback to the savepoint leaving the "overall" transaction valid.
Thanks, that works.

Quote:
But to me the whole concept does not sound that good.

Why not simply insert them with a generated key that might not be
consecutive, but generate the consecutive numbers during the display
in the front end.
In my case, duplicates are rare to begin with, thus I'd like to have an
easy and very localized way to handle them. In particular, I don't want
to make display code any more complicated because of them.

Michael

--
Michael Schuerig
mailto:michael (AT) schuerig (DOT) de
http://www.schuerig.de/michael/


Reply With Quote
  #4  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Continuing a transaction after INSERT failure - 04-02-2009 , 03:20 AM



Michael Schuerig wrote:
Quote:
I want to give people with the same name consecutive serial numbers.
The way I'm trying to achieve this is to insert everyone with a
serial number of 1 at first and catch exceptions that may result from
a violated uniqueness constraint. Then I retry with an incremented
serial number.

But to me the whole concept does not sound that good.

Why not simply insert them with a generated key that might not be
consecutive, but generate the consecutive numbers during the display
in the front end.

In my case, duplicates are rare to begin with, thus I'd like to have an
easy and very localized way to handle them. In particular, I don't want
to make display code any more complicated because of them.
You will never get around the problem with "holes" in your sequence
if you ever delete rows.

But apart from that, your technique seems very expensive and slow:
If there are already n rows in the table, you will need n+1 INSERT
statements, each of which will result in index and table reads.

Why don't you either start with "SELECT max(id)+1 FROM tab"
instead of 1 or keep the current maximum in a separate one row
table which you use with
UPDATE counter SET maxid=maxid+1 RETURNING maxid
to get the next value.
That would be much cheaper.

Yours,
Laurenz Albe




Reply With Quote
  #5  
Old   
Michael Schuerig
 
Posts: n/a

Default Re: Continuing a transaction after INSERT failure - 04-02-2009 , 07:24 AM



Laurenz Albe wrote:

[Detecting duplicate names and assigning a serial number by detecting a
uniqueness violation.]
Quote:
You will never get around the problem with "holes" in your sequence
if you ever delete rows.

But apart from that, your technique seems very expensive and slow:
If there are already n rows in the table, you will need n+1 INSERT
statements, each of which will result in index and table reads.
I see, I think you're misunderstanding me. The uniqueness constraint is
on lastname, firstname, serial_number, i.e., it is only triggered when
there are duplicate names to begin with. But you're right, there's an
opportunity for optimization in determining the biggest relevant serial
number in a case a collision occurs.

Michael

--
Michael Schuerig
mailto:michael (AT) schuerig (DOT) de
http://www.schuerig.de/michael/


Reply With Quote
  #6  
Old   
Laurenz Albe
 
Posts: n/a

Default Re: Continuing a transaction after INSERT failure - 04-03-2009 , 01:53 AM



Michael Schuerig wrote:
Quote:
But apart from that, your technique seems very expensive and slow:
If there are already n rows in the table, you will need n+1 INSERT
statements, each of which will result in index and table reads.

I see, I think you're misunderstanding me. The uniqueness constraint is
on lastname, firstname, serial_number, i.e., it is only triggered when
there are duplicate names to begin with.
Oh, yes, I misunderstood that.
If the danger of constraint violation upon INSERT is low,
your technique is actually good.

Yours,
Laurenz Albe




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.