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
Views: Raw Message | Whole Thread | Download mbox | Resend email
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

Browse pgsql-sql by date

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