Re: serialization errors when inserting new records - Mailing list pgsql-general

From Ralph van Etten
Subject Re: serialization errors when inserting new records
Date
Msg-id Pine.LNX.4.44.0501240732140.2282-100000@exp-toy.et10.loc
Whole thread Raw
In response to Re: serialization errors when inserting new records  (William Yu <wyu@talisys.com>)
Responses Re: serialization errors when inserting new records
List pgsql-general
On Sun, 23 Jan 2005, William Yu wrote:

> Ralph van Etten wrote:
> > I agree that a serial would be better.
> >
> > But I think there are situations where a serial isn't convenient
> > Like when you want an primary key which consists of the current
> > year and an sequence number. Like ('05', 1), ('05', 2), ('05', 3) etc.
> > With a sequence you must write extra code to reset the sequence every year
> > and you get into trouble if someone inserts data from the previous year...
> > A 'MAX(id)+1' is much simpler and cleaner then.
>
> My personal experience is trying to get primary keys to "mean" something
> is a pain in the ass. In your example, I'd much rather stick with serial
> as the primary key and store the year/sequence in another "display"
> field. Think about this situation:

Ok, but then I have the same problem. The "display" field has to be
unique and it needs to use a SELECT MAX in an INSERT query and this gives
the same problem. Only not with the primary key but with possible
duplicate values in the "display" field.

CREATE TABLE test (
  pk    SERIAL,
  disp1 CHAR(2),
  disp2 INT,

  UNIQUE(disp1, disp2),
  PRIMARY KEY(pk)
)

and insert with something like:

INSERT INTO test (disp1, disp2)
SELECT 'XX', COALESCE(MAX(disp2)+1, 1)
  FROM test WHERE disp1 = 'XX';

This gives the same serialization errors.

Ralph.



pgsql-general by date:

Previous
From: Devrim GUNDUZ
Date:
Subject: Weird problem in 8.0.0
Next
From: Devrim GUNDUZ
Date:
Subject: Re: Weird problem in 8.0.0