Re: serial columns & loads misfeature?

From: Manfred Koizar <mkoi-pg(at)aon(dot)at>
To: Gregory Seidman <gss+pg(at)cs(dot)brown(dot)edu>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: serial columns & loads misfeature?
Date: 2002-06-28 22:53:55
Message-ID: c5pphugtmoebbrv3448in8fi61eabjpfhb@4ax.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, 28 Jun 2002 18:16:30 -0400, Gregory Seidman
<gss+pg(at)cs(dot)brown(dot)edu> wrote:
>CREATE TABLE Foo (
> id int UNIQUE NOT NULL, -- no default yet
> -- ... whatever else
> primary key (id)
>);
>
>CREATE FUNCTION next_foo() RETURNS int
>AS 'SELECT COALESCE(max(id), 0)+1 FROM Foo'
>LANGUAGE SQL;
>
>ALTER TABLE Foo ALTER COLUMN id SET DEFAULT next_foo();

Greg, yes this works, but ...

it turns your application into a single user system, because
concurrent transactions could see the same max(id) and therefore try
to insert duplicate keys,

and the performance hit soon gets unacceptable, because select max()
always does a table scan.

Kevin, stick to serials. They offer the best performance and you will
get used to them. And you do the migration only once, don't you?

Servus
Manfred

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Gregory Seidman 2002-06-28 23:48:01 Re: select min row in a group
Previous Message Manfred Koizar 2002-06-28 22:27:41 Re: select min row in a group