Re: Sequences without blank holes - Mailing list pgsql-general

From Richard Huxton
Subject Re: Sequences without blank holes
Date
Msg-id 200311060919.02481.dev@archonet.com
Whole thread Raw
In response to Sequences without blank holes  (MaRcElO PeReIrA <gandalf_mp@yahoo.com.br>)
List pgsql-general
On Thursday 06 November 2003 08:01, MaRcElO PeReIrA wrote:
> Hi guys,
>
> I have been using the following table (short, short,
> short version):
>
> CREATE TABLE products (
>     prod_id SERIAL,
>     description TEXT
> );
>
> BUT, there is lots os users blaming because the holes
> in the [prod_id] field (and, of course it as supposed
> to be like this, because sequences only increase their
> values and never rollback).

Well, whatever you do you're going to serialise any additions to the products
table, so that's going to be a bottleneck.

I personally tend to have a system_settings table with a next_id row.
CREATE TABLE system_settings_int (
  setting varchar(100),
  value int4
);

You need to :
 - lock the row in question
 - increment it and read the new value
 - insert your product with the id in question
 - commit the transaction, releasing the lock

Of course this means that no other users can insert until the first user
inserts, and you'll need to deal with failed inserts in your application.

What you don't want to do is get the next value, let the user edit the product
details then insert - that'll make everything grind to a halt.
--
  Richard Huxton
  Archonet Ltd

pgsql-general by date:

Previous
From: Alex
Date:
Subject: Re: Problems with PLPGSQL
Next
From: Richard Huxton
Date:
Subject: Re: Database Design & Application Server Design