pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Andreas Tille <tillea@rki.de>
To: PostgreSQL SQL <pgsql-sql@postgresql.org>
Subject: Re: Explicite typecasting of functions
Date: Wed, 14 Aug 2002 12:10:29 +0100
Message-ID: <200208141210.29995.dev@archonet.com> (raw)
In-Reply-To: <Pine.LNX.4.44.0208141123010.8253-100000@wr-linux02.rki.ivbb.bund.de>
References: <Pine.LNX.4.44.0208141123010.8253-100000@wr-linux02.rki.ivbb.bund.de>

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 trying 
to insert the next value.

Can I suggest two sequences: item_low_seq and item_high_seq? Set the initial 
value of each to 1,000,000 and 99,000,000 (or whatever) and then use 
whichever sequence is appropriate.

In the example above you'd want something like:
  id int not null default nextval('item_low_seq')

- Richard Huxton



view thread (6+ messages)  latest in thread

Message-ID: <200208141210.29995.dev@archonet.com>
Permalink:  ../200208141210.29995.dev@archonet.com/
Also on:    postgresql.org/message-id/200208141210.29995.dev@archonet.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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, tillea@rki.de
  Subject: Re: Explicite typecasting of functions
  In-Reply-To: <200208141210.29995.dev@archonet.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