pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedfinding (and recycling) holes in sequences
3+ messages / 3 participants
[nested] [flat]
* finding (and recycling) holes in sequences
@ 2000-06-25 07:52 Louis-David Mitterrand <cunctator@apartia.ch>
0 siblings, 0 replies; 3+ messages in thread
From: Louis-David Mitterrand @ 2000-06-25 07:52 UTC (permalink / raw)
To: pgsql-sql
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.
TIA
--
Louis-David Mitterrand - ldm@apartia.org - http://www.apartia.fr
"For a list of the ways which technology has failed to improve our
quality of life, press 3."
^ permalink raw reply [nested|flat] 3+ messages in thread
* finding (and recycling) holes in sequences
@ 2000-06-26 15:35 ` Kyle Bateman <kyle@actarg.com>
1 sibling, 0 replies; 3+ messages in thread
From: Kyle Bateman @ 2000-06-26 15:35 UTC (permalink / raw)
To: pgsql-sql; +Cc: ldm@apartia.ch
>
> 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?
Attachments:
kyle.vcf (text/x-vcard, 290B) download
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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: finding (and recycling) holes in sequences
@ 2000-06-28 15:36 ` Erik Tennant <lists@webpd.com>
1 sibling, 0 replies; 3+ messages in thread
From: Erik Tennant @ 2000-06-28 15:36 UTC (permalink / raw)
To: Kyle Bateman <kyle@actarg.com>; pgsql-sql; +Cc: ldm@apartia.ch
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"
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2000-06-28 15:36 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-06-25 07:52 finding (and recycling) holes in sequences Louis-David Mitterrand <cunctator@apartia.ch>
2000-06-26 15:35 ` Kyle Bateman <kyle@actarg.com>
2000-06-28 15:36 ` Erik Tennant <lists@webpd.com>
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