Skip site navigation (1) Skip section navigation (2)

Re: Change the behaviour of the SERIAL "Type"

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Dani Oderbolz <oderbolz(at)ecologic(dot)de>
Cc: Randall Lucas <rlucas(at)tercent(dot)net>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Change the behaviour of the SERIAL "Type"
Date: 2003-06-27 14:26:35
Message-ID: 20030627142635.GB15269@wolff.to (view raw or flat)
Thread:
Lists: pgsql-sql
On Fri, Jun 27, 2003 at 10:45:07 +0200,
  Dani Oderbolz <oderbolz(at)ecologic(dot)de> wrote:
> 
> Yea, fine, but I propose a different (deeper) approach.
> Why does SERIAL only enforce a DEFAULT?

Because it is faster.

> This is not an exact imitation of an autoincrement, as a DEFAULT can be 
> overwritten.

There are probably other differences as well, since serial only provides
a way to get unique values. If you want more meaning than that you
have to be careful.

> In my oppinion, SERIAL should implicitly create a Trigger on the table, 
> which then
> handles this transparently.
> Would that be difficult?

It shouldn't be too difficult to write some triggers that make something
closer to autoincrement. It probably won't work very well if there are
lots of concurrent updates though. You can either lock the table with
the column exclusively and then find the largest value and then use
that value plus one. Don't use max for this. Make an index on the
autoincrement column and use order by and limit 1 to get the largest
value. The other option is to keep the sequence value in other table.
You can use select for update to update it. You will want to vacuum
this table often enough that it will stay on one page.

> (I am already writing a Procedure which gets all the info needed out of 
> the Catalog,
> but my problem is that I need some dynamic statements in there...)
> 
> Cheers, Dani
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 8: explain analyze is your friend

In response to

Responses

pgsql-sql by date

Next:From: Dani OderbolzDate: 2003-06-27 14:35:36
Subject: Re: Change the behaviour of the SERIAL "Type"
Previous:From: Együd CsabaDate: 2003-06-27 14:18:10
Subject: Re: Getting all rows even if not a member of any groups

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group