pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Can I prevent my sequence to increment if an insert is rejected?
5+ messages / 4 participants
[nested] [flat]

* Can I prevent my sequence to increment if an insert is rejected?
@ 1999-07-20 16:54 Frederic De Leersnijder <frederic.de.leersnijder@pandora.be>
  1999-07-20 19:01 ` Re: [SQL] Can I prevent my sequence to increment if an insert is rejected? tjk@tksoft.com <tk@uno.tksoft.com>
  1999-07-21 09:15 ` Re: [SQL] Can I prevent my sequence to increment if an insert is rejected? Herouth Maoz <herouth@oumail.openu.ac.il>
  0 siblings, 2 replies; 5+ messages in thread

From: Frederic De Leersnijder @ 1999-07-20 16:54 UTC (permalink / raw)
  To: pgsql-sql@hub.org <pgsql-sql@hub.org>

Hi,

when I try to insert a record and it curses with fields with a unique
key on them my sequence I use for my primary key does increment. How can
I prevent this from happening?


Thanks
Frederic



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [SQL] Can I prevent my sequence to increment if an insert is rejected?
  1999-07-20 16:54 Can I prevent my sequence to increment if an insert is rejected? Frederic De Leersnijder <frederic.de.leersnijder@pandora.be>
@ 1999-07-20 19:01 ` tjk@tksoft.com <tk@uno.tksoft.com>
  1 sibling, 0 replies; 5+ messages in thread

From: tjk@tksoft.com @ 1999-07-20 19:01 UTC (permalink / raw)
  To: frederic.de.leersnijder@pandora.be; +Cc: pgsql-sql@hub.org

Frederic,

You could (a) do the work inside a transaction, 
or (b) do the insert after you have retrieved
the key and verified it doesn't exist in the table.
I.e. get the nextval from the sequence first and then
do the insert. 

P.S. If a record was inserted with a
used key, wouldn't you want to skip that
sequence value anyway?


Troy

Troy Korjuslommi                Tksoft OY, Inc.
tjk@tksoft.com                  Software Development
                                Open Source Solutions
                                Hosting Services




> 
> Hi,
> 
> when I try to insert a record and it curses with fields with a unique
> key on them my sequence I use for my primary key does increment. How can
> I prevent this from happening?
> 
> 
> Thanks
> Frederic
> 
> 




^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [SQL] Can I prevent my sequence to increment if an insert is rejected?
  1999-07-20 16:54 Can I prevent my sequence to increment if an insert is rejected? Frederic De Leersnijder <frederic.de.leersnijder@pandora.be>
@ 1999-07-21 09:15 ` Herouth Maoz <herouth@oumail.openu.ac.il>
  1999-07-21 09:36   ` Re: [SQL] Can I prevent my sequence to increment if an insert isrejected? Chris Bitmead <chris@tech.com.au>
  1999-07-21 09:45   ` Re: [SQL] Can I prevent my sequence to increment if an insert isrejected? Herouth Maoz <herouth@oumail.openu.ac.il>
  1 sibling, 2 replies; 5+ messages in thread

From: Herouth Maoz @ 1999-07-21 09:15 UTC (permalink / raw)
  To: Frederic De Leersnijder <frederic.de.leersnijder@pandora.be>; pgsql-sql@hub.org <pgsql-sql@hub.org>

At 19:54 +0300 on 20/07/1999, Frederic De Leersnijder wrote:


> Hi,
>
> when I try to insert a record and it curses with fields with a unique
> key on them my sequence I use for my primary key does increment. How can
> I prevent this from happening?

You can't. The point of sequences is that they ensure you of unique values,
but they DO NOT ensure you of gap-free values. This allows sequences to be
used momentarily, without being locked for the duration of a transaction,
thus allowing many users to get values with minimal contention.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [SQL] Can I prevent my sequence to increment if an insert isrejected?
  1999-07-20 16:54 Can I prevent my sequence to increment if an insert is rejected? Frederic De Leersnijder <frederic.de.leersnijder@pandora.be>
  1999-07-21 09:15 ` Re: [SQL] Can I prevent my sequence to increment if an insert is rejected? Herouth Maoz <herouth@oumail.openu.ac.il>
@ 1999-07-21 09:36   ` Chris Bitmead <chris@tech.com.au>
  1 sibling, 0 replies; 5+ messages in thread

From: Chris Bitmead @ 1999-07-21 09:36 UTC (permalink / raw)
  To: Herouth Maoz <herouth@oumail.openu.ac.il>; +Cc: Frederic De Leersnijder <frederic.de.leersnijder@pandora.be>; pgsql-sql@hub.org <pgsql-sql@hub.org>

Herouth Maoz wrote:
> 
> At 19:54 +0300 on 20/07/1999, Frederic De Leersnijder wrote:
> 
> > Hi,
> >
> > when I try to insert a record and it curses with fields with a unique
> > key on them my sequence I use for my primary key does increment. How can
> > I prevent this from happening?
> 
> You can't. The point of sequences is that they ensure you of unique values,
> but they DO NOT ensure you of gap-free values. This allows sequences to be
> used momentarily, without being locked for the duration of a transaction,
> thus allowing many users to get values with minimal contention.

I missed the original question, but don't you get sequential numbers as
long as cache is 0?

-- 
Chris Bitmead
mailto:chris@tech.com.au
http://www.techphoto.org - Photography News, Stuff that Matters



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [SQL] Can I prevent my sequence to increment if an insert isrejected?
  1999-07-20 16:54 Can I prevent my sequence to increment if an insert is rejected? Frederic De Leersnijder <frederic.de.leersnijder@pandora.be>
  1999-07-21 09:15 ` Re: [SQL] Can I prevent my sequence to increment if an insert is rejected? Herouth Maoz <herouth@oumail.openu.ac.il>
@ 1999-07-21 09:45   ` Herouth Maoz <herouth@oumail.openu.ac.il>
  1 sibling, 0 replies; 5+ messages in thread

From: Herouth Maoz @ 1999-07-21 09:45 UTC (permalink / raw)
  To: Chris Bitmead <chris@tech.com.au>; +Cc: pgsql-sql@hub.org <pgsql-sql@hub.org>

At 12:36 +0300 on 21/07/1999, Chris Bitmead wrote:


> I missed the original question, but don't you get sequential numbers as
> long as cache is 0?

No. If an insert failed or a transaction failed, the sequence remains in
its new state.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~1999-07-21 09:45 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-07-20 16:54 Can I prevent my sequence to increment if an insert is rejected? Frederic De Leersnijder <frederic.de.leersnijder@pandora.be>
1999-07-20 19:01 ` tjk@tksoft.com <tk@uno.tksoft.com>
1999-07-21 09:15 ` Herouth Maoz <herouth@oumail.openu.ac.il>
1999-07-21 09:36   ` Re: [SQL] Can I prevent my sequence to increment if an insert isrejected? Chris Bitmead <chris@tech.com.au>
1999-07-21 09:45   ` Re: [SQL] Can I prevent my sequence to increment if an insert isrejected? Herouth Maoz <herouth@oumail.openu.ac.il>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox