Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id DB365476686 for ; Wed, 14 Aug 2002 07:15:00 -0400 (EDT) Received: from anchor-post-32.mail.demon.net (anchor-post-32.mail.demon.net [194.217.242.90]) by postgresql.org (Postfix) with ESMTP id 39AC947667A for ; Wed, 14 Aug 2002 07:15:00 -0400 (EDT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-32.mail.demon.net with esmtp (Exim 3.35 #1) id 17ew7B-0006XH-0W; Wed, 14 Aug 2002 12:15:02 +0100 Received: from client.archonet.com (client.archonet.com [192.168.1.16]) by mainbox.archonet.com (Postfix) with ESMTP id B1B681359D; Wed, 14 Aug 2002 12:10:32 +0100 (BST) Content-Type: text/plain; charset="iso-8859-1" From: Richard Huxton Organization: Archonet Ltd To: Andreas Tille , PostgreSQL SQL Subject: Re: Explicite typecasting of functions Date: Wed, 14 Aug 2002 12:10:29 +0100 User-Agent: KMail/1.4.1 References: In-Reply-To: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Message-Id: <200208141210.29995.dev@archonet.com> X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200208/220 X-Sequence-Number: 9066 On Wednesday 14 Aug 2002 10:30 am, Andreas Tille wrote: > Hello, > > I want to insert new data into a table with increasing data set ids. > The table has two separate "regions" of data: Those with Ids below > 1000000 and other. If I want to create a new Id in the "lower region" > I tried the following (simplified example): > > > CREATE TABLE Items ( > Id int DEFAULT NextItem() > CREATE FUNCTION NextItem() RETURNS INT4 > AS 'select max(Id)+1 from Items where Id < 1000000;' > LANGUAGE 'sql'; > ERROR: Relation "items" does not exist > > Any hint to solve this kind of chicken-egg-problem? Your solution is not safe anyway - you could end up with two processes tryi= ng=20 to insert the next value. Can I suggest two sequences: item_low_seq and item_high_seq? Set the initia= l=20 value of each to 1,000,000 and 99,000,000 (or whatever) and then use=20 whichever sequence is appropriate. In the example above you'd want something like: id int not null default nextval('item_low_seq') - Richard Huxton