pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Erik Tennant <lists@webpd.com>
To: Kyle Bateman <kyle@actarg.com>
To: pgsql-sql@postgresql.org
Cc: ldm@apartia.ch
Subject: Re: finding (and recycling) holes in sequences
Date: Wed, 28 Jun 2000 09:36:24 -0600
Message-ID: <3.0.6.32.20000628093624.00aacc30@www.webpd.com> (raw)
In-Reply-To: <39577854.138C7E2C@actarg.com>
References: <200006252200.e5PM0X112526@hub.org>

There is a similar discussion going on in the jonas users mailing list..
Your approach below does not appear to be thread safe. There is no
guarantee that the same value will not be selected twice on two concurrent
inserts.. Even more so since it sounds like he is going to be doing the
maxid() and the insert statements separately..

-Erik


At 09:35 AM 6/26/00 -0600, Kyle Bateman wrote:
>     
>If one has a unique-id generating sequence that sometimes is bound to 
>have holes in it (ie: it could happen that a nextval(seq) happens 
>without a corresponding INSERT in the table), then how could one 
>efficiently scan for these holes to recycle them in subsequent INSERTs? I'm
>just looking for a "standard" way of doing this if such a thing 
>exists. Louis-David Mitterrand - ldm@apartia.org - http://www.apartia.fr 
> 
>      Here's an example: create function maxid () returns int4 as ' 
>    declare 
>        mv int4; 
>    begin 
>        select max(id) into mv from mytab; 
>        if mv is null then return 0; end if; 
>        return mv; 
>    end; 
>' language 'plpgsql'; create table mytab ( 
>    id int4 primary key default (maxid() + 1), 
>    data text 
>); insert into mytab (data) values ('Hi there'); 
>insert into mytab (data) values ('Howdy'); 
>insert into mytab (data) values ('Guten Tag'); 
>insert into mytab (data) values ('Terve'); select * from mytab; 
>  
>  BTW hackers, this is a common enough request, I wonder if there should be
>a built-in feature to make it easier for people to put this into the
>default value in the table definition i.e.: create table mytab ( 
>    keycolumn int4 primary key default coalesce(max(keycolumn),0)+1, 
>    data text 
>); Wouldn't this eliminate the need for rollback-able sequences? 
>   Attachment Converted: "c:\program files\eudora lists\attach\kyle13.vcf" 




view thread (3+ messages)

Message-ID: <3.0.6.32.20000628093624.00aacc30@www.webpd.com>
Permalink:  ../3.0.6.32.20000628093624.00aacc30@www.webpd.com/
Also on:    postgresql.org/message-id/3.0.6.32.20000628093624.00aacc30@www.webpd.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: lists@webpd.com, kyle@actarg.com, ldm@apartia.ch
  Subject: Re: finding (and recycling) holes in sequences
  In-Reply-To: <3.0.6.32.20000628093624.00aacc30@www.webpd.com>
  References: <200006252200.e5PM0X112526@hub.org> <3.0.6.32.20000628093624.00aacc30@www.webpd.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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