Re: Explicite typecasting of functions

From: Richard Huxton <dev(at)archonet(dot)com>
To: Andreas Tille <tillea(at)rki(dot)de>, PostgreSQL SQL <pgsql-sql(at)postgresql(dot)org>
Subject: Re: Explicite typecasting of functions
Date: 2002-08-14 11:10:29
Message-ID: 200208141210.29995.dev@archonet.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Raymond Chui 2002-08-14 12:09:36 How to execute my trigger when update certain columns but not all?
Previous Message Andreas Tille 2002-08-14 09:30:25 Explicite typecasting of functions