Received: from actarg.com ([209.180.91.25]) by hub.org (8.10.1/8.10.1) with ESMTP id e5QFb1133810 for ; Mon, 26 Jun 2000 11:37:02 -0400 (EDT) Received: from chi.actarg.com (IDENT:root@chi.actarg.com [192.168.2.16]) by actarg.com (8.9.3/8.9.3) with ESMTP id JAA17963; Mon, 26 Jun 2000 09:35:54 -0600 Received: from actarg.com (IDENT:kyle@localhost [127.0.0.1]) by chi.actarg.com (8.9.3/8.9.3) with ESMTP id JAA09162; Mon, 26 Jun 2000 09:35:48 -0600 Message-ID: <39577854.138C7E2C@actarg.com> Date: Mon, 26 Jun 2000 09:35:48 -0600 From: Kyle Bateman Organization: Action Target Inc X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.12-20 i686) X-Accept-Language: en MIME-Version: 1.0 To: pgsql-sql@postgresql.org CC: ldm@apartia.ch Subject: finding (and recycling) holes in sequences References: <200006252200.e5PM0X112526@hub.org> Content-Type: multipart/mixed; boundary="------------F2D06A0BAAF980824741057B" X-Archive-Number: 200006/226 This is a multi-part message in MIME format. --------------F2D06A0BAAF980824741057B Content-Type: multipart/alternative; boundary="------------B64B0FFEC65E67E012106B8E" --------------B64B0FFEC65E67E012106B8E Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit > > 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 > If you want to create a sequence of numbers with no holes, you might consider NOT using sequences at all (the are really meant for something different). Rather, each time you wish to do an insert, query the existing data to see what the maximum value is existing in the database. Add 1 to that and use this as your new value. This is guaranteed to not have any holes in the sequence. To make inserts easier, you can define an sql function that will return the max value. 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? --------------B64B0FFEC65E67E012106B8E Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit
 
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
 

If you want to create a sequence of numbers with no holes, you might consider NOT using sequences at all (the are really meant for something different).  Rather, each time you wish to do an insert, query the existing data to see what the maximum value is existing in the database.  Add 1 to that and use this as your new value.  This is guaranteed to not have any holes in the sequence.  To make inserts easier, you can define an sql function that will return the max value.  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?
  --------------B64B0FFEC65E67E012106B8E-- --------------F2D06A0BAAF980824741057B Content-Type: text/x-vcard; charset=us-ascii; name="kyle.vcf" Content-Transfer-Encoding: 7bit Content-Description: Card for Kyle Bateman Content-Disposition: attachment; filename="kyle.vcf" begin:vcard n:Bateman;Kyle tel;fax:801-377-8096 tel;work:801-377-8033x101 x-mozilla-html:FALSE url:www.actiontarget.com org:Action Target Inc adr:;;PO Box 636;Provo;UT;84603;US version:2.1 email;internet:kyle@actiontarget.com title:President x-mozilla-cpt:;-15520 fn:Kyle Bateman end:vcard --------------F2D06A0BAAF980824741057B--